blob: 1b491c280a8ee479295d63c5f2c27c65f3fa2784 [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 Moolenaar00bf8cd2018-10-07 20:26:20 +0200133static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100134
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaard76ce852018-05-01 15:02:04 +0200136static 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 +0200137static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200139static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200140static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200142static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static win_T *qf_find_win(qf_info_T *qi);
146static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200147static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200148static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
150static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
151static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
152static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200154// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000155#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200156// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000157#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200158
159// Quickfix and location list stack check helper macros
160#define IS_QF_STACK(qi) (qi == &ql_info)
161#define IS_LL_STACK(qi) (qi != &ql_info)
162
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000163/*
164 * Return location list for window 'wp'
165 * For location list window, return the referenced location list
166 */
167#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200170 * Looking up a buffer can be slow if there are many. Remember the last one
171 * to make this a lot faster if there are multiple matches in the same file.
172 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200173static char_u *qf_last_bufname = NULL;
174static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200175
Bram Moolenaar3c097222017-12-21 20:54:49 +0100176static char *e_loc_list_changed =
177 N_("E926: Current location list was changed");
178
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200179/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200180 * Maximum number of bytes allowed per line while reading a errorfile.
181 */
182#define LINE_MAXLEN 4096
183
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200184static struct fmtpattern
185{
186 char_u convchar;
187 char *pattern;
188} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200189 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200190 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200191 {'n', "\\d\\+"},
192 {'l', "\\d\\+"},
193 {'c', "\\d\\+"},
194 {'t', "."},
195 {'m', ".\\+"},
196 {'r', ".*"},
197 {'p', "[- .]*"},
198 {'v', "\\d\\+"},
199 {'s', ".\\+"},
200 {'o', ".\\+"}
201 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200202
203/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200204 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200205 * See fmt_pat definition above for the list of supported patterns. The
206 * pattern specifier is supplied in "efmpat". The converted pattern is stored
207 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200208 */
209 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200210efmpat_to_regpat(
211 char_u *efmpat,
212 char_u *regpat,
213 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200214 int idx,
215 int round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200216 char_u *errmsg)
217{
218 char_u *srcptr;
219
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200220 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200221 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200222 // Each errorformat pattern can occur only once
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200223 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200224 _("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200225 EMSG(errmsg);
226 return NULL;
227 }
228 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200229 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200230 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200231 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200232 {
233 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200234 _("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200235 EMSG(errmsg);
236 return NULL;
237 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200238 efminfo->addr[idx] = (char_u)++round;
239 *regpat++ = '\\';
240 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200241#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200242 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200243 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200244 // Also match "c:" in the file name, even when
245 // checking for a colon next: "%f:".
246 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200247 STRCPY(regpat, "\\%(\\a:\\)\\=");
248 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200249 }
250#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200251 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200252 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200253 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200254 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200255 // A file name may contain spaces, but this isn't
256 // in "\f". For "%f:%l:%m" there may be a ":" in
257 // the file name. Use ".\{-1,}x" instead (x is
258 // the next character), the requirement that :999:
259 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200260 STRCPY(regpat, ".\\{-1,}");
261 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200262 }
263 else
264 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200265 // File name followed by '\\' or '%': include as
266 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200267 STRCPY(regpat, "\\f\\+");
268 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200269 }
270 }
271 else
272 {
273 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200274 while ((*regpat = *srcptr++) != NUL)
275 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200276 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200277 *regpat++ = '\\';
278 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200279
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200280 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200281}
282
283/*
284 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200285 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200286 */
287 static char_u *
288scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200289 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200290 char_u *efm,
291 int len,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200292 char_u *regpat,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200293 char_u *errmsg)
294{
295 char_u *efmp = *pefmp;
296
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200297 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200298 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200299 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200300 {
301 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200302 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200303 if (efmp < efm + len)
304 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200305 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200306 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200307 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200308 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200309 if (efmp == efm + len)
310 {
311 EMSG(_("E374: Missing ] in format string"));
312 return NULL;
313 }
314 }
315 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200316 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200317 *regpat++ = *++efmp;
318 *regpat++ = '\\';
319 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200320 }
321 else
322 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200323 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200324 sprintf((char *)errmsg,
325 _("E375: Unsupported %%%c in format string"), *efmp);
326 EMSG(errmsg);
327 return NULL;
328 }
329
330 *pefmp = efmp;
331
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200332 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200333}
334
335/*
336 * Analyze/parse an errorformat prefix.
337 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200338 static char_u *
339efm_analyze_prefix(char_u *efmp, efm_T *efminfo, char_u *errmsg)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200340{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200341 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200342 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200343 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200344 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200345 else
346 {
347 sprintf((char *)errmsg,
348 _("E376: Invalid %%%c in format string prefix"), *efmp);
349 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200350 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200351 }
352
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200353 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200354}
355
356/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200357 * Converts a 'errorformat' string part in 'efm' to a regular expression
358 * pattern. The resulting regex pattern is returned in "regpat". Additional
359 * information about the 'erroformat' pattern is returned in "fmt_ptr".
360 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200361 */
362 static int
363efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200364 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200365 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200366 efm_T *fmt_ptr,
367 char_u *regpat,
368 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200369{
370 char_u *ptr;
371 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200372 int round;
373 int idx = 0;
374
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200375 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200376 ptr = regpat;
377 *ptr++ = '^';
378 round = 0;
379 for (efmp = efm; efmp < efm + len; ++efmp)
380 {
381 if (*efmp == '%')
382 {
383 ++efmp;
384 for (idx = 0; idx < FMT_PATTERNS; ++idx)
385 if (fmt_pat[idx].convchar == *efmp)
386 break;
387 if (idx < FMT_PATTERNS)
388 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200389 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200390 errmsg);
391 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200392 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200393 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200394 }
395 else if (*efmp == '*')
396 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200397 ++efmp;
398 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr, errmsg);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200399 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200400 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200401 }
402 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200403 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200404 else if (*efmp == '#')
405 *ptr++ = '*';
406 else if (*efmp == '>')
407 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200408 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200409 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200410 // prefix is allowed only at the beginning of the errorformat
411 // option part
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200412 efmp = efm_analyze_prefix(efmp, fmt_ptr, errmsg);
413 if (efmp == NULL)
414 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200415 }
416 else
417 {
418 sprintf((char *)errmsg,
419 _("E377: Invalid %%%c in format string"), *efmp);
420 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200421 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200422 }
423 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200424 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200425 {
426 if (*efmp == '\\' && efmp + 1 < efm + len)
427 ++efmp;
428 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200429 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200430 if (*efmp)
431 *ptr++ = *efmp;
432 }
433 }
434 *ptr++ = '$';
435 *ptr = NUL;
436
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200437 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200438}
439
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200440/*
441 * Free the 'errorformat' information list
442 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200443 static void
444free_efm_list(efm_T **efm_first)
445{
446 efm_T *efm_ptr;
447
448 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
449 {
450 *efm_first = efm_ptr->next;
451 vim_regfree(efm_ptr->prog);
452 vim_free(efm_ptr);
453 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100454 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200455}
456
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200457/*
458 * Compute the size of the buffer used to convert a 'errorformat' pattern into
459 * a regular expression pattern.
460 */
461 static int
462efm_regpat_bufsz(char_u *efm)
463{
464 int sz;
465 int i;
466
467 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
468 for (i = FMT_PATTERNS; i > 0; )
469 sz += (int)STRLEN(fmt_pat[--i].pattern);
470#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200471 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200472#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200473 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200474#endif
475
476 return sz;
477}
478
479/*
480 * Return the length of a 'errorformat' option part (separated by ",").
481 */
482 static int
483efm_option_part_len(char_u *efm)
484{
485 int len;
486
487 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
488 if (efm[len] == '\\' && efm[len + 1] != NUL)
489 ++len;
490
491 return len;
492}
493
494/*
495 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
496 * are parsed and converted to regular expressions. Returns information about
497 * the parsed 'errorformat' option.
498 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200499 static efm_T *
500parse_efm_option(char_u *efm)
501{
502 char_u *errmsg = NULL;
503 int errmsglen;
504 efm_T *fmt_ptr = NULL;
505 efm_T *fmt_first = NULL;
506 efm_T *fmt_last = NULL;
507 char_u *fmtstr = NULL;
508 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200509 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200510
511 errmsglen = CMDBUFFSIZE + 1;
512 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
513 if (errmsg == NULL)
514 goto parse_efm_end;
515
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200516 // Each part of the format string is copied and modified from errorformat
517 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200518
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200519 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200520 sz = efm_regpat_bufsz(efm);
521 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200522 goto parse_efm_error;
523
524 while (efm[0] != NUL)
525 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200526 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200527 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
528 if (fmt_ptr == NULL)
529 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200530 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200531 fmt_first = fmt_ptr;
532 else
533 fmt_last->next = fmt_ptr;
534 fmt_last = fmt_ptr;
535
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200536 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200537 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200538
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200539 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200540 goto parse_efm_error;
541 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
542 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200543 // Advance to next part
544 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200545 }
546
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200547 if (fmt_first == NULL) // nothing found
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200548 EMSG(_("E378: 'errorformat' contains no pattern"));
549
550 goto parse_efm_end;
551
552parse_efm_error:
553 free_efm_list(&fmt_first);
554
555parse_efm_end:
556 vim_free(fmtstr);
557 vim_free(errmsg);
558
559 return fmt_first;
560}
561
Bram Moolenaare0d37972016-07-15 22:36:01 +0200562enum {
563 QF_FAIL = 0,
564 QF_OK = 1,
565 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200566 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200567 QF_IGNORE_LINE = 4,
568 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200569};
570
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200571/*
572 * State information used to parse lines and add entries to a quickfix/location
573 * list.
574 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200575typedef struct {
576 char_u *linebuf;
577 int linelen;
578 char_u *growbuf;
579 int growbufsiz;
580 FILE *fd;
581 typval_T *tv;
582 char_u *p_str;
583 listitem_T *p_li;
584 buf_T *buf;
585 linenr_T buflnum;
586 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100587 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200588} qfstate_T;
589
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200590/*
591 * Allocate more memory for the line buffer used for parsing lines.
592 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200593 static char_u *
594qf_grow_linebuf(qfstate_T *state, int newsz)
595{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200596 char_u *p;
597
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200598 // If the line exceeds LINE_MAXLEN exclude the last
599 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200600 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
601 if (state->growbuf == NULL)
602 {
603 state->growbuf = alloc(state->linelen + 1);
604 if (state->growbuf == NULL)
605 return NULL;
606 state->growbufsiz = state->linelen;
607 }
608 else if (state->linelen > state->growbufsiz)
609 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200610 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200611 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200612 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200613 state->growbufsiz = state->linelen;
614 }
615 return state->growbuf;
616}
617
618/*
619 * Get the next string (separated by newline) from state->p_str.
620 */
621 static int
622qf_get_next_str_line(qfstate_T *state)
623{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200624 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200625 char_u *p_str = state->p_str;
626 char_u *p;
627 int len;
628
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200629 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200630 return QF_END_OF_INPUT;
631
632 p = vim_strchr(p_str, '\n');
633 if (p != NULL)
634 len = (int)(p - p_str) + 1;
635 else
636 len = (int)STRLEN(p_str);
637
638 if (len > IOSIZE - 2)
639 {
640 state->linebuf = qf_grow_linebuf(state, len);
641 if (state->linebuf == NULL)
642 return QF_NOMEM;
643 }
644 else
645 {
646 state->linebuf = IObuff;
647 state->linelen = len;
648 }
649 vim_strncpy(state->linebuf, p_str, state->linelen);
650
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200651 // Increment using len in order to discard the rest of the
652 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200653 p_str += len;
654 state->p_str = p_str;
655
656 return QF_OK;
657}
658
659/*
660 * Get the next string from state->p_Li.
661 */
662 static int
663qf_get_next_list_line(qfstate_T *state)
664{
665 listitem_T *p_li = state->p_li;
666 int len;
667
668 while (p_li != NULL
669 && (p_li->li_tv.v_type != VAR_STRING
670 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200671 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200672
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200673 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200674 {
675 state->p_li = NULL;
676 return QF_END_OF_INPUT;
677 }
678
679 len = (int)STRLEN(p_li->li_tv.vval.v_string);
680 if (len > IOSIZE - 2)
681 {
682 state->linebuf = qf_grow_linebuf(state, len);
683 if (state->linebuf == NULL)
684 return QF_NOMEM;
685 }
686 else
687 {
688 state->linebuf = IObuff;
689 state->linelen = len;
690 }
691
692 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
693
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200694 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200695 return QF_OK;
696}
697
698/*
699 * Get the next string from state->buf.
700 */
701 static int
702qf_get_next_buf_line(qfstate_T *state)
703{
704 char_u *p_buf = NULL;
705 int len;
706
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200707 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200708 if (state->buflnum > state->lnumlast)
709 return QF_END_OF_INPUT;
710
711 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
712 state->buflnum += 1;
713
714 len = (int)STRLEN(p_buf);
715 if (len > IOSIZE - 2)
716 {
717 state->linebuf = qf_grow_linebuf(state, len);
718 if (state->linebuf == NULL)
719 return QF_NOMEM;
720 }
721 else
722 {
723 state->linebuf = IObuff;
724 state->linelen = len;
725 }
726 vim_strncpy(state->linebuf, p_buf, state->linelen);
727
728 return QF_OK;
729}
730
731/*
732 * Get the next string from file state->fd.
733 */
734 static int
735qf_get_next_file_line(qfstate_T *state)
736{
737 int discard;
738 int growbuflen;
739
740 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
741 return QF_END_OF_INPUT;
742
743 discard = FALSE;
744 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200745 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200746 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200747 // The current line exceeds IObuff, continue reading using
748 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200749 if (state->growbuf == NULL)
750 {
751 state->growbufsiz = 2 * (IOSIZE - 1);
752 state->growbuf = alloc(state->growbufsiz);
753 if (state->growbuf == NULL)
754 return QF_NOMEM;
755 }
756
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200757 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200758 memcpy(state->growbuf, IObuff, IOSIZE - 1);
759 growbuflen = state->linelen;
760
761 for (;;)
762 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200763 char_u *p;
764
Bram Moolenaare0d37972016-07-15 22:36:01 +0200765 if (fgets((char *)state->growbuf + growbuflen,
766 state->growbufsiz - growbuflen, state->fd) == NULL)
767 break;
768 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
769 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200770 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200771 break;
772 if (state->growbufsiz == LINE_MAXLEN)
773 {
774 discard = TRUE;
775 break;
776 }
777
778 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
779 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200780 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200781 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200782 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200783 }
784
785 while (discard)
786 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200787 // The current line is longer than LINE_MAXLEN, continue
788 // reading but discard everything until EOL or EOF is
789 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200790 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
791 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200792 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200793 break;
794 }
795
796 state->linebuf = state->growbuf;
797 state->linelen = growbuflen;
798 }
799 else
800 state->linebuf = IObuff;
801
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100802#ifdef FEAT_MBYTE
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200803 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200804 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
805 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100806 char_u *line;
807
808 line = string_convert(&state->vc, state->linebuf, &state->linelen);
809 if (line != NULL)
810 {
811 if (state->linelen < IOSIZE)
812 {
813 STRCPY(state->linebuf, line);
814 vim_free(line);
815 }
816 else
817 {
818 vim_free(state->growbuf);
819 state->linebuf = state->growbuf = line;
820 state->growbufsiz = state->linelen < LINE_MAXLEN
821 ? state->linelen : LINE_MAXLEN;
822 }
823 }
824 }
825#endif
826
Bram Moolenaare0d37972016-07-15 22:36:01 +0200827 return QF_OK;
828}
829
830/*
831 * Get the next string from a file/buffer/list/string.
832 */
833 static int
834qf_get_nextline(qfstate_T *state)
835{
836 int status = QF_FAIL;
837
838 if (state->fd == NULL)
839 {
840 if (state->tv != NULL)
841 {
842 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200843 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200844 status = qf_get_next_str_line(state);
845 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200846 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200847 status = qf_get_next_list_line(state);
848 }
849 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200850 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200851 status = qf_get_next_buf_line(state);
852 }
853 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200854 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200855 status = qf_get_next_file_line(state);
856
857 if (status != QF_OK)
858 return status;
859
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200860 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200861 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200862 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200863 state->linebuf[state->linelen - 1] = NUL;
864#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200865 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
866 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200867#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200868 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200869
870#ifdef FEAT_MBYTE
871 remove_bom(state->linebuf);
872#endif
873
874 return QF_OK;
875}
876
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200877typedef struct {
878 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200879 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200880 char_u *errmsg;
881 int errmsglen;
882 long lnum;
883 int col;
884 char_u use_viscol;
885 char_u *pattern;
886 int enr;
887 int type;
888 int valid;
889} qffields_T;
890
891/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200892 * Parse the match for filename ('%f') pattern in regmatch.
893 * Return the matched value in "fields->namebuf".
894 */
895 static int
896qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
897{
898 int c;
899
900 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
901 return QF_FAIL;
902
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200903 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200904 c = *rmp->endp[midx];
905 *rmp->endp[midx] = NUL;
906 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
907 *rmp->endp[midx] = c;
908
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200909 // For separate filename patterns (%O, %P and %Q), the specified file
910 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200911 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
912 && mch_getperm(fields->namebuf) == -1)
913 return QF_FAIL;
914
915 return QF_OK;
916}
917
918/*
919 * Parse the match for error number ('%n') pattern in regmatch.
920 * Return the matched value in "fields->enr".
921 */
922 static int
923qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
924{
925 if (rmp->startp[midx] == NULL)
926 return QF_FAIL;
927 fields->enr = (int)atol((char *)rmp->startp[midx]);
928 return QF_OK;
929}
930
931/*
932 * Parse the match for line number (%l') pattern in regmatch.
933 * Return the matched value in "fields->lnum".
934 */
935 static int
936qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
937{
938 if (rmp->startp[midx] == NULL)
939 return QF_FAIL;
940 fields->lnum = atol((char *)rmp->startp[midx]);
941 return QF_OK;
942}
943
944/*
945 * Parse the match for column number ('%c') pattern in regmatch.
946 * Return the matched value in "fields->col".
947 */
948 static int
949qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
950{
951 if (rmp->startp[midx] == NULL)
952 return QF_FAIL;
953 fields->col = (int)atol((char *)rmp->startp[midx]);
954 return QF_OK;
955}
956
957/*
958 * Parse the match for error type ('%t') pattern in regmatch.
959 * Return the matched value in "fields->type".
960 */
961 static int
962qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
963{
964 if (rmp->startp[midx] == NULL)
965 return QF_FAIL;
966 fields->type = *rmp->startp[midx];
967 return QF_OK;
968}
969
970/*
971 * Parse the match for '%+' format pattern. The whole matching line is included
972 * in the error string. Return the matched line in "fields->errmsg".
973 */
974 static int
975qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
976{
977 char_u *p;
978
979 if (linelen >= fields->errmsglen)
980 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200981 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200982 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
983 return QF_NOMEM;
984 fields->errmsg = p;
985 fields->errmsglen = linelen + 1;
986 }
987 vim_strncpy(fields->errmsg, linebuf, linelen);
988 return QF_OK;
989}
990
991/*
992 * Parse the match for error message ('%m') pattern in regmatch.
993 * Return the matched value in "fields->errmsg".
994 */
995 static int
996qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
997{
998 char_u *p;
999 int len;
1000
1001 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1002 return QF_FAIL;
1003 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1004 if (len >= fields->errmsglen)
1005 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001006 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001007 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1008 return QF_NOMEM;
1009 fields->errmsg = p;
1010 fields->errmsglen = len + 1;
1011 }
1012 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1013 return QF_OK;
1014}
1015
1016/*
1017 * Parse the match for rest of a single-line file message ('%r') pattern.
1018 * Return the matched value in "tail".
1019 */
1020 static int
1021qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1022{
1023 if (rmp->startp[midx] == NULL)
1024 return QF_FAIL;
1025 *tail = rmp->startp[midx];
1026 return QF_OK;
1027}
1028
1029/*
1030 * Parse the match for the pointer line ('%p') pattern in regmatch.
1031 * Return the matched value in "fields->col".
1032 */
1033 static int
1034qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1035{
1036 char_u *match_ptr;
1037
1038 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1039 return QF_FAIL;
1040 fields->col = 0;
1041 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1042 ++match_ptr)
1043 {
1044 ++fields->col;
1045 if (*match_ptr == TAB)
1046 {
1047 fields->col += 7;
1048 fields->col -= fields->col % 8;
1049 }
1050 }
1051 ++fields->col;
1052 fields->use_viscol = TRUE;
1053 return QF_OK;
1054}
1055
1056/*
1057 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1058 * Return the matched value in "fields->col".
1059 */
1060 static int
1061qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1062{
1063 if (rmp->startp[midx] == NULL)
1064 return QF_FAIL;
1065 fields->col = (int)atol((char *)rmp->startp[midx]);
1066 fields->use_viscol = TRUE;
1067 return QF_OK;
1068}
1069
1070/*
1071 * Parse the match for the search text ('%s') pattern in regmatch.
1072 * Return the matched value in "fields->pattern".
1073 */
1074 static int
1075qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1076{
1077 int len;
1078
1079 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1080 return QF_FAIL;
1081 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1082 if (len > CMDBUFFSIZE - 5)
1083 len = CMDBUFFSIZE - 5;
1084 STRCPY(fields->pattern, "^\\V");
1085 STRNCAT(fields->pattern, rmp->startp[midx], len);
1086 fields->pattern[len + 3] = '\\';
1087 fields->pattern[len + 4] = '$';
1088 fields->pattern[len + 5] = NUL;
1089 return QF_OK;
1090}
1091
1092/*
1093 * Parse the match for the module ('%o') pattern in regmatch.
1094 * Return the matched value in "fields->module".
1095 */
1096 static int
1097qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1098{
1099 int len;
1100
1101 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1102 return QF_FAIL;
1103 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1104 if (len > CMDBUFFSIZE)
1105 len = CMDBUFFSIZE;
1106 STRNCAT(fields->module, rmp->startp[midx], len);
1107 return QF_OK;
1108}
1109
1110/*
1111 * 'errorformat' format pattern parser functions.
1112 * The '%f' and '%r' formats are parsed differently from other formats.
1113 * See qf_parse_match() for details.
1114 */
1115static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1116{
1117 NULL,
1118 qf_parse_fmt_n,
1119 qf_parse_fmt_l,
1120 qf_parse_fmt_c,
1121 qf_parse_fmt_t,
1122 qf_parse_fmt_m,
1123 NULL,
1124 qf_parse_fmt_p,
1125 qf_parse_fmt_v,
1126 qf_parse_fmt_s,
1127 qf_parse_fmt_o
1128};
1129
1130/*
1131 * Parse the error format pattern matches in "regmatch" and set the values in
1132 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1133 * match. Returns QF_OK if all the matches are successfully parsed. On
1134 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001135 */
1136 static int
1137qf_parse_match(
1138 char_u *linebuf,
1139 int linelen,
1140 efm_T *fmt_ptr,
1141 regmatch_T *regmatch,
1142 qffields_T *fields,
1143 int qf_multiline,
1144 int qf_multiscan,
1145 char_u **tail)
1146{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001147 int idx = fmt_ptr->prefix;
1148 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001149 int midx;
1150 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001151
1152 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1153 return QF_FAIL;
1154 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1155 fields->type = idx;
1156 else
1157 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001158
1159 // Extract error message data from matched line.
1160 // We check for an actual submatch, because "\[" and "\]" in
1161 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001162 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001163 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001164 status = QF_OK;
1165 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001166 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001167 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1168 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001169 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001170 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001171 status = qf_parse_fmt_plus(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001172 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001173 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001174 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001175 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001176 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001177 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001178 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001179
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001180 if (status != QF_OK)
1181 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001182 }
1183
1184 return QF_OK;
1185}
1186
1187/*
1188 * Parse an error line in 'linebuf' using a single error format string in
1189 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1190 * Returns QF_OK if the efm format matches completely and the fields are
1191 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1192 */
1193 static int
1194qf_parse_get_fields(
1195 char_u *linebuf,
1196 int linelen,
1197 efm_T *fmt_ptr,
1198 qffields_T *fields,
1199 int qf_multiline,
1200 int qf_multiscan,
1201 char_u **tail)
1202{
1203 regmatch_T regmatch;
1204 int status = QF_FAIL;
1205 int r;
1206
1207 if (qf_multiscan &&
1208 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1209 return QF_FAIL;
1210
1211 fields->namebuf[0] = NUL;
1212 fields->module[0] = NUL;
1213 fields->pattern[0] = NUL;
1214 if (!qf_multiscan)
1215 fields->errmsg[0] = NUL;
1216 fields->lnum = 0;
1217 fields->col = 0;
1218 fields->use_viscol = FALSE;
1219 fields->enr = -1;
1220 fields->type = 0;
1221 *tail = NULL;
1222
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001223 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001224 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001225 regmatch.regprog = fmt_ptr->prog;
1226 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1227 fmt_ptr->prog = regmatch.regprog;
1228 if (r)
1229 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1230 fields, qf_multiline, qf_multiscan, tail);
1231
1232 return status;
1233}
1234
1235/*
1236 * Parse directory error format prefixes (%D and %X).
1237 * Push and pop directories from the directory stack when scanning directory
1238 * names.
1239 */
1240 static int
1241qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1242{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001243 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001244 {
1245 if (*fields->namebuf == NUL)
1246 {
1247 EMSG(_("E379: Missing or empty directory name"));
1248 return QF_FAIL;
1249 }
1250 qfl->qf_directory =
1251 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1252 if (qfl->qf_directory == NULL)
1253 return QF_FAIL;
1254 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001255 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001256 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1257
1258 return QF_OK;
1259}
1260
1261/*
1262 * Parse global file name error format prefixes (%O, %P and %Q).
1263 */
1264 static int
1265qf_parse_file_pfx(
1266 int idx,
1267 qffields_T *fields,
1268 qf_list_T *qfl,
1269 char_u *tail)
1270{
1271 fields->valid = FALSE;
1272 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1273 {
1274 if (*fields->namebuf && idx == 'P')
1275 qfl->qf_currfile =
1276 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1277 else if (idx == 'Q')
1278 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1279 *fields->namebuf = NUL;
1280 if (tail && *tail)
1281 {
1282 STRMOVE(IObuff, skipwhite(tail));
1283 qfl->qf_multiscan = TRUE;
1284 return QF_MULTISCAN;
1285 }
1286 }
1287
1288 return QF_OK;
1289}
1290
1291/*
1292 * Parse a non-error line (a line which doesn't match any of the error
1293 * format in 'efm').
1294 */
1295 static int
1296qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1297{
1298 char_u *p;
1299
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001300 fields->namebuf[0] = NUL; // no match found, remove file name
1301 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001302 fields->valid = FALSE;
1303 if (linelen >= fields->errmsglen)
1304 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001305 // linelen + null terminator
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001306 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1307 return QF_NOMEM;
1308 fields->errmsg = p;
1309 fields->errmsglen = linelen + 1;
1310 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001311 // copy whole line to error message
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001312 vim_strncpy(fields->errmsg, linebuf, linelen);
1313
1314 return QF_OK;
1315}
1316
1317/*
1318 * Parse multi-line error format prefixes (%C and %Z)
1319 */
1320 static int
1321qf_parse_multiline_pfx(
1322 qf_info_T *qi,
1323 int qf_idx,
1324 int idx,
1325 qf_list_T *qfl,
1326 qffields_T *fields)
1327{
1328 char_u *ptr;
1329 int len;
1330
1331 if (!qfl->qf_multiignore)
1332 {
1333 qfline_T *qfprev = qfl->qf_last;
1334
1335 if (qfprev == NULL)
1336 return QF_FAIL;
1337 if (*fields->errmsg && !qfl->qf_multiignore)
1338 {
1339 len = (int)STRLEN(qfprev->qf_text);
1340 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1341 == NULL)
1342 return QF_FAIL;
1343 STRCPY(ptr, qfprev->qf_text);
1344 vim_free(qfprev->qf_text);
1345 qfprev->qf_text = ptr;
1346 *(ptr += len) = '\n';
1347 STRCPY(++ptr, fields->errmsg);
1348 }
1349 if (qfprev->qf_nr == -1)
1350 qfprev->qf_nr = fields->enr;
1351 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001352 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001353 qfprev->qf_type = fields->type;
1354
1355 if (!qfprev->qf_lnum)
1356 qfprev->qf_lnum = fields->lnum;
1357 if (!qfprev->qf_col)
1358 qfprev->qf_col = fields->col;
1359 qfprev->qf_viscol = fields->use_viscol;
1360 if (!qfprev->qf_fnum)
1361 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1362 qfl->qf_directory,
1363 *fields->namebuf || qfl->qf_directory != NULL
1364 ? fields->namebuf
1365 : qfl->qf_currfile != NULL && fields->valid
1366 ? qfl->qf_currfile : 0);
1367 }
1368 if (idx == 'Z')
1369 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1370 line_breakcheck();
1371
1372 return QF_IGNORE_LINE;
1373}
1374
1375/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001376 * Parse a line and get the quickfix fields.
1377 * Return the QF_ status.
1378 */
1379 static int
1380qf_parse_line(
1381 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001382 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001383 char_u *linebuf,
1384 int linelen,
1385 efm_T *fmt_first,
1386 qffields_T *fields)
1387{
1388 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001389 int idx = 0;
1390 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001391 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001392 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001393
Bram Moolenaare333e792018-04-08 13:27:39 +02001394restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001395 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001396 if (fmt_start == NULL)
1397 fmt_ptr = fmt_first;
1398 else
1399 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001400 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001401 fmt_ptr = fmt_start;
1402 fmt_start = NULL;
1403 }
1404
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001405 // Try to match each part of 'errorformat' until we find a complete
1406 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001407 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001408 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1409 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001410 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001411 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1412 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1413 if (status == QF_NOMEM)
1414 return status;
1415 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001416 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001417 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001418 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001419
1420 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1421 {
1422 if (fmt_ptr != NULL)
1423 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001424 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001425 status = qf_parse_dir_pfx(idx, fields, qfl);
1426 if (status != QF_OK)
1427 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001428 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001429
1430 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1431 if (status != QF_OK)
1432 return status;
1433
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001434 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001435 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001436 }
1437 else if (fmt_ptr != NULL)
1438 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001439 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001440 if (fmt_ptr->conthere)
1441 fmt_start = fmt_ptr;
1442
1443 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1444 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001445 qfl->qf_multiline = TRUE; // start of a multi-line message
1446 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001447 }
1448 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001449 { // continuation of multi-line msg
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001450 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1451 if (status != QF_OK)
1452 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001453 }
1454 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001455 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001456 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1457 if (status == QF_MULTISCAN)
1458 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001459 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001460 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001461 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001462 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001463 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001464 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001465 return QF_IGNORE_LINE;
1466 }
1467 }
1468
1469 return QF_OK;
1470}
1471
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001472/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001473 * Returns TRUE if the specified quickfix/location stack is empty
1474 */
1475 static int
1476qf_stack_empty(qf_info_T *qi)
1477{
1478 return qi == NULL || qi->qf_listcount <= 0;
1479}
1480
1481/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001482 * Returns TRUE if the specified quickfix/location list is empty.
1483 */
1484 static int
1485qf_list_empty(qf_info_T *qi, int qf_idx)
1486{
1487 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1488 return TRUE;
1489 return qi->qf_lists[qf_idx].qf_count <= 0;
1490}
1491
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001492/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001493 * Allocate the fields used for parsing lines and populating a quickfix list.
1494 */
1495 static int
1496qf_alloc_fields(qffields_T *pfields)
1497{
1498 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1499 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1500 pfields->errmsglen = CMDBUFFSIZE + 1;
1501 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1502 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1503 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1504 || pfields->pattern == NULL || pfields->module == NULL)
1505 return FAIL;
1506
1507 return OK;
1508}
1509
1510/*
1511 * Free the fields used for parsing lines and populating a quickfix list.
1512 */
1513 static void
1514qf_free_fields(qffields_T *pfields)
1515{
1516 vim_free(pfields->namebuf);
1517 vim_free(pfields->module);
1518 vim_free(pfields->errmsg);
1519 vim_free(pfields->pattern);
1520}
1521
1522/*
1523 * Setup the state information used for parsing lines and populating a
1524 * quickfix list.
1525 */
1526 static int
1527qf_setup_state(
1528 qfstate_T *pstate,
1529 char_u *enc,
1530 char_u *efile,
1531 typval_T *tv,
1532 buf_T *buf,
1533 linenr_T lnumfirst,
1534 linenr_T lnumlast)
1535{
1536#ifdef FEAT_MBYTE
1537 pstate->vc.vc_type = CONV_NONE;
1538 if (enc != NULL && *enc != NUL)
1539 convert_setup(&pstate->vc, enc, p_enc);
1540#endif
1541
1542 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1543 {
1544 EMSG2(_(e_openerrf), efile);
1545 return FAIL;
1546 }
1547
1548 if (tv != NULL)
1549 {
1550 if (tv->v_type == VAR_STRING)
1551 pstate->p_str = tv->vval.v_string;
1552 else if (tv->v_type == VAR_LIST)
1553 pstate->p_li = tv->vval.v_list->lv_first;
1554 pstate->tv = tv;
1555 }
1556 pstate->buf = buf;
1557 pstate->buflnum = lnumfirst;
1558 pstate->lnumlast = lnumlast;
1559
1560 return OK;
1561}
1562
1563/*
1564 * Cleanup the state information used for parsing lines and populating a
1565 * quickfix list.
1566 */
1567 static void
1568qf_cleanup_state(qfstate_T *pstate)
1569{
1570 if (pstate->fd != NULL)
1571 fclose(pstate->fd);
1572
1573 vim_free(pstate->growbuf);
1574#ifdef FEAT_MBYTE
1575 if (pstate->vc.vc_type != CONV_NONE)
1576 convert_setup(&pstate->vc, NULL, NULL);
1577#endif
1578}
1579
1580/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001581 * Read the errorfile "efile" into memory, line by line, building the error
1582 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001583 * Alternative: when "efile" is NULL read errors from buffer "buf".
1584 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001585 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001586 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1587 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001588 * Return -1 for error, number of errors for success.
1589 */
1590 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001591qf_init_ext(
1592 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001593 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001594 char_u *efile,
1595 buf_T *buf,
1596 typval_T *tv,
1597 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001598 int newlist, // TRUE: start a new error list
1599 linenr_T lnumfirst, // first line number to use
1600 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001601 char_u *qf_title,
1602 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001603{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001604 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001605 qfstate_T state;
1606 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001607 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001608 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001609 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001611 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001612 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001613 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001615 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001616 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001617
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001618 vim_memset(&state, 0, sizeof(state));
1619 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001620 if ((qf_alloc_fields(&fields) == FAIL) ||
1621 (qf_setup_state(&state, enc, efile, tv, buf,
1622 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 goto qf_init_end;
1624
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001625 if (newlist || qf_idx == qi->qf_listcount)
1626 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001627 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001628 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001629 qf_idx = qi->qf_curlist;
1630 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001631 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001632 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001633 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001634 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001635 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001636 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001639 qfl = &qi->qf_lists[qf_idx];
1640
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001641 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001642 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001643 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 else
1645 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001647 // If the errorformat didn't change between calls, then reuse the
1648 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001649 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1650 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001651 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001652 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001653 free_efm_list(&fmt_first);
1654
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001655 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001656 fmt_first = parse_efm_option(efm);
1657 if (fmt_first != NULL)
1658 last_efm = vim_strsave(efm);
1659 }
1660
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001661 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001664 // got_int is reset here, because it was probably set when killing the
1665 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 got_int = FALSE;
1667
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001668 // Read the lines in the error file one by one.
1669 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001670 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001672 // Get the next line from a file/buffer/list/string
Bram Moolenaare0d37972016-07-15 22:36:01 +02001673 status = qf_get_nextline(&state);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001674 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001675 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001676 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001677 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001679 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1680 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001681 if (status == QF_FAIL)
1682 goto error2;
1683 if (status == QF_NOMEM)
1684 goto qf_init_end;
1685 if (status == QF_IGNORE_LINE)
1686 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001688 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001689 qf_idx,
1690 qfl->qf_directory,
1691 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001692 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001693 : ((qfl->qf_currfile != NULL && fields.valid)
1694 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001695 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001696 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001697 fields.errmsg,
1698 fields.lnum,
1699 fields.col,
1700 fields.use_viscol,
1701 fields.pattern,
1702 fields.enr,
1703 fields.type,
1704 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 goto error2;
1706 line_breakcheck();
1707 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001708 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001710 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001712 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001713 qfl->qf_ptr = qfl->qf_start;
1714 qfl->qf_index = 1;
1715 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 }
1717 else
1718 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001719 qfl->qf_nonevalid = FALSE;
1720 if (qfl->qf_ptr == NULL)
1721 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001723 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001724 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001725 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 }
1727 EMSG(_(e_readerrf));
1728error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001729 if (!adding)
1730 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001731 // Error when creating a new list. Free the new list
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001732 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001733 qi->qf_listcount--;
1734 if (qi->qf_curlist > 0)
1735 --qi->qf_curlist;
1736 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001737qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001738 if (qf_idx == qi->qf_curlist)
1739 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001740 qf_cleanup_state(&state);
1741 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
1743 return retval;
1744}
1745
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001746/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001747 * Read the errorfile "efile" into memory, line by line, building the error
1748 * list. Set the error list's title to qf_title.
1749 * Return -1 for error, number of errors for success.
1750 */
1751 int
1752qf_init(win_T *wp,
1753 char_u *efile,
1754 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001755 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001756 char_u *qf_title,
1757 char_u *enc)
1758{
1759 qf_info_T *qi = &ql_info;
1760
1761 if (wp != NULL)
1762 {
1763 qi = ll_get_or_alloc_list(wp);
1764 if (qi == NULL)
1765 return FAIL;
1766 }
1767
1768 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1769 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1770}
1771
1772/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001773 * Set the title of the specified quickfix list. Frees the previous title.
1774 * Prepends ':' to the title.
1775 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001776 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001777qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001778{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001779 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001780
Bram Moolenaarfb604092014-07-23 15:55:00 +02001781 if (title != NULL)
1782 {
1783 char_u *p = alloc((int)STRLEN(title) + 2);
1784
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001785 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001786 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001787 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001788 }
1789}
1790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001792 * The title of a quickfix/location list is set, by default, to the command
1793 * that created the quickfix list with the ":" prefix.
1794 * Create a quickfix list title string by prepending ":" to a user command.
1795 * Returns a pointer to a static buffer with the title.
1796 */
1797 static char_u *
1798qf_cmdtitle(char_u *cmd)
1799{
1800 static char_u qftitle_str[IOSIZE];
1801
1802 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1803 return qftitle_str;
1804}
1805
1806/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001807 * Prepare for adding a new quickfix list. If the current list is in the
1808 * middle of the stack, then all the following lists are freed and then
1809 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 */
1811 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001812qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813{
1814 int i;
1815
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001816 // If the current entry is not the last entry, delete entries beyond
1817 // the current entry. This makes it possible to browse in a tree-like
1818 // way with ":grep'.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001819 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001820 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001822 // When the stack is full, remove to oldest entry
1823 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001824 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001826 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001828 qi->qf_lists[i - 1] = qi->qf_lists[i];
1829 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 }
1831 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001832 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001833 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001834 qf_store_title(&qi->qf_lists[qi->qf_curlist], qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001835 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836}
1837
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001838/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001839 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001840 */
1841 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001842ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001843{
1844 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001845 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001846
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001847 qi = *pqi;
1848 if (qi == NULL)
1849 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001850 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001851
1852 qi->qf_refcount--;
1853 if (qi->qf_refcount < 1)
1854 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001855 // No references to this location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001856 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001857 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001858 vim_free(qi);
1859 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001860}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001861
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001862/*
1863 * Free all the quickfix/location lists in the stack.
1864 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001865 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001866qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001867{
1868 int i;
1869 qf_info_T *qi = &ql_info;
1870
1871 if (wp != NULL)
1872 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001873 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001874 ll_free_all(&wp->w_llist);
1875 ll_free_all(&wp->w_llist_ref);
1876 }
1877 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001878 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001879 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001880 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001881}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883/*
1884 * Add an entry to the end of the list of errors.
1885 * Returns OK or FAIL.
1886 */
1887 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001888qf_add_entry(
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001889 qf_info_T *qi, // quickfix list
1890 int qf_idx, // list index
1891 char_u *dir, // optional directory name
1892 char_u *fname, // file name or NULL
1893 char_u *module, // module name or NULL
1894 int bufnum, // buffer number or zero
1895 char_u *mesg, // message
1896 long lnum, // line number
1897 int col, // column
1898 int vis_col, // using visual column
1899 char_u *pattern, // search pattern
1900 int nr, // error number
1901 int type, // type character
1902 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001904 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001905 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001906 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001908 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001910 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001911 {
1912 buf_T *buf = buflist_findnr(bufnum);
1913
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001914 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001915 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001916 buf->b_has_qf_entry |=
Bram Moolenaar4d77c652018-08-18 19:59:54 +02001917 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001918 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001919 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001920 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1922 {
1923 vim_free(qfp);
1924 return FAIL;
1925 }
1926 qfp->qf_lnum = lnum;
1927 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001928 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001929 if (pattern == NULL || *pattern == NUL)
1930 qfp->qf_pattern = NULL;
1931 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1932 {
1933 vim_free(qfp->qf_text);
1934 vim_free(qfp);
1935 return FAIL;
1936 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001937 if (module == NULL || *module == NUL)
1938 qfp->qf_module = NULL;
1939 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
1940 {
1941 vim_free(qfp->qf_text);
1942 vim_free(qfp->qf_pattern);
1943 vim_free(qfp);
1944 return FAIL;
1945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001947 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 type = 0;
1949 qfp->qf_type = type;
1950 qfp->qf_valid = valid;
1951
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001952 lastp = &qfl->qf_last;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001953 if (qf_list_empty(qi, qf_idx)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001955 qfl->qf_start = qfp;
1956 qfl->qf_ptr = qfp;
1957 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001958 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 }
1960 else
1961 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001962 qfp->qf_prev = *lastp;
1963 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001965 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001967 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001968 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001969 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001971 qfl->qf_index = qfl->qf_count;
1972 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 }
1974
1975 return OK;
1976}
1977
1978/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001979 * Allocate a new location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001980 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001981 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001982ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001983{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001984 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001985
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02001986 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001987 if (qi != NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001988 qi->qf_refcount++;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001989 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001990}
1991
1992/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001993 * Return the location list stack for window 'wp'.
1994 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001995 */
1996 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001997ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001998{
1999 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002000 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002001 return wp->w_llist_ref;
2002
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002003 // For a non-location list window, w_llist_ref should not point to a
2004 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002005 ll_free_all(&wp->w_llist_ref);
2006
2007 if (wp->w_llist == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002008 wp->w_llist = ll_new_list(); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002009 return wp->w_llist;
2010}
2011
2012/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002013 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2014 */
2015 static int
2016copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2017{
2018 int i;
2019 qfline_T *from_qfp;
2020 qfline_T *prevp;
2021
2022 // copy all the location entries in this list
2023 for (i = 0, from_qfp = from_qfl->qf_start;
2024 i < from_qfl->qf_count && from_qfp != NULL;
2025 ++i, from_qfp = from_qfp->qf_next)
2026 {
2027 if (qf_add_entry(to_qi,
2028 to_qi->qf_curlist,
2029 NULL,
2030 NULL,
2031 from_qfp->qf_module,
2032 0,
2033 from_qfp->qf_text,
2034 from_qfp->qf_lnum,
2035 from_qfp->qf_col,
2036 from_qfp->qf_viscol,
2037 from_qfp->qf_pattern,
2038 from_qfp->qf_nr,
2039 0,
2040 from_qfp->qf_valid) == FAIL)
2041 return FAIL;
2042
2043 // qf_add_entry() will not set the qf_num field, as the
2044 // directory and file names are not supplied. So the qf_fnum
2045 // field is copied here.
2046 prevp = to_qfl->qf_last;
2047 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2048 prevp->qf_type = from_qfp->qf_type; // error type
2049 if (from_qfl->qf_ptr == from_qfp)
2050 to_qfl->qf_ptr = prevp; // current location
2051 }
2052
2053 return OK;
2054}
2055
2056/*
2057 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2058 */
2059 static int
2060copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2061{
2062 // Some of the fields are populated by qf_add_entry()
2063 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2064 to_qfl->qf_count = 0;
2065 to_qfl->qf_index = 0;
2066 to_qfl->qf_start = NULL;
2067 to_qfl->qf_last = NULL;
2068 to_qfl->qf_ptr = NULL;
2069 if (from_qfl->qf_title != NULL)
2070 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2071 else
2072 to_qfl->qf_title = NULL;
2073 if (from_qfl->qf_ctx != NULL)
2074 {
2075 to_qfl->qf_ctx = alloc_tv();
2076 if (to_qfl->qf_ctx != NULL)
2077 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2078 }
2079 else
2080 to_qfl->qf_ctx = NULL;
2081
2082 if (from_qfl->qf_count)
2083 if (copy_loclist_entries(from_qfl, to_qfl, to_qi) == FAIL)
2084 return FAIL;
2085
2086 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2087
2088 // Assign a new ID for the location list
2089 to_qfl->qf_id = ++last_qf_id;
2090 to_qfl->qf_changedtick = 0L;
2091
2092 // When no valid entries are present in the list, qf_ptr points to
2093 // the first item in the list
2094 if (to_qfl->qf_nonevalid)
2095 {
2096 to_qfl->qf_ptr = to_qfl->qf_start;
2097 to_qfl->qf_index = 1;
2098 }
2099
2100 return OK;
2101}
2102
2103/*
2104 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002105 */
2106 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002107copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002108{
2109 qf_info_T *qi;
2110 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002111
Bram Moolenaar09037502018-09-25 22:08:14 +02002112 // When copying from a location list window, copy the referenced
2113 // location list. For other windows, copy the location list for
2114 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002115 if (IS_LL_WINDOW(from))
2116 qi = from->w_llist_ref;
2117 else
2118 qi = from->w_llist;
2119
Bram Moolenaar09037502018-09-25 22:08:14 +02002120 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002121 return;
2122
Bram Moolenaar09037502018-09-25 22:08:14 +02002123 // allocate a new location list
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002124 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002125 return;
2126
2127 to->w_llist->qf_listcount = qi->qf_listcount;
2128
Bram Moolenaar09037502018-09-25 22:08:14 +02002129 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002130 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002131 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002132 to->w_llist->qf_curlist = idx;
2133
Bram Moolenaar09037502018-09-25 22:08:14 +02002134 if (copy_loclist(&qi->qf_lists[idx],
2135 &to->w_llist->qf_lists[idx], to->w_llist) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002136 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002137 qf_free_all(to);
2138 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002139 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002140 }
2141
Bram Moolenaar09037502018-09-25 22:08:14 +02002142 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002143}
2144
2145/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002146 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002147 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 */
2149 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002150qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151{
Bram Moolenaar82404332016-07-10 17:00:38 +02002152 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002153 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002154 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002155
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002156 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158
Bram Moolenaare60acc12011-05-10 16:41:25 +02002159#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002160 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002161#endif
2162#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002163 if (directory != NULL)
2164 slash_adjust(directory);
2165 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002166#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002167 if (directory != NULL && !vim_isAbsName(fname)
2168 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2169 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002170 // Here we check if the file really exists.
2171 // This should normally be true, but if make works without
2172 // "leaving directory"-messages we might have missed a
2173 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002174 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 vim_free(ptr);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002177 directory = qf_guess_filepath(&qi->qf_lists[qf_idx], fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002178 if (directory)
2179 ptr = concat_fnames(directory, fname, TRUE);
2180 else
2181 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002183 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002184 bufname = ptr;
2185 }
2186 else
2187 bufname = fname;
2188
2189 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002190 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002191 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002192 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002193 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002195 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002196 {
2197 vim_free(qf_last_bufname);
2198 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2199 if (bufname == ptr)
2200 qf_last_bufname = bufname;
2201 else
2202 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002203 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002204 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002205 if (buf == NULL)
2206 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002207
Bram Moolenaarc1542742016-07-20 21:44:37 +02002208 buf->b_has_qf_entry =
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002209 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002210 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211}
2212
2213/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002214 * Push dirbuf onto the directory stack and return pointer to actual dir or
2215 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 */
2217 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002218qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219{
2220 struct dir_stack_T *ds_new;
2221 struct dir_stack_T *ds_ptr;
2222
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002223 // allocate new stack element and hook it in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2225 if (ds_new == NULL)
2226 return NULL;
2227
2228 ds_new->next = *stackptr;
2229 *stackptr = ds_new;
2230
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002231 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 if (vim_isAbsName(dirbuf)
2233 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002234 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 (*stackptr)->dirname = vim_strsave(dirbuf);
2236 else
2237 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002238 // Okay we don't have an absolute path.
2239 // dirbuf must be a subdir of one of the directories on the stack.
2240 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 ds_new = (*stackptr)->next;
2242 (*stackptr)->dirname = NULL;
2243 while (ds_new)
2244 {
2245 vim_free((*stackptr)->dirname);
2246 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2247 TRUE);
2248 if (mch_isdir((*stackptr)->dirname) == TRUE)
2249 break;
2250
2251 ds_new = ds_new->next;
2252 }
2253
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002254 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 while ((*stackptr)->next != ds_new)
2256 {
2257 ds_ptr = (*stackptr)->next;
2258 (*stackptr)->next = (*stackptr)->next->next;
2259 vim_free(ds_ptr->dirname);
2260 vim_free(ds_ptr);
2261 }
2262
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002263 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 if (ds_new == NULL)
2265 {
2266 vim_free((*stackptr)->dirname);
2267 (*stackptr)->dirname = vim_strsave(dirbuf);
2268 }
2269 }
2270
2271 if ((*stackptr)->dirname != NULL)
2272 return (*stackptr)->dirname;
2273 else
2274 {
2275 ds_ptr = *stackptr;
2276 *stackptr = (*stackptr)->next;
2277 vim_free(ds_ptr);
2278 return NULL;
2279 }
2280}
2281
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282/*
2283 * pop dirbuf from the directory stack and return previous directory or NULL if
2284 * stack is empty
2285 */
2286 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002287qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
2289 struct dir_stack_T *ds_ptr;
2290
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002291 // TODO: Should we check if dirbuf is the directory on top of the stack?
2292 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002294 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 if (*stackptr != NULL)
2296 {
2297 ds_ptr = *stackptr;
2298 *stackptr = (*stackptr)->next;
2299 vim_free(ds_ptr->dirname);
2300 vim_free(ds_ptr);
2301 }
2302
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002303 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 return *stackptr ? (*stackptr)->dirname : NULL;
2305}
2306
2307/*
2308 * clean up directory stack
2309 */
2310 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002311qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312{
2313 struct dir_stack_T *ds_ptr;
2314
2315 while ((ds_ptr = *stackptr) != NULL)
2316 {
2317 *stackptr = (*stackptr)->next;
2318 vim_free(ds_ptr->dirname);
2319 vim_free(ds_ptr);
2320 }
2321}
2322
2323/*
2324 * Check in which directory of the directory stack the given file can be
2325 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002326 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 * Cleans up intermediate directory entries.
2328 *
2329 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002330 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 * ./
2332 * ./aa
2333 * ./aa/bb
2334 * ./bb
2335 * ./bb/x.c
2336 * and make says:
2337 * making all in aa
2338 * making all in bb
2339 * x.c:9: Error
2340 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2341 * qf_guess_filepath will return NULL.
2342 */
2343 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002344qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345{
2346 struct dir_stack_T *ds_ptr;
2347 struct dir_stack_T *ds_tmp;
2348 char_u *fullname;
2349
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002350 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002351 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 return NULL;
2353
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002354 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 fullname = NULL;
2356 while (ds_ptr)
2357 {
2358 vim_free(fullname);
2359 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2360
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002361 // If concat_fnames failed, just go on. The worst thing that can happen
2362 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2364 break;
2365
2366 ds_ptr = ds_ptr->next;
2367 }
2368
2369 vim_free(fullname);
2370
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002371 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002372 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002374 ds_tmp = qfl->qf_dir_stack->next;
2375 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 vim_free(ds_tmp->dirname);
2377 vim_free(ds_tmp);
2378 }
2379
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002380 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381}
2382
2383/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002384 * Returns TRUE if a quickfix/location list with the given identifier exists.
2385 */
2386 static int
2387qflist_valid (win_T *wp, int_u qf_id)
2388{
2389 qf_info_T *qi = &ql_info;
2390 int i;
2391
2392 if (wp != NULL)
2393 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002394 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002395 if (qi == NULL)
2396 return FALSE;
2397 }
2398
2399 for (i = 0; i < qi->qf_listcount; ++i)
2400 if (qi->qf_lists[i].qf_id == qf_id)
2401 return TRUE;
2402
2403 return FALSE;
2404}
2405
2406/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002407 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002408 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002409 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002410 * Similar to location list.
2411 */
2412 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002413is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002414{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002415 qfline_T *qfp;
2416 int i;
2417
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002418 // Search for the entry in the current list
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002419 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2420 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002421 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002422 break;
2423
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002424 if (i == qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002425 return FALSE;
2426
2427 return TRUE;
2428}
2429
2430/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002431 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002432 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002433 */
2434 static qfline_T *
2435get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002436 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002437 qfline_T *qf_ptr,
2438 int *qf_index,
2439 int dir)
2440{
2441 int idx;
2442 int old_qf_fnum;
2443
2444 idx = *qf_index;
2445 old_qf_fnum = qf_ptr->qf_fnum;
2446
2447 do
2448 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002449 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002450 return NULL;
2451 ++idx;
2452 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002453 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002454 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2455
2456 *qf_index = idx;
2457 return qf_ptr;
2458}
2459
2460/*
2461 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002462 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002463 */
2464 static qfline_T *
2465get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002466 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002467 qfline_T *qf_ptr,
2468 int *qf_index,
2469 int dir)
2470{
2471 int idx;
2472 int old_qf_fnum;
2473
2474 idx = *qf_index;
2475 old_qf_fnum = qf_ptr->qf_fnum;
2476
2477 do
2478 {
2479 if (idx == 1 || qf_ptr->qf_prev == NULL)
2480 return NULL;
2481 --idx;
2482 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002483 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002484 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2485
2486 *qf_index = idx;
2487 return qf_ptr;
2488}
2489
2490/*
2491 * Get the n'th (errornr) previous/next valid entry from the current entry in
2492 * the quickfix list.
2493 * dir == FORWARD or FORWARD_FILE: next valid entry
2494 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2495 */
2496 static qfline_T *
2497get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002498 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002499 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002500 int dir,
2501 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002502{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002503 qfline_T *qf_ptr = qfl->qf_ptr;
2504 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002505 qfline_T *prev_qf_ptr;
2506 int prev_index;
2507 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2508 char_u *err = e_no_more_items;
2509
2510 while (errornr--)
2511 {
2512 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002513 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002514
2515 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002516 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002517 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002518 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002519 if (qf_ptr == NULL)
2520 {
2521 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002522 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002523 if (err != NULL)
2524 {
2525 EMSG(_(err));
2526 return NULL;
2527 }
2528 break;
2529 }
2530
2531 err = NULL;
2532 }
2533
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002534 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002535 return qf_ptr;
2536}
2537
2538/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002539 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2540 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002541 */
2542 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002543get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002544{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002545 qfline_T *qf_ptr = qfl->qf_ptr;
2546 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002547
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002548 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002549 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2550 {
2551 --qf_idx;
2552 qf_ptr = qf_ptr->qf_prev;
2553 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002554 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002555 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2556 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002557 {
2558 ++qf_idx;
2559 qf_ptr = qf_ptr->qf_next;
2560 }
2561
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002562 *new_qfidx = qf_idx;
2563 return qf_ptr;
2564}
2565
2566/*
2567 * Get a entry specied by 'errornr' and 'dir' from the current
2568 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2569 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2570 * Returns a pointer to the entry and the index of the new entry is stored in
2571 * 'new_qfidx'.
2572 */
2573 static qfline_T *
2574qf_get_entry(
2575 qf_list_T *qfl,
2576 int errornr,
2577 int dir,
2578 int *new_qfidx)
2579{
2580 qfline_T *qf_ptr = qfl->qf_ptr;
2581 int qfidx = qfl->qf_index;
2582
2583 if (dir != 0) // next/prev valid entry
2584 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2585 else if (errornr != 0) // go to specified number
2586 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2587
2588 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002589 return qf_ptr;
2590}
2591
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002592/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002593 * Find a window displaying a Vim help file.
2594 */
2595 static win_T *
2596qf_find_help_win(void)
2597{
2598 win_T *wp;
2599
2600 FOR_ALL_WINDOWS(wp)
2601 if (bt_help(wp->w_buffer))
2602 return wp;
2603
2604 return NULL;
2605}
2606
2607/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002608 * Find a help window or open one.
2609 */
2610 static int
2611jump_to_help_window(qf_info_T *qi, int *opened_window)
2612{
2613 win_T *wp;
2614 int flags;
2615
2616 if (cmdmod.tab != 0)
2617 wp = NULL;
2618 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002619 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002620 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2621 win_enter(wp, TRUE);
2622 else
2623 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002624 // Split off help window; put it at far top if no position
2625 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002626 flags = WSP_HELP;
2627 if (cmdmod.split == 0 && curwin->w_width != Columns
2628 && curwin->w_width < 80)
2629 flags |= WSP_TOP;
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002630 if (IS_LL_STACK(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002631 flags |= WSP_NEWLOC; // don't copy the location list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002632
2633 if (win_split(0, flags) == FAIL)
2634 return FAIL;
2635
2636 *opened_window = TRUE;
2637
2638 if (curwin->w_height < p_hh)
2639 win_setheight((int)p_hh);
2640
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002641 if (IS_LL_STACK(qi)) // not a quickfix list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002642 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002643 // The new window should use the supplied location list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002644 curwin->w_llist = qi;
2645 qi->qf_refcount++;
2646 }
2647 }
2648
2649 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002650 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002651
2652 return OK;
2653}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002654
2655/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002656 * Find a non-quickfix window using the given location list.
2657 * Returns NULL if a matching window is not found.
2658 */
2659 static win_T *
2660qf_find_win_with_loclist(qf_info_T *ll)
2661{
2662 win_T *wp;
2663
2664 FOR_ALL_WINDOWS(wp)
2665 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2666 return wp;
2667
2668 return NULL;
2669}
2670
2671/*
2672 * Find a window containing a normal buffer
2673 */
2674 static win_T *
2675qf_find_win_with_normal_buf(void)
2676{
2677 win_T *wp;
2678
2679 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002680 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002681 return wp;
2682
2683 return NULL;
2684}
2685
2686/*
2687 * Go to a window in any tabpage containing the specified file. Returns TRUE
2688 * if successfully jumped to the window. Otherwise returns FALSE.
2689 */
2690 static int
2691qf_goto_tabwin_with_file(int fnum)
2692{
2693 tabpage_T *tp;
2694 win_T *wp;
2695
2696 FOR_ALL_TAB_WINDOWS(tp, wp)
2697 if (wp->w_buffer->b_fnum == fnum)
2698 {
2699 goto_tabpage_win(tp, wp);
2700 return TRUE;
2701 }
2702
2703 return FALSE;
2704}
2705
2706/*
2707 * Create a new window to show a file above the quickfix window. Called when
2708 * only the quickfix window is present.
2709 */
2710 static int
2711qf_open_new_file_win(qf_info_T *ll_ref)
2712{
2713 int flags;
2714
2715 flags = WSP_ABOVE;
2716 if (ll_ref != NULL)
2717 flags |= WSP_NEWLOC;
2718 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002719 return FAIL; // not enough room for window
2720 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002721 swb_flags = 0;
2722 RESET_BINDING(curwin);
2723 if (ll_ref != NULL)
2724 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002725 // The new window should use the location list from the
2726 // location list window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002727 curwin->w_llist = ll_ref;
2728 ll_ref->qf_refcount++;
2729 }
2730 return OK;
2731}
2732
2733/*
2734 * Go to a window that shows the right buffer. If the window is not found, go
2735 * to the window just above the location list window. This is used for opening
2736 * a file from a location window and not from a quickfix window. If some usable
2737 * window is previously found, then it is supplied in 'use_win'.
2738 */
2739 static void
2740qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2741{
2742 win_T *win = use_win;
2743
2744 if (win == NULL)
2745 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002746 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002747 FOR_ALL_WINDOWS(win)
2748 if (win->w_buffer->b_fnum == qf_fnum)
2749 break;
2750 if (win == NULL)
2751 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002752 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002753 win = curwin;
2754 do
2755 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002756 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002757 break;
2758 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002759 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002760 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002761 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002762 } while (win != curwin);
2763 }
2764 }
2765 win_goto(win);
2766
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002767 // If the location list for the window is not set, then set it
2768 // to the location list from the location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002769 if (win->w_llist == NULL)
2770 {
2771 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002772 if (ll_ref != NULL)
2773 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002774 }
2775}
2776
2777/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002778 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2779 * not found, then go to the window just above the quickfix window. This is
2780 * used for opening a file from a quickfix window and not from a location
2781 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002782 */
2783 static void
2784qf_goto_win_with_qfl_file(int qf_fnum)
2785{
2786 win_T *win;
2787 win_T *altwin;
2788
2789 win = curwin;
2790 altwin = NULL;
2791 for (;;)
2792 {
2793 if (win->w_buffer->b_fnum == qf_fnum)
2794 break;
2795 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002796 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002797 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002798 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002799
2800 if (IS_QF_WINDOW(win))
2801 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002802 // Didn't find it, go to the window before the quickfix
2803 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002804 if (altwin != NULL)
2805 win = altwin;
2806 else if (curwin->w_prev != NULL)
2807 win = curwin->w_prev;
2808 else
2809 win = curwin->w_next;
2810 break;
2811 }
2812
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002813 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02002814 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002815 altwin = win;
2816 }
2817
2818 win_goto(win);
2819}
2820
2821/*
2822 * Find a suitable window for opening a file (qf_fnum) from the
2823 * quickfix/location list and jump to it. If the file is already opened in a
2824 * window, jump to it. Otherwise open a new window to display the file. This is
2825 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002826 */
2827 static int
2828qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2829{
2830 win_T *usable_win_ptr = NULL;
2831 int usable_win;
2832 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002833 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002834
2835 usable_win = 0;
2836
2837 ll_ref = curwin->w_llist_ref;
2838 if (ll_ref != NULL)
2839 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002840 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002841 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2842 if (usable_win_ptr != NULL)
2843 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002844 }
2845
2846 if (!usable_win)
2847 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002848 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002849 win = qf_find_win_with_normal_buf();
2850 if (win != NULL)
2851 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002852 }
2853
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002854 // If no usable window is found and 'switchbuf' contains "usetab"
2855 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002856 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002857 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002858
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002859 // If there is only one window and it is the quickfix window, create a
2860 // new one above the quickfix window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002861 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2862 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002863 if (qf_open_new_file_win(ll_ref) != OK)
2864 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002865 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002866 }
2867 else
2868 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002869 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002870 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002871 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002872 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002873 }
2874
2875 return OK;
2876}
2877
2878/*
2879 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002880 * Returns OK if successfully edited the file, FAIL on failing to open the
2881 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
2882 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002883 */
2884 static int
2885qf_jump_edit_buffer(
2886 qf_info_T *qi,
2887 qfline_T *qf_ptr,
2888 int forceit,
2889 win_T *oldwin,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002890 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002891{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002892 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002893 int retval = OK;
2894
2895 if (qf_ptr->qf_type == 1)
2896 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002897 // Open help file (do_ecmd() will set b_help flag, readfile() will
2898 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002899 if (!can_abandon(curbuf, forceit))
2900 {
2901 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002902 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002903 }
2904 else
2905 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2906 ECMD_HIDE + ECMD_SET_HELP,
2907 oldwin == curwin ? curwin : NULL);
2908 }
2909 else
2910 {
2911 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002912 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002913
2914 retval = buflist_getfile(qf_ptr->qf_fnum,
2915 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002916
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002917 if (IS_LL_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002918 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002919 // Location list. Check whether the associated window is still
2920 // present and the list is still valid.
Bram Moolenaar3c097222017-12-21 20:54:49 +01002921 if (!win_valid_any_tab(oldwin))
2922 {
2923 EMSG(_("E924: Current window was closed"));
Bram Moolenaar3c097222017-12-21 20:54:49 +01002924 *opened_window = FALSE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002925 return NOTDONE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002926 }
2927 else if (!qflist_valid(oldwin, save_qfid))
2928 {
2929 EMSG(_(e_loc_list_changed));
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002930 return NOTDONE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002931 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002932 }
2933 else if (old_qf_curlist != qi->qf_curlist
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002934 || !is_qf_entry_present(qfl, qf_ptr))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002935 {
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002936 if (IS_QF_STACK(qi))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002937 EMSG(_("E925: Current quickfix was changed"));
2938 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002939 EMSG(_(e_loc_list_changed));
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002940 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002941 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002942 }
2943
2944 return retval;
2945}
2946
2947/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002948 * Go to the error line in the current file using either line/column number or
2949 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002950 */
2951 static void
2952qf_jump_goto_line(
2953 linenr_T qf_lnum,
2954 int qf_col,
2955 char_u qf_viscol,
2956 char_u *qf_pattern)
2957{
2958 linenr_T i;
2959 char_u *line;
2960 colnr_T screen_col;
2961 colnr_T char_col;
2962
2963 if (qf_pattern == NULL)
2964 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002965 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002966 i = qf_lnum;
2967 if (i > 0)
2968 {
2969 if (i > curbuf->b_ml.ml_line_count)
2970 i = curbuf->b_ml.ml_line_count;
2971 curwin->w_cursor.lnum = i;
2972 }
2973 if (qf_col > 0)
2974 {
2975 curwin->w_cursor.col = qf_col - 1;
2976#ifdef FEAT_VIRTUALEDIT
2977 curwin->w_cursor.coladd = 0;
2978#endif
2979 if (qf_viscol == TRUE)
2980 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002981 // Check each character from the beginning of the error
2982 // line up to the error column. For each tab character
2983 // found, reduce the error column value by the length of
2984 // a tab character.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002985 line = ml_get_curline();
2986 screen_col = 0;
2987 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
2988 {
2989 if (*line == NUL)
2990 break;
2991 if (*line++ == '\t')
2992 {
2993 curwin->w_cursor.col -= 7 - (screen_col % 8);
2994 screen_col += 8 - (screen_col % 8);
2995 }
2996 else
2997 ++screen_col;
2998 }
2999 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003000 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003001 check_cursor();
3002 }
3003 else
3004 beginline(BL_WHITE | BL_FIX);
3005 }
3006 else
3007 {
3008 pos_T save_cursor;
3009
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003010 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003011 save_cursor = curwin->w_cursor;
3012 curwin->w_cursor.lnum = 0;
3013 if (!do_search(NULL, '/', qf_pattern, (long)1,
3014 SEARCH_KEEP, NULL, NULL))
3015 curwin->w_cursor = save_cursor;
3016 }
3017}
3018
3019/*
3020 * Display quickfix list index and size message
3021 */
3022 static void
3023qf_jump_print_msg(
3024 qf_info_T *qi,
3025 int qf_index,
3026 qfline_T *qf_ptr,
3027 buf_T *old_curbuf,
3028 linenr_T old_lnum)
3029{
3030 linenr_T i;
3031 int len;
3032
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003033 // Update the screen before showing the message, unless the screen
3034 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003035 if (!msg_scrolled)
3036 update_topline_redraw();
3037 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3038 qi->qf_lists[qi->qf_curlist].qf_count,
3039 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3040 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003041 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003042 len = (int)STRLEN(IObuff);
3043 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3044
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003045 // Output the message. Overwrite to avoid scrolling when the 'O'
3046 // flag is present in 'shortmess'; But when not jumping, print the
3047 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003048 i = msg_scroll;
3049 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3050 msg_scroll = TRUE;
3051 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3052 msg_scroll = FALSE;
3053 msg_attr_keep(IObuff, 0, TRUE);
3054 msg_scroll = i;
3055}
3056
3057/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003058 * Find a usable window for opening a file from the quickfix/location list. If
3059 * a window is not found then open a new window.
3060 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3061 * able to jump/open a window. Returns NOTDONE if a file is not associated
3062 * with the entry.
3063 */
3064 static int
3065qf_jump_open_window(qf_info_T *qi, qfline_T *qf_ptr, int *opened_window)
3066{
3067 // For ":helpgrep" find a help window or open one.
3068 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
3069 if (jump_to_help_window(qi, opened_window) == FAIL)
3070 return FAIL;
3071
3072 // If currently in the quickfix window, find another window to show the
3073 // file in.
3074 if (bt_quickfix(curbuf) && !*opened_window)
3075 {
3076 // If there is no file specified, we don't know where to go.
3077 // But do advance, otherwise ":cn" gets stuck.
3078 if (qf_ptr->qf_fnum == 0)
3079 return NOTDONE;
3080
3081 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, opened_window) == FAIL)
3082 return FAIL;
3083 }
3084
3085 return OK;
3086}
3087
3088/*
3089 * Edit a selected file from the quickfix/location list and jump to a
3090 * particular line/column, adjust the folds and display a message about the
3091 * jump.
3092 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3093 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3094 * the file.
3095 */
3096 static int
3097qf_jump_to_buffer(
3098 qf_info_T *qi,
3099 int qf_index,
3100 qfline_T *qf_ptr,
3101 int forceit,
3102 win_T *oldwin,
3103 int *opened_window,
3104 int openfold,
3105 int print_message)
3106{
3107 buf_T *old_curbuf;
3108 linenr_T old_lnum;
3109 int retval = OK;
3110
3111 // If there is a file name, read the wanted file if needed, and check
3112 // autowrite etc.
3113 old_curbuf = curbuf;
3114 old_lnum = curwin->w_cursor.lnum;
3115
3116 if (qf_ptr->qf_fnum != 0)
3117 {
3118 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3119 opened_window);
3120 if (retval != OK)
3121 return retval;
3122 }
3123
3124 // When not switched to another buffer, still need to set pc mark
3125 if (curbuf == old_curbuf)
3126 setpcmark();
3127
3128 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3129 qf_ptr->qf_pattern);
3130
3131#ifdef FEAT_FOLDING
3132 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3133 foldOpenCursor();
3134#endif
3135 if (print_message)
3136 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3137
3138 return retval;
3139}
3140
3141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * jump to a quickfix line
3143 * if dir == FORWARD go "errornr" valid entries forward
3144 * if dir == BACKWARD go "errornr" valid entries backward
3145 * if dir == FORWARD_FILE go "errornr" valid entries files backward
3146 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
3147 * else if "errornr" is zero, redisplay the same line
3148 * else go to entry "errornr"
3149 */
3150 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003151qf_jump(qf_info_T *qi,
3152 int dir,
3153 int errornr,
3154 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003156 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003157 qfline_T *qf_ptr;
3158 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003161 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003162 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003164 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003167 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003169 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003171 if (qi == NULL)
3172 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003173
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003174 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
3176 EMSG(_(e_quickfix));
3177 return;
3178 }
3179
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003180 qfl = &qi->qf_lists[qi->qf_curlist];
3181
3182 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003184 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003186
3187 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3188 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003190 qf_ptr = old_qf_ptr;
3191 qf_index = old_qf_index;
3192 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003195 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003196 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003197 // No need to print the error message if it's visible in the error
3198 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 print_message = FALSE;
3200
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003201 retval = qf_jump_open_window(qi, qf_ptr, &opened_window);
3202 if (retval == FAIL)
3203 goto failed;
3204 if (retval == NOTDONE)
3205 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003206
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003207 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, oldwin,
3208 &opened_window, old_KeyTyped, print_message);
3209 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003211 // Quickfix/location list is freed by an autocmd
3212 qi = NULL;
3213 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003216 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003219 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003220 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003222 // Couldn't open file, so put index back where it was. This could
3223 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 qf_ptr = old_qf_ptr;
3226 qf_index = old_qf_index;
3227 }
3228 }
3229theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003230 if (qi != NULL)
3231 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003232 qfl->qf_ptr = qf_ptr;
3233 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 if (p_swb != old_swb && opened_window)
3236 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003237 // Restore old 'switchbuf' value, but not when an autocommand or
3238 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003242 swb_flags = old_swb_flags;
3243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 else
3245 free_string_option(old_swb);
3246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247}
3248
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003249// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003250static int qfFileAttr;
3251static int qfSepAttr;
3252static int qfLineAttr;
3253
3254/*
3255 * Display information about a single entry from the quickfix/location list.
3256 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003257 * 'cursel' will be set to TRUE for the currently selected entry in the
3258 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003259 */
3260 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003261qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003262{
3263 char_u *fname;
3264 buf_T *buf;
3265 int filter_entry;
3266
3267 fname = NULL;
3268 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3269 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3270 (char *)qfp->qf_module);
3271 else {
3272 if (qfp->qf_fnum != 0
3273 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3274 {
3275 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003276 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003277 fname = gettail(fname);
3278 }
3279 if (fname == NULL)
3280 sprintf((char *)IObuff, "%2d", qf_idx);
3281 else
3282 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3283 qf_idx, (char *)fname);
3284 }
3285
3286 // Support for filtering entries using :filter /pat/ clist
3287 // Match against the module name, file name, search pattern and
3288 // text of the entry.
3289 filter_entry = TRUE;
3290 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3291 filter_entry &= message_filtered(qfp->qf_module);
3292 if (filter_entry && fname != NULL)
3293 filter_entry &= message_filtered(fname);
3294 if (filter_entry && qfp->qf_pattern != NULL)
3295 filter_entry &= message_filtered(qfp->qf_pattern);
3296 if (filter_entry)
3297 filter_entry &= message_filtered(qfp->qf_text);
3298 if (filter_entry)
3299 return;
3300
3301 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003302 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003303
3304 if (qfp->qf_lnum != 0)
3305 msg_puts_attr((char_u *)":", qfSepAttr);
3306 if (qfp->qf_lnum == 0)
3307 IObuff[0] = NUL;
3308 else if (qfp->qf_col == 0)
3309 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3310 else
3311 sprintf((char *)IObuff, "%ld col %d",
3312 qfp->qf_lnum, qfp->qf_col);
3313 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3314 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3315 msg_puts_attr(IObuff, qfLineAttr);
3316 msg_puts_attr((char_u *)":", qfSepAttr);
3317 if (qfp->qf_pattern != NULL)
3318 {
3319 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3320 msg_puts(IObuff);
3321 msg_puts_attr((char_u *)":", qfSepAttr);
3322 }
3323 msg_puts((char_u *)" ");
3324
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003325 // Remove newlines and leading whitespace from the text. For an
3326 // unrecognized line keep the indent, the compiler may mark a word
3327 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003328 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3329 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3330 IObuff, IOSIZE);
3331 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003332 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003333}
3334
3335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003337 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 */
3339 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003340qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003342 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003343 qfline_T *qfp;
3344 int i;
3345 int idx1 = 1;
3346 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003347 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003348 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003349 int all = eap->forceit; // if not :cl!, only show
3350 // recognised errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003351 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352
Bram Moolenaar39665952018-08-15 20:59:48 +02003353 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003354 {
3355 qi = GET_LOC_LIST(curwin);
3356 if (qi == NULL)
3357 {
3358 EMSG(_(e_loclist));
3359 return;
3360 }
3361 }
3362
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003363 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
3365 EMSG(_(e_quickfix));
3366 return;
3367 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003368 if (*arg == '+')
3369 {
3370 ++arg;
3371 plus = TRUE;
3372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3374 {
3375 EMSG(_(e_trailing));
3376 return;
3377 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003378 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003379 if (plus)
3380 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003381 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003382 idx2 = i + idx1;
3383 idx1 = i;
3384 }
3385 else
3386 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003387 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003388 if (idx1 < 0)
3389 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3390 if (idx2 < 0)
3391 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003394 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003395 shorten_fnames(FALSE);
3396
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003397 // Get the attributes for the different quickfix highlight items. Note
3398 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003399 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3400 if (qfFileAttr == 0)
3401 qfFileAttr = HL_ATTR(HLF_D);
3402 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3403 if (qfSepAttr == 0)
3404 qfSepAttr = HL_ATTR(HLF_D);
3405 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3406 if (qfLineAttr == 0)
3407 qfLineAttr = HL_ATTR(HLF_N);
3408
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003409 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003411 qfp = qfl->qf_start;
3412 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 {
3414 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3415 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003416 if (got_int)
3417 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003418
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003419 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003421
3422 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003423 if (qfp == NULL)
3424 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003425 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 ui_breakcheck();
3427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428}
3429
3430/*
3431 * Remove newlines and leading whitespace from an error message.
3432 * Put the result in "buf[bufsize]".
3433 */
3434 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003435qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 int i;
3438 char_u *p = text;
3439
3440 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3441 {
3442 if (*p == '\n')
3443 {
3444 buf[i] = ' ';
3445 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003446 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 break;
3448 }
3449 else
3450 buf[i] = *p++;
3451 }
3452 buf[i] = NUL;
3453}
3454
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003455/*
3456 * Display information (list number, list size and the title) about a
3457 * quickfix/location list.
3458 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003459 static void
3460qf_msg(qf_info_T *qi, int which, char *lead)
3461{
3462 char *title = (char *)qi->qf_lists[which].qf_title;
3463 int count = qi->qf_lists[which].qf_count;
3464 char_u buf[IOSIZE];
3465
3466 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3467 lead,
3468 which + 1,
3469 qi->qf_listcount,
3470 count);
3471
3472 if (title != NULL)
3473 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003474 size_t len = STRLEN(buf);
3475
3476 if (len < 34)
3477 {
3478 vim_memset(buf + len, ' ', 34 - len);
3479 buf[34] = NUL;
3480 }
3481 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003482 }
3483 trunc_string(buf, buf, Columns - 1, IOSIZE);
3484 msg(buf);
3485}
3486
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487/*
3488 * ":colder [count]": Up in the quickfix stack.
3489 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003490 * ":lolder [count]": Up in the location list stack.
3491 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 */
3493 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003494qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003496 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 int count;
3498
Bram Moolenaar39665952018-08-15 20:59:48 +02003499 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003500 {
3501 qi = GET_LOC_LIST(curwin);
3502 if (qi == NULL)
3503 {
3504 EMSG(_(e_loclist));
3505 return;
3506 }
3507 }
3508
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 if (eap->addr_count != 0)
3510 count = eap->line2;
3511 else
3512 count = 1;
3513 while (count--)
3514 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003515 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003517 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
3519 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003520 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003522 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524 else
3525 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003526 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003529 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003531 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 }
3533 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003534 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003535 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536}
3537
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003538/*
3539 * Display the information about all the quickfix/location lists in the stack
3540 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003541 void
3542qf_history(exarg_T *eap)
3543{
3544 qf_info_T *qi = &ql_info;
3545 int i;
3546
Bram Moolenaar39665952018-08-15 20:59:48 +02003547 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003548 qi = GET_LOC_LIST(curwin);
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003549 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003550 MSG(_("No entries"));
3551 else
3552 for (i = 0; i < qi->qf_listcount; ++i)
3553 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3554}
3555
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003557 * Free all the entries in the error list "idx". Note that other information
3558 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 */
3560 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003561qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003563 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003564 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003565 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003567 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003569 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003570 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003571 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003572 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003573 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003574 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003575 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003576 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003577 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003578 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003579 // Somehow qf_count may have an incorrect value, set it to 1
3580 // to avoid crashing when it's wrong.
3581 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003582 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003583 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003584 qfl->qf_start = qfpnext;
3585 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003587
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003588 qfl->qf_index = 0;
3589 qfl->qf_start = NULL;
3590 qfl->qf_last = NULL;
3591 qfl->qf_ptr = NULL;
3592 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003593
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003594 qf_clean_dir_stack(&qfl->qf_dir_stack);
3595 qfl->qf_directory = NULL;
3596 qf_clean_dir_stack(&qfl->qf_file_stack);
3597 qfl->qf_currfile = NULL;
3598 qfl->qf_multiline = FALSE;
3599 qfl->qf_multiignore = FALSE;
3600 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601}
3602
3603/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003604 * Free error list "idx". Frees all the entries in the quickfix list,
3605 * associated context information and the title.
3606 */
3607 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003608qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003609{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003610 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003611
Bram Moolenaard23a8232018-02-10 18:45:26 +01003612 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003613 free_tv(qfl->qf_ctx);
3614 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003615 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003616 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003617}
3618
3619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 * qf_mark_adjust: adjust marks
3621 */
3622 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003623qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003624 win_T *wp,
3625 linenr_T line1,
3626 linenr_T line2,
3627 long amount,
3628 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003630 int i;
3631 qfline_T *qfp;
3632 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003633 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003634 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003635 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
Bram Moolenaarc1542742016-07-20 21:44:37 +02003637 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003638 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003639 if (wp != NULL)
3640 {
3641 if (wp->w_llist == NULL)
3642 return;
3643 qi = wp->w_llist;
3644 }
3645
3646 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003647 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003648 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003649 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3650 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 if (qfp->qf_fnum == curbuf->b_fnum)
3652 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003653 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3655 {
3656 if (amount == MAXLNUM)
3657 qfp->qf_cleared = TRUE;
3658 else
3659 qfp->qf_lnum += amount;
3660 }
3661 else if (amount_after && qfp->qf_lnum > line2)
3662 qfp->qf_lnum += amount_after;
3663 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003664
3665 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003666 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667}
3668
3669/*
3670 * Make a nice message out of the error character and the error number:
3671 * char number message
3672 * e or E 0 " error"
3673 * w or W 0 " warning"
3674 * i or I 0 " info"
3675 * 0 0 ""
3676 * other 0 " c"
3677 * e or E n " error n"
3678 * w or W n " warning n"
3679 * i or I n " info n"
3680 * 0 n " error n"
3681 * other n " c n"
3682 * 1 x "" :helpgrep
3683 */
3684 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003685qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
3687 static char_u buf[20];
3688 static char_u cc[3];
3689 char_u *p;
3690
3691 if (c == 'W' || c == 'w')
3692 p = (char_u *)" warning";
3693 else if (c == 'I' || c == 'i')
3694 p = (char_u *)" info";
3695 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3696 p = (char_u *)" error";
3697 else if (c == 0 || c == 1)
3698 p = (char_u *)"";
3699 else
3700 {
3701 cc[0] = ' ';
3702 cc[1] = c;
3703 cc[2] = NUL;
3704 p = cc;
3705 }
3706
3707 if (nr <= 0)
3708 return p;
3709
3710 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3711 return buf;
3712}
3713
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003715 * When "split" is FALSE: Open the entry/result under the cursor.
3716 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3717 */
3718 void
3719qf_view_result(int split)
3720{
3721 qf_info_T *qi = &ql_info;
3722
3723 if (!bt_quickfix(curbuf))
3724 return;
3725
3726 if (IS_LL_WINDOW(curwin))
3727 qi = GET_LOC_LIST(curwin);
3728
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003729 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003730 {
3731 EMSG(_(e_quickfix));
3732 return;
3733 }
3734
3735 if (split)
3736 {
3737 char_u cmd[32];
3738
3739 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3740 (long)curwin->w_cursor.lnum,
3741 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3742 if (do_cmdline_cmd(cmd) == OK)
3743 do_cmdline_cmd((char_u *) "clearjumps");
3744 return;
3745 }
3746
3747 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3748}
3749
3750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 * ":cwindow": open the quickfix window if we have errors to display,
3752 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003753 * ":lwindow": open the location list window if we have locations to display,
3754 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 */
3756 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003757ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003759 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 win_T *win;
3761
Bram Moolenaar39665952018-08-15 20:59:48 +02003762 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003763 {
3764 qi = GET_LOC_LIST(curwin);
3765 if (qi == NULL)
3766 return;
3767 }
3768
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003769 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003770 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003772 // If a quickfix window is open but we have no errors to display,
3773 // close the window. If a quickfix window is not open, then open
3774 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003775 if (qf_stack_empty(qi)
3776 || qi->qf_lists[qi->qf_curlist].qf_nonevalid
3777 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 {
3779 if (win != NULL)
3780 ex_cclose(eap);
3781 }
3782 else if (win == NULL)
3783 ex_copen(eap);
3784}
3785
3786/*
3787 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003788 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003791ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003793 win_T *win = NULL;
3794 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
Bram Moolenaar39665952018-08-15 20:59:48 +02003796 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003797 {
3798 qi = GET_LOC_LIST(curwin);
3799 if (qi == NULL)
3800 return;
3801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003803 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003804 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 if (win != NULL)
3806 win_close(win, FALSE);
3807}
3808
3809/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003810 * Set "w:quickfix_title" if "qi" has a title.
3811 */
3812 static void
3813qf_set_title_var(qf_list_T *qfl)
3814{
3815 if (qfl->qf_title != NULL)
3816 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3817}
3818
3819/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003820 * Goto a quickfix or location list window (if present).
3821 * Returns OK if the window is found, FAIL otherwise.
3822 */
3823 static int
3824qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3825{
3826 win_T *win;
3827
3828 win = qf_find_win(qi);
3829 if (win == NULL)
3830 return FAIL;
3831
3832 win_goto(win);
3833 if (resize)
3834 {
3835 if (vertsplit)
3836 {
3837 if (sz != win->w_width)
3838 win_setwidth(sz);
3839 }
3840 else if (sz != win->w_height)
3841 win_setheight(sz);
3842 }
3843
3844 return OK;
3845}
3846
3847/*
3848 * Open a new quickfix or location list window, load the quickfix buffer and
3849 * set the appropriate options for the window.
3850 * Returns FAIL if the window could not be opened.
3851 */
3852 static int
3853qf_open_new_cwindow(qf_info_T *qi, int height)
3854{
3855 buf_T *qf_buf;
3856 win_T *oldwin = curwin;
3857 tabpage_T *prevtab = curtab;
3858 int flags = 0;
3859 win_T *win;
3860
3861 qf_buf = qf_find_buf(qi);
3862
3863 // The current window becomes the previous window afterwards.
3864 win = curwin;
3865
3866 if (IS_QF_STACK(qi) && cmdmod.split == 0)
3867 // Create the new quickfix window at the very bottom, except when
3868 // :belowright or :aboveleft is used.
3869 win_goto(lastwin);
3870 // Default is to open the window below the current window
3871 if (cmdmod.split == 0)
3872 flags = WSP_BELOW;
3873 flags |= WSP_NEWLOC;
3874 if (win_split(height, flags) == FAIL)
3875 return FAIL; // not enough room for window
3876 RESET_BINDING(curwin);
3877
3878 if (IS_LL_STACK(qi))
3879 {
3880 // For the location list window, create a reference to the
3881 // location list from the window 'win'.
3882 curwin->w_llist_ref = win->w_llist;
3883 win->w_llist->qf_refcount++;
3884 }
3885
3886 if (oldwin != curwin)
3887 oldwin = NULL; // don't store info when in another window
3888 if (qf_buf != NULL)
3889 {
3890 // Use the existing quickfix buffer
3891 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
3892 ECMD_HIDE + ECMD_OLDBUF, oldwin);
3893 }
3894 else
3895 {
3896 // Create a new quickfix buffer
3897 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
3898
3899 // switch off 'swapfile'
3900 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3901 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
3902 OPT_LOCAL);
3903 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
3904 RESET_BINDING(curwin);
3905#ifdef FEAT_DIFF
3906 curwin->w_p_diff = FALSE;
3907#endif
3908#ifdef FEAT_FOLDING
3909 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3910 OPT_LOCAL);
3911#endif
3912 }
3913
3914 // Only set the height when still in the same tab page and there is no
3915 // window to the side.
3916 if (curtab == prevtab && curwin->w_width == Columns)
3917 win_setheight(height);
3918 curwin->w_p_wfh = TRUE; // set 'winfixheight'
3919 if (win_valid(win))
3920 prevwin = win;
3921
3922 return OK;
3923}
3924
3925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003927 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 */
3929 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003930ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003932 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003934 int status = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
Bram Moolenaar39665952018-08-15 20:59:48 +02003936 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003937 {
3938 qi = GET_LOC_LIST(curwin);
3939 if (qi == NULL)
3940 {
3941 EMSG(_(e_loclist));
3942 return;
3943 }
3944 }
3945
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 if (eap->addr_count != 0)
3947 height = eap->line2;
3948 else
3949 height = QF_WINHEIGHT;
3950
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003951 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952#ifdef FEAT_GUI
3953 need_mouse_correct = TRUE;
3954#endif
3955
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003956 // Find an existing quickfix window, or open a new one.
3957 if (cmdmod.tab == 0)
3958 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
3959 cmdmod.split & WSP_VERT);
3960 if (status == FAIL)
3961 if (qf_open_new_cwindow(qi, height) == FAIL)
3962 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003964 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003965
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003966 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02003967 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003969 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 curwin->w_cursor.col = 0;
3971 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003972 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973}
3974
3975/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003976 * Move the cursor in the quickfix window to "lnum".
3977 */
3978 static void
3979qf_win_goto(win_T *win, linenr_T lnum)
3980{
3981 win_T *old_curwin = curwin;
3982
3983 curwin = win;
3984 curbuf = win->w_buffer;
3985 curwin->w_cursor.lnum = lnum;
3986 curwin->w_cursor.col = 0;
3987#ifdef FEAT_VIRTUALEDIT
3988 curwin->w_cursor.coladd = 0;
3989#endif
3990 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003991 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02003992 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003993 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02003994 curwin = old_curwin;
3995 curbuf = curwin->w_buffer;
3996}
3997
3998/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003999 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004000 */
4001 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004002ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004003{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004004 qf_info_T *qi = &ql_info;
4005 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004006
Bram Moolenaar39665952018-08-15 20:59:48 +02004007 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004008 {
4009 qi = GET_LOC_LIST(curwin);
4010 if (qi == NULL)
4011 {
4012 EMSG(_(e_loclist));
4013 return;
4014 }
4015 }
4016
4017 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004018 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4019 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4020}
4021
4022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 * Return the number of the current entry (line number in the quickfix
4024 * window).
4025 */
4026 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004027qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004029 qf_info_T *qi = &ql_info;
4030
4031 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004032 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004033 qi = wp->w_llist_ref;
4034
4035 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036}
4037
4038/*
4039 * Update the cursor position in the quickfix window to the current error.
4040 * Return TRUE if there is a quickfix window.
4041 */
4042 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004043qf_win_pos_update(
4044 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004045 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046{
4047 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004048 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004050 // Put the cursor on the current error in the quickfix window, so that
4051 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004052 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 if (win != NULL
4054 && qf_index <= win->w_buffer->b_ml.ml_line_count
4055 && old_qf_index != qf_index)
4056 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 if (qf_index > old_qf_index)
4058 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004059 win->w_redraw_top = old_qf_index;
4060 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062 else
4063 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004064 win->w_redraw_top = qf_index;
4065 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004067 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
4069 return win != NULL;
4070}
4071
4072/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004073 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004074 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004075 */
4076 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004077is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004078{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004079 // A window displaying the quickfix buffer will have the w_llist_ref field
4080 // set to NULL.
4081 // A window displaying a location list buffer will have the w_llist_ref
4082 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004083 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004084 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4085 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004086 return TRUE;
4087
4088 return FALSE;
4089}
4090
4091/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004092 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004093 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004094 */
4095 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004096qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004097{
4098 win_T *win;
4099
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004100 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004101 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004102 return win;
4103 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004104}
4105
4106/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004107 * Find a quickfix buffer.
4108 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 */
4110 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004111qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004113 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004114 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
Bram Moolenaar9c102382006-05-03 21:26:49 +00004116 FOR_ALL_TAB_WINDOWS(tp, win)
4117 if (is_qf_win(win, qi))
4118 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004119
Bram Moolenaar9c102382006-05-03 21:26:49 +00004120 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121}
4122
4123/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004124 * Update the w:quickfix_title variable in the quickfix/location list window
4125 */
4126 static void
4127qf_update_win_titlevar(qf_info_T *qi)
4128{
4129 win_T *win;
4130 win_T *curwin_save;
4131
4132 if ((win = qf_find_win(qi)) != NULL)
4133 {
4134 curwin_save = curwin;
4135 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004136 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004137 curwin = curwin_save;
4138 }
4139}
4140
4141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 * Find the quickfix buffer. If it exists, update the contents.
4143 */
4144 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004145qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146{
4147 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004148 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004151 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004152 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 if (buf != NULL)
4154 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004155 linenr_T old_line_count = buf->b_ml.ml_line_count;
4156
4157 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004158 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004159 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
Bram Moolenaard823fa92016-08-12 16:29:27 +02004161 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004162
Bram Moolenaar864293a2016-06-02 13:40:04 +02004163 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004164 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004165
Bram Moolenaar864293a2016-06-02 13:40:04 +02004166 if (old_last == NULL)
4167 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004168 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004169
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004170 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004171 aucmd_restbuf(&aco);
4172 }
4173
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004174 // Only redraw when added lines are visible. This avoids flickering
4175 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004176 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4177 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179}
4180
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004181/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004182 * Add an error line to the quickfix buffer.
4183 */
4184 static int
4185qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4186{
4187 int len;
4188 buf_T *errbuf;
4189
4190 if (qfp->qf_module != NULL)
4191 {
4192 STRCPY(IObuff, qfp->qf_module);
4193 len = (int)STRLEN(IObuff);
4194 }
4195 else if (qfp->qf_fnum != 0
4196 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4197 && errbuf->b_fname != NULL)
4198 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004199 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004200 STRCPY(IObuff, gettail(errbuf->b_fname));
4201 else
4202 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004203 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004204 if (errbuf->b_sfname == NULL
4205 || mch_isFullName(errbuf->b_sfname))
4206 {
4207 if (*dirname == NUL)
4208 mch_dirname(dirname, MAXPATHL);
4209 shorten_buf_fname(errbuf, dirname, FALSE);
4210 }
4211 STRCPY(IObuff, errbuf->b_fname);
4212 }
4213 len = (int)STRLEN(IObuff);
4214 }
4215 else
4216 len = 0;
4217 IObuff[len++] = '|';
4218
4219 if (qfp->qf_lnum > 0)
4220 {
4221 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4222 len += (int)STRLEN(IObuff + len);
4223
4224 if (qfp->qf_col > 0)
4225 {
4226 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4227 len += (int)STRLEN(IObuff + len);
4228 }
4229
4230 sprintf((char *)IObuff + len, "%s",
4231 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4232 len += (int)STRLEN(IObuff + len);
4233 }
4234 else if (qfp->qf_pattern != NULL)
4235 {
4236 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4237 len += (int)STRLEN(IObuff + len);
4238 }
4239 IObuff[len++] = '|';
4240 IObuff[len++] = ' ';
4241
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004242 // Remove newlines and leading whitespace from the text.
4243 // For an unrecognized line keep the indent, the compiler may
4244 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004245 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4246 IObuff + len, IOSIZE - len);
4247
4248 if (ml_append_buf(buf, lnum, IObuff,
4249 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4250 return FAIL;
4251
4252 return OK;
4253}
4254
4255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 * Fill current buffer with quickfix errors, replacing any previous contents.
4257 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004258 * If "old_last" is not NULL append the items after this one.
4259 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4260 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 */
4262 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004263qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004265 linenr_T lnum;
4266 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004267 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268
Bram Moolenaar864293a2016-06-02 13:40:04 +02004269 if (old_last == NULL)
4270 {
4271 if (buf != curbuf)
4272 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004273 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004274 return;
4275 }
4276
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004277 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004278 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4279 (void)ml_delete((linenr_T)1, FALSE);
4280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004282 // Check if there is anything to display
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004283 if (!qf_stack_empty(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
Bram Moolenaara796d462018-05-01 14:30:36 +02004285 char_u dirname[MAXPATHL];
4286
4287 *dirname = NUL;
4288
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004289 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004290 if (old_last == NULL)
4291 {
4292 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4293 lnum = 0;
4294 }
4295 else
4296 {
4297 qfp = old_last->qf_next;
4298 lnum = buf->b_ml.ml_line_count;
4299 }
4300 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004302 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004304
Bram Moolenaar864293a2016-06-02 13:40:04 +02004305 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004307 if (qfp == NULL)
4308 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004310
4311 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004312 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004313 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004316 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 check_lnums(TRUE);
4318
Bram Moolenaar864293a2016-06-02 13:40:04 +02004319 if (old_last == NULL)
4320 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004321 // Set the 'filetype' to "qf" each time after filling the buffer.
4322 // This resembles reading a file into a buffer, it's more logical when
4323 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004324 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004325 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4326 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004328 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004329 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004331 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004333 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004334 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004335
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004336 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004337 redraw_curbuf_later(NOT_VALID);
4338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004340 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 KeyTyped = old_KeyTyped;
4342}
4343
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004344/*
4345 * For every change made to the quickfix list, update the changed tick.
4346 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004347 static void
4348qf_list_changed(qf_info_T *qi, int qf_idx)
4349{
4350 qi->qf_lists[qf_idx].qf_changedtick++;
4351}
4352
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004354 * Return the quickfix/location list number with the given identifier.
4355 * Returns -1 if list is not found.
4356 */
4357 static int
4358qf_id2nr(qf_info_T *qi, int_u qfid)
4359{
4360 int qf_idx;
4361
4362 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4363 if (qi->qf_lists[qf_idx].qf_id == qfid)
4364 return qf_idx;
4365 return INVALID_QFIDX;
4366}
4367
4368/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004369 * If the current list is not "save_qfid" and we can find the list with that ID
4370 * then make it the current list.
4371 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004372 * Returns OK if successfully restored the list. Returns FAIL if the list with
4373 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004374 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004375 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004376qf_restore_list(qf_info_T *qi, int_u save_qfid)
4377{
4378 int curlist;
4379
4380 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4381 {
4382 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004383 if (curlist < 0)
4384 // list is not present
4385 return FAIL;
4386 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004387 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004388 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004389}
4390
4391/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004392 * Jump to the first entry if there is one.
4393 */
4394 static void
4395qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4396{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004397 if (qf_restore_list(qi, save_qfid) == FAIL)
4398 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004399
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004400 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004401 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004402 qf_jump(qi, 0, 0, forceit);
4403}
4404
4405/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004406 * Return TRUE when using ":vimgrep" for ":grep".
4407 */
4408 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004409grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004410{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004411 return ((cmdidx == CMD_grep
4412 || cmdidx == CMD_lgrep
4413 || cmdidx == CMD_grepadd
4414 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004415 && STRCMP("internal",
4416 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4417}
4418
4419/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004420 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004422 static char_u *
4423make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004425 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004426 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004427 case CMD_make: return (char_u *)"make";
4428 case CMD_lmake: return (char_u *)"lmake";
4429 case CMD_grep: return (char_u *)"grep";
4430 case CMD_lgrep: return (char_u *)"lgrep";
4431 case CMD_grepadd: return (char_u *)"grepadd";
4432 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4433 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435}
4436
4437/*
4438 * Return the name for the errorfile, in allocated memory.
4439 * Find a new unique name when 'makeef' contains "##".
4440 * Returns NULL for error.
4441 */
4442 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004443get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444{
4445 char_u *p;
4446 char_u *name;
4447 static int start = -1;
4448 static int off = 0;
4449#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004450 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451#endif
4452
4453 if (*p_mef == NUL)
4454 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004455 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 if (name == NULL)
4457 EMSG(_(e_notmp));
4458 return name;
4459 }
4460
4461 for (p = p_mef; *p; ++p)
4462 if (p[0] == '#' && p[1] == '#')
4463 break;
4464
4465 if (*p == NUL)
4466 return vim_strsave(p_mef);
4467
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004468 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 for (;;)
4470 {
4471 if (start == -1)
4472 start = mch_get_pid();
4473 else
4474 off += 19;
4475
4476 name = alloc((unsigned)STRLEN(p_mef) + 30);
4477 if (name == NULL)
4478 break;
4479 STRCPY(name, p_mef);
4480 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4481 STRCAT(name, p + 2);
4482 if (mch_getperm(name) < 0
4483#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004484 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 && mch_lstat((char *)name, &sb) < 0
4486#endif
4487 )
4488 break;
4489 vim_free(name);
4490 }
4491 return name;
4492}
4493
4494/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004495 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4496 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4497 */
4498 static char_u *
4499make_get_fullcmd(char_u *makecmd, char_u *fname)
4500{
4501 char_u *cmd;
4502 unsigned len;
4503
4504 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4505 if (*p_sp != NUL)
4506 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4507 cmd = alloc(len);
4508 if (cmd == NULL)
4509 return NULL;
4510 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4511 (char *)p_shq);
4512
4513 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4514 if (*p_sp != NUL)
4515 append_redir(cmd, len, p_sp, fname);
4516
4517 // Display the fully formed command. Output a newline if there's something
4518 // else than the :make command that was typed (in which case the cursor is
4519 // in column 0).
4520 if (msg_col == 0)
4521 msg_didout = FALSE;
4522 msg_start();
4523 MSG_PUTS(":!");
4524 msg_outtrans(cmd); // show what we are doing
4525
4526 return cmd;
4527}
4528
4529/*
4530 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4531 */
4532 void
4533ex_make(exarg_T *eap)
4534{
4535 char_u *fname;
4536 char_u *cmd;
4537 char_u *enc = NULL;
4538 win_T *wp = NULL;
4539 qf_info_T *qi = &ql_info;
4540 int res;
4541 char_u *au_name = NULL;
4542 int_u save_qfid;
4543
4544 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4545 if (grep_internal(eap->cmdidx))
4546 {
4547 ex_vimgrep(eap);
4548 return;
4549 }
4550
4551 au_name = make_get_auname(eap->cmdidx);
4552 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4553 curbuf->b_fname, TRUE, curbuf))
4554 {
4555#ifdef FEAT_EVAL
4556 if (aborting())
4557 return;
4558#endif
4559 }
4560#ifdef FEAT_MBYTE
4561 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4562#endif
4563
4564 if (is_loclist_cmd(eap->cmdidx))
4565 wp = curwin;
4566
4567 autowrite_all();
4568 fname = get_mef_name();
4569 if (fname == NULL)
4570 return;
4571 mch_remove(fname); // in case it's not unique
4572
4573 cmd = make_get_fullcmd(eap->arg, fname);
4574 if (cmd == NULL)
4575 return;
4576
4577 // let the shell know if we are redirecting output or not
4578 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4579
4580#ifdef AMIGA
4581 out_flush();
4582 // read window status report and redraw before message
4583 (void)char_avail();
4584#endif
4585
4586 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4587 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4588 (eap->cmdidx != CMD_grepadd
4589 && eap->cmdidx != CMD_lgrepadd),
4590 qf_cmdtitle(*eap->cmdlinep), enc);
4591 if (wp != NULL)
4592 {
4593 qi = GET_LOC_LIST(wp);
4594 if (qi == NULL)
4595 goto cleanup;
4596 }
4597 if (res >= 0)
4598 qf_list_changed(qi, qi->qf_curlist);
4599
4600 // Remember the current quickfix list identifier, so that we can
4601 // check for autocommands changing the current quickfix list.
4602 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4603 if (au_name != NULL)
4604 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4605 curbuf->b_fname, TRUE, curbuf);
4606 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4607 // display the first error
4608 qf_jump_first(qi, save_qfid, FALSE);
4609
4610cleanup:
4611 mch_remove(fname);
4612 vim_free(fname);
4613 vim_free(cmd);
4614}
4615
4616/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004617 * Returns the number of valid entries in the current quickfix/location list.
4618 */
4619 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004620qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004621{
4622 qf_info_T *qi = &ql_info;
4623 qfline_T *qfp;
4624 int i, sz = 0;
4625 int prev_fnum = 0;
4626
Bram Moolenaar39665952018-08-15 20:59:48 +02004627 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004628 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004629 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004630 qi = GET_LOC_LIST(curwin);
4631 if (qi == NULL)
4632 return 0;
4633 }
4634
4635 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004636 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004637 ++i, qfp = qfp->qf_next)
4638 {
4639 if (qfp->qf_valid)
4640 {
4641 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004642 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004643 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4644 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004645 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004646 sz++;
4647 prev_fnum = qfp->qf_fnum;
4648 }
4649 }
4650 }
4651
4652 return sz;
4653}
4654
4655/*
4656 * Returns the current index of the quickfix/location list.
4657 * Returns 0 if there is an error.
4658 */
4659 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004660qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004661{
4662 qf_info_T *qi = &ql_info;
4663
Bram Moolenaar39665952018-08-15 20:59:48 +02004664 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004665 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004666 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004667 qi = GET_LOC_LIST(curwin);
4668 if (qi == NULL)
4669 return 0;
4670 }
4671
4672 return qi->qf_lists[qi->qf_curlist].qf_index;
4673}
4674
4675/*
4676 * Returns the current index in the quickfix/location list (counting only valid
4677 * entries). If no valid entries are in the list, then returns 1.
4678 */
4679 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004680qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004681{
4682 qf_info_T *qi = &ql_info;
4683 qf_list_T *qfl;
4684 qfline_T *qfp;
4685 int i, eidx = 0;
4686 int prev_fnum = 0;
4687
Bram Moolenaar39665952018-08-15 20:59:48 +02004688 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004689 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004690 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004691 qi = GET_LOC_LIST(curwin);
4692 if (qi == NULL)
4693 return 1;
4694 }
4695
4696 qfl = &qi->qf_lists[qi->qf_curlist];
4697 qfp = qfl->qf_start;
4698
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004699 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004700 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4701 return 1;
4702
4703 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4704 {
4705 if (qfp->qf_valid)
4706 {
4707 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4708 {
4709 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4710 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004711 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004712 eidx++;
4713 prev_fnum = qfp->qf_fnum;
4714 }
4715 }
4716 else
4717 eidx++;
4718 }
4719 }
4720
4721 return eidx ? eidx : 1;
4722}
4723
4724/*
4725 * Get the 'n'th valid error entry in the quickfix or location list.
4726 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4727 * For :cdo and :ldo returns the 'n'th valid error entry.
4728 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4729 */
4730 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004731qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004732{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004733 qfline_T *qfp = qfl->qf_start;
4734 int i, eidx;
4735 int prev_fnum = 0;
4736
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004737 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004738 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4739 return 1;
4740
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004741 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004742 i++, qfp = qfp->qf_next)
4743 {
4744 if (qfp->qf_valid)
4745 {
4746 if (fdo)
4747 {
4748 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4749 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004750 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004751 eidx++;
4752 prev_fnum = qfp->qf_fnum;
4753 }
4754 }
4755 else
4756 eidx++;
4757 }
4758
4759 if (eidx == n)
4760 break;
4761 }
4762
4763 if (i <= qfl->qf_count)
4764 return i;
4765 else
4766 return 1;
4767}
4768
4769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004771 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004772 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 */
4774 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004775ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004777 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004778 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004779
Bram Moolenaar39665952018-08-15 20:59:48 +02004780 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004781 {
4782 qi = GET_LOC_LIST(curwin);
4783 if (qi == NULL)
4784 {
4785 EMSG(_(e_loclist));
4786 return;
4787 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004788 }
4789
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004790 if (eap->addr_count > 0)
4791 errornr = (int)eap->line2;
4792 else
4793 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004794 switch (eap->cmdidx)
4795 {
4796 case CMD_cc: case CMD_ll:
4797 errornr = 0;
4798 break;
4799 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4800 case CMD_lfirst:
4801 errornr = 1;
4802 break;
4803 default:
4804 errornr = 32767;
4805 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004806 }
4807
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004808 // For cdo and ldo commands, jump to the nth valid error.
4809 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02004810 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4811 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004812 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004813 eap->addr_count > 0 ? (int)eap->line1 : 1,
4814 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4815
4816 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817}
4818
4819/*
4820 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004821 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004822 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 */
4824 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004825ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004827 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004828 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004829 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004830
Bram Moolenaar39665952018-08-15 20:59:48 +02004831 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004832 {
4833 qi = GET_LOC_LIST(curwin);
4834 if (qi == NULL)
4835 {
4836 EMSG(_(e_loclist));
4837 return;
4838 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004839 }
4840
Bram Moolenaar55b69262017-08-13 13:42:01 +02004841 if (eap->addr_count > 0
4842 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4843 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004844 errornr = (int)eap->line2;
4845 else
4846 errornr = 1;
4847
Bram Moolenaar39665952018-08-15 20:59:48 +02004848 // Depending on the command jump to either next or previous entry/file.
4849 switch (eap->cmdidx)
4850 {
4851 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4852 dir = FORWARD;
4853 break;
4854 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4855 case CMD_lNext:
4856 dir = BACKWARD;
4857 break;
4858 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4859 dir = FORWARD_FILE;
4860 break;
4861 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4862 dir = BACKWARD_FILE;
4863 break;
4864 default:
4865 dir = FORWARD;
4866 break;
4867 }
4868
4869 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870}
4871
4872/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004873 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004874 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 */
4876 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004877ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004879 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004880 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004881 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004882 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004883 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004884 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004885
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004886 switch (eap->cmdidx)
4887 {
4888 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4889 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4890 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4891 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4892 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4893 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4894 default: break;
4895 }
4896 if (au_name != NULL)
4897 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004898#ifdef FEAT_MBYTE
4899 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4900#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004901#ifdef FEAT_BROWSE
4902 if (cmdmod.browse)
4903 {
4904 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004905 NULL, NULL,
4906 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004907 if (browse_file == NULL)
4908 return;
4909 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4910 vim_free(browse_file);
4911 }
4912 else
4913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004915 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004916
Bram Moolenaar39665952018-08-15 20:59:48 +02004917 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004918 wp = curwin;
4919
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004920 // This function is used by the :cfile, :cgetfile and :caddfile
4921 // commands.
4922 // :cfile always creates a new quickfix list and jumps to the
4923 // first error.
4924 // :cgetfile creates a new quickfix list but doesn't jump to the
4925 // first error.
4926 // :caddfile adds to an existing quickfix list. If there is no
4927 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004928 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004929 && eap->cmdidx != CMD_laddfile),
4930 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004931 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004932 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01004933 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004934 if (qi == NULL)
4935 return;
4936 }
4937 if (res >= 0)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004938 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004939 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004940 if (au_name != NULL)
4941 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004942
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004943 // Jump to the first error for a new list and if autocmds didn't
4944 // free the list.
4945 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
4946 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004947 // display the first error
4948 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004949}
4950
4951/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004952 * Return the vimgrep autocmd name.
4953 */
4954 static char_u *
4955vgr_get_auname(cmdidx_T cmdidx)
4956{
4957 switch (cmdidx)
4958 {
4959 case CMD_vimgrep: return (char_u *)"vimgrep";
4960 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4961 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4962 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4963 case CMD_grep: return (char_u *)"grep";
4964 case CMD_lgrep: return (char_u *)"lgrep";
4965 case CMD_grepadd: return (char_u *)"grepadd";
4966 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4967 default: return NULL;
4968 }
4969}
4970
4971/*
4972 * Initialize the regmatch used by vimgrep for pattern "s".
4973 */
4974 static void
4975vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4976{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004977 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004978 regmatch->regprog = NULL;
4979
4980 if (s == NULL || *s == NUL)
4981 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004982 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004983 if (last_search_pat() == NULL)
4984 {
4985 EMSG(_(e_noprevre));
4986 return;
4987 }
4988 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4989 }
4990 else
4991 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4992
4993 regmatch->rmm_ic = p_ic;
4994 regmatch->rmm_maxcol = 0;
4995}
4996
4997/*
4998 * Display a file name when vimgrep is running.
4999 */
5000 static void
5001vgr_display_fname(char_u *fname)
5002{
5003 char_u *p;
5004
5005 msg_start();
5006 p = msg_strtrunc(fname, TRUE);
5007 if (p == NULL)
5008 msg_outtrans(fname);
5009 else
5010 {
5011 msg_outtrans(p);
5012 vim_free(p);
5013 }
5014 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005015 msg_didout = FALSE; // overwrite this message
5016 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005017 msg_col = 0;
5018 out_flush();
5019}
5020
5021/*
5022 * Load a dummy buffer to search for a pattern using vimgrep.
5023 */
5024 static buf_T *
5025vgr_load_dummy_buf(
5026 char_u *fname,
5027 char_u *dirname_start,
5028 char_u *dirname_now)
5029{
5030 int save_mls;
5031#if defined(FEAT_SYN_HL)
5032 char_u *save_ei = NULL;
5033#endif
5034 buf_T *buf;
5035
5036#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005037 // Don't do Filetype autocommands to avoid loading syntax and
5038 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005039 save_ei = au_event_disable(",Filetype");
5040#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005041 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005042 save_mls = p_mls;
5043 p_mls = 0;
5044
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005045 // Load file into a buffer, so that 'fileencoding' is detected,
5046 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005047 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5048
5049 p_mls = save_mls;
5050#if defined(FEAT_SYN_HL)
5051 au_event_restore(save_ei);
5052#endif
5053
5054 return buf;
5055}
5056
5057/*
5058 * Check whether a quickfix/location list valid. Autocmds may remove or change
5059 * a quickfix list when vimgrep is running. If the list is not found, create a
5060 * new list.
5061 */
5062 static int
5063vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005064 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005065 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005066 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005067 char_u *title)
5068{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005069 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005070 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005071 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005072 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005073 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005074 // An autocmd has freed the location list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005075 EMSG(_(e_loc_list_changed));
5076 return FALSE;
5077 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005078 else
5079 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005080 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005081 qf_new_list(qi, title);
5082 return TRUE;
5083 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005084 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005085
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005086 if (qf_restore_list(qi, qfid) == FAIL)
5087 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005088
5089 return TRUE;
5090}
5091
5092/*
5093 * Search for a pattern in all the lines in a buffer and add the matching lines
5094 * to a quickfix list.
5095 */
5096 static int
5097vgr_match_buflines(
5098 qf_info_T *qi,
5099 char_u *fname,
5100 buf_T *buf,
5101 regmmatch_T *regmatch,
5102 long tomatch,
5103 int duplicate_name,
5104 int flags)
5105{
5106 int found_match = FALSE;
5107 long lnum;
5108 colnr_T col;
5109
5110 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5111 {
5112 col = 0;
5113 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5114 col, NULL, NULL) > 0)
5115 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005116 // Pass the buffer number so that it gets used even for a
5117 // dummy buffer, unless duplicate_name is set, then the
5118 // buffer will be wiped out below.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005119 if (qf_add_entry(qi,
5120 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005121 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005122 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005123 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005124 duplicate_name ? 0 : buf->b_fnum,
5125 ml_get_buf(buf,
5126 regmatch->startpos[0].lnum + lnum, FALSE),
5127 regmatch->startpos[0].lnum + lnum,
5128 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005129 FALSE, // vis_col
5130 NULL, // search pattern
5131 0, // nr
5132 0, // type
5133 TRUE // valid
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005134 ) == FAIL)
5135 {
5136 got_int = TRUE;
5137 break;
5138 }
5139 found_match = TRUE;
5140 if (--tomatch == 0)
5141 break;
5142 if ((flags & VGR_GLOBAL) == 0
5143 || regmatch->endpos[0].lnum > 0)
5144 break;
5145 col = regmatch->endpos[0].col
5146 + (col == regmatch->endpos[0].col);
5147 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5148 break;
5149 }
5150 line_breakcheck();
5151 if (got_int)
5152 break;
5153 }
5154
5155 return found_match;
5156}
5157
5158/*
5159 * Jump to the first match and update the directory.
5160 */
5161 static void
5162vgr_jump_to_match(
5163 qf_info_T *qi,
5164 int forceit,
5165 int *redraw_for_dummy,
5166 buf_T *first_match_buf,
5167 char_u *target_dir)
5168{
5169 buf_T *buf;
5170
5171 buf = curbuf;
5172 qf_jump(qi, 0, 0, forceit);
5173 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005174 // If we jumped to another buffer redrawing will already be
5175 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005176 *redraw_for_dummy = FALSE;
5177
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005178 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005179 if (curbuf == first_match_buf && target_dir != NULL)
5180 {
5181 exarg_T ea;
5182
5183 ea.arg = target_dir;
5184 ea.cmdidx = CMD_lcd;
5185 ex_cd(&ea);
5186 }
5187}
5188
5189/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005190 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005191 * ":vimgrepadd {pattern} file(s)"
5192 * ":lvimgrep {pattern} file(s)"
5193 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005194 */
5195 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005196ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005197{
Bram Moolenaar81695252004-12-29 20:58:21 +00005198 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005199 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005200 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005201 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005202 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005203 char_u *s;
5204 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005205 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005206 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005207 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005208 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005209 buf_T *buf;
5210 int duplicate_name = FALSE;
5211 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005212 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005213 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005214 buf_T *first_match_buf = NULL;
5215 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005216 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005217 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005218 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005219 char_u *dirname_start = NULL;
5220 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005221 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005222 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005223
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005224 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005225 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5226 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005227 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005228#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005229 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005230 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005231#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005232 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005233
Bram Moolenaar39665952018-08-15 20:59:48 +02005234 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005235 {
5236 qi = ll_get_or_alloc_list(curwin);
5237 if (qi == NULL)
5238 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005239 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005240 }
5241
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005242 if (eap->addr_count > 0)
5243 tomatch = eap->line2;
5244 else
5245 tomatch = MAXLNUM;
5246
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005247 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005248 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005249 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005250 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005251 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005252 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005253 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005254 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005255 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005256
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005257 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005258 if (regmatch.regprog == NULL)
5259 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005260
5261 p = skipwhite(p);
5262 if (*p == NUL)
5263 {
5264 EMSG(_("E683: File name missing or invalid pattern"));
5265 goto theend;
5266 }
5267
Bram Moolenaar55b69262017-08-13 13:42:01 +02005268 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005269 && eap->cmdidx != CMD_vimgrepadd
5270 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005271 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005272 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005273 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005274
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005275 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005276 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005277 goto theend;
5278 if (fcount == 0)
5279 {
5280 EMSG(_(e_nomatch));
5281 goto theend;
5282 }
5283
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005284 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5285 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005286 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005287 {
5288 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005289 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005290 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005291
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005292 // Remember the current directory, because a BufRead autocommand that does
5293 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005294 mch_dirname(dirname_start, MAXPATHL);
5295
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005296 // Remember the current quickfix list identifier, so that we can check for
5297 // autocommands changing the current quickfix list.
Bram Moolenaar3c097222017-12-21 20:54:49 +01005298 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005299
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005300 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005301 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005302 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005303 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005304 if (time(NULL) > seconds)
5305 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005306 // Display the file name every second or so, show the user we are
5307 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005308 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005309 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005310 }
5311
Bram Moolenaar81695252004-12-29 20:58:21 +00005312 buf = buflist_findname_exp(fnames[fi]);
5313 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5314 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005315 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005316 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005317 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005318 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005319
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005320 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005321 }
5322 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005323 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005324 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005325
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005326 // Check whether the quickfix list is still valid. When loading a
5327 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005328 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005329 {
5330 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005331 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005332 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005333 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005334
Bram Moolenaar81695252004-12-29 20:58:21 +00005335 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005336 {
5337 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005338 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005339 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005340 else
5341 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005342 // Try for a match in all lines of the buffer.
5343 // For ":1vimgrep" look for first match only.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005344 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5345 tomatch, duplicate_name, flags);
5346
Bram Moolenaar81695252004-12-29 20:58:21 +00005347 if (using_dummy)
5348 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005349 if (found_match && first_match_buf == NULL)
5350 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005351 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005352 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005353 // Never keep a dummy buffer if there is another buffer
5354 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005355 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005356 buf = NULL;
5357 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005358 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005359 || buf->b_p_bh[0] == 'u' // "unload"
5360 || buf->b_p_bh[0] == 'w' // "wipe"
5361 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005362 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005363 // When no match was found we don't need to remember the
5364 // buffer, wipe it out. If there was a match and it
5365 // wasn't the first one or we won't jump there: only
5366 // unload the buffer.
5367 // Ignore 'hidden' here, because it may lead to having too
5368 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005369 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005370 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005371 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005372 buf = NULL;
5373 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005374 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005375 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005376 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005377 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005378 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005379 buf = NULL;
5380 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005381 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005382
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005383 if (buf != NULL)
5384 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005385 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005386 buf->b_flags &= ~BF_DUMMY;
5387
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005388 // If the buffer is still loaded we need to use the
5389 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005390 if (buf == first_match_buf
5391 && target_dir == NULL
5392 && STRCMP(dirname_start, dirname_now) != 0)
5393 target_dir = vim_strsave(dirname_now);
5394
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005395 // The buffer is still loaded, the Filetype autocommands
5396 // need to be done now, in that buffer. And the modelines
5397 // need to be done (again). But not the window-local
5398 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005399 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005400#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005401 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5402 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005403#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005404 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005405 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005406 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005407 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005408 }
5409 }
5410
5411 FreeWild(fcount, fnames);
5412
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005413 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5414 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
5415 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005416 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005417
Bram Moolenaar864293a2016-06-02 13:40:04 +02005418 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005419
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005420 if (au_name != NULL)
5421 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5422 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005423 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5424 // is still valid.
Bram Moolenaar3c097222017-12-21 20:54:49 +01005425 if (!qflist_valid(wp, save_qfid))
5426 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005427
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005428 if (qf_restore_list(qi, save_qfid) == FAIL)
5429 goto theend;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005430
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005431 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005432 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005433 {
5434 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005435 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5436 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005437 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005438 else
5439 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005440
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005441 // If we loaded a dummy buffer into the current window, the autocommands
5442 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005443 if (redraw_for_dummy)
5444 {
5445#ifdef FEAT_FOLDING
5446 foldUpdateAll(curwin);
5447#else
5448 redraw_later(NOT_VALID);
5449#endif
5450 }
5451
Bram Moolenaar86b68352004-12-27 21:59:20 +00005452theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005453 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005454 vim_free(dirname_now);
5455 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005456 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005457 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005458}
5459
5460/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005461 * Restore current working directory to "dirname_start" if they differ, taking
5462 * into account whether it is set locally or globally.
5463 */
5464 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005465restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005466{
5467 char_u *dirname_now = alloc(MAXPATHL);
5468
5469 if (NULL != dirname_now)
5470 {
5471 mch_dirname(dirname_now, MAXPATHL);
5472 if (STRCMP(dirname_start, dirname_now) != 0)
5473 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005474 // If the directory has changed, change it back by building up an
5475 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005476 exarg_T ea;
5477
5478 ea.arg = dirname_start;
5479 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5480 ex_cd(&ea);
5481 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005482 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005483 }
5484}
5485
5486/*
5487 * Load file "fname" into a dummy buffer and return the buffer pointer,
5488 * placing the directory resulting from the buffer load into the
5489 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5490 * prior to calling this function. Restores directory to "dirname_start" prior
5491 * to returning, if autocmds or the 'autochdir' option have changed it.
5492 *
5493 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5494 * or wipe_dummy_buffer() later!
5495 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005496 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005497 */
5498 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005499load_dummy_buffer(
5500 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005501 char_u *dirname_start, // in: old directory
5502 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005503{
5504 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005505 bufref_T newbufref;
5506 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005507 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005508 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005509 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005510
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005511 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005512 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5513 if (newbuf == NULL)
5514 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005515 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005516
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005517 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005518 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5519
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005520 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005521 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005522 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005523 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005524 ++newbuf->b_locked;
5525
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005526 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005527 aucmd_prepbuf(&aco, newbuf);
5528
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005529 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005530 (void)setfname(curbuf, fname, NULL, FALSE);
5531
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005532 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005533 check_need_swap(TRUE);
5534
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005535 // Remove the "dummy" flag, otherwise autocommands may not
5536 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005537 curbuf->b_flags &= ~BF_DUMMY;
5538
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005539 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005540 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005541 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005542 NULL, READ_NEW | READ_DUMMY);
5543 --newbuf->b_locked;
5544 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005545 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005546 && !(curbuf->b_flags & BF_NEW))
5547 {
5548 failed = FALSE;
5549 if (curbuf != newbuf)
5550 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005551 // Bloody autocommands changed the buffer! Can happen when
5552 // using netrw and editing a remote file. Use the current
5553 // buffer instead, delete the dummy one after restoring the
5554 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005555 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005556 newbuf = curbuf;
5557 }
5558 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005559
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005560 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005561 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005562 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5563 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005564
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005565 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5566 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005567 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005568 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005569
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005570 // When autocommands/'autochdir' option changed directory: go back.
5571 // Let the caller know what the resulting dir was first, in case it is
5572 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005573 mch_dirname(resulting_dir, MAXPATHL);
5574 restore_start_dir(dirname_start);
5575
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005576 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005577 return NULL;
5578 if (failed)
5579 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005580 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005581 return NULL;
5582 }
5583 return newbuf;
5584}
5585
5586/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005587 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5588 * directory to "dirname_start" prior to returning, if autocmds or the
5589 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005590 */
5591 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005592wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005593{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005594 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005595 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005596#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005597 cleanup_T cs;
5598
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005599 // Reset the error/interrupt/exception state here so that aborting()
5600 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5601 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005602 enter_cleanup(&cs);
5603#endif
5604
Bram Moolenaar81695252004-12-29 20:58:21 +00005605 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005606
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005607#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005608 // Restore the error/interrupt/exception state if not discarded by a
5609 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005610 leave_cleanup(&cs);
5611#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005612 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005613 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005614 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005615}
5616
5617/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005618 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5619 * directory to "dirname_start" prior to returning, if autocmds or the
5620 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005621 */
5622 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005623unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005624{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005625 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005626 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005627 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005628
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005629 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005630 restore_start_dir(dirname_start);
5631 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005632}
5633
Bram Moolenaar05159a02005-02-26 23:04:13 +00005634#if defined(FEAT_EVAL) || defined(PROTO)
5635/*
5636 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005637 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005638 */
5639 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005640get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005641{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005642 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005643 dict_T *dict;
5644 char_u buf[2];
5645 qfline_T *qfp;
5646 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005647 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005648
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005649 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005650 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005651 qi = &ql_info;
5652 if (wp != NULL)
5653 {
5654 qi = GET_LOC_LIST(wp);
5655 if (qi == NULL)
5656 return FAIL;
5657 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005658 }
5659
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005660 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005661 qf_idx = qi->qf_curlist;
5662
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005663 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005664 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005665
Bram Moolenaard823fa92016-08-12 16:29:27 +02005666 qfp = qi->qf_lists[qf_idx].qf_start;
5667 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005668 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005669 // Handle entries with a non-existing buffer number.
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005670 bufnum = qfp->qf_fnum;
5671 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5672 bufnum = 0;
5673
Bram Moolenaar05159a02005-02-26 23:04:13 +00005674 if ((dict = dict_alloc()) == NULL)
5675 return FAIL;
5676 if (list_append_dict(list, dict) == FAIL)
5677 return FAIL;
5678
5679 buf[0] = qfp->qf_type;
5680 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005681 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5682 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5683 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5684 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5685 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5686 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5687 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5688 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5689 || dict_add_string(dict, "type", buf) == FAIL
5690 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005691 return FAIL;
5692
5693 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005694 if (qfp == NULL)
5695 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005696 }
5697 return OK;
5698}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005699
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005700// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005701enum {
5702 QF_GETLIST_NONE = 0x0,
5703 QF_GETLIST_TITLE = 0x1,
5704 QF_GETLIST_ITEMS = 0x2,
5705 QF_GETLIST_NR = 0x4,
5706 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005707 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005708 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005709 QF_GETLIST_IDX = 0x40,
5710 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005711 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005712 QF_GETLIST_FILEWINID = 0x200,
5713 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005714};
5715
5716/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005717 * Parse text from 'di' and return the quickfix list items.
5718 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005719 */
5720 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005721qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005722{
5723 int status = FAIL;
5724 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005725 char_u *errorformat = p_efm;
5726 dictitem_T *efm_di;
5727 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005728
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005729 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005730 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005731 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005732 // If errorformat is supplied then use it, otherwise use the 'efm'
5733 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005734 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5735 {
5736 if (efm_di->di_tv.v_type != VAR_STRING ||
5737 efm_di->di_tv.vval.v_string == NULL)
5738 return FAIL;
5739 errorformat = efm_di->di_tv.vval.v_string;
5740 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005741
Bram Moolenaar36538222017-09-02 19:51:44 +02005742 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005743 if (l == NULL)
5744 return FAIL;
5745
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005746 qi = ll_new_list();
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005747 if (qi != NULL)
5748 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005749 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005750 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5751 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005752 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005753 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005754 }
5755 free(qi);
5756 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005757 dict_add_list(retdict, "items", l);
5758 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005759 }
5760
5761 return status;
5762}
5763
5764/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005765 * Return the quickfix/location list window identifier in the current tabpage.
5766 */
5767 static int
5768qf_winid(qf_info_T *qi)
5769{
5770 win_T *win;
5771
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005772 // The quickfix window can be opened even if the quickfix list is not set
5773 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005774 if (qi == NULL)
5775 return 0;
5776 win = qf_find_win(qi);
5777 if (win != NULL)
5778 return win->w_id;
5779 return 0;
5780}
5781
5782/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005783 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005784 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005785 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005786qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005787{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005788 int flags = QF_GETLIST_NONE;
5789
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005790 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005791 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005792 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005793 if (!loclist)
5794 // File window ID is applicable only to location list windows
5795 flags &= ~ QF_GETLIST_FILEWINID;
5796 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005797
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005798 if (dict_find(what, (char_u *)"title", -1) != NULL)
5799 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005800
Bram Moolenaara6d48492017-12-12 22:45:31 +01005801 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5802 flags |= QF_GETLIST_NR;
5803
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005804 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5805 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005806
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005807 if (dict_find(what, (char_u *)"context", -1) != NULL)
5808 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005809
Bram Moolenaara6d48492017-12-12 22:45:31 +01005810 if (dict_find(what, (char_u *)"id", -1) != NULL)
5811 flags |= QF_GETLIST_ID;
5812
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005813 if (dict_find(what, (char_u *)"items", -1) != NULL)
5814 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005815
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005816 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5817 flags |= QF_GETLIST_IDX;
5818
5819 if (dict_find(what, (char_u *)"size", -1) != NULL)
5820 flags |= QF_GETLIST_SIZE;
5821
Bram Moolenaarb254af32017-12-18 19:48:58 +01005822 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5823 flags |= QF_GETLIST_TICK;
5824
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005825 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5826 flags |= QF_GETLIST_FILEWINID;
5827
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005828 return flags;
5829}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005830
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005831/*
5832 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5833 * If 'nr' and 'id' are not present in 'what' then return the current
5834 * quickfix list index.
5835 * If 'nr' is zero then return the current quickfix list index.
5836 * If 'nr' is '$' then return the last quickfix list index.
5837 * If 'id' is present then return the index of the quickfix list with that id.
5838 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5839 * Return -1, if quickfix list is not present or if the stack is empty.
5840 */
5841 static int
5842qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5843{
5844 int qf_idx;
5845 dictitem_T *di;
5846
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005847 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005848 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5849 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005850 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005851 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005852 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005853 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005854 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005855 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005856 qf_idx = di->di_tv.vval.v_number - 1;
5857 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005858 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005859 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005860 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005861 else if (di->di_tv.v_type == VAR_STRING
5862 && di->di_tv.vval.v_string != NULL
5863 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005864 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005865 qf_idx = qi->qf_listcount - 1;
5866 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005867 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005868 }
5869
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005870 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5871 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005872 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005873 if (di->di_tv.v_type == VAR_NUMBER)
5874 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005875 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005876 if (di->di_tv.vval.v_number != 0)
5877 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5878 }
5879 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005880 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005881 }
5882
5883 return qf_idx;
5884}
5885
5886/*
5887 * Return default values for quickfix list properties in retdict.
5888 */
5889 static int
5890qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5891{
5892 int status = OK;
5893
5894 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005895 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005896 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5897 {
5898 list_T *l = list_alloc();
5899 if (l != NULL)
5900 status = dict_add_list(retdict, "items", l);
5901 else
5902 status = FAIL;
5903 }
5904 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005905 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005906 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005907 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005908 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005909 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005910 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005911 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005912 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005913 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005914 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005915 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005916 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005917 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005918 if ((status == OK) && (qi != &ql_info) && (flags & QF_GETLIST_FILEWINID))
5919 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005920
5921 return status;
5922}
5923
5924/*
5925 * Return the quickfix list title as 'title' in retdict
5926 */
5927 static int
5928qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5929{
Bram Moolenaare0be1672018-07-08 16:50:37 +02005930 return dict_add_string(retdict, "title", qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005931}
5932
5933/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005934 * Returns the identifier of the window used to display files from a location
5935 * list. If there is no associated window, then returns 0. Useful only when
5936 * called from a location list window.
5937 */
5938 static int
5939qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
5940{
5941 int winid = 0;
5942
5943 if (wp != NULL && IS_LL_WINDOW(wp))
5944 {
5945 win_T *ll_wp = qf_find_win_with_loclist(qi);
5946 if (ll_wp != NULL)
5947 winid = ll_wp->w_id;
5948 }
5949
5950 return dict_add_number(retdict, "filewinid", winid);
5951}
5952
5953/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005954 * Return the quickfix list items/entries as 'items' in retdict
5955 */
5956 static int
5957qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5958{
5959 int status = OK;
5960 list_T *l = list_alloc();
5961 if (l != NULL)
5962 {
5963 (void)get_errorlist(qi, NULL, qf_idx, l);
5964 dict_add_list(retdict, "items", l);
5965 }
5966 else
5967 status = FAIL;
5968
5969 return status;
5970}
5971
5972/*
5973 * Return the quickfix list context (if any) as 'context' in retdict.
5974 */
5975 static int
5976qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5977{
5978 int status;
5979 dictitem_T *di;
5980
5981 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5982 {
5983 di = dictitem_alloc((char_u *)"context");
5984 if (di != NULL)
5985 {
5986 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5987 status = dict_add(retdict, di);
5988 if (status == FAIL)
5989 dictitem_free(di);
5990 }
5991 else
5992 status = FAIL;
5993 }
5994 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02005995 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005996
5997 return status;
5998}
5999
6000/*
6001 * Return the quickfix list index as 'idx' in retdict
6002 */
6003 static int
6004qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6005{
6006 int idx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006007 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006008 // For empty lists, qf_index is set to 1
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006009 idx = 0;
Bram Moolenaare0be1672018-07-08 16:50:37 +02006010 return dict_add_number(retdict, "idx", idx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006011}
6012
6013/*
6014 * Return quickfix/location list details (title) as a
6015 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6016 * then current list is used. Otherwise the specified list is used.
6017 */
6018 int
6019qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6020{
6021 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006022 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006023 int status = OK;
6024 int qf_idx;
6025 dictitem_T *di;
6026 int flags = QF_GETLIST_NONE;
6027
6028 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6029 return qf_get_list_from_lines(what, di, retdict);
6030
6031 if (wp != NULL)
6032 qi = GET_LOC_LIST(wp);
6033
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006034 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006035
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006036 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006037 qf_idx = qf_getprop_qfidx(qi, what);
6038
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006039 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006040 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006041 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006042
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006043 qfl = &qi->qf_lists[qf_idx];
6044
Bram Moolenaard823fa92016-08-12 16:29:27 +02006045 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006046 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006047 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006048 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006049 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006050 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006051 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006052 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006053 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006054 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006055 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006056 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006057 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006058 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006059 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006060 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006061 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006062 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006063 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6064 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006065
Bram Moolenaard823fa92016-08-12 16:29:27 +02006066 return status;
6067}
6068
6069/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006070 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
6071 * items in the dict 'd'.
6072 */
6073 static int
6074qf_add_entry_from_dict(
6075 qf_info_T *qi,
6076 int qf_idx,
6077 dict_T *d,
6078 int first_entry)
6079{
6080 static int did_bufnr_emsg;
6081 char_u *filename, *module, *pattern, *text, *type;
6082 int bufnum, valid, status, col, vcol, nr;
6083 long lnum;
6084
6085 if (first_entry)
6086 did_bufnr_emsg = FALSE;
6087
6088 filename = get_dict_string(d, (char_u *)"filename", TRUE);
6089 module = get_dict_string(d, (char_u *)"module", TRUE);
6090 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6091 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6092 col = (int)get_dict_number(d, (char_u *)"col");
6093 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6094 nr = (int)get_dict_number(d, (char_u *)"nr");
6095 type = get_dict_string(d, (char_u *)"type", TRUE);
6096 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6097 text = get_dict_string(d, (char_u *)"text", TRUE);
6098 if (text == NULL)
6099 text = vim_strsave((char_u *)"");
6100
6101 valid = TRUE;
6102 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6103 valid = FALSE;
6104
6105 // Mark entries with non-existing buffer number as not valid. Give the
6106 // error message only once.
6107 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6108 {
6109 if (!did_bufnr_emsg)
6110 {
6111 did_bufnr_emsg = TRUE;
6112 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6113 }
6114 valid = FALSE;
6115 bufnum = 0;
6116 }
6117
6118 // If the 'valid' field is present it overrules the detected value.
6119 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6120 valid = (int)get_dict_number(d, (char_u *)"valid");
6121
6122 status = qf_add_entry(qi,
6123 qf_idx,
6124 NULL, // dir
6125 filename,
6126 module,
6127 bufnum,
6128 text,
6129 lnum,
6130 col,
6131 vcol, // vis_col
6132 pattern, // search pattern
6133 nr,
6134 type == NULL ? NUL : *type,
6135 valid);
6136
6137 vim_free(filename);
6138 vim_free(module);
6139 vim_free(pattern);
6140 vim_free(text);
6141 vim_free(type);
6142
6143 return status;
6144}
6145
6146/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006147 * Add list of entries to quickfix/location list. Each list entry is
6148 * a dictionary with item information.
6149 */
6150 static int
6151qf_add_entries(
6152 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006153 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006154 list_T *list,
6155 char_u *title,
6156 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006157{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006158 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006159 listitem_T *li;
6160 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006161 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006162 int retval = OK;
6163
Bram Moolenaara3921f42017-06-04 15:30:34 +02006164 if (action == ' ' || qf_idx == qi->qf_listcount)
6165 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006166 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006167 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006168 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006169 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006170 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006171 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006172 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006173 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006174 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006175 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006176 qf_free_items(qfl);
6177 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006178 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006179
6180 for (li = list->lv_first; li != NULL; li = li->li_next)
6181 {
6182 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006183 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006184
6185 d = li->li_tv.vval.v_dict;
6186 if (d == NULL)
6187 continue;
6188
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006189 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first);
6190 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006191 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006192 }
6193
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006194 if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006195 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006196 qfl->qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006197 else
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006198 qfl->qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006199 if (action != 'a')
6200 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006201 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006202 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006203 qfl->qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006204 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006205
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006206 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006207 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006208
6209 return retval;
6210}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006211
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006212/*
6213 * Get the quickfix list index from 'nr' or 'id'
6214 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006215 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006216qf_setprop_get_qfidx(
6217 qf_info_T *qi,
6218 dict_T *what,
6219 int action,
6220 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006221{
6222 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006223 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006224
Bram Moolenaard823fa92016-08-12 16:29:27 +02006225 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6226 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006227 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006228 if (di->di_tv.v_type == VAR_NUMBER)
6229 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006230 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006231 if (di->di_tv.vval.v_number != 0)
6232 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006233
Bram Moolenaar55b69262017-08-13 13:42:01 +02006234 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6235 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006236 // When creating a new list, accept qf_idx pointing to the next
6237 // non-available list and add the new list at the end of the
6238 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006239 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006240 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006241 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006242 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006243 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006244 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006245 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006246 }
6247 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006248 && di->di_tv.vval.v_string != NULL
6249 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006250 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006251 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006252 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006253 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006254 qf_idx = 0;
6255 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006256 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006257 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006258 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006259 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006260 }
6261
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006262 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006263 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006264 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006265 if (di->di_tv.v_type != VAR_NUMBER)
6266 return INVALID_QFIDX;
6267
6268 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006269 }
6270
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006271 return qf_idx;
6272}
6273
6274/*
6275 * Set the quickfix list title.
6276 */
6277 static int
6278qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6279{
6280 if (di->di_tv.v_type != VAR_STRING)
6281 return FAIL;
6282
6283 vim_free(qi->qf_lists[qf_idx].qf_title);
6284 qi->qf_lists[qf_idx].qf_title =
6285 get_dict_string(what, (char_u *)"title", TRUE);
6286 if (qf_idx == qi->qf_curlist)
6287 qf_update_win_titlevar(qi);
6288
6289 return OK;
6290}
6291
6292/*
6293 * Set quickfix list items/entries.
6294 */
6295 static int
6296qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6297{
6298 int retval = FAIL;
6299 char_u *title_save;
6300
6301 if (di->di_tv.v_type != VAR_LIST)
6302 return FAIL;
6303
6304 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6305 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6306 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006307 vim_free(title_save);
6308
6309 return retval;
6310}
6311
6312/*
6313 * Set quickfix list items/entries from a list of lines.
6314 */
6315 static int
6316qf_setprop_items_from_lines(
6317 qf_info_T *qi,
6318 int qf_idx,
6319 dict_T *what,
6320 dictitem_T *di,
6321 int action)
6322{
6323 char_u *errorformat = p_efm;
6324 dictitem_T *efm_di;
6325 int retval = FAIL;
6326
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006327 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006328 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6329 {
6330 if (efm_di->di_tv.v_type != VAR_STRING ||
6331 efm_di->di_tv.vval.v_string == NULL)
6332 return FAIL;
6333 errorformat = efm_di->di_tv.vval.v_string;
6334 }
6335
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006336 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006337 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6338 return FAIL;
6339
6340 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006341 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006342 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6343 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6344 retval = OK;
6345
6346 return retval;
6347}
6348
6349/*
6350 * Set quickfix list context.
6351 */
6352 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006353qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006354{
6355 typval_T *ctx;
6356
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006357 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006358 ctx = alloc_tv();
6359 if (ctx != NULL)
6360 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006361 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006362
6363 return OK;
6364}
6365
6366/*
6367 * Set quickfix/location list properties (title, items, context).
6368 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006369 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006370 */
6371 static int
6372qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6373{
6374 dictitem_T *di;
6375 int retval = FAIL;
6376 int qf_idx;
6377 int newlist = FALSE;
6378
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006379 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006380 newlist = TRUE;
6381
6382 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006383 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006384 return FAIL;
6385
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006386 if (newlist)
6387 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006388 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006389 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006390 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006391 }
6392
6393 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006394 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006395 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006396 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006397 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006398 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006399 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006400 retval = qf_setprop_context(&qi->qf_lists[qf_idx], di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006401
Bram Moolenaarb254af32017-12-18 19:48:58 +01006402 if (retval == OK)
6403 qf_list_changed(qi, qf_idx);
6404
Bram Moolenaard823fa92016-08-12 16:29:27 +02006405 return retval;
6406}
6407
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006408/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006409 * Find the non-location list window with the specified location list in the
6410 * current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006411 */
6412 static win_T *
6413find_win_with_ll(qf_info_T *qi)
6414{
6415 win_T *wp = NULL;
6416
6417 FOR_ALL_WINDOWS(wp)
6418 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6419 return wp;
6420
6421 return NULL;
6422}
6423
6424/*
6425 * Free the entire quickfix/location list stack.
6426 * If the quickfix/location list window is open, then clear it.
6427 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006428 static void
6429qf_free_stack(win_T *wp, qf_info_T *qi)
6430{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006431 win_T *qfwin = qf_find_win(qi);
6432 win_T *llwin = NULL;
6433 win_T *orig_wp = wp;
6434
6435 if (qfwin != NULL)
6436 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006437 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006438 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006439 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006440 qf_update_buffer(qi, NULL);
6441 }
6442
6443 if (wp != NULL && IS_LL_WINDOW(wp))
6444 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006445 // If in the location list window, then use the non-location list
6446 // window with this location list (if present)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006447 llwin = find_win_with_ll(qi);
6448 if (llwin != NULL)
6449 wp = llwin;
6450 }
6451
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006452 qf_free_all(wp);
6453 if (wp == NULL)
6454 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006455 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006456 qi->qf_curlist = 0;
6457 qi->qf_listcount = 0;
6458 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006459 else if (IS_LL_WINDOW(orig_wp))
6460 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006461 // If the location list window is open, then create a new empty
6462 // location list
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006463 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006464
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006465 // first free the list reference in the location list window
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006466 ll_free_all(&orig_wp->w_llist_ref);
6467
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006468 orig_wp->w_llist_ref = new_ll;
6469 if (llwin != NULL)
6470 {
6471 llwin->w_llist = new_ll;
6472 new_ll->qf_refcount++;
6473 }
6474 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006475}
6476
Bram Moolenaard823fa92016-08-12 16:29:27 +02006477/*
6478 * Populate the quickfix list with the items supplied in the list
6479 * of dictionaries. "title" will be copied to w:quickfix_title.
6480 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6481 */
6482 int
6483set_errorlist(
6484 win_T *wp,
6485 list_T *list,
6486 int action,
6487 char_u *title,
6488 dict_T *what)
6489{
6490 qf_info_T *qi = &ql_info;
6491 int retval = OK;
6492
6493 if (wp != NULL)
6494 {
6495 qi = ll_get_or_alloc_list(wp);
6496 if (qi == NULL)
6497 return FAIL;
6498 }
6499
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006500 if (action == 'f')
6501 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006502 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006503 qf_free_stack(wp, qi);
6504 }
6505 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006506 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006507 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006508 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006509 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006510 if (retval == OK)
6511 qf_list_changed(qi, qi->qf_curlist);
6512 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006513
6514 return retval;
6515}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006516
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006517/*
6518 * Mark the context as in use for all the lists in a quickfix stack.
6519 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006520 static int
6521mark_quickfix_ctx(qf_info_T *qi, int copyID)
6522{
6523 int i;
6524 int abort = FALSE;
6525 typval_T *ctx;
6526
6527 for (i = 0; i < LISTCOUNT && !abort; ++i)
6528 {
6529 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006530 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6531 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006532 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6533 }
6534
6535 return abort;
6536}
6537
6538/*
6539 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006540 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006541 */
6542 int
6543set_ref_in_quickfix(int copyID)
6544{
6545 int abort = FALSE;
6546 tabpage_T *tp;
6547 win_T *win;
6548
6549 abort = mark_quickfix_ctx(&ql_info, copyID);
6550 if (abort)
6551 return abort;
6552
6553 FOR_ALL_TAB_WINDOWS(tp, win)
6554 {
6555 if (win->w_llist != NULL)
6556 {
6557 abort = mark_quickfix_ctx(win->w_llist, copyID);
6558 if (abort)
6559 return abort;
6560 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006561 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6562 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006563 // In a location list window and none of the other windows is
6564 // referring to this location list. Mark the location list
6565 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006566 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6567 if (abort)
6568 return abort;
6569 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006570 }
6571
6572 return abort;
6573}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006574#endif
6575
Bram Moolenaar81695252004-12-29 20:58:21 +00006576/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006577 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006578 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006579 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006580 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006581 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006582 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006583 */
6584 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006585ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006586{
6587 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006588 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006589 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006590 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006591 int_u save_qfid;
6592 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006593
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006594 switch (eap->cmdidx)
6595 {
6596 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6597 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6598 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6599 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6600 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6601 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6602 default: break;
6603 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006604 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6605 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006606 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006607#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006608 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006609 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006610#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006611 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006612
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006613 // Must come after autocommands.
Bram Moolenaar39665952018-08-15 20:59:48 +02006614 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006615 {
6616 qi = ll_get_or_alloc_list(curwin);
6617 if (qi == NULL)
6618 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006619 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006620 }
6621
Bram Moolenaar86b68352004-12-27 21:59:20 +00006622 if (*eap->arg == NUL)
6623 buf = curbuf;
6624 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6625 buf = buflist_findnr(atoi((char *)eap->arg));
6626 if (buf == NULL)
6627 EMSG(_(e_invarg));
6628 else if (buf->b_ml.ml_mfp == NULL)
6629 EMSG(_("E681: Buffer is not loaded"));
6630 else
6631 {
6632 if (eap->addr_count == 0)
6633 {
6634 eap->line1 = 1;
6635 eap->line2 = buf->b_ml.ml_line_count;
6636 }
6637 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6638 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6639 EMSG(_(e_invrange));
6640 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006641 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006642 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006643
6644 if (buf->b_sfname)
6645 {
6646 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6647 (char *)qf_title, (char *)buf->b_sfname);
6648 qf_title = IObuff;
6649 }
6650
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006651 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006652 (eap->cmdidx != CMD_caddbuffer
6653 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006654 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006655 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006656 if (res >= 0)
6657 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006658
6659 // Remember the current quickfix list identifier, so that we can
6660 // check for autocommands changing the current quickfix list.
6661 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006662 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006663 {
6664 buf_T *curbuf_old = curbuf;
6665
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006666 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6667 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006668 if (curbuf != curbuf_old)
6669 // Autocommands changed buffer, don't jump now, "qi" may
6670 // be invalid.
6671 res = 0;
6672 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006673 // Jump to the first error for a new list and if autocmds didn't
6674 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006675 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006676 eap->cmdidx == CMD_lbuffer)
6677 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006678 // display the first error
6679 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar754b5602006-02-09 23:53:20 +00006680 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006681 }
6682}
6683
Bram Moolenaar1e015462005-09-25 22:16:38 +00006684#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006685/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006686 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6687 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006688 */
6689 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006690ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006691{
6692 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006693 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006694 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006695 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006696 int_u save_qfid;
6697 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006698
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006699 switch (eap->cmdidx)
6700 {
6701 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6702 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6703 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6704 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6705 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6706 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6707 default: break;
6708 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006709 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6710 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006711 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006712#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006713 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006714 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006715#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006716 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006717
Bram Moolenaar39665952018-08-15 20:59:48 +02006718 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006719 {
6720 qi = ll_get_or_alloc_list(curwin);
6721 if (qi == NULL)
6722 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006723 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006724 }
6725
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006726 // Evaluate the expression. When the result is a string or a list we can
6727 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006728 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006729 if (tv != NULL)
6730 {
6731 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6732 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6733 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006734 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006735 (eap->cmdidx != CMD_caddexpr
6736 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006737 (linenr_T)0, (linenr_T)0,
6738 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006739 if (res >= 0)
6740 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006741
6742 // Remember the current quickfix list identifier, so that we can
6743 // check for autocommands changing the current quickfix list.
6744 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006745 if (au_name != NULL)
6746 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6747 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006748
6749 // Jump to the first error for a new list and if autocmds didn't
6750 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006751 if (res > 0 && (eap->cmdidx == CMD_cexpr
6752 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006753 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006754 // display the first error
6755 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006756 }
6757 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006758 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006759 free_tv(tv);
6760 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006761}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006762#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006763
6764/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006765 * Get the location list for ":lhelpgrep"
6766 */
6767 static qf_info_T *
6768hgr_get_ll(int *new_ll)
6769{
6770 win_T *wp;
6771 qf_info_T *qi;
6772
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006773 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006774 if (bt_help(curwin->w_buffer))
6775 wp = curwin;
6776 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006777 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006778 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006779
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006780 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006781 qi = NULL;
6782 else
6783 qi = wp->w_llist;
6784
6785 if (qi == NULL)
6786 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006787 // Allocate a new location list for help text matches
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006788 if ((qi = ll_new_list()) == NULL)
6789 return NULL;
6790 *new_ll = TRUE;
6791 }
6792
6793 return qi;
6794}
6795
6796/*
6797 * Search for a pattern in a help file.
6798 */
6799 static void
6800hgr_search_file(
6801 qf_info_T *qi,
6802 char_u *fname,
6803#ifdef FEAT_MBYTE
6804 vimconv_T *p_vc,
6805#endif
6806 regmatch_T *p_regmatch)
6807{
6808 FILE *fd;
6809 long lnum;
6810
6811 fd = mch_fopen((char *)fname, "r");
6812 if (fd == NULL)
6813 return;
6814
6815 lnum = 1;
6816 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6817 {
6818 char_u *line = IObuff;
6819#ifdef FEAT_MBYTE
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006820 // Convert a line if 'encoding' is not utf-8 and
6821 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006822 if (p_vc->vc_type != CONV_NONE
6823 && has_non_ascii(IObuff))
6824 {
6825 line = string_convert(p_vc, IObuff, NULL);
6826 if (line == NULL)
6827 line = IObuff;
6828 }
6829#endif
6830
6831 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6832 {
6833 int l = (int)STRLEN(line);
6834
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006835 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006836 while (l > 0 && line[l - 1] <= ' ')
6837 line[--l] = NUL;
6838
6839 if (qf_add_entry(qi,
6840 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006841 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006842 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006843 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006844 0,
6845 line,
6846 lnum,
6847 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006848 + 1, // col
6849 FALSE, // vis_col
6850 NULL, // search pattern
6851 0, // nr
6852 1, // type
6853 TRUE // valid
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006854 ) == FAIL)
6855 {
6856 got_int = TRUE;
6857#ifdef FEAT_MBYTE
6858 if (line != IObuff)
6859 vim_free(line);
6860#endif
6861 break;
6862 }
6863 }
6864#ifdef FEAT_MBYTE
6865 if (line != IObuff)
6866 vim_free(line);
6867#endif
6868 ++lnum;
6869 line_breakcheck();
6870 }
6871 fclose(fd);
6872}
6873
6874/*
6875 * Search for a pattern in all the help files in the doc directory under
6876 * the given directory.
6877 */
6878 static void
6879hgr_search_files_in_dir(
6880 qf_info_T *qi,
6881 char_u *dirname,
6882 regmatch_T *p_regmatch
6883#ifdef FEAT_MBYTE
6884 , vimconv_T *p_vc
6885#endif
6886#ifdef FEAT_MULTI_LANG
6887 , char_u *lang
6888#endif
6889 )
6890{
6891 int fcount;
6892 char_u **fnames;
6893 int fi;
6894
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006895 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006896 add_pathsep(dirname);
6897 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6898 if (gen_expand_wildcards(1, &dirname, &fcount,
6899 &fnames, EW_FILE|EW_SILENT) == OK
6900 && fcount > 0)
6901 {
6902 for (fi = 0; fi < fcount && !got_int; ++fi)
6903 {
6904#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006905 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006906 if (lang != NULL
6907 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006908 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006909 && !(STRNICMP(lang, "en", 2) == 0
6910 && STRNICMP("txt", fnames[fi]
6911 + STRLEN(fnames[fi]) - 3, 3) == 0))
6912 continue;
6913#endif
6914
6915 hgr_search_file(qi, fnames[fi],
6916#ifdef FEAT_MBYTE
6917 p_vc,
6918#endif
6919 p_regmatch);
6920 }
6921 FreeWild(fcount, fnames);
6922 }
6923}
6924
6925/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006926 * Search for a pattern in all the help files in the 'runtimepath'
6927 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006928 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006929 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006930 */
6931 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006932hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006933{
6934 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006935
6936#ifdef FEAT_MBYTE
6937 vimconv_T vc;
6938
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006939 // Help files are in utf-8 or latin1, convert lines when 'encoding'
6940 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006941 vc.vc_type = CONV_NONE;
6942 if (!enc_utf8)
6943 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6944#endif
6945
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006946
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006947 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006948 p = p_rtp;
6949 while (*p != NUL && !got_int)
6950 {
6951 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6952
6953 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6954#ifdef FEAT_MBYTE
6955 , &vc
6956#endif
6957#ifdef FEAT_MULTI_LANG
6958 , lang
6959#endif
6960 );
6961 }
6962
6963#ifdef FEAT_MBYTE
6964 if (vc.vc_type != CONV_NONE)
6965 convert_setup(&vc, NULL, NULL);
6966#endif
6967}
6968
6969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 * ":helpgrep {pattern}"
6971 */
6972 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006973ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974{
6975 regmatch_T regmatch;
6976 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006977 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006978 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006979 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006980 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
Bram Moolenaar73633f82012-01-20 13:39:07 +01006982 switch (eap->cmdidx)
6983 {
6984 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6985 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6986 default: break;
6987 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006988 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6989 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006990 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006991#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006992 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006993 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006994#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006995 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006996
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006997 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01006998 save_cpo = p_cpo;
6999 p_cpo = empty_option;
7000
Bram Moolenaar39665952018-08-15 20:59:48 +02007001 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007002 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007003 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007004 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007005 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007006 }
7007
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007008#ifdef FEAT_MULTI_LANG
7009 // Check for a specified language
7010 lang = check_help_lang(eap->arg);
7011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7013 regmatch.rm_ic = FALSE;
7014 if (regmatch.regprog != NULL)
7015 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007016 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007017 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007019 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007020
Bram Moolenaar473de612013-06-08 18:19:48 +02007021 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007023 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
7024 qi->qf_lists[qi->qf_curlist].qf_ptr =
7025 qi->qf_lists[qi->qf_curlist].qf_start;
7026 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 }
7028
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007029 if (p_cpo == empty_option)
7030 p_cpo = save_cpo;
7031 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007032 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007033 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034
Bram Moolenaarb254af32017-12-18 19:48:58 +01007035 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02007036 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
Bram Moolenaar73633f82012-01-20 13:39:07 +01007038 if (au_name != NULL)
7039 {
7040 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7041 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007042 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007043 // autocommands made "qi" invalid
Bram Moolenaar73633f82012-01-20 13:39:07 +01007044 return;
7045 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007046
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007047 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007048 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007049 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007050 else
7051 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007052
7053 if (eap->cmdidx == CMD_lhelpgrep)
7054 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007055 // If the help window is not opened or if it already points to the
7056 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007057 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007058 {
7059 if (new_qi)
7060 ll_free_all(&qi);
7061 }
7062 else if (curwin->w_llist == NULL)
7063 curwin->w_llist = qi;
7064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065}
7066
7067#endif /* FEAT_QUICKFIX */