blob: b899612d874b13af9c350eb96265589c6796e29a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020030 qfline_T *qf_next; // pointer to next error in the list
31 qfline_T *qf_prev; // pointer to previous error in the list
32 linenr_T qf_lnum; // line number where the error occurred
33 int qf_fnum; // file number for the line
34 int qf_col; // column where the error occurred
35 int qf_nr; // error number
36 char_u *qf_module; // module name for this error
37 char_u *qf_pattern; // search pattern for the error
38 char_u *qf_text; // description of the error
39 char_u qf_viscol; // set to TRUE if qf_col is screen column
40 char_u qf_cleared; // set to TRUE if line has been deleted
41 char_u qf_type; // type of the error (mostly 'E'); 1 for
42 // :helpgrep
43 char_u qf_valid; // valid error message detected
Bram Moolenaar071d4272004-06-13 20:20:40 +000044};
45
46/*
47 * There is a stack of error lists.
48 */
49#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020050#define INVALID_QFIDX (-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020052/*
53 * Quickfix/Location list definition
54 * Contains a list of entries (qfline_T). qf_start points to the first entry
55 * and qf_last points to the last entry. qf_count contains the list size.
56 *
57 * Usually the list contains one or more entries. But an empty list can be
58 * created using setqflist()/setloclist() with a title and/or user context
59 * information and entries can be added later using setqflist()/setloclist().
60 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000062{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020063 int_u qf_id; // Unique identifier for this list
64 qfline_T *qf_start; // pointer to the first error
65 qfline_T *qf_last; // pointer to the last error
66 qfline_T *qf_ptr; // pointer to the current error
67 int qf_count; // number of errors (0 means empty list)
68 int qf_index; // current index in the error list
69 int qf_nonevalid; // TRUE if not a single valid entry found
70 char_u *qf_title; // title derived from the command that created
71 // the error list or set by setqflist
72 typval_T *qf_ctx; // context set by setqflist/setloclist
Bram Moolenaara7df8c72017-07-19 13:23:06 +020073
74 struct dir_stack_T *qf_dir_stack;
75 char_u *qf_directory;
76 struct dir_stack_T *qf_file_stack;
77 char_u *qf_currfile;
78 int qf_multiline;
79 int qf_multiignore;
80 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010081 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000082} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020084/*
85 * Quickfix/Location list stack definition
86 * Contains a list of quickfix/location lists (qf_list_T)
87 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000088struct qf_info_S
89{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020090 // Count of references to this list. Used only for location lists.
91 // When a location list window reference this list, qf_refcount
92 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
93 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +000094 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020095 int qf_listcount; // current number of lists
96 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +000097 qf_list_T qf_lists[LISTCOUNT];
98};
99
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200100static qf_info_T ql_info; // global quickfix list
101static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200103#define FMT_PATTERNS 11 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
105/*
106 * Structure used to hold the info of one part of 'errorformat'
107 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000108typedef struct efm_S efm_T;
109struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200111 regprog_T *prog; // pre-formatted part of 'errorformat'
112 efm_T *next; // pointer to next (NULL if last)
113 char_u addr[FMT_PATTERNS]; // indices of used % patterns
114 char_u prefix; // prefix of this format line:
115 // 'D' enter directory
116 // 'X' leave directory
117 // 'A' start of multi-line message
118 // 'E' error message
119 // 'W' warning message
120 // 'I' informational message
121 // 'C' continuation line
122 // 'Z' end of multi-line message
123 // 'G' general, unspecific message
124 // 'P' push file (partial) message
125 // 'Q' pop/quit file (partial) message
126 // 'O' overread (partial) message
127 char_u flags; // additional flags given in prefix
128 // '-' do not include this line
129 // '+' include whole line in message
130 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131};
132
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200133// List of location lists to be deleted.
134// Used to delay the deletion of locations lists by autocmds.
135typedef struct qf_delq_S
136{
137 struct qf_delq_S *next;
138 qf_info_T *qi;
139} qf_delq_T;
140static qf_delq_T *qf_delq_head = NULL;
141
142// Counter to prevent autocmds from freeing up location lists when they are
143// still being used.
144static int quickfix_busy = 0;
145
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200146static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100147
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaard76ce852018-05-01 15:02:04 +0200149static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200150static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200152static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200153static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200155static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100158static win_T *qf_find_win(qf_info_T *qi);
159static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200160static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200161static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100162static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
163static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
164static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
165static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200167// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000168#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200169// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000170#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200171
172// Quickfix and location list stack check helper macros
173#define IS_QF_STACK(qi) (qi == &ql_info)
174#define IS_LL_STACK(qi) (qi != &ql_info)
175
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000176/*
177 * Return location list for window 'wp'
178 * For location list window, return the referenced location list
179 */
180#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200183 * Looking up a buffer can be slow if there are many. Remember the last one
184 * to make this a lot faster if there are multiple matches in the same file.
185 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200186static char_u *qf_last_bufname = NULL;
187static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200188
Bram Moolenaar3c097222017-12-21 20:54:49 +0100189static char *e_loc_list_changed =
190 N_("E926: Current location list was changed");
191
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200192/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200193 * Maximum number of bytes allowed per line while reading a errorfile.
194 */
195#define LINE_MAXLEN 4096
196
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200197static struct fmtpattern
198{
199 char_u convchar;
200 char *pattern;
201} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200202 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200203 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200204 {'n', "\\d\\+"},
205 {'l', "\\d\\+"},
206 {'c', "\\d\\+"},
207 {'t', "."},
208 {'m', ".\\+"},
209 {'r', ".*"},
210 {'p', "[- .]*"},
211 {'v', "\\d\\+"},
212 {'s', ".\\+"},
213 {'o', ".\\+"}
214 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200215
216/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200217 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200218 * See fmt_pat definition above for the list of supported patterns. The
219 * pattern specifier is supplied in "efmpat". The converted pattern is stored
220 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200221 */
222 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200223efmpat_to_regpat(
224 char_u *efmpat,
225 char_u *regpat,
226 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200227 int idx,
228 int round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200229 char_u *errmsg)
230{
231 char_u *srcptr;
232
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200233 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200234 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200235 // Each errorformat pattern can occur only once
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200236 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200237 _("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200238 EMSG(errmsg);
239 return NULL;
240 }
241 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200242 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200243 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200244 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200245 {
246 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200247 _("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200248 EMSG(errmsg);
249 return NULL;
250 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200251 efminfo->addr[idx] = (char_u)++round;
252 *regpat++ = '\\';
253 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200254#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200255 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200256 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200257 // Also match "c:" in the file name, even when
258 // checking for a colon next: "%f:".
259 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200260 STRCPY(regpat, "\\%(\\a:\\)\\=");
261 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200262 }
263#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200264 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200265 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200266 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200267 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200268 // A file name may contain spaces, but this isn't
269 // in "\f". For "%f:%l:%m" there may be a ":" in
270 // the file name. Use ".\{-1,}x" instead (x is
271 // the next character), the requirement that :999:
272 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200273 STRCPY(regpat, ".\\{-1,}");
274 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200275 }
276 else
277 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200278 // File name followed by '\\' or '%': include as
279 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200280 STRCPY(regpat, "\\f\\+");
281 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200282 }
283 }
284 else
285 {
286 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200287 while ((*regpat = *srcptr++) != NUL)
288 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200289 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200290 *regpat++ = '\\';
291 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200292
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200293 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200294}
295
296/*
297 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200298 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200299 */
300 static char_u *
301scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200302 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200303 char_u *efm,
304 int len,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200305 char_u *regpat,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200306 char_u *errmsg)
307{
308 char_u *efmp = *pefmp;
309
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200310 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200311 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200312 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200313 {
314 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200315 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200316 if (efmp < efm + len)
317 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200318 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200319 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200320 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200321 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200322 if (efmp == efm + len)
323 {
324 EMSG(_("E374: Missing ] in format string"));
325 return NULL;
326 }
327 }
328 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200329 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200330 *regpat++ = *++efmp;
331 *regpat++ = '\\';
332 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200333 }
334 else
335 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200336 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200337 sprintf((char *)errmsg,
338 _("E375: Unsupported %%%c in format string"), *efmp);
339 EMSG(errmsg);
340 return NULL;
341 }
342
343 *pefmp = efmp;
344
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200345 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200346}
347
348/*
349 * Analyze/parse an errorformat prefix.
350 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200351 static char_u *
352efm_analyze_prefix(char_u *efmp, efm_T *efminfo, char_u *errmsg)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200353{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200354 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200355 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200356 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200357 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200358 else
359 {
360 sprintf((char *)errmsg,
361 _("E376: Invalid %%%c in format string prefix"), *efmp);
362 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200363 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200364 }
365
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200366 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200367}
368
369/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200370 * Converts a 'errorformat' string part in 'efm' to a regular expression
371 * pattern. The resulting regex pattern is returned in "regpat". Additional
372 * information about the 'erroformat' pattern is returned in "fmt_ptr".
373 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200374 */
375 static int
376efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200377 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200378 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200379 efm_T *fmt_ptr,
380 char_u *regpat,
381 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200382{
383 char_u *ptr;
384 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200385 int round;
386 int idx = 0;
387
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200388 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200389 ptr = regpat;
390 *ptr++ = '^';
391 round = 0;
392 for (efmp = efm; efmp < efm + len; ++efmp)
393 {
394 if (*efmp == '%')
395 {
396 ++efmp;
397 for (idx = 0; idx < FMT_PATTERNS; ++idx)
398 if (fmt_pat[idx].convchar == *efmp)
399 break;
400 if (idx < FMT_PATTERNS)
401 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200402 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200403 errmsg);
404 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200405 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200406 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200407 }
408 else if (*efmp == '*')
409 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200410 ++efmp;
411 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr, errmsg);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200412 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200413 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200414 }
415 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200416 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200417 else if (*efmp == '#')
418 *ptr++ = '*';
419 else if (*efmp == '>')
420 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200421 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200422 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200423 // prefix is allowed only at the beginning of the errorformat
424 // option part
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200425 efmp = efm_analyze_prefix(efmp, fmt_ptr, errmsg);
426 if (efmp == NULL)
427 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200428 }
429 else
430 {
431 sprintf((char *)errmsg,
432 _("E377: Invalid %%%c in format string"), *efmp);
433 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200434 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200435 }
436 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200437 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200438 {
439 if (*efmp == '\\' && efmp + 1 < efm + len)
440 ++efmp;
441 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200442 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200443 if (*efmp)
444 *ptr++ = *efmp;
445 }
446 }
447 *ptr++ = '$';
448 *ptr = NUL;
449
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200450 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200451}
452
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200453/*
454 * Free the 'errorformat' information list
455 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200456 static void
457free_efm_list(efm_T **efm_first)
458{
459 efm_T *efm_ptr;
460
461 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
462 {
463 *efm_first = efm_ptr->next;
464 vim_regfree(efm_ptr->prog);
465 vim_free(efm_ptr);
466 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100467 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200468}
469
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200470/*
471 * Compute the size of the buffer used to convert a 'errorformat' pattern into
472 * a regular expression pattern.
473 */
474 static int
475efm_regpat_bufsz(char_u *efm)
476{
477 int sz;
478 int i;
479
480 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
481 for (i = FMT_PATTERNS; i > 0; )
482 sz += (int)STRLEN(fmt_pat[--i].pattern);
483#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200484 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200485#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200486 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200487#endif
488
489 return sz;
490}
491
492/*
493 * Return the length of a 'errorformat' option part (separated by ",").
494 */
495 static int
496efm_option_part_len(char_u *efm)
497{
498 int len;
499
500 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
501 if (efm[len] == '\\' && efm[len + 1] != NUL)
502 ++len;
503
504 return len;
505}
506
507/*
508 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
509 * are parsed and converted to regular expressions. Returns information about
510 * the parsed 'errorformat' option.
511 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200512 static efm_T *
513parse_efm_option(char_u *efm)
514{
515 char_u *errmsg = NULL;
516 int errmsglen;
517 efm_T *fmt_ptr = NULL;
518 efm_T *fmt_first = NULL;
519 efm_T *fmt_last = NULL;
520 char_u *fmtstr = NULL;
521 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200522 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200523
524 errmsglen = CMDBUFFSIZE + 1;
525 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
526 if (errmsg == NULL)
527 goto parse_efm_end;
528
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200529 // Each part of the format string is copied and modified from errorformat
530 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200531
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200532 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200533 sz = efm_regpat_bufsz(efm);
534 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200535 goto parse_efm_error;
536
537 while (efm[0] != NUL)
538 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200539 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200540 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
541 if (fmt_ptr == NULL)
542 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200543 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200544 fmt_first = fmt_ptr;
545 else
546 fmt_last->next = fmt_ptr;
547 fmt_last = fmt_ptr;
548
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200549 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200550 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200551
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200552 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200553 goto parse_efm_error;
554 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
555 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200556 // Advance to next part
557 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200558 }
559
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200560 if (fmt_first == NULL) // nothing found
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200561 EMSG(_("E378: 'errorformat' contains no pattern"));
562
563 goto parse_efm_end;
564
565parse_efm_error:
566 free_efm_list(&fmt_first);
567
568parse_efm_end:
569 vim_free(fmtstr);
570 vim_free(errmsg);
571
572 return fmt_first;
573}
574
Bram Moolenaare0d37972016-07-15 22:36:01 +0200575enum {
576 QF_FAIL = 0,
577 QF_OK = 1,
578 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200579 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200580 QF_IGNORE_LINE = 4,
581 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200582};
583
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200584/*
585 * State information used to parse lines and add entries to a quickfix/location
586 * list.
587 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200588typedef struct {
589 char_u *linebuf;
590 int linelen;
591 char_u *growbuf;
592 int growbufsiz;
593 FILE *fd;
594 typval_T *tv;
595 char_u *p_str;
596 listitem_T *p_li;
597 buf_T *buf;
598 linenr_T buflnum;
599 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100600 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200601} qfstate_T;
602
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200603/*
604 * Allocate more memory for the line buffer used for parsing lines.
605 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200606 static char_u *
607qf_grow_linebuf(qfstate_T *state, int newsz)
608{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200609 char_u *p;
610
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200611 // If the line exceeds LINE_MAXLEN exclude the last
612 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200613 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
614 if (state->growbuf == NULL)
615 {
616 state->growbuf = alloc(state->linelen + 1);
617 if (state->growbuf == NULL)
618 return NULL;
619 state->growbufsiz = state->linelen;
620 }
621 else if (state->linelen > state->growbufsiz)
622 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200623 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200624 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200625 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200626 state->growbufsiz = state->linelen;
627 }
628 return state->growbuf;
629}
630
631/*
632 * Get the next string (separated by newline) from state->p_str.
633 */
634 static int
635qf_get_next_str_line(qfstate_T *state)
636{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200637 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200638 char_u *p_str = state->p_str;
639 char_u *p;
640 int len;
641
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200642 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200643 return QF_END_OF_INPUT;
644
645 p = vim_strchr(p_str, '\n');
646 if (p != NULL)
647 len = (int)(p - p_str) + 1;
648 else
649 len = (int)STRLEN(p_str);
650
651 if (len > IOSIZE - 2)
652 {
653 state->linebuf = qf_grow_linebuf(state, len);
654 if (state->linebuf == NULL)
655 return QF_NOMEM;
656 }
657 else
658 {
659 state->linebuf = IObuff;
660 state->linelen = len;
661 }
662 vim_strncpy(state->linebuf, p_str, state->linelen);
663
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200664 // Increment using len in order to discard the rest of the
665 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200666 p_str += len;
667 state->p_str = p_str;
668
669 return QF_OK;
670}
671
672/*
673 * Get the next string from state->p_Li.
674 */
675 static int
676qf_get_next_list_line(qfstate_T *state)
677{
678 listitem_T *p_li = state->p_li;
679 int len;
680
681 while (p_li != NULL
682 && (p_li->li_tv.v_type != VAR_STRING
683 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200684 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200685
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200686 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200687 {
688 state->p_li = NULL;
689 return QF_END_OF_INPUT;
690 }
691
692 len = (int)STRLEN(p_li->li_tv.vval.v_string);
693 if (len > IOSIZE - 2)
694 {
695 state->linebuf = qf_grow_linebuf(state, len);
696 if (state->linebuf == NULL)
697 return QF_NOMEM;
698 }
699 else
700 {
701 state->linebuf = IObuff;
702 state->linelen = len;
703 }
704
705 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
706
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200707 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200708 return QF_OK;
709}
710
711/*
712 * Get the next string from state->buf.
713 */
714 static int
715qf_get_next_buf_line(qfstate_T *state)
716{
717 char_u *p_buf = NULL;
718 int len;
719
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200720 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200721 if (state->buflnum > state->lnumlast)
722 return QF_END_OF_INPUT;
723
724 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
725 state->buflnum += 1;
726
727 len = (int)STRLEN(p_buf);
728 if (len > IOSIZE - 2)
729 {
730 state->linebuf = qf_grow_linebuf(state, len);
731 if (state->linebuf == NULL)
732 return QF_NOMEM;
733 }
734 else
735 {
736 state->linebuf = IObuff;
737 state->linelen = len;
738 }
739 vim_strncpy(state->linebuf, p_buf, state->linelen);
740
741 return QF_OK;
742}
743
744/*
745 * Get the next string from file state->fd.
746 */
747 static int
748qf_get_next_file_line(qfstate_T *state)
749{
750 int discard;
751 int growbuflen;
752
753 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
754 return QF_END_OF_INPUT;
755
756 discard = FALSE;
757 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200758 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200759 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200760 // The current line exceeds IObuff, continue reading using
761 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200762 if (state->growbuf == NULL)
763 {
764 state->growbufsiz = 2 * (IOSIZE - 1);
765 state->growbuf = alloc(state->growbufsiz);
766 if (state->growbuf == NULL)
767 return QF_NOMEM;
768 }
769
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200770 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200771 memcpy(state->growbuf, IObuff, IOSIZE - 1);
772 growbuflen = state->linelen;
773
774 for (;;)
775 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200776 char_u *p;
777
Bram Moolenaare0d37972016-07-15 22:36:01 +0200778 if (fgets((char *)state->growbuf + growbuflen,
779 state->growbufsiz - growbuflen, state->fd) == NULL)
780 break;
781 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
782 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200783 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200784 break;
785 if (state->growbufsiz == LINE_MAXLEN)
786 {
787 discard = TRUE;
788 break;
789 }
790
791 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
792 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200793 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200794 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200795 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200796 }
797
798 while (discard)
799 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200800 // The current line is longer than LINE_MAXLEN, continue
801 // reading but discard everything until EOL or EOF is
802 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200803 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
804 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200805 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200806 break;
807 }
808
809 state->linebuf = state->growbuf;
810 state->linelen = growbuflen;
811 }
812 else
813 state->linebuf = IObuff;
814
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100815#ifdef FEAT_MBYTE
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200816 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200817 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
818 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100819 char_u *line;
820
821 line = string_convert(&state->vc, state->linebuf, &state->linelen);
822 if (line != NULL)
823 {
824 if (state->linelen < IOSIZE)
825 {
826 STRCPY(state->linebuf, line);
827 vim_free(line);
828 }
829 else
830 {
831 vim_free(state->growbuf);
832 state->linebuf = state->growbuf = line;
833 state->growbufsiz = state->linelen < LINE_MAXLEN
834 ? state->linelen : LINE_MAXLEN;
835 }
836 }
837 }
838#endif
839
Bram Moolenaare0d37972016-07-15 22:36:01 +0200840 return QF_OK;
841}
842
843/*
844 * Get the next string from a file/buffer/list/string.
845 */
846 static int
847qf_get_nextline(qfstate_T *state)
848{
849 int status = QF_FAIL;
850
851 if (state->fd == NULL)
852 {
853 if (state->tv != NULL)
854 {
855 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200856 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200857 status = qf_get_next_str_line(state);
858 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200859 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200860 status = qf_get_next_list_line(state);
861 }
862 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200863 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200864 status = qf_get_next_buf_line(state);
865 }
866 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200867 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200868 status = qf_get_next_file_line(state);
869
870 if (status != QF_OK)
871 return status;
872
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200873 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200874 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200875 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200876 state->linebuf[state->linelen - 1] = NUL;
877#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200878 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
879 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200880#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200881 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200882
883#ifdef FEAT_MBYTE
884 remove_bom(state->linebuf);
885#endif
886
887 return QF_OK;
888}
889
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200890typedef struct {
891 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200892 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200893 char_u *errmsg;
894 int errmsglen;
895 long lnum;
896 int col;
897 char_u use_viscol;
898 char_u *pattern;
899 int enr;
900 int type;
901 int valid;
902} qffields_T;
903
904/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200905 * Parse the match for filename ('%f') pattern in regmatch.
906 * Return the matched value in "fields->namebuf".
907 */
908 static int
909qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
910{
911 int c;
912
913 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
914 return QF_FAIL;
915
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200916 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200917 c = *rmp->endp[midx];
918 *rmp->endp[midx] = NUL;
919 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
920 *rmp->endp[midx] = c;
921
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200922 // For separate filename patterns (%O, %P and %Q), the specified file
923 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200924 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
925 && mch_getperm(fields->namebuf) == -1)
926 return QF_FAIL;
927
928 return QF_OK;
929}
930
931/*
932 * Parse the match for error number ('%n') pattern in regmatch.
933 * Return the matched value in "fields->enr".
934 */
935 static int
936qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
937{
938 if (rmp->startp[midx] == NULL)
939 return QF_FAIL;
940 fields->enr = (int)atol((char *)rmp->startp[midx]);
941 return QF_OK;
942}
943
944/*
945 * Parse the match for line number (%l') pattern in regmatch.
946 * Return the matched value in "fields->lnum".
947 */
948 static int
949qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
950{
951 if (rmp->startp[midx] == NULL)
952 return QF_FAIL;
953 fields->lnum = atol((char *)rmp->startp[midx]);
954 return QF_OK;
955}
956
957/*
958 * Parse the match for column number ('%c') pattern in regmatch.
959 * Return the matched value in "fields->col".
960 */
961 static int
962qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
963{
964 if (rmp->startp[midx] == NULL)
965 return QF_FAIL;
966 fields->col = (int)atol((char *)rmp->startp[midx]);
967 return QF_OK;
968}
969
970/*
971 * Parse the match for error type ('%t') pattern in regmatch.
972 * Return the matched value in "fields->type".
973 */
974 static int
975qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
976{
977 if (rmp->startp[midx] == NULL)
978 return QF_FAIL;
979 fields->type = *rmp->startp[midx];
980 return QF_OK;
981}
982
983/*
984 * Parse the match for '%+' format pattern. The whole matching line is included
985 * in the error string. Return the matched line in "fields->errmsg".
986 */
987 static int
988qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
989{
990 char_u *p;
991
992 if (linelen >= fields->errmsglen)
993 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200994 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200995 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
996 return QF_NOMEM;
997 fields->errmsg = p;
998 fields->errmsglen = linelen + 1;
999 }
1000 vim_strncpy(fields->errmsg, linebuf, linelen);
1001 return QF_OK;
1002}
1003
1004/*
1005 * Parse the match for error message ('%m') pattern in regmatch.
1006 * Return the matched value in "fields->errmsg".
1007 */
1008 static int
1009qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1010{
1011 char_u *p;
1012 int len;
1013
1014 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1015 return QF_FAIL;
1016 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1017 if (len >= fields->errmsglen)
1018 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001019 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001020 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1021 return QF_NOMEM;
1022 fields->errmsg = p;
1023 fields->errmsglen = len + 1;
1024 }
1025 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1026 return QF_OK;
1027}
1028
1029/*
1030 * Parse the match for rest of a single-line file message ('%r') pattern.
1031 * Return the matched value in "tail".
1032 */
1033 static int
1034qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1035{
1036 if (rmp->startp[midx] == NULL)
1037 return QF_FAIL;
1038 *tail = rmp->startp[midx];
1039 return QF_OK;
1040}
1041
1042/*
1043 * Parse the match for the pointer line ('%p') pattern in regmatch.
1044 * Return the matched value in "fields->col".
1045 */
1046 static int
1047qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1048{
1049 char_u *match_ptr;
1050
1051 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1052 return QF_FAIL;
1053 fields->col = 0;
1054 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1055 ++match_ptr)
1056 {
1057 ++fields->col;
1058 if (*match_ptr == TAB)
1059 {
1060 fields->col += 7;
1061 fields->col -= fields->col % 8;
1062 }
1063 }
1064 ++fields->col;
1065 fields->use_viscol = TRUE;
1066 return QF_OK;
1067}
1068
1069/*
1070 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1071 * Return the matched value in "fields->col".
1072 */
1073 static int
1074qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1075{
1076 if (rmp->startp[midx] == NULL)
1077 return QF_FAIL;
1078 fields->col = (int)atol((char *)rmp->startp[midx]);
1079 fields->use_viscol = TRUE;
1080 return QF_OK;
1081}
1082
1083/*
1084 * Parse the match for the search text ('%s') pattern in regmatch.
1085 * Return the matched value in "fields->pattern".
1086 */
1087 static int
1088qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1089{
1090 int len;
1091
1092 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1093 return QF_FAIL;
1094 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1095 if (len > CMDBUFFSIZE - 5)
1096 len = CMDBUFFSIZE - 5;
1097 STRCPY(fields->pattern, "^\\V");
1098 STRNCAT(fields->pattern, rmp->startp[midx], len);
1099 fields->pattern[len + 3] = '\\';
1100 fields->pattern[len + 4] = '$';
1101 fields->pattern[len + 5] = NUL;
1102 return QF_OK;
1103}
1104
1105/*
1106 * Parse the match for the module ('%o') pattern in regmatch.
1107 * Return the matched value in "fields->module".
1108 */
1109 static int
1110qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1111{
1112 int len;
1113
1114 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1115 return QF_FAIL;
1116 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1117 if (len > CMDBUFFSIZE)
1118 len = CMDBUFFSIZE;
1119 STRNCAT(fields->module, rmp->startp[midx], len);
1120 return QF_OK;
1121}
1122
1123/*
1124 * 'errorformat' format pattern parser functions.
1125 * The '%f' and '%r' formats are parsed differently from other formats.
1126 * See qf_parse_match() for details.
1127 */
1128static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1129{
1130 NULL,
1131 qf_parse_fmt_n,
1132 qf_parse_fmt_l,
1133 qf_parse_fmt_c,
1134 qf_parse_fmt_t,
1135 qf_parse_fmt_m,
1136 NULL,
1137 qf_parse_fmt_p,
1138 qf_parse_fmt_v,
1139 qf_parse_fmt_s,
1140 qf_parse_fmt_o
1141};
1142
1143/*
1144 * Parse the error format pattern matches in "regmatch" and set the values in
1145 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1146 * match. Returns QF_OK if all the matches are successfully parsed. On
1147 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001148 */
1149 static int
1150qf_parse_match(
1151 char_u *linebuf,
1152 int linelen,
1153 efm_T *fmt_ptr,
1154 regmatch_T *regmatch,
1155 qffields_T *fields,
1156 int qf_multiline,
1157 int qf_multiscan,
1158 char_u **tail)
1159{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001160 int idx = fmt_ptr->prefix;
1161 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001162 int midx;
1163 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001164
1165 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1166 return QF_FAIL;
1167 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1168 fields->type = idx;
1169 else
1170 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001171
1172 // Extract error message data from matched line.
1173 // We check for an actual submatch, because "\[" and "\]" in
1174 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001175 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001176 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001177 status = QF_OK;
1178 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001179 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001180 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1181 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001182 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001183 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001184 status = qf_parse_fmt_plus(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001185 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001186 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001187 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001188 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001189 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001190 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001191 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001192
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001193 if (status != QF_OK)
1194 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001195 }
1196
1197 return QF_OK;
1198}
1199
1200/*
1201 * Parse an error line in 'linebuf' using a single error format string in
1202 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1203 * Returns QF_OK if the efm format matches completely and the fields are
1204 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1205 */
1206 static int
1207qf_parse_get_fields(
1208 char_u *linebuf,
1209 int linelen,
1210 efm_T *fmt_ptr,
1211 qffields_T *fields,
1212 int qf_multiline,
1213 int qf_multiscan,
1214 char_u **tail)
1215{
1216 regmatch_T regmatch;
1217 int status = QF_FAIL;
1218 int r;
1219
1220 if (qf_multiscan &&
1221 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1222 return QF_FAIL;
1223
1224 fields->namebuf[0] = NUL;
1225 fields->module[0] = NUL;
1226 fields->pattern[0] = NUL;
1227 if (!qf_multiscan)
1228 fields->errmsg[0] = NUL;
1229 fields->lnum = 0;
1230 fields->col = 0;
1231 fields->use_viscol = FALSE;
1232 fields->enr = -1;
1233 fields->type = 0;
1234 *tail = NULL;
1235
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001236 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001237 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001238 regmatch.regprog = fmt_ptr->prog;
1239 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1240 fmt_ptr->prog = regmatch.regprog;
1241 if (r)
1242 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1243 fields, qf_multiline, qf_multiscan, tail);
1244
1245 return status;
1246}
1247
1248/*
1249 * Parse directory error format prefixes (%D and %X).
1250 * Push and pop directories from the directory stack when scanning directory
1251 * names.
1252 */
1253 static int
1254qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1255{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001256 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001257 {
1258 if (*fields->namebuf == NUL)
1259 {
1260 EMSG(_("E379: Missing or empty directory name"));
1261 return QF_FAIL;
1262 }
1263 qfl->qf_directory =
1264 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1265 if (qfl->qf_directory == NULL)
1266 return QF_FAIL;
1267 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001268 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001269 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1270
1271 return QF_OK;
1272}
1273
1274/*
1275 * Parse global file name error format prefixes (%O, %P and %Q).
1276 */
1277 static int
1278qf_parse_file_pfx(
1279 int idx,
1280 qffields_T *fields,
1281 qf_list_T *qfl,
1282 char_u *tail)
1283{
1284 fields->valid = FALSE;
1285 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1286 {
1287 if (*fields->namebuf && idx == 'P')
1288 qfl->qf_currfile =
1289 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1290 else if (idx == 'Q')
1291 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1292 *fields->namebuf = NUL;
1293 if (tail && *tail)
1294 {
1295 STRMOVE(IObuff, skipwhite(tail));
1296 qfl->qf_multiscan = TRUE;
1297 return QF_MULTISCAN;
1298 }
1299 }
1300
1301 return QF_OK;
1302}
1303
1304/*
1305 * Parse a non-error line (a line which doesn't match any of the error
1306 * format in 'efm').
1307 */
1308 static int
1309qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1310{
1311 char_u *p;
1312
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001313 fields->namebuf[0] = NUL; // no match found, remove file name
1314 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001315 fields->valid = FALSE;
1316 if (linelen >= fields->errmsglen)
1317 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001318 // linelen + null terminator
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001319 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1320 return QF_NOMEM;
1321 fields->errmsg = p;
1322 fields->errmsglen = linelen + 1;
1323 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001324 // copy whole line to error message
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001325 vim_strncpy(fields->errmsg, linebuf, linelen);
1326
1327 return QF_OK;
1328}
1329
1330/*
1331 * Parse multi-line error format prefixes (%C and %Z)
1332 */
1333 static int
1334qf_parse_multiline_pfx(
1335 qf_info_T *qi,
1336 int qf_idx,
1337 int idx,
1338 qf_list_T *qfl,
1339 qffields_T *fields)
1340{
1341 char_u *ptr;
1342 int len;
1343
1344 if (!qfl->qf_multiignore)
1345 {
1346 qfline_T *qfprev = qfl->qf_last;
1347
1348 if (qfprev == NULL)
1349 return QF_FAIL;
1350 if (*fields->errmsg && !qfl->qf_multiignore)
1351 {
1352 len = (int)STRLEN(qfprev->qf_text);
1353 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1354 == NULL)
1355 return QF_FAIL;
1356 STRCPY(ptr, qfprev->qf_text);
1357 vim_free(qfprev->qf_text);
1358 qfprev->qf_text = ptr;
1359 *(ptr += len) = '\n';
1360 STRCPY(++ptr, fields->errmsg);
1361 }
1362 if (qfprev->qf_nr == -1)
1363 qfprev->qf_nr = fields->enr;
1364 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001365 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001366 qfprev->qf_type = fields->type;
1367
1368 if (!qfprev->qf_lnum)
1369 qfprev->qf_lnum = fields->lnum;
1370 if (!qfprev->qf_col)
1371 qfprev->qf_col = fields->col;
1372 qfprev->qf_viscol = fields->use_viscol;
1373 if (!qfprev->qf_fnum)
1374 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1375 qfl->qf_directory,
1376 *fields->namebuf || qfl->qf_directory != NULL
1377 ? fields->namebuf
1378 : qfl->qf_currfile != NULL && fields->valid
1379 ? qfl->qf_currfile : 0);
1380 }
1381 if (idx == 'Z')
1382 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1383 line_breakcheck();
1384
1385 return QF_IGNORE_LINE;
1386}
1387
1388/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001389 * Parse a line and get the quickfix fields.
1390 * Return the QF_ status.
1391 */
1392 static int
1393qf_parse_line(
1394 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001395 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001396 char_u *linebuf,
1397 int linelen,
1398 efm_T *fmt_first,
1399 qffields_T *fields)
1400{
1401 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001402 int idx = 0;
1403 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001404 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001405 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001406
Bram Moolenaare333e792018-04-08 13:27:39 +02001407restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001408 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001409 if (fmt_start == NULL)
1410 fmt_ptr = fmt_first;
1411 else
1412 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001413 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001414 fmt_ptr = fmt_start;
1415 fmt_start = NULL;
1416 }
1417
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001418 // Try to match each part of 'errorformat' until we find a complete
1419 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001420 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001421 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1422 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001423 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001424 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1425 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1426 if (status == QF_NOMEM)
1427 return status;
1428 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001429 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001430 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001431 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001432
1433 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1434 {
1435 if (fmt_ptr != NULL)
1436 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001437 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001438 status = qf_parse_dir_pfx(idx, fields, qfl);
1439 if (status != QF_OK)
1440 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001441 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001442
1443 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1444 if (status != QF_OK)
1445 return status;
1446
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001447 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001448 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001449 }
1450 else if (fmt_ptr != NULL)
1451 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001452 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001453 if (fmt_ptr->conthere)
1454 fmt_start = fmt_ptr;
1455
1456 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1457 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001458 qfl->qf_multiline = TRUE; // start of a multi-line message
1459 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001460 }
1461 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001462 { // continuation of multi-line msg
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001463 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1464 if (status != QF_OK)
1465 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001466 }
1467 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001468 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001469 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1470 if (status == QF_MULTISCAN)
1471 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001472 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001473 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001474 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001475 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001476 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001477 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001478 return QF_IGNORE_LINE;
1479 }
1480 }
1481
1482 return QF_OK;
1483}
1484
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001485/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001486 * Returns TRUE if the specified quickfix/location stack is empty
1487 */
1488 static int
1489qf_stack_empty(qf_info_T *qi)
1490{
1491 return qi == NULL || qi->qf_listcount <= 0;
1492}
1493
1494/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001495 * Returns TRUE if the specified quickfix/location list is empty.
1496 */
1497 static int
1498qf_list_empty(qf_info_T *qi, int qf_idx)
1499{
1500 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1501 return TRUE;
1502 return qi->qf_lists[qf_idx].qf_count <= 0;
1503}
1504
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001505/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001506 * Allocate the fields used for parsing lines and populating a quickfix list.
1507 */
1508 static int
1509qf_alloc_fields(qffields_T *pfields)
1510{
1511 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1512 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1513 pfields->errmsglen = CMDBUFFSIZE + 1;
1514 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1515 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1516 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1517 || pfields->pattern == NULL || pfields->module == NULL)
1518 return FAIL;
1519
1520 return OK;
1521}
1522
1523/*
1524 * Free the fields used for parsing lines and populating a quickfix list.
1525 */
1526 static void
1527qf_free_fields(qffields_T *pfields)
1528{
1529 vim_free(pfields->namebuf);
1530 vim_free(pfields->module);
1531 vim_free(pfields->errmsg);
1532 vim_free(pfields->pattern);
1533}
1534
1535/*
1536 * Setup the state information used for parsing lines and populating a
1537 * quickfix list.
1538 */
1539 static int
1540qf_setup_state(
1541 qfstate_T *pstate,
1542 char_u *enc,
1543 char_u *efile,
1544 typval_T *tv,
1545 buf_T *buf,
1546 linenr_T lnumfirst,
1547 linenr_T lnumlast)
1548{
1549#ifdef FEAT_MBYTE
1550 pstate->vc.vc_type = CONV_NONE;
1551 if (enc != NULL && *enc != NUL)
1552 convert_setup(&pstate->vc, enc, p_enc);
1553#endif
1554
1555 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1556 {
1557 EMSG2(_(e_openerrf), efile);
1558 return FAIL;
1559 }
1560
1561 if (tv != NULL)
1562 {
1563 if (tv->v_type == VAR_STRING)
1564 pstate->p_str = tv->vval.v_string;
1565 else if (tv->v_type == VAR_LIST)
1566 pstate->p_li = tv->vval.v_list->lv_first;
1567 pstate->tv = tv;
1568 }
1569 pstate->buf = buf;
1570 pstate->buflnum = lnumfirst;
1571 pstate->lnumlast = lnumlast;
1572
1573 return OK;
1574}
1575
1576/*
1577 * Cleanup the state information used for parsing lines and populating a
1578 * quickfix list.
1579 */
1580 static void
1581qf_cleanup_state(qfstate_T *pstate)
1582{
1583 if (pstate->fd != NULL)
1584 fclose(pstate->fd);
1585
1586 vim_free(pstate->growbuf);
1587#ifdef FEAT_MBYTE
1588 if (pstate->vc.vc_type != CONV_NONE)
1589 convert_setup(&pstate->vc, NULL, NULL);
1590#endif
1591}
1592
1593/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001594 * Read the errorfile "efile" into memory, line by line, building the error
1595 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001596 * Alternative: when "efile" is NULL read errors from buffer "buf".
1597 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001598 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001599 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1600 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001601 * Return -1 for error, number of errors for success.
1602 */
1603 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001604qf_init_ext(
1605 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001606 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001607 char_u *efile,
1608 buf_T *buf,
1609 typval_T *tv,
1610 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001611 int newlist, // TRUE: start a new error list
1612 linenr_T lnumfirst, // first line number to use
1613 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001614 char_u *qf_title,
1615 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001616{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001617 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001618 qfstate_T state;
1619 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001620 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001621 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001622 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001624 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001625 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001626 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001628 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001629 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001630
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001631 vim_memset(&state, 0, sizeof(state));
1632 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001633 if ((qf_alloc_fields(&fields) == FAIL) ||
1634 (qf_setup_state(&state, enc, efile, tv, buf,
1635 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 goto qf_init_end;
1637
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001638 if (newlist || qf_idx == qi->qf_listcount)
1639 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001640 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001641 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001642 qf_idx = qi->qf_curlist;
1643 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001644 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001645 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001646 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001647 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001648 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001649 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001652 qfl = &qi->qf_lists[qf_idx];
1653
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001654 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001655 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001656 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 else
1658 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001660 // If the errorformat didn't change between calls, then reuse the
1661 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001662 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1663 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001664 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001665 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001666 free_efm_list(&fmt_first);
1667
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001668 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001669 fmt_first = parse_efm_option(efm);
1670 if (fmt_first != NULL)
1671 last_efm = vim_strsave(efm);
1672 }
1673
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001674 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001677 // got_int is reset here, because it was probably set when killing the
1678 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 got_int = FALSE;
1680
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001681 // Read the lines in the error file one by one.
1682 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001683 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001685 // Get the next line from a file/buffer/list/string
Bram Moolenaare0d37972016-07-15 22:36:01 +02001686 status = qf_get_nextline(&state);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001687 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001688 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001689 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001690 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001692 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1693 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001694 if (status == QF_FAIL)
1695 goto error2;
1696 if (status == QF_NOMEM)
1697 goto qf_init_end;
1698 if (status == QF_IGNORE_LINE)
1699 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001701 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001702 qf_idx,
1703 qfl->qf_directory,
1704 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001705 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001706 : ((qfl->qf_currfile != NULL && fields.valid)
1707 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001708 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001709 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001710 fields.errmsg,
1711 fields.lnum,
1712 fields.col,
1713 fields.use_viscol,
1714 fields.pattern,
1715 fields.enr,
1716 fields.type,
1717 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 goto error2;
1719 line_breakcheck();
1720 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001721 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001723 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001725 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001726 qfl->qf_ptr = qfl->qf_start;
1727 qfl->qf_index = 1;
1728 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 }
1730 else
1731 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001732 qfl->qf_nonevalid = FALSE;
1733 if (qfl->qf_ptr == NULL)
1734 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001736 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001737 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001738 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
1740 EMSG(_(e_readerrf));
1741error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001742 if (!adding)
1743 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001744 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001745 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001746 qi->qf_listcount--;
1747 if (qi->qf_curlist > 0)
1748 --qi->qf_curlist;
1749 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001750qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001751 if (qf_idx == qi->qf_curlist)
1752 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001753 qf_cleanup_state(&state);
1754 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
1756 return retval;
1757}
1758
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001759/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001760 * Read the errorfile "efile" into memory, line by line, building the error
1761 * list. Set the error list's title to qf_title.
1762 * Return -1 for error, number of errors for success.
1763 */
1764 int
1765qf_init(win_T *wp,
1766 char_u *efile,
1767 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001768 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001769 char_u *qf_title,
1770 char_u *enc)
1771{
1772 qf_info_T *qi = &ql_info;
1773
1774 if (wp != NULL)
1775 {
1776 qi = ll_get_or_alloc_list(wp);
1777 if (qi == NULL)
1778 return FAIL;
1779 }
1780
1781 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1782 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1783}
1784
1785/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001786 * Set the title of the specified quickfix list. Frees the previous title.
1787 * Prepends ':' to the title.
1788 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001789 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001790qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001791{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001792 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001793
Bram Moolenaarfb604092014-07-23 15:55:00 +02001794 if (title != NULL)
1795 {
1796 char_u *p = alloc((int)STRLEN(title) + 2);
1797
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001798 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001799 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001800 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001801 }
1802}
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001805 * The title of a quickfix/location list is set, by default, to the command
1806 * that created the quickfix list with the ":" prefix.
1807 * Create a quickfix list title string by prepending ":" to a user command.
1808 * Returns a pointer to a static buffer with the title.
1809 */
1810 static char_u *
1811qf_cmdtitle(char_u *cmd)
1812{
1813 static char_u qftitle_str[IOSIZE];
1814
1815 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1816 return qftitle_str;
1817}
1818
1819/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001820 * Prepare for adding a new quickfix list. If the current list is in the
1821 * middle of the stack, then all the following lists are freed and then
1822 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 */
1824 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001825qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826{
1827 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001828 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001830 // If the current entry is not the last entry, delete entries beyond
1831 // the current entry. This makes it possible to browse in a tree-like
1832 // way with ":grep'.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001833 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001834 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001836 // When the stack is full, remove to oldest entry
1837 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001838 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001840 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001842 qi->qf_lists[i - 1] = qi->qf_lists[i];
1843 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 }
1845 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001846 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001847 qfl = &qi->qf_lists[qi->qf_curlist];
1848 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1849 qf_store_title(qfl, qf_title);
1850 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851}
1852
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001853/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001854 * Queue location list stack delete request.
1855 */
1856 static void
1857locstack_queue_delreq(qf_info_T *qi)
1858{
1859 qf_delq_T *q;
1860
1861 q = (qf_delq_T *)alloc((unsigned)sizeof(qf_delq_T));
1862 if (q != NULL)
1863 {
1864 q->qi = qi;
1865 q->next = qf_delq_head;
1866 qf_delq_head = q;
1867 }
1868}
1869
1870/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001871 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001872 */
1873 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001874ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001875{
1876 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001877 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001878
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001879 qi = *pqi;
1880 if (qi == NULL)
1881 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001882 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001883
1884 qi->qf_refcount--;
1885 if (qi->qf_refcount < 1)
1886 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001887 // No references to this location list.
1888 // If the location list is still in use, then queue the delete request
1889 // to be processed later.
1890 if (quickfix_busy > 0)
1891 locstack_queue_delreq(qi);
1892 else
1893 {
1894 for (i = 0; i < qi->qf_listcount; ++i)
1895 qf_free(&qi->qf_lists[i]);
1896 vim_free(qi);
1897 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001898 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001899}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001900
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001901/*
1902 * Free all the quickfix/location lists in the stack.
1903 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001904 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001905qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001906{
1907 int i;
1908 qf_info_T *qi = &ql_info;
1909
1910 if (wp != NULL)
1911 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001912 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001913 ll_free_all(&wp->w_llist);
1914 ll_free_all(&wp->w_llist_ref);
1915 }
1916 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001917 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001918 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001919 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001920}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001923 * Delay freeing of location list stacks when the quickfix code is running.
1924 * Used to avoid problems with autocmds freeing location list stacks when the
1925 * quickfix code is still referencing the stack.
1926 * Must always call decr_quickfix_busy() exactly once after this.
1927 */
1928 static void
1929incr_quickfix_busy(void)
1930{
1931 quickfix_busy++;
1932}
1933
1934/*
1935 * Safe to free location list stacks. Process any delayed delete requests.
1936 */
1937 static void
1938decr_quickfix_busy(void)
1939{
1940 if (--quickfix_busy == 0)
1941 {
1942 // No longer referencing the location lists. Process all the pending
1943 // delete requests.
1944 while (qf_delq_head != NULL)
1945 {
1946 qf_delq_T *q = qf_delq_head;
1947
1948 qf_delq_head = q->next;
1949 ll_free_all(&q->qi);
1950 vim_free(q);
1951 }
1952 }
1953#ifdef ABORT_ON_INTERNAL_ERROR
1954 if (quickfix_busy < 0)
1955 {
1956 EMSG("quickfix_busy has become negative");
1957 abort();
1958 }
1959#endif
1960}
1961
1962#if defined(EXITFREE) || defined(PROTO)
1963 void
1964check_quickfix_busy(void)
1965{
1966 if (quickfix_busy != 0)
1967 {
1968 EMSGN("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
1969# ifdef ABORT_ON_INTERNAL_ERROR
1970 abort();
1971# endif
1972 }
1973}
1974#endif
1975
1976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 * Add an entry to the end of the list of errors.
1978 * Returns OK or FAIL.
1979 */
1980 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001981qf_add_entry(
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001982 qf_info_T *qi, // quickfix list
1983 int qf_idx, // list index
1984 char_u *dir, // optional directory name
1985 char_u *fname, // file name or NULL
1986 char_u *module, // module name or NULL
1987 int bufnum, // buffer number or zero
1988 char_u *mesg, // message
1989 long lnum, // line number
1990 int col, // column
1991 int vis_col, // using visual column
1992 char_u *pattern, // search pattern
1993 int nr, // error number
1994 int type, // type character
1995 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001997 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001998 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001999 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002001 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002003 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002004 {
2005 buf_T *buf = buflist_findnr(bufnum);
2006
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002007 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002008 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002009 buf->b_has_qf_entry |=
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002010 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002011 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002012 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002013 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2015 {
2016 vim_free(qfp);
2017 return FAIL;
2018 }
2019 qfp->qf_lnum = lnum;
2020 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002021 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002022 if (pattern == NULL || *pattern == NUL)
2023 qfp->qf_pattern = NULL;
2024 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2025 {
2026 vim_free(qfp->qf_text);
2027 vim_free(qfp);
2028 return FAIL;
2029 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002030 if (module == NULL || *module == NUL)
2031 qfp->qf_module = NULL;
2032 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2033 {
2034 vim_free(qfp->qf_text);
2035 vim_free(qfp->qf_pattern);
2036 vim_free(qfp);
2037 return FAIL;
2038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002040 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 type = 0;
2042 qfp->qf_type = type;
2043 qfp->qf_valid = valid;
2044
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002045 lastp = &qfl->qf_last;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002046 if (qf_list_empty(qi, qf_idx)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002048 qfl->qf_start = qfp;
2049 qfl->qf_ptr = qfp;
2050 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002051 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
2053 else
2054 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002055 qfp->qf_prev = *lastp;
2056 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002058 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002060 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002061 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002062 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002064 qfl->qf_index = qfl->qf_count;
2065 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
2067
2068 return OK;
2069}
2070
2071/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002072 * Allocate a new location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002073 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002074 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002075ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002076{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002077 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002078
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002079 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002080 if (qi != NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002081 qi->qf_refcount++;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002082 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002083}
2084
2085/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002086 * Return the location list stack for window 'wp'.
2087 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002088 */
2089 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002090ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002091{
2092 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002093 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002094 return wp->w_llist_ref;
2095
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002096 // For a non-location list window, w_llist_ref should not point to a
2097 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002098 ll_free_all(&wp->w_llist_ref);
2099
2100 if (wp->w_llist == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002101 wp->w_llist = ll_new_list(); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002102 return wp->w_llist;
2103}
2104
2105/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002106 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2107 */
2108 static int
2109copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2110{
2111 int i;
2112 qfline_T *from_qfp;
2113 qfline_T *prevp;
2114
2115 // copy all the location entries in this list
2116 for (i = 0, from_qfp = from_qfl->qf_start;
2117 i < from_qfl->qf_count && from_qfp != NULL;
2118 ++i, from_qfp = from_qfp->qf_next)
2119 {
2120 if (qf_add_entry(to_qi,
2121 to_qi->qf_curlist,
2122 NULL,
2123 NULL,
2124 from_qfp->qf_module,
2125 0,
2126 from_qfp->qf_text,
2127 from_qfp->qf_lnum,
2128 from_qfp->qf_col,
2129 from_qfp->qf_viscol,
2130 from_qfp->qf_pattern,
2131 from_qfp->qf_nr,
2132 0,
2133 from_qfp->qf_valid) == FAIL)
2134 return FAIL;
2135
2136 // qf_add_entry() will not set the qf_num field, as the
2137 // directory and file names are not supplied. So the qf_fnum
2138 // field is copied here.
2139 prevp = to_qfl->qf_last;
2140 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2141 prevp->qf_type = from_qfp->qf_type; // error type
2142 if (from_qfl->qf_ptr == from_qfp)
2143 to_qfl->qf_ptr = prevp; // current location
2144 }
2145
2146 return OK;
2147}
2148
2149/*
2150 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2151 */
2152 static int
2153copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2154{
2155 // Some of the fields are populated by qf_add_entry()
2156 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2157 to_qfl->qf_count = 0;
2158 to_qfl->qf_index = 0;
2159 to_qfl->qf_start = NULL;
2160 to_qfl->qf_last = NULL;
2161 to_qfl->qf_ptr = NULL;
2162 if (from_qfl->qf_title != NULL)
2163 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2164 else
2165 to_qfl->qf_title = NULL;
2166 if (from_qfl->qf_ctx != NULL)
2167 {
2168 to_qfl->qf_ctx = alloc_tv();
2169 if (to_qfl->qf_ctx != NULL)
2170 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2171 }
2172 else
2173 to_qfl->qf_ctx = NULL;
2174
2175 if (from_qfl->qf_count)
2176 if (copy_loclist_entries(from_qfl, to_qfl, to_qi) == FAIL)
2177 return FAIL;
2178
2179 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2180
2181 // Assign a new ID for the location list
2182 to_qfl->qf_id = ++last_qf_id;
2183 to_qfl->qf_changedtick = 0L;
2184
2185 // When no valid entries are present in the list, qf_ptr points to
2186 // the first item in the list
2187 if (to_qfl->qf_nonevalid)
2188 {
2189 to_qfl->qf_ptr = to_qfl->qf_start;
2190 to_qfl->qf_index = 1;
2191 }
2192
2193 return OK;
2194}
2195
2196/*
2197 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002198 */
2199 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002200copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002201{
2202 qf_info_T *qi;
2203 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002204
Bram Moolenaar09037502018-09-25 22:08:14 +02002205 // When copying from a location list window, copy the referenced
2206 // location list. For other windows, copy the location list for
2207 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002208 if (IS_LL_WINDOW(from))
2209 qi = from->w_llist_ref;
2210 else
2211 qi = from->w_llist;
2212
Bram Moolenaar09037502018-09-25 22:08:14 +02002213 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002214 return;
2215
Bram Moolenaar09037502018-09-25 22:08:14 +02002216 // allocate a new location list
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002217 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002218 return;
2219
2220 to->w_llist->qf_listcount = qi->qf_listcount;
2221
Bram Moolenaar09037502018-09-25 22:08:14 +02002222 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002223 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002224 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002225 to->w_llist->qf_curlist = idx;
2226
Bram Moolenaar09037502018-09-25 22:08:14 +02002227 if (copy_loclist(&qi->qf_lists[idx],
2228 &to->w_llist->qf_lists[idx], to->w_llist) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002229 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002230 qf_free_all(to);
2231 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002232 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002233 }
2234
Bram Moolenaar09037502018-09-25 22:08:14 +02002235 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002236}
2237
2238/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002239 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002240 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 */
2242 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002243qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002245 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar82404332016-07-10 17:00:38 +02002246 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002247 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002248 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002249
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002250 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
Bram Moolenaare60acc12011-05-10 16:41:25 +02002253#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002254 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002255#endif
2256#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002257 if (directory != NULL)
2258 slash_adjust(directory);
2259 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002260#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002261 if (directory != NULL && !vim_isAbsName(fname)
2262 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2263 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002264 // Here we check if the file really exists.
2265 // This should normally be true, but if make works without
2266 // "leaving directory"-messages we might have missed a
2267 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002268 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002271 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002272 if (directory)
2273 ptr = concat_fnames(directory, fname, TRUE);
2274 else
2275 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002277 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002278 bufname = ptr;
2279 }
2280 else
2281 bufname = fname;
2282
2283 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002284 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002285 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002286 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002287 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002289 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002290 {
2291 vim_free(qf_last_bufname);
2292 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2293 if (bufname == ptr)
2294 qf_last_bufname = bufname;
2295 else
2296 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002297 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002298 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002299 if (buf == NULL)
2300 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002301
Bram Moolenaarc1542742016-07-20 21:44:37 +02002302 buf->b_has_qf_entry =
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002303 IS_QF_STACK(qi) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002304 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305}
2306
2307/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002308 * Push dirbuf onto the directory stack and return pointer to actual dir or
2309 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 */
2311 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002312qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313{
2314 struct dir_stack_T *ds_new;
2315 struct dir_stack_T *ds_ptr;
2316
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002317 // allocate new stack element and hook it in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2319 if (ds_new == NULL)
2320 return NULL;
2321
2322 ds_new->next = *stackptr;
2323 *stackptr = ds_new;
2324
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002325 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 if (vim_isAbsName(dirbuf)
2327 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002328 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 (*stackptr)->dirname = vim_strsave(dirbuf);
2330 else
2331 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002332 // Okay we don't have an absolute path.
2333 // dirbuf must be a subdir of one of the directories on the stack.
2334 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 ds_new = (*stackptr)->next;
2336 (*stackptr)->dirname = NULL;
2337 while (ds_new)
2338 {
2339 vim_free((*stackptr)->dirname);
2340 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2341 TRUE);
2342 if (mch_isdir((*stackptr)->dirname) == TRUE)
2343 break;
2344
2345 ds_new = ds_new->next;
2346 }
2347
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002348 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 while ((*stackptr)->next != ds_new)
2350 {
2351 ds_ptr = (*stackptr)->next;
2352 (*stackptr)->next = (*stackptr)->next->next;
2353 vim_free(ds_ptr->dirname);
2354 vim_free(ds_ptr);
2355 }
2356
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002357 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 if (ds_new == NULL)
2359 {
2360 vim_free((*stackptr)->dirname);
2361 (*stackptr)->dirname = vim_strsave(dirbuf);
2362 }
2363 }
2364
2365 if ((*stackptr)->dirname != NULL)
2366 return (*stackptr)->dirname;
2367 else
2368 {
2369 ds_ptr = *stackptr;
2370 *stackptr = (*stackptr)->next;
2371 vim_free(ds_ptr);
2372 return NULL;
2373 }
2374}
2375
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376/*
2377 * pop dirbuf from the directory stack and return previous directory or NULL if
2378 * stack is empty
2379 */
2380 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002381qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382{
2383 struct dir_stack_T *ds_ptr;
2384
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002385 // TODO: Should we check if dirbuf is the directory on top of the stack?
2386 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002388 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 if (*stackptr != NULL)
2390 {
2391 ds_ptr = *stackptr;
2392 *stackptr = (*stackptr)->next;
2393 vim_free(ds_ptr->dirname);
2394 vim_free(ds_ptr);
2395 }
2396
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002397 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 return *stackptr ? (*stackptr)->dirname : NULL;
2399}
2400
2401/*
2402 * clean up directory stack
2403 */
2404 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002405qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
2407 struct dir_stack_T *ds_ptr;
2408
2409 while ((ds_ptr = *stackptr) != NULL)
2410 {
2411 *stackptr = (*stackptr)->next;
2412 vim_free(ds_ptr->dirname);
2413 vim_free(ds_ptr);
2414 }
2415}
2416
2417/*
2418 * Check in which directory of the directory stack the given file can be
2419 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002420 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 * Cleans up intermediate directory entries.
2422 *
2423 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002424 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 * ./
2426 * ./aa
2427 * ./aa/bb
2428 * ./bb
2429 * ./bb/x.c
2430 * and make says:
2431 * making all in aa
2432 * making all in bb
2433 * x.c:9: Error
2434 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2435 * qf_guess_filepath will return NULL.
2436 */
2437 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002438qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
2440 struct dir_stack_T *ds_ptr;
2441 struct dir_stack_T *ds_tmp;
2442 char_u *fullname;
2443
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002444 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002445 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 return NULL;
2447
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002448 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 fullname = NULL;
2450 while (ds_ptr)
2451 {
2452 vim_free(fullname);
2453 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2454
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002455 // If concat_fnames failed, just go on. The worst thing that can happen
2456 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2458 break;
2459
2460 ds_ptr = ds_ptr->next;
2461 }
2462
2463 vim_free(fullname);
2464
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002465 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002466 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002468 ds_tmp = qfl->qf_dir_stack->next;
2469 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 vim_free(ds_tmp->dirname);
2471 vim_free(ds_tmp);
2472 }
2473
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002474 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475}
2476
2477/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002478 * Returns TRUE if a quickfix/location list with the given identifier exists.
2479 */
2480 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002481qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002482{
2483 qf_info_T *qi = &ql_info;
2484 int i;
2485
2486 if (wp != NULL)
2487 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002488 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002489 if (qi == NULL)
2490 return FALSE;
2491 }
2492
2493 for (i = 0; i < qi->qf_listcount; ++i)
2494 if (qi->qf_lists[i].qf_id == qf_id)
2495 return TRUE;
2496
2497 return FALSE;
2498}
2499
2500/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002501 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002502 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002503 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002504 * Similar to location list.
2505 */
2506 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002507is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002508{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002509 qfline_T *qfp;
2510 int i;
2511
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002512 // Search for the entry in the current list
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002513 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2514 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002515 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002516 break;
2517
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002518 if (i == qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002519 return FALSE;
2520
2521 return TRUE;
2522}
2523
2524/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002525 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002526 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002527 */
2528 static qfline_T *
2529get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002530 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002531 qfline_T *qf_ptr,
2532 int *qf_index,
2533 int dir)
2534{
2535 int idx;
2536 int old_qf_fnum;
2537
2538 idx = *qf_index;
2539 old_qf_fnum = qf_ptr->qf_fnum;
2540
2541 do
2542 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002543 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002544 return NULL;
2545 ++idx;
2546 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002547 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002548 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2549
2550 *qf_index = idx;
2551 return qf_ptr;
2552}
2553
2554/*
2555 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002556 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002557 */
2558 static qfline_T *
2559get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002560 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002561 qfline_T *qf_ptr,
2562 int *qf_index,
2563 int dir)
2564{
2565 int idx;
2566 int old_qf_fnum;
2567
2568 idx = *qf_index;
2569 old_qf_fnum = qf_ptr->qf_fnum;
2570
2571 do
2572 {
2573 if (idx == 1 || qf_ptr->qf_prev == NULL)
2574 return NULL;
2575 --idx;
2576 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002577 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002578 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2579
2580 *qf_index = idx;
2581 return qf_ptr;
2582}
2583
2584/*
2585 * Get the n'th (errornr) previous/next valid entry from the current entry in
2586 * the quickfix list.
2587 * dir == FORWARD or FORWARD_FILE: next valid entry
2588 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2589 */
2590 static qfline_T *
2591get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002592 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002593 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002594 int dir,
2595 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002596{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002597 qfline_T *qf_ptr = qfl->qf_ptr;
2598 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002599 qfline_T *prev_qf_ptr;
2600 int prev_index;
2601 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2602 char_u *err = e_no_more_items;
2603
2604 while (errornr--)
2605 {
2606 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002607 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002608
2609 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002610 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002611 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002612 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002613 if (qf_ptr == NULL)
2614 {
2615 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002616 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002617 if (err != NULL)
2618 {
2619 EMSG(_(err));
2620 return NULL;
2621 }
2622 break;
2623 }
2624
2625 err = NULL;
2626 }
2627
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002628 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002629 return qf_ptr;
2630}
2631
2632/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002633 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2634 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002635 */
2636 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002637get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002638{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002639 qfline_T *qf_ptr = qfl->qf_ptr;
2640 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002641
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002642 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002643 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2644 {
2645 --qf_idx;
2646 qf_ptr = qf_ptr->qf_prev;
2647 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002648 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002649 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2650 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002651 {
2652 ++qf_idx;
2653 qf_ptr = qf_ptr->qf_next;
2654 }
2655
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002656 *new_qfidx = qf_idx;
2657 return qf_ptr;
2658}
2659
2660/*
2661 * Get a entry specied by 'errornr' and 'dir' from the current
2662 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2663 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2664 * Returns a pointer to the entry and the index of the new entry is stored in
2665 * 'new_qfidx'.
2666 */
2667 static qfline_T *
2668qf_get_entry(
2669 qf_list_T *qfl,
2670 int errornr,
2671 int dir,
2672 int *new_qfidx)
2673{
2674 qfline_T *qf_ptr = qfl->qf_ptr;
2675 int qfidx = qfl->qf_index;
2676
2677 if (dir != 0) // next/prev valid entry
2678 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2679 else if (errornr != 0) // go to specified number
2680 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2681
2682 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002683 return qf_ptr;
2684}
2685
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002686/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002687 * Find a window displaying a Vim help file.
2688 */
2689 static win_T *
2690qf_find_help_win(void)
2691{
2692 win_T *wp;
2693
2694 FOR_ALL_WINDOWS(wp)
2695 if (bt_help(wp->w_buffer))
2696 return wp;
2697
2698 return NULL;
2699}
2700
2701/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002702 * Find a help window or open one.
2703 */
2704 static int
2705jump_to_help_window(qf_info_T *qi, int *opened_window)
2706{
2707 win_T *wp;
2708 int flags;
2709
2710 if (cmdmod.tab != 0)
2711 wp = NULL;
2712 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002713 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002714 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2715 win_enter(wp, TRUE);
2716 else
2717 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002718 // Split off help window; put it at far top if no position
2719 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002720 flags = WSP_HELP;
2721 if (cmdmod.split == 0 && curwin->w_width != Columns
2722 && curwin->w_width < 80)
2723 flags |= WSP_TOP;
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002724 if (IS_LL_STACK(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002725 flags |= WSP_NEWLOC; // don't copy the location list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002726
2727 if (win_split(0, flags) == FAIL)
2728 return FAIL;
2729
2730 *opened_window = TRUE;
2731
2732 if (curwin->w_height < p_hh)
2733 win_setheight((int)p_hh);
2734
Bram Moolenaar4d77c652018-08-18 19:59:54 +02002735 if (IS_LL_STACK(qi)) // not a quickfix list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002736 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002737 // The new window should use the supplied location list
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002738 curwin->w_llist = qi;
2739 qi->qf_refcount++;
2740 }
2741 }
2742
2743 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002744 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002745
2746 return OK;
2747}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002748
2749/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002750 * Find a non-quickfix window using the given location list.
2751 * Returns NULL if a matching window is not found.
2752 */
2753 static win_T *
2754qf_find_win_with_loclist(qf_info_T *ll)
2755{
2756 win_T *wp;
2757
2758 FOR_ALL_WINDOWS(wp)
2759 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2760 return wp;
2761
2762 return NULL;
2763}
2764
2765/*
2766 * Find a window containing a normal buffer
2767 */
2768 static win_T *
2769qf_find_win_with_normal_buf(void)
2770{
2771 win_T *wp;
2772
2773 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002774 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002775 return wp;
2776
2777 return NULL;
2778}
2779
2780/*
2781 * Go to a window in any tabpage containing the specified file. Returns TRUE
2782 * if successfully jumped to the window. Otherwise returns FALSE.
2783 */
2784 static int
2785qf_goto_tabwin_with_file(int fnum)
2786{
2787 tabpage_T *tp;
2788 win_T *wp;
2789
2790 FOR_ALL_TAB_WINDOWS(tp, wp)
2791 if (wp->w_buffer->b_fnum == fnum)
2792 {
2793 goto_tabpage_win(tp, wp);
2794 return TRUE;
2795 }
2796
2797 return FALSE;
2798}
2799
2800/*
2801 * Create a new window to show a file above the quickfix window. Called when
2802 * only the quickfix window is present.
2803 */
2804 static int
2805qf_open_new_file_win(qf_info_T *ll_ref)
2806{
2807 int flags;
2808
2809 flags = WSP_ABOVE;
2810 if (ll_ref != NULL)
2811 flags |= WSP_NEWLOC;
2812 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002813 return FAIL; // not enough room for window
2814 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002815 swb_flags = 0;
2816 RESET_BINDING(curwin);
2817 if (ll_ref != NULL)
2818 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002819 // The new window should use the location list from the
2820 // location list window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002821 curwin->w_llist = ll_ref;
2822 ll_ref->qf_refcount++;
2823 }
2824 return OK;
2825}
2826
2827/*
2828 * Go to a window that shows the right buffer. If the window is not found, go
2829 * to the window just above the location list window. This is used for opening
2830 * a file from a location window and not from a quickfix window. If some usable
2831 * window is previously found, then it is supplied in 'use_win'.
2832 */
2833 static void
2834qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2835{
2836 win_T *win = use_win;
2837
2838 if (win == NULL)
2839 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002840 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002841 FOR_ALL_WINDOWS(win)
2842 if (win->w_buffer->b_fnum == qf_fnum)
2843 break;
2844 if (win == NULL)
2845 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002846 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002847 win = curwin;
2848 do
2849 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002850 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002851 break;
2852 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002853 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002854 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002855 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002856 } while (win != curwin);
2857 }
2858 }
2859 win_goto(win);
2860
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002861 // If the location list for the window is not set, then set it
2862 // to the location list from the location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002863 if (win->w_llist == NULL)
2864 {
2865 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002866 if (ll_ref != NULL)
2867 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002868 }
2869}
2870
2871/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002872 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2873 * not found, then go to the window just above the quickfix window. This is
2874 * used for opening a file from a quickfix window and not from a location
2875 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002876 */
2877 static void
2878qf_goto_win_with_qfl_file(int qf_fnum)
2879{
2880 win_T *win;
2881 win_T *altwin;
2882
2883 win = curwin;
2884 altwin = NULL;
2885 for (;;)
2886 {
2887 if (win->w_buffer->b_fnum == qf_fnum)
2888 break;
2889 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002890 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002891 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002892 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002893
2894 if (IS_QF_WINDOW(win))
2895 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002896 // Didn't find it, go to the window before the quickfix
2897 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002898 if (altwin != NULL)
2899 win = altwin;
2900 else if (curwin->w_prev != NULL)
2901 win = curwin->w_prev;
2902 else
2903 win = curwin->w_next;
2904 break;
2905 }
2906
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002907 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02002908 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002909 altwin = win;
2910 }
2911
2912 win_goto(win);
2913}
2914
2915/*
2916 * Find a suitable window for opening a file (qf_fnum) from the
2917 * quickfix/location list and jump to it. If the file is already opened in a
2918 * window, jump to it. Otherwise open a new window to display the file. This is
2919 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002920 */
2921 static int
2922qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2923{
2924 win_T *usable_win_ptr = NULL;
2925 int usable_win;
2926 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002927 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002928
2929 usable_win = 0;
2930
2931 ll_ref = curwin->w_llist_ref;
2932 if (ll_ref != NULL)
2933 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002934 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002935 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2936 if (usable_win_ptr != NULL)
2937 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002938 }
2939
2940 if (!usable_win)
2941 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002942 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002943 win = qf_find_win_with_normal_buf();
2944 if (win != NULL)
2945 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002946 }
2947
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002948 // If no usable window is found and 'switchbuf' contains "usetab"
2949 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002950 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002951 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002952
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002953 // If there is only one window and it is the quickfix window, create a
2954 // new one above the quickfix window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002955 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2956 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002957 if (qf_open_new_file_win(ll_ref) != OK)
2958 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002959 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002960 }
2961 else
2962 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002963 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002964 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002965 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002966 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002967 }
2968
2969 return OK;
2970}
2971
2972/*
2973 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002974 * Returns OK if successfully edited the file, FAIL on failing to open the
2975 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
2976 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002977 */
2978 static int
2979qf_jump_edit_buffer(
2980 qf_info_T *qi,
2981 qfline_T *qf_ptr,
2982 int forceit,
2983 win_T *oldwin,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002984 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002985{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002986 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002987 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02002988 int old_qf_curlist = qi->qf_curlist;
2989 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002990
2991 if (qf_ptr->qf_type == 1)
2992 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002993 // Open help file (do_ecmd() will set b_help flag, readfile() will
2994 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002995 if (!can_abandon(curbuf, forceit))
2996 {
2997 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02002998 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002999 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003000
3001 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3002 ECMD_HIDE + ECMD_SET_HELP,
3003 oldwin == curwin ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003004 }
3005 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003006 retval = buflist_getfile(qf_ptr->qf_fnum,
3007 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003008
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003009 // If a location list, check whether the associated window is still
3010 // present.
3011 if (IS_LL_STACK(qi) && !win_valid_any_tab(oldwin))
3012 {
3013 EMSG(_("E924: Current window was closed"));
3014 *opened_window = FALSE;
3015 return NOTDONE;
3016 }
3017
3018 if (IS_QF_STACK(qi) && !qflist_valid(NULL, save_qfid))
3019 {
3020 EMSG(_("E925: Current quickfix was changed"));
3021 return NOTDONE;
3022 }
3023
3024 if (old_qf_curlist != qi->qf_curlist
3025 || !is_qf_entry_present(qfl, qf_ptr))
3026 {
3027 if (IS_QF_STACK(qi))
3028 EMSG(_("E925: Current quickfix was changed"));
3029 else
3030 EMSG(_(e_loc_list_changed));
3031 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003032 }
3033
3034 return retval;
3035}
3036
3037/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003038 * Go to the error line in the current file using either line/column number or
3039 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003040 */
3041 static void
3042qf_jump_goto_line(
3043 linenr_T qf_lnum,
3044 int qf_col,
3045 char_u qf_viscol,
3046 char_u *qf_pattern)
3047{
3048 linenr_T i;
3049 char_u *line;
3050 colnr_T screen_col;
3051 colnr_T char_col;
3052
3053 if (qf_pattern == NULL)
3054 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003055 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003056 i = qf_lnum;
3057 if (i > 0)
3058 {
3059 if (i > curbuf->b_ml.ml_line_count)
3060 i = curbuf->b_ml.ml_line_count;
3061 curwin->w_cursor.lnum = i;
3062 }
3063 if (qf_col > 0)
3064 {
3065 curwin->w_cursor.col = qf_col - 1;
3066#ifdef FEAT_VIRTUALEDIT
3067 curwin->w_cursor.coladd = 0;
3068#endif
3069 if (qf_viscol == TRUE)
3070 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003071 // Check each character from the beginning of the error
3072 // line up to the error column. For each tab character
3073 // found, reduce the error column value by the length of
3074 // a tab character.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003075 line = ml_get_curline();
3076 screen_col = 0;
3077 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3078 {
3079 if (*line == NUL)
3080 break;
3081 if (*line++ == '\t')
3082 {
3083 curwin->w_cursor.col -= 7 - (screen_col % 8);
3084 screen_col += 8 - (screen_col % 8);
3085 }
3086 else
3087 ++screen_col;
3088 }
3089 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003090 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003091 check_cursor();
3092 }
3093 else
3094 beginline(BL_WHITE | BL_FIX);
3095 }
3096 else
3097 {
3098 pos_T save_cursor;
3099
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003100 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003101 save_cursor = curwin->w_cursor;
3102 curwin->w_cursor.lnum = 0;
3103 if (!do_search(NULL, '/', qf_pattern, (long)1,
3104 SEARCH_KEEP, NULL, NULL))
3105 curwin->w_cursor = save_cursor;
3106 }
3107}
3108
3109/*
3110 * Display quickfix list index and size message
3111 */
3112 static void
3113qf_jump_print_msg(
3114 qf_info_T *qi,
3115 int qf_index,
3116 qfline_T *qf_ptr,
3117 buf_T *old_curbuf,
3118 linenr_T old_lnum)
3119{
3120 linenr_T i;
3121 int len;
3122
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003123 // Update the screen before showing the message, unless the screen
3124 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003125 if (!msg_scrolled)
3126 update_topline_redraw();
3127 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3128 qi->qf_lists[qi->qf_curlist].qf_count,
3129 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3130 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003131 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003132 len = (int)STRLEN(IObuff);
3133 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3134
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003135 // Output the message. Overwrite to avoid scrolling when the 'O'
3136 // flag is present in 'shortmess'; But when not jumping, print the
3137 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003138 i = msg_scroll;
3139 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3140 msg_scroll = TRUE;
3141 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3142 msg_scroll = FALSE;
3143 msg_attr_keep(IObuff, 0, TRUE);
3144 msg_scroll = i;
3145}
3146
3147/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003148 * Find a usable window for opening a file from the quickfix/location list. If
3149 * a window is not found then open a new window.
3150 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3151 * able to jump/open a window. Returns NOTDONE if a file is not associated
3152 * with the entry.
3153 */
3154 static int
3155qf_jump_open_window(qf_info_T *qi, qfline_T *qf_ptr, int *opened_window)
3156{
3157 // For ":helpgrep" find a help window or open one.
3158 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
3159 if (jump_to_help_window(qi, opened_window) == FAIL)
3160 return FAIL;
3161
3162 // If currently in the quickfix window, find another window to show the
3163 // file in.
3164 if (bt_quickfix(curbuf) && !*opened_window)
3165 {
3166 // If there is no file specified, we don't know where to go.
3167 // But do advance, otherwise ":cn" gets stuck.
3168 if (qf_ptr->qf_fnum == 0)
3169 return NOTDONE;
3170
3171 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, opened_window) == FAIL)
3172 return FAIL;
3173 }
3174
3175 return OK;
3176}
3177
3178/*
3179 * Edit a selected file from the quickfix/location list and jump to a
3180 * particular line/column, adjust the folds and display a message about the
3181 * jump.
3182 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3183 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3184 * the file.
3185 */
3186 static int
3187qf_jump_to_buffer(
3188 qf_info_T *qi,
3189 int qf_index,
3190 qfline_T *qf_ptr,
3191 int forceit,
3192 win_T *oldwin,
3193 int *opened_window,
3194 int openfold,
3195 int print_message)
3196{
3197 buf_T *old_curbuf;
3198 linenr_T old_lnum;
3199 int retval = OK;
3200
3201 // If there is a file name, read the wanted file if needed, and check
3202 // autowrite etc.
3203 old_curbuf = curbuf;
3204 old_lnum = curwin->w_cursor.lnum;
3205
3206 if (qf_ptr->qf_fnum != 0)
3207 {
3208 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3209 opened_window);
3210 if (retval != OK)
3211 return retval;
3212 }
3213
3214 // When not switched to another buffer, still need to set pc mark
3215 if (curbuf == old_curbuf)
3216 setpcmark();
3217
3218 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3219 qf_ptr->qf_pattern);
3220
3221#ifdef FEAT_FOLDING
3222 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3223 foldOpenCursor();
3224#endif
3225 if (print_message)
3226 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3227
3228 return retval;
3229}
3230
3231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 * jump to a quickfix line
3233 * if dir == FORWARD go "errornr" valid entries forward
3234 * if dir == BACKWARD go "errornr" valid entries backward
3235 * if dir == FORWARD_FILE go "errornr" valid entries files backward
3236 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
3237 * else if "errornr" is zero, redisplay the same line
3238 * else go to entry "errornr"
3239 */
3240 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003241qf_jump(qf_info_T *qi,
3242 int dir,
3243 int errornr,
3244 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003246 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003247 qfline_T *qf_ptr;
3248 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003251 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003252 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003254 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003257 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003259 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003261 if (qi == NULL)
3262 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003263
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003264 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
3266 EMSG(_(e_quickfix));
3267 return;
3268 }
3269
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003270 qfl = &qi->qf_lists[qi->qf_curlist];
3271
3272 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003274 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003276
3277 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3278 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003280 qf_ptr = old_qf_ptr;
3281 qf_index = old_qf_index;
3282 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003285 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003286 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003287 // No need to print the error message if it's visible in the error
3288 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 print_message = FALSE;
3290
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003291 retval = qf_jump_open_window(qi, qf_ptr, &opened_window);
3292 if (retval == FAIL)
3293 goto failed;
3294 if (retval == NOTDONE)
3295 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003296
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003297 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, oldwin,
3298 &opened_window, old_KeyTyped, print_message);
3299 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003301 // Quickfix/location list is freed by an autocmd
3302 qi = NULL;
3303 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003306 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003309 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003310 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003312 // Couldn't open file, so put index back where it was. This could
3313 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 qf_ptr = old_qf_ptr;
3316 qf_index = old_qf_index;
3317 }
3318 }
3319theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003320 if (qi != NULL)
3321 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003322 qfl->qf_ptr = qf_ptr;
3323 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 if (p_swb != old_swb && opened_window)
3326 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003327 // Restore old 'switchbuf' value, but not when an autocommand or
3328 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003332 swb_flags = old_swb_flags;
3333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 else
3335 free_string_option(old_swb);
3336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337}
3338
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003339// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003340static int qfFileAttr;
3341static int qfSepAttr;
3342static int qfLineAttr;
3343
3344/*
3345 * Display information about a single entry from the quickfix/location list.
3346 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003347 * 'cursel' will be set to TRUE for the currently selected entry in the
3348 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003349 */
3350 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003351qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003352{
3353 char_u *fname;
3354 buf_T *buf;
3355 int filter_entry;
3356
3357 fname = NULL;
3358 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3359 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3360 (char *)qfp->qf_module);
3361 else {
3362 if (qfp->qf_fnum != 0
3363 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3364 {
3365 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003366 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003367 fname = gettail(fname);
3368 }
3369 if (fname == NULL)
3370 sprintf((char *)IObuff, "%2d", qf_idx);
3371 else
3372 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3373 qf_idx, (char *)fname);
3374 }
3375
3376 // Support for filtering entries using :filter /pat/ clist
3377 // Match against the module name, file name, search pattern and
3378 // text of the entry.
3379 filter_entry = TRUE;
3380 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3381 filter_entry &= message_filtered(qfp->qf_module);
3382 if (filter_entry && fname != NULL)
3383 filter_entry &= message_filtered(fname);
3384 if (filter_entry && qfp->qf_pattern != NULL)
3385 filter_entry &= message_filtered(qfp->qf_pattern);
3386 if (filter_entry)
3387 filter_entry &= message_filtered(qfp->qf_text);
3388 if (filter_entry)
3389 return;
3390
3391 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003392 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003393
3394 if (qfp->qf_lnum != 0)
3395 msg_puts_attr((char_u *)":", qfSepAttr);
3396 if (qfp->qf_lnum == 0)
3397 IObuff[0] = NUL;
3398 else if (qfp->qf_col == 0)
3399 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3400 else
3401 sprintf((char *)IObuff, "%ld col %d",
3402 qfp->qf_lnum, qfp->qf_col);
3403 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3404 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3405 msg_puts_attr(IObuff, qfLineAttr);
3406 msg_puts_attr((char_u *)":", qfSepAttr);
3407 if (qfp->qf_pattern != NULL)
3408 {
3409 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3410 msg_puts(IObuff);
3411 msg_puts_attr((char_u *)":", qfSepAttr);
3412 }
3413 msg_puts((char_u *)" ");
3414
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003415 // Remove newlines and leading whitespace from the text. For an
3416 // unrecognized line keep the indent, the compiler may mark a word
3417 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003418 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3419 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3420 IObuff, IOSIZE);
3421 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003422 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003423}
3424
3425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003427 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 */
3429 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003430qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003432 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003433 qfline_T *qfp;
3434 int i;
3435 int idx1 = 1;
3436 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003437 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003438 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003439 int all = eap->forceit; // if not :cl!, only show
3440 // recognised errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003441 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaar39665952018-08-15 20:59:48 +02003443 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003444 {
3445 qi = GET_LOC_LIST(curwin);
3446 if (qi == NULL)
3447 {
3448 EMSG(_(e_loclist));
3449 return;
3450 }
3451 }
3452
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003453 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
3455 EMSG(_(e_quickfix));
3456 return;
3457 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003458 if (*arg == '+')
3459 {
3460 ++arg;
3461 plus = TRUE;
3462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3464 {
3465 EMSG(_(e_trailing));
3466 return;
3467 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003468 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003469 if (plus)
3470 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003471 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003472 idx2 = i + idx1;
3473 idx1 = i;
3474 }
3475 else
3476 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003477 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003478 if (idx1 < 0)
3479 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3480 if (idx2 < 0)
3481 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003484 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003485 shorten_fnames(FALSE);
3486
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003487 // Get the attributes for the different quickfix highlight items. Note
3488 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003489 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3490 if (qfFileAttr == 0)
3491 qfFileAttr = HL_ATTR(HLF_D);
3492 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3493 if (qfSepAttr == 0)
3494 qfSepAttr = HL_ATTR(HLF_D);
3495 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3496 if (qfLineAttr == 0)
3497 qfLineAttr = HL_ATTR(HLF_N);
3498
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003499 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003501 qfp = qfl->qf_start;
3502 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
3504 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3505 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003506 if (got_int)
3507 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003508
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003509 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003511
3512 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003513 if (qfp == NULL)
3514 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003515 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 ui_breakcheck();
3517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518}
3519
3520/*
3521 * Remove newlines and leading whitespace from an error message.
3522 * Put the result in "buf[bufsize]".
3523 */
3524 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003525qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
3527 int i;
3528 char_u *p = text;
3529
3530 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3531 {
3532 if (*p == '\n')
3533 {
3534 buf[i] = ' ';
3535 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003536 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 break;
3538 }
3539 else
3540 buf[i] = *p++;
3541 }
3542 buf[i] = NUL;
3543}
3544
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003545/*
3546 * Display information (list number, list size and the title) about a
3547 * quickfix/location list.
3548 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003549 static void
3550qf_msg(qf_info_T *qi, int which, char *lead)
3551{
3552 char *title = (char *)qi->qf_lists[which].qf_title;
3553 int count = qi->qf_lists[which].qf_count;
3554 char_u buf[IOSIZE];
3555
3556 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3557 lead,
3558 which + 1,
3559 qi->qf_listcount,
3560 count);
3561
3562 if (title != NULL)
3563 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003564 size_t len = STRLEN(buf);
3565
3566 if (len < 34)
3567 {
3568 vim_memset(buf + len, ' ', 34 - len);
3569 buf[34] = NUL;
3570 }
3571 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003572 }
3573 trunc_string(buf, buf, Columns - 1, IOSIZE);
3574 msg(buf);
3575}
3576
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577/*
3578 * ":colder [count]": Up in the quickfix stack.
3579 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003580 * ":lolder [count]": Up in the location list stack.
3581 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 */
3583 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003584qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003586 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 int count;
3588
Bram Moolenaar39665952018-08-15 20:59:48 +02003589 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003590 {
3591 qi = GET_LOC_LIST(curwin);
3592 if (qi == NULL)
3593 {
3594 EMSG(_(e_loclist));
3595 return;
3596 }
3597 }
3598
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (eap->addr_count != 0)
3600 count = eap->line2;
3601 else
3602 count = 1;
3603 while (count--)
3604 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003605 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003607 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
3609 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003610 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003612 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 }
3614 else
3615 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003616 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003619 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003621 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 }
3623 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003624 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003625 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626}
3627
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003628/*
3629 * Display the information about all the quickfix/location lists in the stack
3630 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003631 void
3632qf_history(exarg_T *eap)
3633{
3634 qf_info_T *qi = &ql_info;
3635 int i;
3636
Bram Moolenaar39665952018-08-15 20:59:48 +02003637 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003638 qi = GET_LOC_LIST(curwin);
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003639 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003640 MSG(_("No entries"));
3641 else
3642 for (i = 0; i < qi->qf_listcount; ++i)
3643 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3644}
3645
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003647 * Free all the entries in the error list "idx". Note that other information
3648 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 */
3650 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003651qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003653 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003654 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003655 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003657 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003659 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003660 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003661 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003662 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003663 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003664 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003665 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003666 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003667 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003668 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003669 // Somehow qf_count may have an incorrect value, set it to 1
3670 // to avoid crashing when it's wrong.
3671 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003672 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003673 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003674 qfl->qf_start = qfpnext;
3675 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003677
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003678 qfl->qf_index = 0;
3679 qfl->qf_start = NULL;
3680 qfl->qf_last = NULL;
3681 qfl->qf_ptr = NULL;
3682 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003683
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003684 qf_clean_dir_stack(&qfl->qf_dir_stack);
3685 qfl->qf_directory = NULL;
3686 qf_clean_dir_stack(&qfl->qf_file_stack);
3687 qfl->qf_currfile = NULL;
3688 qfl->qf_multiline = FALSE;
3689 qfl->qf_multiignore = FALSE;
3690 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691}
3692
3693/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003694 * Free error list "idx". Frees all the entries in the quickfix list,
3695 * associated context information and the title.
3696 */
3697 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003698qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003699{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003700 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003701
Bram Moolenaard23a8232018-02-10 18:45:26 +01003702 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003703 free_tv(qfl->qf_ctx);
3704 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003705 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003706 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003707}
3708
3709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 * qf_mark_adjust: adjust marks
3711 */
3712 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003713qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003714 win_T *wp,
3715 linenr_T line1,
3716 linenr_T line2,
3717 long amount,
3718 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003720 int i;
3721 qfline_T *qfp;
3722 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003723 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003724 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003725 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
Bram Moolenaarc1542742016-07-20 21:44:37 +02003727 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003728 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003729 if (wp != NULL)
3730 {
3731 if (wp->w_llist == NULL)
3732 return;
3733 qi = wp->w_llist;
3734 }
3735
3736 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003737 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003738 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003739 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3740 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 if (qfp->qf_fnum == curbuf->b_fnum)
3742 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003743 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3745 {
3746 if (amount == MAXLNUM)
3747 qfp->qf_cleared = TRUE;
3748 else
3749 qfp->qf_lnum += amount;
3750 }
3751 else if (amount_after && qfp->qf_lnum > line2)
3752 qfp->qf_lnum += amount_after;
3753 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003754
3755 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003756 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757}
3758
3759/*
3760 * Make a nice message out of the error character and the error number:
3761 * char number message
3762 * e or E 0 " error"
3763 * w or W 0 " warning"
3764 * i or I 0 " info"
3765 * 0 0 ""
3766 * other 0 " c"
3767 * e or E n " error n"
3768 * w or W n " warning n"
3769 * i or I n " info n"
3770 * 0 n " error n"
3771 * other n " c n"
3772 * 1 x "" :helpgrep
3773 */
3774 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003775qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776{
3777 static char_u buf[20];
3778 static char_u cc[3];
3779 char_u *p;
3780
3781 if (c == 'W' || c == 'w')
3782 p = (char_u *)" warning";
3783 else if (c == 'I' || c == 'i')
3784 p = (char_u *)" info";
3785 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3786 p = (char_u *)" error";
3787 else if (c == 0 || c == 1)
3788 p = (char_u *)"";
3789 else
3790 {
3791 cc[0] = ' ';
3792 cc[1] = c;
3793 cc[2] = NUL;
3794 p = cc;
3795 }
3796
3797 if (nr <= 0)
3798 return p;
3799
3800 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3801 return buf;
3802}
3803
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003805 * When "split" is FALSE: Open the entry/result under the cursor.
3806 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3807 */
3808 void
3809qf_view_result(int split)
3810{
3811 qf_info_T *qi = &ql_info;
3812
3813 if (!bt_quickfix(curbuf))
3814 return;
3815
3816 if (IS_LL_WINDOW(curwin))
3817 qi = GET_LOC_LIST(curwin);
3818
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003819 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003820 {
3821 EMSG(_(e_quickfix));
3822 return;
3823 }
3824
3825 if (split)
3826 {
3827 char_u cmd[32];
3828
3829 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3830 (long)curwin->w_cursor.lnum,
3831 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3832 if (do_cmdline_cmd(cmd) == OK)
3833 do_cmdline_cmd((char_u *) "clearjumps");
3834 return;
3835 }
3836
3837 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3838}
3839
3840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 * ":cwindow": open the quickfix window if we have errors to display,
3842 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003843 * ":lwindow": open the location list window if we have locations to display,
3844 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 */
3846 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003847ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003849 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003850 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 win_T *win;
3852
Bram Moolenaar39665952018-08-15 20:59:48 +02003853 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003854 {
3855 qi = GET_LOC_LIST(curwin);
3856 if (qi == NULL)
3857 return;
3858 }
3859
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003860 qfl = &qi->qf_lists[qi->qf_curlist];
3861
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003862 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003863 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003865 // If a quickfix window is open but we have no errors to display,
3866 // close the window. If a quickfix window is not open, then open
3867 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003868 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003869 || qfl->qf_nonevalid
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003870 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 {
3872 if (win != NULL)
3873 ex_cclose(eap);
3874 }
3875 else if (win == NULL)
3876 ex_copen(eap);
3877}
3878
3879/*
3880 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003881 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003884ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003886 win_T *win = NULL;
3887 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888
Bram Moolenaar39665952018-08-15 20:59:48 +02003889 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003890 {
3891 qi = GET_LOC_LIST(curwin);
3892 if (qi == NULL)
3893 return;
3894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003896 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003897 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 if (win != NULL)
3899 win_close(win, FALSE);
3900}
3901
3902/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003903 * Set "w:quickfix_title" if "qi" has a title.
3904 */
3905 static void
3906qf_set_title_var(qf_list_T *qfl)
3907{
3908 if (qfl->qf_title != NULL)
3909 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3910}
3911
3912/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003913 * Goto a quickfix or location list window (if present).
3914 * Returns OK if the window is found, FAIL otherwise.
3915 */
3916 static int
3917qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3918{
3919 win_T *win;
3920
3921 win = qf_find_win(qi);
3922 if (win == NULL)
3923 return FAIL;
3924
3925 win_goto(win);
3926 if (resize)
3927 {
3928 if (vertsplit)
3929 {
3930 if (sz != win->w_width)
3931 win_setwidth(sz);
3932 }
3933 else if (sz != win->w_height)
3934 win_setheight(sz);
3935 }
3936
3937 return OK;
3938}
3939
3940/*
3941 * Open a new quickfix or location list window, load the quickfix buffer and
3942 * set the appropriate options for the window.
3943 * Returns FAIL if the window could not be opened.
3944 */
3945 static int
3946qf_open_new_cwindow(qf_info_T *qi, int height)
3947{
3948 buf_T *qf_buf;
3949 win_T *oldwin = curwin;
3950 tabpage_T *prevtab = curtab;
3951 int flags = 0;
3952 win_T *win;
3953
3954 qf_buf = qf_find_buf(qi);
3955
3956 // The current window becomes the previous window afterwards.
3957 win = curwin;
3958
3959 if (IS_QF_STACK(qi) && cmdmod.split == 0)
3960 // Create the new quickfix window at the very bottom, except when
3961 // :belowright or :aboveleft is used.
3962 win_goto(lastwin);
3963 // Default is to open the window below the current window
3964 if (cmdmod.split == 0)
3965 flags = WSP_BELOW;
3966 flags |= WSP_NEWLOC;
3967 if (win_split(height, flags) == FAIL)
3968 return FAIL; // not enough room for window
3969 RESET_BINDING(curwin);
3970
3971 if (IS_LL_STACK(qi))
3972 {
3973 // For the location list window, create a reference to the
3974 // location list from the window 'win'.
3975 curwin->w_llist_ref = win->w_llist;
3976 win->w_llist->qf_refcount++;
3977 }
3978
3979 if (oldwin != curwin)
3980 oldwin = NULL; // don't store info when in another window
3981 if (qf_buf != NULL)
3982 {
3983 // Use the existing quickfix buffer
3984 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
3985 ECMD_HIDE + ECMD_OLDBUF, oldwin);
3986 }
3987 else
3988 {
3989 // Create a new quickfix buffer
3990 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
3991
3992 // switch off 'swapfile'
3993 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3994 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
3995 OPT_LOCAL);
3996 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
3997 RESET_BINDING(curwin);
3998#ifdef FEAT_DIFF
3999 curwin->w_p_diff = FALSE;
4000#endif
4001#ifdef FEAT_FOLDING
4002 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4003 OPT_LOCAL);
4004#endif
4005 }
4006
4007 // Only set the height when still in the same tab page and there is no
4008 // window to the side.
4009 if (curtab == prevtab && curwin->w_width == Columns)
4010 win_setheight(height);
4011 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4012 if (win_valid(win))
4013 prevwin = win;
4014
4015 return OK;
4016}
4017
4018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004020 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 */
4022 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004023ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004025 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004026 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004028 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004029 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030
Bram Moolenaar39665952018-08-15 20:59:48 +02004031 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004032 {
4033 qi = GET_LOC_LIST(curwin);
4034 if (qi == NULL)
4035 {
4036 EMSG(_(e_loclist));
4037 return;
4038 }
4039 }
4040
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004041 incr_quickfix_busy();
4042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (eap->addr_count != 0)
4044 height = eap->line2;
4045 else
4046 height = QF_WINHEIGHT;
4047
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004048 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049#ifdef FEAT_GUI
4050 need_mouse_correct = TRUE;
4051#endif
4052
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004053 // Find an existing quickfix window, or open a new one.
4054 if (cmdmod.tab == 0)
4055 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4056 cmdmod.split & WSP_VERT);
4057 if (status == FAIL)
4058 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004059 {
4060 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004061 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004064 qfl = &qi->qf_lists[qi->qf_curlist];
4065 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004066 // Save the current index here, as updating the quickfix buffer may free
4067 // the quickfix list
4068 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004069
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004070 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004071 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004073 decr_quickfix_busy();
4074
4075 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 curwin->w_cursor.col = 0;
4077 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004078 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079}
4080
4081/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004082 * Move the cursor in the quickfix window to "lnum".
4083 */
4084 static void
4085qf_win_goto(win_T *win, linenr_T lnum)
4086{
4087 win_T *old_curwin = curwin;
4088
4089 curwin = win;
4090 curbuf = win->w_buffer;
4091 curwin->w_cursor.lnum = lnum;
4092 curwin->w_cursor.col = 0;
4093#ifdef FEAT_VIRTUALEDIT
4094 curwin->w_cursor.coladd = 0;
4095#endif
4096 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004097 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004098 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004099 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004100 curwin = old_curwin;
4101 curbuf = curwin->w_buffer;
4102}
4103
4104/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004105 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004106 */
4107 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004108ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004109{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004110 qf_info_T *qi = &ql_info;
4111 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004112
Bram Moolenaar39665952018-08-15 20:59:48 +02004113 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004114 {
4115 qi = GET_LOC_LIST(curwin);
4116 if (qi == NULL)
4117 {
4118 EMSG(_(e_loclist));
4119 return;
4120 }
4121 }
4122
4123 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004124 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4125 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4126}
4127
4128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 * Return the number of the current entry (line number in the quickfix
4130 * window).
4131 */
4132 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004133qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004135 qf_info_T *qi = &ql_info;
4136
4137 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004138 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004139 qi = wp->w_llist_ref;
4140
4141 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142}
4143
4144/*
4145 * Update the cursor position in the quickfix window to the current error.
4146 * Return TRUE if there is a quickfix window.
4147 */
4148 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004149qf_win_pos_update(
4150 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004151 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
4153 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004154 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004156 // Put the cursor on the current error in the quickfix window, so that
4157 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004158 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 if (win != NULL
4160 && qf_index <= win->w_buffer->b_ml.ml_line_count
4161 && old_qf_index != qf_index)
4162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 if (qf_index > old_qf_index)
4164 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004165 win->w_redraw_top = old_qf_index;
4166 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 }
4168 else
4169 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004170 win->w_redraw_top = qf_index;
4171 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004173 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 }
4175 return win != NULL;
4176}
4177
4178/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004179 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004180 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004181 */
4182 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004183is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004184{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004185 // A window displaying the quickfix buffer will have the w_llist_ref field
4186 // set to NULL.
4187 // A window displaying a location list buffer will have the w_llist_ref
4188 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004189 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004190 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4191 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004192 return TRUE;
4193
4194 return FALSE;
4195}
4196
4197/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004198 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004199 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004200 */
4201 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004202qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004203{
4204 win_T *win;
4205
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004206 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004207 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004208 return win;
4209 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004210}
4211
4212/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004213 * Find a quickfix buffer.
4214 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 */
4216 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004217qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004219 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004220 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221
Bram Moolenaar9c102382006-05-03 21:26:49 +00004222 FOR_ALL_TAB_WINDOWS(tp, win)
4223 if (is_qf_win(win, qi))
4224 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004225
Bram Moolenaar9c102382006-05-03 21:26:49 +00004226 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227}
4228
4229/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004230 * Update the w:quickfix_title variable in the quickfix/location list window
4231 */
4232 static void
4233qf_update_win_titlevar(qf_info_T *qi)
4234{
4235 win_T *win;
4236 win_T *curwin_save;
4237
4238 if ((win = qf_find_win(qi)) != NULL)
4239 {
4240 curwin_save = curwin;
4241 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004242 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004243 curwin = curwin_save;
4244 }
4245}
4246
4247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 * Find the quickfix buffer. If it exists, update the contents.
4249 */
4250 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004251qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252{
4253 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004254 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004257 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004258 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 if (buf != NULL)
4260 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004261 linenr_T old_line_count = buf->b_ml.ml_line_count;
4262
4263 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004264 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004265 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
Bram Moolenaard823fa92016-08-12 16:29:27 +02004267 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004268
Bram Moolenaar864293a2016-06-02 13:40:04 +02004269 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004270 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004271
Bram Moolenaar864293a2016-06-02 13:40:04 +02004272 if (old_last == NULL)
4273 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004274 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004275
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004276 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004277 aucmd_restbuf(&aco);
4278 }
4279
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004280 // Only redraw when added lines are visible. This avoids flickering
4281 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004282 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4283 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285}
4286
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004287/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004288 * Add an error line to the quickfix buffer.
4289 */
4290 static int
4291qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4292{
4293 int len;
4294 buf_T *errbuf;
4295
4296 if (qfp->qf_module != NULL)
4297 {
4298 STRCPY(IObuff, qfp->qf_module);
4299 len = (int)STRLEN(IObuff);
4300 }
4301 else if (qfp->qf_fnum != 0
4302 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4303 && errbuf->b_fname != NULL)
4304 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004305 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004306 STRCPY(IObuff, gettail(errbuf->b_fname));
4307 else
4308 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004309 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004310 if (errbuf->b_sfname == NULL
4311 || mch_isFullName(errbuf->b_sfname))
4312 {
4313 if (*dirname == NUL)
4314 mch_dirname(dirname, MAXPATHL);
4315 shorten_buf_fname(errbuf, dirname, FALSE);
4316 }
4317 STRCPY(IObuff, errbuf->b_fname);
4318 }
4319 len = (int)STRLEN(IObuff);
4320 }
4321 else
4322 len = 0;
4323 IObuff[len++] = '|';
4324
4325 if (qfp->qf_lnum > 0)
4326 {
4327 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4328 len += (int)STRLEN(IObuff + len);
4329
4330 if (qfp->qf_col > 0)
4331 {
4332 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4333 len += (int)STRLEN(IObuff + len);
4334 }
4335
4336 sprintf((char *)IObuff + len, "%s",
4337 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4338 len += (int)STRLEN(IObuff + len);
4339 }
4340 else if (qfp->qf_pattern != NULL)
4341 {
4342 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4343 len += (int)STRLEN(IObuff + len);
4344 }
4345 IObuff[len++] = '|';
4346 IObuff[len++] = ' ';
4347
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004348 // Remove newlines and leading whitespace from the text.
4349 // For an unrecognized line keep the indent, the compiler may
4350 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004351 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4352 IObuff + len, IOSIZE - len);
4353
4354 if (ml_append_buf(buf, lnum, IObuff,
4355 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4356 return FAIL;
4357
4358 return OK;
4359}
4360
4361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 * Fill current buffer with quickfix errors, replacing any previous contents.
4363 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004364 * If "old_last" is not NULL append the items after this one.
4365 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4366 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 */
4368 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004369qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004371 linenr_T lnum;
4372 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004373 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374
Bram Moolenaar864293a2016-06-02 13:40:04 +02004375 if (old_last == NULL)
4376 {
4377 if (buf != curbuf)
4378 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004379 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004380 return;
4381 }
4382
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004383 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004384 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4385 (void)ml_delete((linenr_T)1, FALSE);
4386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004388 // Check if there is anything to display
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004389 if (!qf_stack_empty(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004391 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4392 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004393
4394 *dirname = NUL;
4395
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004396 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004397 if (old_last == NULL)
4398 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004399 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004400 lnum = 0;
4401 }
4402 else
4403 {
4404 qfp = old_last->qf_next;
4405 lnum = buf->b_ml.ml_line_count;
4406 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004407 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004409 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004411
Bram Moolenaar864293a2016-06-02 13:40:04 +02004412 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004414 if (qfp == NULL)
4415 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004417
4418 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004419 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004420 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 }
4422
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004423 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 check_lnums(TRUE);
4425
Bram Moolenaar864293a2016-06-02 13:40:04 +02004426 if (old_last == NULL)
4427 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004428 // Set the 'filetype' to "qf" each time after filling the buffer.
4429 // This resembles reading a file into a buffer, it's more logical when
4430 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004431 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004432 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4433 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004435 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004436 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004438 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004440 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004441 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004442
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004443 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004444 redraw_curbuf_later(NOT_VALID);
4445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004447 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 KeyTyped = old_KeyTyped;
4449}
4450
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004451/*
4452 * For every change made to the quickfix list, update the changed tick.
4453 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004454 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004455qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004456{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004457 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004458}
4459
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004461 * Return the quickfix/location list number with the given identifier.
4462 * Returns -1 if list is not found.
4463 */
4464 static int
4465qf_id2nr(qf_info_T *qi, int_u qfid)
4466{
4467 int qf_idx;
4468
4469 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4470 if (qi->qf_lists[qf_idx].qf_id == qfid)
4471 return qf_idx;
4472 return INVALID_QFIDX;
4473}
4474
4475/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004476 * If the current list is not "save_qfid" and we can find the list with that ID
4477 * then make it the current list.
4478 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004479 * Returns OK if successfully restored the list. Returns FAIL if the list with
4480 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004481 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004482 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004483qf_restore_list(qf_info_T *qi, int_u save_qfid)
4484{
4485 int curlist;
4486
4487 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4488 {
4489 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004490 if (curlist < 0)
4491 // list is not present
4492 return FAIL;
4493 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004494 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004495 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004496}
4497
4498/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004499 * Jump to the first entry if there is one.
4500 */
4501 static void
4502qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4503{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004504 if (qf_restore_list(qi, save_qfid) == FAIL)
4505 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004506
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004507 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004508 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004509 qf_jump(qi, 0, 0, forceit);
4510}
4511
4512/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004513 * Return TRUE when using ":vimgrep" for ":grep".
4514 */
4515 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004516grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004517{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004518 return ((cmdidx == CMD_grep
4519 || cmdidx == CMD_lgrep
4520 || cmdidx == CMD_grepadd
4521 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004522 && STRCMP("internal",
4523 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4524}
4525
4526/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004527 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004529 static char_u *
4530make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004532 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004533 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004534 case CMD_make: return (char_u *)"make";
4535 case CMD_lmake: return (char_u *)"lmake";
4536 case CMD_grep: return (char_u *)"grep";
4537 case CMD_lgrep: return (char_u *)"lgrep";
4538 case CMD_grepadd: return (char_u *)"grepadd";
4539 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4540 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542}
4543
4544/*
4545 * Return the name for the errorfile, in allocated memory.
4546 * Find a new unique name when 'makeef' contains "##".
4547 * Returns NULL for error.
4548 */
4549 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004550get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551{
4552 char_u *p;
4553 char_u *name;
4554 static int start = -1;
4555 static int off = 0;
4556#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004557 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558#endif
4559
4560 if (*p_mef == NUL)
4561 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004562 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 if (name == NULL)
4564 EMSG(_(e_notmp));
4565 return name;
4566 }
4567
4568 for (p = p_mef; *p; ++p)
4569 if (p[0] == '#' && p[1] == '#')
4570 break;
4571
4572 if (*p == NUL)
4573 return vim_strsave(p_mef);
4574
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004575 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 for (;;)
4577 {
4578 if (start == -1)
4579 start = mch_get_pid();
4580 else
4581 off += 19;
4582
4583 name = alloc((unsigned)STRLEN(p_mef) + 30);
4584 if (name == NULL)
4585 break;
4586 STRCPY(name, p_mef);
4587 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4588 STRCAT(name, p + 2);
4589 if (mch_getperm(name) < 0
4590#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004591 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 && mch_lstat((char *)name, &sb) < 0
4593#endif
4594 )
4595 break;
4596 vim_free(name);
4597 }
4598 return name;
4599}
4600
4601/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004602 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4603 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4604 */
4605 static char_u *
4606make_get_fullcmd(char_u *makecmd, char_u *fname)
4607{
4608 char_u *cmd;
4609 unsigned len;
4610
4611 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4612 if (*p_sp != NUL)
4613 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4614 cmd = alloc(len);
4615 if (cmd == NULL)
4616 return NULL;
4617 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4618 (char *)p_shq);
4619
4620 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4621 if (*p_sp != NUL)
4622 append_redir(cmd, len, p_sp, fname);
4623
4624 // Display the fully formed command. Output a newline if there's something
4625 // else than the :make command that was typed (in which case the cursor is
4626 // in column 0).
4627 if (msg_col == 0)
4628 msg_didout = FALSE;
4629 msg_start();
4630 MSG_PUTS(":!");
4631 msg_outtrans(cmd); // show what we are doing
4632
4633 return cmd;
4634}
4635
4636/*
4637 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4638 */
4639 void
4640ex_make(exarg_T *eap)
4641{
4642 char_u *fname;
4643 char_u *cmd;
4644 char_u *enc = NULL;
4645 win_T *wp = NULL;
4646 qf_info_T *qi = &ql_info;
4647 int res;
4648 char_u *au_name = NULL;
4649 int_u save_qfid;
4650
4651 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4652 if (grep_internal(eap->cmdidx))
4653 {
4654 ex_vimgrep(eap);
4655 return;
4656 }
4657
4658 au_name = make_get_auname(eap->cmdidx);
4659 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4660 curbuf->b_fname, TRUE, curbuf))
4661 {
4662#ifdef FEAT_EVAL
4663 if (aborting())
4664 return;
4665#endif
4666 }
4667#ifdef FEAT_MBYTE
4668 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4669#endif
4670
4671 if (is_loclist_cmd(eap->cmdidx))
4672 wp = curwin;
4673
4674 autowrite_all();
4675 fname = get_mef_name();
4676 if (fname == NULL)
4677 return;
4678 mch_remove(fname); // in case it's not unique
4679
4680 cmd = make_get_fullcmd(eap->arg, fname);
4681 if (cmd == NULL)
4682 return;
4683
4684 // let the shell know if we are redirecting output or not
4685 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4686
4687#ifdef AMIGA
4688 out_flush();
4689 // read window status report and redraw before message
4690 (void)char_avail();
4691#endif
4692
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004693 incr_quickfix_busy();
4694
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004695 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4696 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4697 (eap->cmdidx != CMD_grepadd
4698 && eap->cmdidx != CMD_lgrepadd),
4699 qf_cmdtitle(*eap->cmdlinep), enc);
4700 if (wp != NULL)
4701 {
4702 qi = GET_LOC_LIST(wp);
4703 if (qi == NULL)
4704 goto cleanup;
4705 }
4706 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004707 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004708
4709 // Remember the current quickfix list identifier, so that we can
4710 // check for autocommands changing the current quickfix list.
4711 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4712 if (au_name != NULL)
4713 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4714 curbuf->b_fname, TRUE, curbuf);
4715 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4716 // display the first error
4717 qf_jump_first(qi, save_qfid, FALSE);
4718
4719cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004720 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004721 mch_remove(fname);
4722 vim_free(fname);
4723 vim_free(cmd);
4724}
4725
4726/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004727 * Returns the number of valid entries in the current quickfix/location list.
4728 */
4729 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004730qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004731{
4732 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004733 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004734 qfline_T *qfp;
4735 int i, sz = 0;
4736 int prev_fnum = 0;
4737
Bram Moolenaar39665952018-08-15 20:59:48 +02004738 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004739 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004740 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004741 qi = GET_LOC_LIST(curwin);
4742 if (qi == NULL)
4743 return 0;
4744 }
4745
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004746 qfl = &qi->qf_lists[qi->qf_curlist];
4747 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004748 ++i, qfp = qfp->qf_next)
4749 {
4750 if (qfp->qf_valid)
4751 {
4752 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004753 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004754 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4755 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004756 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004757 sz++;
4758 prev_fnum = qfp->qf_fnum;
4759 }
4760 }
4761 }
4762
4763 return sz;
4764}
4765
4766/*
4767 * Returns the current index of the quickfix/location list.
4768 * Returns 0 if there is an error.
4769 */
4770 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004771qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004772{
4773 qf_info_T *qi = &ql_info;
4774
Bram Moolenaar39665952018-08-15 20:59:48 +02004775 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004776 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004777 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004778 qi = GET_LOC_LIST(curwin);
4779 if (qi == NULL)
4780 return 0;
4781 }
4782
4783 return qi->qf_lists[qi->qf_curlist].qf_index;
4784}
4785
4786/*
4787 * Returns the current index in the quickfix/location list (counting only valid
4788 * entries). If no valid entries are in the list, then returns 1.
4789 */
4790 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004791qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004792{
4793 qf_info_T *qi = &ql_info;
4794 qf_list_T *qfl;
4795 qfline_T *qfp;
4796 int i, eidx = 0;
4797 int prev_fnum = 0;
4798
Bram Moolenaar39665952018-08-15 20:59:48 +02004799 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004800 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004801 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004802 qi = GET_LOC_LIST(curwin);
4803 if (qi == NULL)
4804 return 1;
4805 }
4806
4807 qfl = &qi->qf_lists[qi->qf_curlist];
4808 qfp = qfl->qf_start;
4809
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004810 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004811 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4812 return 1;
4813
4814 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4815 {
4816 if (qfp->qf_valid)
4817 {
4818 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4819 {
4820 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4821 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004822 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004823 eidx++;
4824 prev_fnum = qfp->qf_fnum;
4825 }
4826 }
4827 else
4828 eidx++;
4829 }
4830 }
4831
4832 return eidx ? eidx : 1;
4833}
4834
4835/*
4836 * Get the 'n'th valid error entry in the quickfix or location list.
4837 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4838 * For :cdo and :ldo returns the 'n'th valid error entry.
4839 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4840 */
4841 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004842qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004843{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004844 qfline_T *qfp = qfl->qf_start;
4845 int i, eidx;
4846 int prev_fnum = 0;
4847
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004848 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004849 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4850 return 1;
4851
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004852 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004853 i++, qfp = qfp->qf_next)
4854 {
4855 if (qfp->qf_valid)
4856 {
4857 if (fdo)
4858 {
4859 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4860 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004861 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004862 eidx++;
4863 prev_fnum = qfp->qf_fnum;
4864 }
4865 }
4866 else
4867 eidx++;
4868 }
4869
4870 if (eidx == n)
4871 break;
4872 }
4873
4874 if (i <= qfl->qf_count)
4875 return i;
4876 else
4877 return 1;
4878}
4879
4880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004882 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004883 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 */
4885 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004886ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004888 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004889 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004890
Bram Moolenaar39665952018-08-15 20:59:48 +02004891 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004892 {
4893 qi = GET_LOC_LIST(curwin);
4894 if (qi == NULL)
4895 {
4896 EMSG(_(e_loclist));
4897 return;
4898 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004899 }
4900
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004901 if (eap->addr_count > 0)
4902 errornr = (int)eap->line2;
4903 else
4904 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004905 switch (eap->cmdidx)
4906 {
4907 case CMD_cc: case CMD_ll:
4908 errornr = 0;
4909 break;
4910 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4911 case CMD_lfirst:
4912 errornr = 1;
4913 break;
4914 default:
4915 errornr = 32767;
4916 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004917 }
4918
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004919 // For cdo and ldo commands, jump to the nth valid error.
4920 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02004921 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4922 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004923 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004924 eap->addr_count > 0 ? (int)eap->line1 : 1,
4925 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4926
4927 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928}
4929
4930/*
4931 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004932 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004933 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 */
4935 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004936ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004938 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004939 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004940 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004941
Bram Moolenaar39665952018-08-15 20:59:48 +02004942 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004943 {
4944 qi = GET_LOC_LIST(curwin);
4945 if (qi == NULL)
4946 {
4947 EMSG(_(e_loclist));
4948 return;
4949 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004950 }
4951
Bram Moolenaar55b69262017-08-13 13:42:01 +02004952 if (eap->addr_count > 0
4953 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4954 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004955 errornr = (int)eap->line2;
4956 else
4957 errornr = 1;
4958
Bram Moolenaar39665952018-08-15 20:59:48 +02004959 // Depending on the command jump to either next or previous entry/file.
4960 switch (eap->cmdidx)
4961 {
4962 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4963 dir = FORWARD;
4964 break;
4965 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4966 case CMD_lNext:
4967 dir = BACKWARD;
4968 break;
4969 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4970 dir = FORWARD_FILE;
4971 break;
4972 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4973 dir = BACKWARD_FILE;
4974 break;
4975 default:
4976 dir = FORWARD;
4977 break;
4978 }
4979
4980 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981}
4982
4983/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004984 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004985 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 */
4987 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004988ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004990 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004991 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004992 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004993 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004994 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004995 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004996
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004997 switch (eap->cmdidx)
4998 {
4999 case CMD_cfile: au_name = (char_u *)"cfile"; break;
5000 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
5001 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
5002 case CMD_lfile: au_name = (char_u *)"lfile"; break;
5003 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
5004 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
5005 default: break;
5006 }
5007 if (au_name != NULL)
5008 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005009#ifdef FEAT_MBYTE
5010 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
5011#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02005012#ifdef FEAT_BROWSE
5013 if (cmdmod.browse)
5014 {
5015 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005016 NULL, NULL,
5017 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005018 if (browse_file == NULL)
5019 return;
5020 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5021 vim_free(browse_file);
5022 }
5023 else
5024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005026 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005027
Bram Moolenaar39665952018-08-15 20:59:48 +02005028 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005029 wp = curwin;
5030
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005031 incr_quickfix_busy();
5032
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005033 // This function is used by the :cfile, :cgetfile and :caddfile
5034 // commands.
5035 // :cfile always creates a new quickfix list and jumps to the
5036 // first error.
5037 // :cgetfile creates a new quickfix list but doesn't jump to the
5038 // first error.
5039 // :caddfile adds to an existing quickfix list. If there is no
5040 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005041 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005042 && eap->cmdidx != CMD_laddfile),
5043 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005044 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005045 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005046 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005047 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005048 {
5049 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005050 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005051 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005052 }
5053 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005054 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005055 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005056 if (au_name != NULL)
5057 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005058
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005059 // Jump to the first error for a new list and if autocmds didn't
5060 // free the list.
5061 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5062 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005063 // display the first error
5064 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005065
5066 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005067}
5068
5069/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005070 * Return the vimgrep autocmd name.
5071 */
5072 static char_u *
5073vgr_get_auname(cmdidx_T cmdidx)
5074{
5075 switch (cmdidx)
5076 {
5077 case CMD_vimgrep: return (char_u *)"vimgrep";
5078 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5079 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5080 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5081 case CMD_grep: return (char_u *)"grep";
5082 case CMD_lgrep: return (char_u *)"lgrep";
5083 case CMD_grepadd: return (char_u *)"grepadd";
5084 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5085 default: return NULL;
5086 }
5087}
5088
5089/*
5090 * Initialize the regmatch used by vimgrep for pattern "s".
5091 */
5092 static void
5093vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5094{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005095 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005096 regmatch->regprog = NULL;
5097
5098 if (s == NULL || *s == NUL)
5099 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005100 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005101 if (last_search_pat() == NULL)
5102 {
5103 EMSG(_(e_noprevre));
5104 return;
5105 }
5106 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5107 }
5108 else
5109 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5110
5111 regmatch->rmm_ic = p_ic;
5112 regmatch->rmm_maxcol = 0;
5113}
5114
5115/*
5116 * Display a file name when vimgrep is running.
5117 */
5118 static void
5119vgr_display_fname(char_u *fname)
5120{
5121 char_u *p;
5122
5123 msg_start();
5124 p = msg_strtrunc(fname, TRUE);
5125 if (p == NULL)
5126 msg_outtrans(fname);
5127 else
5128 {
5129 msg_outtrans(p);
5130 vim_free(p);
5131 }
5132 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005133 msg_didout = FALSE; // overwrite this message
5134 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005135 msg_col = 0;
5136 out_flush();
5137}
5138
5139/*
5140 * Load a dummy buffer to search for a pattern using vimgrep.
5141 */
5142 static buf_T *
5143vgr_load_dummy_buf(
5144 char_u *fname,
5145 char_u *dirname_start,
5146 char_u *dirname_now)
5147{
5148 int save_mls;
5149#if defined(FEAT_SYN_HL)
5150 char_u *save_ei = NULL;
5151#endif
5152 buf_T *buf;
5153
5154#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005155 // Don't do Filetype autocommands to avoid loading syntax and
5156 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005157 save_ei = au_event_disable(",Filetype");
5158#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005159 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005160 save_mls = p_mls;
5161 p_mls = 0;
5162
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005163 // Load file into a buffer, so that 'fileencoding' is detected,
5164 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005165 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5166
5167 p_mls = save_mls;
5168#if defined(FEAT_SYN_HL)
5169 au_event_restore(save_ei);
5170#endif
5171
5172 return buf;
5173}
5174
5175/*
5176 * Check whether a quickfix/location list valid. Autocmds may remove or change
5177 * a quickfix list when vimgrep is running. If the list is not found, create a
5178 * new list.
5179 */
5180 static int
5181vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005182 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005183 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005184 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005185 char_u *title)
5186{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005187 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005188 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005189 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005190 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005191 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005192 // An autocmd has freed the location list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005193 EMSG(_(e_loc_list_changed));
5194 return FALSE;
5195 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005196 else
5197 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005198 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005199 qf_new_list(qi, title);
5200 return TRUE;
5201 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005202 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005203
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005204 if (qf_restore_list(qi, qfid) == FAIL)
5205 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005206
5207 return TRUE;
5208}
5209
5210/*
5211 * Search for a pattern in all the lines in a buffer and add the matching lines
5212 * to a quickfix list.
5213 */
5214 static int
5215vgr_match_buflines(
5216 qf_info_T *qi,
5217 char_u *fname,
5218 buf_T *buf,
5219 regmmatch_T *regmatch,
5220 long tomatch,
5221 int duplicate_name,
5222 int flags)
5223{
5224 int found_match = FALSE;
5225 long lnum;
5226 colnr_T col;
5227
5228 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5229 {
5230 col = 0;
5231 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5232 col, NULL, NULL) > 0)
5233 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005234 // Pass the buffer number so that it gets used even for a
5235 // dummy buffer, unless duplicate_name is set, then the
5236 // buffer will be wiped out below.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005237 if (qf_add_entry(qi,
5238 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005239 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005240 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005241 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005242 duplicate_name ? 0 : buf->b_fnum,
5243 ml_get_buf(buf,
5244 regmatch->startpos[0].lnum + lnum, FALSE),
5245 regmatch->startpos[0].lnum + lnum,
5246 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005247 FALSE, // vis_col
5248 NULL, // search pattern
5249 0, // nr
5250 0, // type
5251 TRUE // valid
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005252 ) == FAIL)
5253 {
5254 got_int = TRUE;
5255 break;
5256 }
5257 found_match = TRUE;
5258 if (--tomatch == 0)
5259 break;
5260 if ((flags & VGR_GLOBAL) == 0
5261 || regmatch->endpos[0].lnum > 0)
5262 break;
5263 col = regmatch->endpos[0].col
5264 + (col == regmatch->endpos[0].col);
5265 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5266 break;
5267 }
5268 line_breakcheck();
5269 if (got_int)
5270 break;
5271 }
5272
5273 return found_match;
5274}
5275
5276/*
5277 * Jump to the first match and update the directory.
5278 */
5279 static void
5280vgr_jump_to_match(
5281 qf_info_T *qi,
5282 int forceit,
5283 int *redraw_for_dummy,
5284 buf_T *first_match_buf,
5285 char_u *target_dir)
5286{
5287 buf_T *buf;
5288
5289 buf = curbuf;
5290 qf_jump(qi, 0, 0, forceit);
5291 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005292 // If we jumped to another buffer redrawing will already be
5293 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005294 *redraw_for_dummy = FALSE;
5295
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005296 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005297 if (curbuf == first_match_buf && target_dir != NULL)
5298 {
5299 exarg_T ea;
5300
5301 ea.arg = target_dir;
5302 ea.cmdidx = CMD_lcd;
5303 ex_cd(&ea);
5304 }
5305}
5306
5307/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005308 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005309 * ":vimgrepadd {pattern} file(s)"
5310 * ":lvimgrep {pattern} file(s)"
5311 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005312 */
5313 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005314ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005315{
Bram Moolenaar81695252004-12-29 20:58:21 +00005316 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005317 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005318 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005319 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005320 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005321 char_u *s;
5322 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005323 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005324 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005325 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005326 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005327 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005328 buf_T *buf;
5329 int duplicate_name = FALSE;
5330 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005331 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005332 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005333 buf_T *first_match_buf = NULL;
5334 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005335 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005336 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005337 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005338 char_u *dirname_start = NULL;
5339 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005340 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005341 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005342
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005343 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005344 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5345 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005346 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005347#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005348 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005349 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005350#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005351 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005352
Bram Moolenaar39665952018-08-15 20:59:48 +02005353 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005354 {
5355 qi = ll_get_or_alloc_list(curwin);
5356 if (qi == NULL)
5357 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005358 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005359 }
5360
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005361 if (eap->addr_count > 0)
5362 tomatch = eap->line2;
5363 else
5364 tomatch = MAXLNUM;
5365
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005366 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005367 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005368 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005369 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005370 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005371 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005372 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005373 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005374 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005375
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005376 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005377 if (regmatch.regprog == NULL)
5378 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005379
5380 p = skipwhite(p);
5381 if (*p == NUL)
5382 {
5383 EMSG(_("E683: File name missing or invalid pattern"));
5384 goto theend;
5385 }
5386
Bram Moolenaar55b69262017-08-13 13:42:01 +02005387 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005388 && eap->cmdidx != CMD_vimgrepadd
5389 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005390 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005391 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005392 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005393
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005394 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005395 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005396 goto theend;
5397 if (fcount == 0)
5398 {
5399 EMSG(_(e_nomatch));
5400 goto theend;
5401 }
5402
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005403 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5404 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005405 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005406 {
5407 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005408 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005409 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005410
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005411 // Remember the current directory, because a BufRead autocommand that does
5412 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005413 mch_dirname(dirname_start, MAXPATHL);
5414
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005415 incr_quickfix_busy();
5416
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005417 // Remember the current quickfix list identifier, so that we can check for
5418 // autocommands changing the current quickfix list.
Bram Moolenaar3c097222017-12-21 20:54:49 +01005419 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005420
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005421 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005422 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005423 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005424 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005425 if (time(NULL) > seconds)
5426 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005427 // Display the file name every second or so, show the user we are
5428 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005429 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005430 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005431 }
5432
Bram Moolenaar81695252004-12-29 20:58:21 +00005433 buf = buflist_findname_exp(fnames[fi]);
5434 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5435 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005436 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005437 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005438 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005439 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005440
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005441 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005442 }
5443 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005444 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005445 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005446
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005447 // Check whether the quickfix list is still valid. When loading a
5448 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005449 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005450 {
5451 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005452 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005453 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005454 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005455 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005456
Bram Moolenaar81695252004-12-29 20:58:21 +00005457 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005458 {
5459 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005460 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005461 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005462 else
5463 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005464 // Try for a match in all lines of the buffer.
5465 // For ":1vimgrep" look for first match only.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005466 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5467 tomatch, duplicate_name, flags);
5468
Bram Moolenaar81695252004-12-29 20:58:21 +00005469 if (using_dummy)
5470 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005471 if (found_match && first_match_buf == NULL)
5472 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005473 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005474 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005475 // Never keep a dummy buffer if there is another buffer
5476 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005477 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005478 buf = NULL;
5479 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005480 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005481 || buf->b_p_bh[0] == 'u' // "unload"
5482 || buf->b_p_bh[0] == 'w' // "wipe"
5483 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005484 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005485 // When no match was found we don't need to remember the
5486 // buffer, wipe it out. If there was a match and it
5487 // wasn't the first one or we won't jump there: only
5488 // unload the buffer.
5489 // Ignore 'hidden' here, because it may lead to having too
5490 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005491 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005492 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005493 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005494 buf = NULL;
5495 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005496 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005497 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005498 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005499 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005500 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005501 buf = NULL;
5502 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005503 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005504
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005505 if (buf != NULL)
5506 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005507 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005508 buf->b_flags &= ~BF_DUMMY;
5509
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005510 // If the buffer is still loaded we need to use the
5511 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005512 if (buf == first_match_buf
5513 && target_dir == NULL
5514 && STRCMP(dirname_start, dirname_now) != 0)
5515 target_dir = vim_strsave(dirname_now);
5516
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005517 // The buffer is still loaded, the Filetype autocommands
5518 // need to be done now, in that buffer. And the modelines
5519 // need to be done (again). But not the window-local
5520 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005521 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005522#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005523 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5524 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005525#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005526 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005527 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005528 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005529 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005530 }
5531 }
5532
5533 FreeWild(fcount, fnames);
5534
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005535 qfl = &qi->qf_lists[qi->qf_curlist];
5536 qfl->qf_nonevalid = FALSE;
5537 qfl->qf_ptr = qfl->qf_start;
5538 qfl->qf_index = 1;
5539 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005540
Bram Moolenaar864293a2016-06-02 13:40:04 +02005541 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005542
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005543 if (au_name != NULL)
5544 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5545 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005546 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5547 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005548 if (!qflist_valid(wp, save_qfid)
5549 || qf_restore_list(qi, save_qfid) == FAIL)
5550 {
5551 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005552 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005553 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005554
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005555 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005556 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005557 {
5558 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005559 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5560 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005561 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005562 else
5563 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005564
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005565 decr_quickfix_busy();
5566
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005567 // If we loaded a dummy buffer into the current window, the autocommands
5568 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005569 if (redraw_for_dummy)
5570 {
5571#ifdef FEAT_FOLDING
5572 foldUpdateAll(curwin);
5573#else
5574 redraw_later(NOT_VALID);
5575#endif
5576 }
5577
Bram Moolenaar86b68352004-12-27 21:59:20 +00005578theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005579 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005580 vim_free(dirname_now);
5581 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005582 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005583 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005584}
5585
5586/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005587 * Restore current working directory to "dirname_start" if they differ, taking
5588 * into account whether it is set locally or globally.
5589 */
5590 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005591restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005592{
5593 char_u *dirname_now = alloc(MAXPATHL);
5594
5595 if (NULL != dirname_now)
5596 {
5597 mch_dirname(dirname_now, MAXPATHL);
5598 if (STRCMP(dirname_start, dirname_now) != 0)
5599 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005600 // If the directory has changed, change it back by building up an
5601 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005602 exarg_T ea;
5603
5604 ea.arg = dirname_start;
5605 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5606 ex_cd(&ea);
5607 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005608 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005609 }
5610}
5611
5612/*
5613 * Load file "fname" into a dummy buffer and return the buffer pointer,
5614 * placing the directory resulting from the buffer load into the
5615 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5616 * prior to calling this function. Restores directory to "dirname_start" prior
5617 * to returning, if autocmds or the 'autochdir' option have changed it.
5618 *
5619 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5620 * or wipe_dummy_buffer() later!
5621 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005622 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005623 */
5624 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005625load_dummy_buffer(
5626 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005627 char_u *dirname_start, // in: old directory
5628 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005629{
5630 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005631 bufref_T newbufref;
5632 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005633 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005634 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005635 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005636
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005637 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005638 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5639 if (newbuf == NULL)
5640 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005641 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005642
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005643 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005644 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5645
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005646 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005647 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005648 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005649 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005650 ++newbuf->b_locked;
5651
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005652 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005653 aucmd_prepbuf(&aco, newbuf);
5654
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005655 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005656 (void)setfname(curbuf, fname, NULL, FALSE);
5657
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005658 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005659 check_need_swap(TRUE);
5660
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005661 // Remove the "dummy" flag, otherwise autocommands may not
5662 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005663 curbuf->b_flags &= ~BF_DUMMY;
5664
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005665 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005666 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005667 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005668 NULL, READ_NEW | READ_DUMMY);
5669 --newbuf->b_locked;
5670 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005671 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005672 && !(curbuf->b_flags & BF_NEW))
5673 {
5674 failed = FALSE;
5675 if (curbuf != newbuf)
5676 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005677 // Bloody autocommands changed the buffer! Can happen when
5678 // using netrw and editing a remote file. Use the current
5679 // buffer instead, delete the dummy one after restoring the
5680 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005681 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005682 newbuf = curbuf;
5683 }
5684 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005685
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005686 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005687 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005688 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5689 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005690
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005691 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5692 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005693 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005694 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005695
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005696 // When autocommands/'autochdir' option changed directory: go back.
5697 // Let the caller know what the resulting dir was first, in case it is
5698 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005699 mch_dirname(resulting_dir, MAXPATHL);
5700 restore_start_dir(dirname_start);
5701
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005702 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005703 return NULL;
5704 if (failed)
5705 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005706 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005707 return NULL;
5708 }
5709 return newbuf;
5710}
5711
5712/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005713 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5714 * directory to "dirname_start" prior to returning, if autocmds or the
5715 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005716 */
5717 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005718wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005719{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005720 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005721 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005722#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005723 cleanup_T cs;
5724
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005725 // Reset the error/interrupt/exception state here so that aborting()
5726 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5727 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005728 enter_cleanup(&cs);
5729#endif
5730
Bram Moolenaar81695252004-12-29 20:58:21 +00005731 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005732
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005733#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005734 // Restore the error/interrupt/exception state if not discarded by a
5735 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005736 leave_cleanup(&cs);
5737#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005738 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005739 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005740 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005741}
5742
5743/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005744 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5745 * directory to "dirname_start" prior to returning, if autocmds or the
5746 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005747 */
5748 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005749unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005750{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005751 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005752 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005753 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005754
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005755 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005756 restore_start_dir(dirname_start);
5757 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005758}
5759
Bram Moolenaar05159a02005-02-26 23:04:13 +00005760#if defined(FEAT_EVAL) || defined(PROTO)
5761/*
5762 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005763 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005764 */
5765 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005766get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005767{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005768 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005769 dict_T *dict;
5770 char_u buf[2];
5771 qfline_T *qfp;
5772 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005773 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005774
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005775 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005776 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005777 qi = &ql_info;
5778 if (wp != NULL)
5779 {
5780 qi = GET_LOC_LIST(wp);
5781 if (qi == NULL)
5782 return FAIL;
5783 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005784 }
5785
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005786 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005787 qf_idx = qi->qf_curlist;
5788
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005789 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005790 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005791
Bram Moolenaard823fa92016-08-12 16:29:27 +02005792 qfp = qi->qf_lists[qf_idx].qf_start;
5793 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005794 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005795 // Handle entries with a non-existing buffer number.
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005796 bufnum = qfp->qf_fnum;
5797 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5798 bufnum = 0;
5799
Bram Moolenaar05159a02005-02-26 23:04:13 +00005800 if ((dict = dict_alloc()) == NULL)
5801 return FAIL;
5802 if (list_append_dict(list, dict) == FAIL)
5803 return FAIL;
5804
5805 buf[0] = qfp->qf_type;
5806 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005807 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5808 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5809 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5810 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5811 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5812 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5813 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5814 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5815 || dict_add_string(dict, "type", buf) == FAIL
5816 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005817 return FAIL;
5818
5819 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005820 if (qfp == NULL)
5821 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005822 }
5823 return OK;
5824}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005825
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005826// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005827enum {
5828 QF_GETLIST_NONE = 0x0,
5829 QF_GETLIST_TITLE = 0x1,
5830 QF_GETLIST_ITEMS = 0x2,
5831 QF_GETLIST_NR = 0x4,
5832 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005833 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005834 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005835 QF_GETLIST_IDX = 0x40,
5836 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005837 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005838 QF_GETLIST_FILEWINID = 0x200,
5839 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005840};
5841
5842/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005843 * Parse text from 'di' and return the quickfix list items.
5844 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005845 */
5846 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005847qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005848{
5849 int status = FAIL;
5850 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005851 char_u *errorformat = p_efm;
5852 dictitem_T *efm_di;
5853 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005854
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005855 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005856 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005857 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005858 // If errorformat is supplied then use it, otherwise use the 'efm'
5859 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005860 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5861 {
5862 if (efm_di->di_tv.v_type != VAR_STRING ||
5863 efm_di->di_tv.vval.v_string == NULL)
5864 return FAIL;
5865 errorformat = efm_di->di_tv.vval.v_string;
5866 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005867
Bram Moolenaar36538222017-09-02 19:51:44 +02005868 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005869 if (l == NULL)
5870 return FAIL;
5871
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005872 qi = ll_new_list();
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005873 if (qi != NULL)
5874 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005875 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005876 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5877 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005878 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005879 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005880 }
5881 free(qi);
5882 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005883 dict_add_list(retdict, "items", l);
5884 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005885 }
5886
5887 return status;
5888}
5889
5890/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005891 * Return the quickfix/location list window identifier in the current tabpage.
5892 */
5893 static int
5894qf_winid(qf_info_T *qi)
5895{
5896 win_T *win;
5897
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005898 // The quickfix window can be opened even if the quickfix list is not set
5899 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005900 if (qi == NULL)
5901 return 0;
5902 win = qf_find_win(qi);
5903 if (win != NULL)
5904 return win->w_id;
5905 return 0;
5906}
5907
5908/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005909 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005910 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005911 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005912qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005913{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005914 int flags = QF_GETLIST_NONE;
5915
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005916 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005917 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005918 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005919 if (!loclist)
5920 // File window ID is applicable only to location list windows
5921 flags &= ~ QF_GETLIST_FILEWINID;
5922 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005923
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005924 if (dict_find(what, (char_u *)"title", -1) != NULL)
5925 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005926
Bram Moolenaara6d48492017-12-12 22:45:31 +01005927 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5928 flags |= QF_GETLIST_NR;
5929
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005930 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5931 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005932
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005933 if (dict_find(what, (char_u *)"context", -1) != NULL)
5934 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005935
Bram Moolenaara6d48492017-12-12 22:45:31 +01005936 if (dict_find(what, (char_u *)"id", -1) != NULL)
5937 flags |= QF_GETLIST_ID;
5938
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005939 if (dict_find(what, (char_u *)"items", -1) != NULL)
5940 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005941
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005942 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5943 flags |= QF_GETLIST_IDX;
5944
5945 if (dict_find(what, (char_u *)"size", -1) != NULL)
5946 flags |= QF_GETLIST_SIZE;
5947
Bram Moolenaarb254af32017-12-18 19:48:58 +01005948 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5949 flags |= QF_GETLIST_TICK;
5950
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005951 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5952 flags |= QF_GETLIST_FILEWINID;
5953
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005954 return flags;
5955}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005956
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005957/*
5958 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5959 * If 'nr' and 'id' are not present in 'what' then return the current
5960 * quickfix list index.
5961 * If 'nr' is zero then return the current quickfix list index.
5962 * If 'nr' is '$' then return the last quickfix list index.
5963 * If 'id' is present then return the index of the quickfix list with that id.
5964 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5965 * Return -1, if quickfix list is not present or if the stack is empty.
5966 */
5967 static int
5968qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5969{
5970 int qf_idx;
5971 dictitem_T *di;
5972
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005973 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005974 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5975 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005976 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005977 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005978 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005979 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005980 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005981 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005982 qf_idx = di->di_tv.vval.v_number - 1;
5983 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005984 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005985 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005986 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005987 else if (di->di_tv.v_type == VAR_STRING
5988 && di->di_tv.vval.v_string != NULL
5989 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005990 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005991 qf_idx = qi->qf_listcount - 1;
5992 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005993 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005994 }
5995
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005996 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5997 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005998 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005999 if (di->di_tv.v_type == VAR_NUMBER)
6000 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006001 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006002 if (di->di_tv.vval.v_number != 0)
6003 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6004 }
6005 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006006 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006007 }
6008
6009 return qf_idx;
6010}
6011
6012/*
6013 * Return default values for quickfix list properties in retdict.
6014 */
6015 static int
6016qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
6017{
6018 int status = OK;
6019
6020 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006021 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006022 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6023 {
6024 list_T *l = list_alloc();
6025 if (l != NULL)
6026 status = dict_add_list(retdict, "items", l);
6027 else
6028 status = FAIL;
6029 }
6030 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006031 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006032 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006033 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006034 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006035 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006036 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006037 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006038 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006039 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006040 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006041 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006042 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006043 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006044 if ((status == OK) && IS_LL_STACK(qi) && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006045 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006046
6047 return status;
6048}
6049
6050/*
6051 * Return the quickfix list title as 'title' in retdict
6052 */
6053 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006054qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006055{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006056 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006057}
6058
6059/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006060 * Returns the identifier of the window used to display files from a location
6061 * list. If there is no associated window, then returns 0. Useful only when
6062 * called from a location list window.
6063 */
6064 static int
6065qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6066{
6067 int winid = 0;
6068
6069 if (wp != NULL && IS_LL_WINDOW(wp))
6070 {
6071 win_T *ll_wp = qf_find_win_with_loclist(qi);
6072 if (ll_wp != NULL)
6073 winid = ll_wp->w_id;
6074 }
6075
6076 return dict_add_number(retdict, "filewinid", winid);
6077}
6078
6079/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006080 * Return the quickfix list items/entries as 'items' in retdict
6081 */
6082 static int
6083qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6084{
6085 int status = OK;
6086 list_T *l = list_alloc();
6087 if (l != NULL)
6088 {
6089 (void)get_errorlist(qi, NULL, qf_idx, l);
6090 dict_add_list(retdict, "items", l);
6091 }
6092 else
6093 status = FAIL;
6094
6095 return status;
6096}
6097
6098/*
6099 * Return the quickfix list context (if any) as 'context' in retdict.
6100 */
6101 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006102qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006103{
6104 int status;
6105 dictitem_T *di;
6106
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006107 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006108 {
6109 di = dictitem_alloc((char_u *)"context");
6110 if (di != NULL)
6111 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006112 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006113 status = dict_add(retdict, di);
6114 if (status == FAIL)
6115 dictitem_free(di);
6116 }
6117 else
6118 status = FAIL;
6119 }
6120 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006121 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006122
6123 return status;
6124}
6125
6126/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006127 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006128 */
6129 static int
6130qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6131{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006132 int curidx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006133 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006134 // For empty lists, current index is set to 0
6135 curidx = 0;
6136 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006137}
6138
6139/*
6140 * Return quickfix/location list details (title) as a
6141 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6142 * then current list is used. Otherwise the specified list is used.
6143 */
6144 int
6145qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6146{
6147 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006148 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006149 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006150 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006151 dictitem_T *di;
6152 int flags = QF_GETLIST_NONE;
6153
6154 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6155 return qf_get_list_from_lines(what, di, retdict);
6156
6157 if (wp != NULL)
6158 qi = GET_LOC_LIST(wp);
6159
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006160 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006161
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006162 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006163 qf_idx = qf_getprop_qfidx(qi, what);
6164
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006165 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006166 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006167 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006168
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006169 qfl = &qi->qf_lists[qf_idx];
6170
Bram Moolenaard823fa92016-08-12 16:29:27 +02006171 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006172 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006173 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006174 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006175 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006176 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006177 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006178 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006179 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006180 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006181 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006182 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006183 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006184 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006185 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006186 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006187 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006188 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006189 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6190 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006191
Bram Moolenaard823fa92016-08-12 16:29:27 +02006192 return status;
6193}
6194
6195/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006196 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
6197 * items in the dict 'd'.
6198 */
6199 static int
6200qf_add_entry_from_dict(
6201 qf_info_T *qi,
6202 int qf_idx,
6203 dict_T *d,
6204 int first_entry)
6205{
6206 static int did_bufnr_emsg;
6207 char_u *filename, *module, *pattern, *text, *type;
6208 int bufnum, valid, status, col, vcol, nr;
6209 long lnum;
6210
6211 if (first_entry)
6212 did_bufnr_emsg = FALSE;
6213
6214 filename = get_dict_string(d, (char_u *)"filename", TRUE);
6215 module = get_dict_string(d, (char_u *)"module", TRUE);
6216 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6217 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6218 col = (int)get_dict_number(d, (char_u *)"col");
6219 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6220 nr = (int)get_dict_number(d, (char_u *)"nr");
6221 type = get_dict_string(d, (char_u *)"type", TRUE);
6222 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6223 text = get_dict_string(d, (char_u *)"text", TRUE);
6224 if (text == NULL)
6225 text = vim_strsave((char_u *)"");
6226
6227 valid = TRUE;
6228 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6229 valid = FALSE;
6230
6231 // Mark entries with non-existing buffer number as not valid. Give the
6232 // error message only once.
6233 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6234 {
6235 if (!did_bufnr_emsg)
6236 {
6237 did_bufnr_emsg = TRUE;
6238 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6239 }
6240 valid = FALSE;
6241 bufnum = 0;
6242 }
6243
6244 // If the 'valid' field is present it overrules the detected value.
6245 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6246 valid = (int)get_dict_number(d, (char_u *)"valid");
6247
6248 status = qf_add_entry(qi,
6249 qf_idx,
6250 NULL, // dir
6251 filename,
6252 module,
6253 bufnum,
6254 text,
6255 lnum,
6256 col,
6257 vcol, // vis_col
6258 pattern, // search pattern
6259 nr,
6260 type == NULL ? NUL : *type,
6261 valid);
6262
6263 vim_free(filename);
6264 vim_free(module);
6265 vim_free(pattern);
6266 vim_free(text);
6267 vim_free(type);
6268
6269 return status;
6270}
6271
6272/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006273 * Add list of entries to quickfix/location list. Each list entry is
6274 * a dictionary with item information.
6275 */
6276 static int
6277qf_add_entries(
6278 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006279 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006280 list_T *list,
6281 char_u *title,
6282 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006283{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006284 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006285 listitem_T *li;
6286 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006287 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006288 int retval = OK;
6289
Bram Moolenaara3921f42017-06-04 15:30:34 +02006290 if (action == ' ' || qf_idx == qi->qf_listcount)
6291 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006292 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006293 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006294 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006295 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006296 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006297 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006298 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006299 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006300 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006301 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006302 qf_free_items(qfl);
6303 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006304 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006305
6306 for (li = list->lv_first; li != NULL; li = li->li_next)
6307 {
6308 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006309 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006310
6311 d = li->li_tv.vval.v_dict;
6312 if (d == NULL)
6313 continue;
6314
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006315 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first);
6316 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006317 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006318 }
6319
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006320 if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006321 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006322 qfl->qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006323 else
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006324 qfl->qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006325 if (action != 'a')
6326 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006327 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006328 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006329 qfl->qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006330 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006331
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006332 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006333 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006334
6335 return retval;
6336}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006337
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006338/*
6339 * Get the quickfix list index from 'nr' or 'id'
6340 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006341 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006342qf_setprop_get_qfidx(
6343 qf_info_T *qi,
6344 dict_T *what,
6345 int action,
6346 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006347{
6348 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006349 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006350
Bram Moolenaard823fa92016-08-12 16:29:27 +02006351 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6352 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006353 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006354 if (di->di_tv.v_type == VAR_NUMBER)
6355 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006356 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006357 if (di->di_tv.vval.v_number != 0)
6358 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006359
Bram Moolenaar55b69262017-08-13 13:42:01 +02006360 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6361 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006362 // When creating a new list, accept qf_idx pointing to the next
6363 // non-available list and add the new list at the end of the
6364 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006365 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006366 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006367 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006368 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006369 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006370 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006371 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006372 }
6373 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006374 && di->di_tv.vval.v_string != NULL
6375 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006376 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006377 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006378 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006379 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006380 qf_idx = 0;
6381 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006382 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006383 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006384 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006385 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006386 }
6387
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006388 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006389 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006390 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006391 if (di->di_tv.v_type != VAR_NUMBER)
6392 return INVALID_QFIDX;
6393
6394 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006395 }
6396
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006397 return qf_idx;
6398}
6399
6400/*
6401 * Set the quickfix list title.
6402 */
6403 static int
6404qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6405{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006406 qf_list_T *qfl = &qi->qf_lists[qf_idx];
6407
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006408 if (di->di_tv.v_type != VAR_STRING)
6409 return FAIL;
6410
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006411 vim_free(qfl->qf_title);
6412 qfl->qf_title = get_dict_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006413 if (qf_idx == qi->qf_curlist)
6414 qf_update_win_titlevar(qi);
6415
6416 return OK;
6417}
6418
6419/*
6420 * Set quickfix list items/entries.
6421 */
6422 static int
6423qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6424{
6425 int retval = FAIL;
6426 char_u *title_save;
6427
6428 if (di->di_tv.v_type != VAR_LIST)
6429 return FAIL;
6430
6431 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6432 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6433 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006434 vim_free(title_save);
6435
6436 return retval;
6437}
6438
6439/*
6440 * Set quickfix list items/entries from a list of lines.
6441 */
6442 static int
6443qf_setprop_items_from_lines(
6444 qf_info_T *qi,
6445 int qf_idx,
6446 dict_T *what,
6447 dictitem_T *di,
6448 int action)
6449{
6450 char_u *errorformat = p_efm;
6451 dictitem_T *efm_di;
6452 int retval = FAIL;
6453
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006454 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006455 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6456 {
6457 if (efm_di->di_tv.v_type != VAR_STRING ||
6458 efm_di->di_tv.vval.v_string == NULL)
6459 return FAIL;
6460 errorformat = efm_di->di_tv.vval.v_string;
6461 }
6462
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006463 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006464 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6465 return FAIL;
6466
6467 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006468 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006469 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6470 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6471 retval = OK;
6472
6473 return retval;
6474}
6475
6476/*
6477 * Set quickfix list context.
6478 */
6479 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006480qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006481{
6482 typval_T *ctx;
6483
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006484 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006485 ctx = alloc_tv();
6486 if (ctx != NULL)
6487 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006488 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006489
6490 return OK;
6491}
6492
6493/*
6494 * Set quickfix/location list properties (title, items, context).
6495 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006496 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006497 */
6498 static int
6499qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6500{
6501 dictitem_T *di;
6502 int retval = FAIL;
6503 int qf_idx;
6504 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006505 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006506
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006507 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006508 newlist = TRUE;
6509
6510 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006511 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006512 return FAIL;
6513
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006514 if (newlist)
6515 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006516 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006517 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006518 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006519 }
6520
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006521 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaard823fa92016-08-12 16:29:27 +02006522 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006523 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006524 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006525 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006526 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006527 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006528 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006529 retval = qf_setprop_context(qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006530
Bram Moolenaarb254af32017-12-18 19:48:58 +01006531 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006532 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006533
Bram Moolenaard823fa92016-08-12 16:29:27 +02006534 return retval;
6535}
6536
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006537/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006538 * Find the non-location list window with the specified location list stack in
6539 * the current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006540 */
6541 static win_T *
6542find_win_with_ll(qf_info_T *qi)
6543{
6544 win_T *wp = NULL;
6545
6546 FOR_ALL_WINDOWS(wp)
6547 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6548 return wp;
6549
6550 return NULL;
6551}
6552
6553/*
6554 * Free the entire quickfix/location list stack.
6555 * If the quickfix/location list window is open, then clear it.
6556 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006557 static void
6558qf_free_stack(win_T *wp, qf_info_T *qi)
6559{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006560 win_T *qfwin = qf_find_win(qi);
6561 win_T *llwin = NULL;
6562 win_T *orig_wp = wp;
6563
6564 if (qfwin != NULL)
6565 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006566 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006567 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006568 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006569 qf_update_buffer(qi, NULL);
6570 }
6571
6572 if (wp != NULL && IS_LL_WINDOW(wp))
6573 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006574 // If in the location list window, then use the non-location list
6575 // window with this location list (if present)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006576 llwin = find_win_with_ll(qi);
6577 if (llwin != NULL)
6578 wp = llwin;
6579 }
6580
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006581 qf_free_all(wp);
6582 if (wp == NULL)
6583 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006584 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006585 qi->qf_curlist = 0;
6586 qi->qf_listcount = 0;
6587 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006588 else if (IS_LL_WINDOW(orig_wp))
6589 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006590 // If the location list window is open, then create a new empty
6591 // location list
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006592 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006593
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006594 // first free the list reference in the location list window
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006595 ll_free_all(&orig_wp->w_llist_ref);
6596
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006597 orig_wp->w_llist_ref = new_ll;
6598 if (llwin != NULL)
6599 {
6600 llwin->w_llist = new_ll;
6601 new_ll->qf_refcount++;
6602 }
6603 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006604}
6605
Bram Moolenaard823fa92016-08-12 16:29:27 +02006606/*
6607 * Populate the quickfix list with the items supplied in the list
6608 * of dictionaries. "title" will be copied to w:quickfix_title.
6609 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6610 */
6611 int
6612set_errorlist(
6613 win_T *wp,
6614 list_T *list,
6615 int action,
6616 char_u *title,
6617 dict_T *what)
6618{
6619 qf_info_T *qi = &ql_info;
6620 int retval = OK;
6621
6622 if (wp != NULL)
6623 {
6624 qi = ll_get_or_alloc_list(wp);
6625 if (qi == NULL)
6626 return FAIL;
6627 }
6628
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006629 if (action == 'f')
6630 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006631 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006632 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006633 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006634 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006635
6636 incr_quickfix_busy();
6637
6638 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006639 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006640 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006641 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006642 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006643 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006644 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006645 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006646
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006647 decr_quickfix_busy();
6648
Bram Moolenaard823fa92016-08-12 16:29:27 +02006649 return retval;
6650}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006651
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006652/*
6653 * Mark the context as in use for all the lists in a quickfix stack.
6654 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006655 static int
6656mark_quickfix_ctx(qf_info_T *qi, int copyID)
6657{
6658 int i;
6659 int abort = FALSE;
6660 typval_T *ctx;
6661
6662 for (i = 0; i < LISTCOUNT && !abort; ++i)
6663 {
6664 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006665 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6666 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006667 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6668 }
6669
6670 return abort;
6671}
6672
6673/*
6674 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006675 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006676 */
6677 int
6678set_ref_in_quickfix(int copyID)
6679{
6680 int abort = FALSE;
6681 tabpage_T *tp;
6682 win_T *win;
6683
6684 abort = mark_quickfix_ctx(&ql_info, copyID);
6685 if (abort)
6686 return abort;
6687
6688 FOR_ALL_TAB_WINDOWS(tp, win)
6689 {
6690 if (win->w_llist != NULL)
6691 {
6692 abort = mark_quickfix_ctx(win->w_llist, copyID);
6693 if (abort)
6694 return abort;
6695 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006696 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6697 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006698 // In a location list window and none of the other windows is
6699 // referring to this location list. Mark the location list
6700 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006701 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6702 if (abort)
6703 return abort;
6704 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006705 }
6706
6707 return abort;
6708}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006709#endif
6710
Bram Moolenaar81695252004-12-29 20:58:21 +00006711/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006712 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006713 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006714 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006715 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006716 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006717 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006718 */
6719 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006720ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006721{
6722 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006723 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006724 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006725 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006726 int_u save_qfid;
6727 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006728
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006729 switch (eap->cmdidx)
6730 {
6731 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6732 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6733 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6734 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6735 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6736 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6737 default: break;
6738 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006739 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6740 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006741 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006742#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006743 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006744 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006745#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006746 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006747
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006748 // Must come after autocommands.
Bram Moolenaar39665952018-08-15 20:59:48 +02006749 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006750 {
6751 qi = ll_get_or_alloc_list(curwin);
6752 if (qi == NULL)
6753 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006754 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006755 }
6756
Bram Moolenaar86b68352004-12-27 21:59:20 +00006757 if (*eap->arg == NUL)
6758 buf = curbuf;
6759 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6760 buf = buflist_findnr(atoi((char *)eap->arg));
6761 if (buf == NULL)
6762 EMSG(_(e_invarg));
6763 else if (buf->b_ml.ml_mfp == NULL)
6764 EMSG(_("E681: Buffer is not loaded"));
6765 else
6766 {
6767 if (eap->addr_count == 0)
6768 {
6769 eap->line1 = 1;
6770 eap->line2 = buf->b_ml.ml_line_count;
6771 }
6772 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6773 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6774 EMSG(_(e_invrange));
6775 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006776 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006777 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006778
6779 if (buf->b_sfname)
6780 {
6781 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6782 (char *)qf_title, (char *)buf->b_sfname);
6783 qf_title = IObuff;
6784 }
6785
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006786 incr_quickfix_busy();
6787
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006788 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006789 (eap->cmdidx != CMD_caddbuffer
6790 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006791 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006792 qf_title, NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006793 if (qf_stack_empty(qi))
6794 {
6795 decr_quickfix_busy();
6796 return;
6797 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006798 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006799 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006800
6801 // Remember the current quickfix list identifier, so that we can
6802 // check for autocommands changing the current quickfix list.
6803 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006804 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006805 {
6806 buf_T *curbuf_old = curbuf;
6807
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006808 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6809 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006810 if (curbuf != curbuf_old)
6811 // Autocommands changed buffer, don't jump now, "qi" may
6812 // be invalid.
6813 res = 0;
6814 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006815 // Jump to the first error for a new list and if autocmds didn't
6816 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006817 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006818 eap->cmdidx == CMD_lbuffer)
6819 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006820 // display the first error
6821 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006822
6823 decr_quickfix_busy();
Bram Moolenaar754b5602006-02-09 23:53:20 +00006824 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006825 }
6826}
6827
Bram Moolenaar1e015462005-09-25 22:16:38 +00006828#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006829/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006830 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6831 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006832 */
6833 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006834ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006835{
6836 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006837 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006838 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006839 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006840 int_u save_qfid;
6841 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006842
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006843 switch (eap->cmdidx)
6844 {
6845 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6846 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6847 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6848 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6849 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6850 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6851 default: break;
6852 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006853 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6854 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006855 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006856#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006857 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006858 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006859#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006860 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006861
Bram Moolenaar39665952018-08-15 20:59:48 +02006862 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006863 {
6864 qi = ll_get_or_alloc_list(curwin);
6865 if (qi == NULL)
6866 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006867 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006868 }
6869
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006870 // Evaluate the expression. When the result is a string or a list we can
6871 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006872 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006873 if (tv != NULL)
6874 {
6875 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6876 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6877 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006878 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006879 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006880 (eap->cmdidx != CMD_caddexpr
6881 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006882 (linenr_T)0, (linenr_T)0,
6883 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006884 if (qf_stack_empty(qi))
6885 {
6886 decr_quickfix_busy();
6887 goto cleanup;
6888 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006889 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006890 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006891
6892 // Remember the current quickfix list identifier, so that we can
6893 // check for autocommands changing the current quickfix list.
6894 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006895 if (au_name != NULL)
6896 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6897 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006898
6899 // Jump to the first error for a new list and if autocmds didn't
6900 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006901 if (res > 0 && (eap->cmdidx == CMD_cexpr
6902 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006903 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006904 // display the first error
6905 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006906 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006907 }
6908 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006909 EMSG(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006910cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00006911 free_tv(tv);
6912 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006913}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006914#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006915
6916/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006917 * Get the location list for ":lhelpgrep"
6918 */
6919 static qf_info_T *
6920hgr_get_ll(int *new_ll)
6921{
6922 win_T *wp;
6923 qf_info_T *qi;
6924
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006925 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006926 if (bt_help(curwin->w_buffer))
6927 wp = curwin;
6928 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006929 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006930 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006931
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006932 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006933 qi = NULL;
6934 else
6935 qi = wp->w_llist;
6936
6937 if (qi == NULL)
6938 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006939 // Allocate a new location list for help text matches
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006940 if ((qi = ll_new_list()) == NULL)
6941 return NULL;
6942 *new_ll = TRUE;
6943 }
6944
6945 return qi;
6946}
6947
6948/*
6949 * Search for a pattern in a help file.
6950 */
6951 static void
6952hgr_search_file(
6953 qf_info_T *qi,
6954 char_u *fname,
6955#ifdef FEAT_MBYTE
6956 vimconv_T *p_vc,
6957#endif
6958 regmatch_T *p_regmatch)
6959{
6960 FILE *fd;
6961 long lnum;
6962
6963 fd = mch_fopen((char *)fname, "r");
6964 if (fd == NULL)
6965 return;
6966
6967 lnum = 1;
6968 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6969 {
6970 char_u *line = IObuff;
6971#ifdef FEAT_MBYTE
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006972 // Convert a line if 'encoding' is not utf-8 and
6973 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006974 if (p_vc->vc_type != CONV_NONE
6975 && has_non_ascii(IObuff))
6976 {
6977 line = string_convert(p_vc, IObuff, NULL);
6978 if (line == NULL)
6979 line = IObuff;
6980 }
6981#endif
6982
6983 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6984 {
6985 int l = (int)STRLEN(line);
6986
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006987 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006988 while (l > 0 && line[l - 1] <= ' ')
6989 line[--l] = NUL;
6990
6991 if (qf_add_entry(qi,
6992 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006993 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006994 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006995 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006996 0,
6997 line,
6998 lnum,
6999 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007000 + 1, // col
7001 FALSE, // vis_col
7002 NULL, // search pattern
7003 0, // nr
7004 1, // type
7005 TRUE // valid
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007006 ) == FAIL)
7007 {
7008 got_int = TRUE;
7009#ifdef FEAT_MBYTE
7010 if (line != IObuff)
7011 vim_free(line);
7012#endif
7013 break;
7014 }
7015 }
7016#ifdef FEAT_MBYTE
7017 if (line != IObuff)
7018 vim_free(line);
7019#endif
7020 ++lnum;
7021 line_breakcheck();
7022 }
7023 fclose(fd);
7024}
7025
7026/*
7027 * Search for a pattern in all the help files in the doc directory under
7028 * the given directory.
7029 */
7030 static void
7031hgr_search_files_in_dir(
7032 qf_info_T *qi,
7033 char_u *dirname,
7034 regmatch_T *p_regmatch
7035#ifdef FEAT_MBYTE
7036 , vimconv_T *p_vc
7037#endif
7038#ifdef FEAT_MULTI_LANG
7039 , char_u *lang
7040#endif
7041 )
7042{
7043 int fcount;
7044 char_u **fnames;
7045 int fi;
7046
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007047 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007048 add_pathsep(dirname);
7049 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7050 if (gen_expand_wildcards(1, &dirname, &fcount,
7051 &fnames, EW_FILE|EW_SILENT) == OK
7052 && fcount > 0)
7053 {
7054 for (fi = 0; fi < fcount && !got_int; ++fi)
7055 {
7056#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007057 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007058 if (lang != NULL
7059 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007060 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007061 && !(STRNICMP(lang, "en", 2) == 0
7062 && STRNICMP("txt", fnames[fi]
7063 + STRLEN(fnames[fi]) - 3, 3) == 0))
7064 continue;
7065#endif
7066
7067 hgr_search_file(qi, fnames[fi],
7068#ifdef FEAT_MBYTE
7069 p_vc,
7070#endif
7071 p_regmatch);
7072 }
7073 FreeWild(fcount, fnames);
7074 }
7075}
7076
7077/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007078 * Search for a pattern in all the help files in the 'runtimepath'
7079 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007080 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007081 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007082 */
7083 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007084hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007085{
7086 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007087
7088#ifdef FEAT_MBYTE
7089 vimconv_T vc;
7090
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007091 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7092 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007093 vc.vc_type = CONV_NONE;
7094 if (!enc_utf8)
7095 convert_setup(&vc, (char_u *)"utf-8", p_enc);
7096#endif
7097
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007098 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007099 p = p_rtp;
7100 while (*p != NUL && !got_int)
7101 {
7102 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7103
7104 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
7105#ifdef FEAT_MBYTE
7106 , &vc
7107#endif
7108#ifdef FEAT_MULTI_LANG
7109 , lang
7110#endif
7111 );
7112 }
7113
7114#ifdef FEAT_MBYTE
7115 if (vc.vc_type != CONV_NONE)
7116 convert_setup(&vc, NULL, NULL);
7117#endif
7118}
7119
7120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 * ":helpgrep {pattern}"
7122 */
7123 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007124ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
7126 regmatch_T regmatch;
7127 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007128 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007129 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007130 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007131 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132
Bram Moolenaar73633f82012-01-20 13:39:07 +01007133 switch (eap->cmdidx)
7134 {
7135 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7136 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7137 default: break;
7138 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007139 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7140 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007141 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007142#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007143 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007144 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007145#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007146 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007147
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007148 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01007149 save_cpo = p_cpo;
7150 p_cpo = empty_option;
7151
Bram Moolenaar39665952018-08-15 20:59:48 +02007152 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007153 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007154 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007155 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007156 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007157 }
7158
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007159 incr_quickfix_busy();
7160
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007161#ifdef FEAT_MULTI_LANG
7162 // Check for a specified language
7163 lang = check_help_lang(eap->arg);
7164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7166 regmatch.rm_ic = FALSE;
7167 if (regmatch.regprog != NULL)
7168 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007169 qf_list_T *qfl;
7170
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007171 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007172 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007174 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007175
Bram Moolenaar473de612013-06-08 18:19:48 +02007176 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007178 qfl = &qi->qf_lists[qi->qf_curlist];
7179 qfl->qf_nonevalid = FALSE;
7180 qfl->qf_ptr = qfl->qf_start;
7181 qfl->qf_index = 1;
7182 qf_list_changed(qfl);
7183 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 }
7185
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007186 if (p_cpo == empty_option)
7187 p_cpo = save_cpo;
7188 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007189 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007190 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191
Bram Moolenaar73633f82012-01-20 13:39:07 +01007192 if (au_name != NULL)
7193 {
7194 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7195 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007196 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007197 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007198 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007199 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007200 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007201 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007202 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007203
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007204 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007205 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007206 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007207 else
7208 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007209
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007210 decr_quickfix_busy();
7211
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007212 if (eap->cmdidx == CMD_lhelpgrep)
7213 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007214 // If the help window is not opened or if it already points to the
7215 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007216 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007217 {
7218 if (new_qi)
7219 ll_free_all(&qi);
7220 }
7221 else if (curwin->w_llist == NULL)
7222 curwin->w_llist = qi;
7223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224}
7225
7226#endif /* FEAT_QUICKFIX */