blob: 244c7a4faa7b792ef34f90359db05196348acdc0 [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 Moolenaar68b76a62005-03-25 21:53:48 +000030 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 */
Bram Moolenaard76ce852018-05-01 15:02:04 +020036 char_u *qf_module; /* module name for this error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000037 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000043 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 Moolenaara539f4f2017-08-30 20:33:55 +020063 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020065 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000066 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020067 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000068 int qf_index; /* current index in the error list */
69 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020070 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020071 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020072 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{
90 /*
91 * Count of references to this list. Used only for location lists.
92 * When a location list window reference this list, qf_refcount
93 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
94 * reaches 0, the list is freed.
95 */
96 int qf_refcount;
97 int qf_listcount; /* current number of lists */
98 int qf_curlist; /* current error list */
99 qf_list_T qf_lists[LISTCOUNT];
100};
101
102static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200103static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
Bram Moolenaard76ce852018-05-01 15:02:04 +0200105#define FMT_PATTERNS 11 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
107/*
108 * Structure used to hold the info of one part of 'errorformat'
109 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000110typedef struct efm_S efm_T;
111struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112{
113 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000114 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
116 char_u prefix; /* prefix of this format line: */
117 /* 'D' enter directory */
118 /* 'X' leave directory */
119 /* 'A' start of multi-line message */
120 /* 'E' error message */
121 /* 'W' warning message */
122 /* 'I' informational message */
123 /* 'C' continuation line */
124 /* 'Z' end of multi-line message */
125 /* 'G' general, unspecific message */
126 /* 'P' push file (partial) message */
127 /* 'Q' pop/quit file (partial) message */
128 /* 'O' overread (partial) message */
129 char_u flags; /* additional flags given in prefix */
130 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000131 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000132 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133};
134
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100135static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
136
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaard76ce852018-05-01 15:02:04 +0200138static 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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void qf_free(qf_info_T *qi, int idx);
140static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200141static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200142static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200144static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static win_T *qf_find_win(qf_info_T *qi);
148static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200149static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200151static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static char_u *get_mef_name(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
154static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
155static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
156static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000158/* Quickfix window check helper macro */
159#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
160/* Location list window check helper macro */
161#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
162/*
163 * Return location list for window 'wp'
164 * For location list window, return the referenced location list
165 */
166#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200169 * Looking up a buffer can be slow if there are many. Remember the last one
170 * to make this a lot faster if there are multiple matches in the same file.
171 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200172static char_u *qf_last_bufname = NULL;
173static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200174
Bram Moolenaar3c097222017-12-21 20:54:49 +0100175static char *e_loc_list_changed =
176 N_("E926: Current location list was changed");
177
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200178/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200179 * Maximum number of bytes allowed per line while reading a errorfile.
180 */
181#define LINE_MAXLEN 4096
182
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200183static struct fmtpattern
184{
185 char_u convchar;
186 char *pattern;
187} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200188 {
189 {'f', ".\\+"}, /* only used when at end */
190 {'n', "\\d\\+"},
191 {'l', "\\d\\+"},
192 {'c', "\\d\\+"},
193 {'t', "."},
194 {'m', ".\\+"},
195 {'r', ".*"},
196 {'p', "[- .]*"},
197 {'v', "\\d\\+"},
198 {'s', ".\\+"},
199 {'o', ".\\+"}
200 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200201
202/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200203 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200204 * See fmt_pat definition above for the list of supported patterns. The
205 * pattern specifier is supplied in "efmpat". The converted pattern is stored
206 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200207 */
208 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200209efmpat_to_regpat(
210 char_u *efmpat,
211 char_u *regpat,
212 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200213 int idx,
214 int round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200215 char_u *errmsg)
216{
217 char_u *srcptr;
218
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200219 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200220 {
221 /* Each errorformat pattern can occur only once */
222 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200223 _("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200224 EMSG(errmsg);
225 return NULL;
226 }
227 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200228 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200229 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200230 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200231 {
232 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200233 _("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200234 EMSG(errmsg);
235 return NULL;
236 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200237 efminfo->addr[idx] = (char_u)++round;
238 *regpat++ = '\\';
239 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200240#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200241 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200242 {
243 /* Also match "c:" in the file name, even when
244 * checking for a colon next: "%f:".
245 * "\%(\a:\)\=" */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200246 STRCPY(regpat, "\\%(\\a:\\)\\=");
247 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200248 }
249#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200250 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200251 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200252 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200253 {
254 /* A file name may contain spaces, but this isn't
255 * in "\f". For "%f:%l:%m" there may be a ":" in
256 * the file name. Use ".\{-1,}x" instead (x is
257 * the next character), the requirement that :999:
258 * follows should work. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200259 STRCPY(regpat, ".\\{-1,}");
260 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200261 }
262 else
263 {
264 /* File name followed by '\\' or '%': include as
265 * many file name chars as possible. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200266 STRCPY(regpat, "\\f\\+");
267 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200268 }
269 }
270 else
271 {
272 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200273 while ((*regpat = *srcptr++) != NUL)
274 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200275 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200276 *regpat++ = '\\';
277 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200278
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200279 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200280}
281
282/*
283 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200284 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200285 */
286 static char_u *
287scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200288 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200289 char_u *efm,
290 int len,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200291 char_u *regpat,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200292 char_u *errmsg)
293{
294 char_u *efmp = *pefmp;
295
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200296 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200297 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200298 if ((*regpat++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200299 {
300 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200301 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200302 if (efmp < efm + len)
303 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200304 *regpat++ = *++efmp; /* could be ']' */
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200305 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200306 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200307 /* skip */;
308 if (efmp == efm + len)
309 {
310 EMSG(_("E374: Missing ] in format string"));
311 return NULL;
312 }
313 }
314 }
315 else if (efmp < efm + len) /* %*\D, %*\s etc. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200316 *regpat++ = *++efmp;
317 *regpat++ = '\\';
318 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200319 }
320 else
321 {
322 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
323 sprintf((char *)errmsg,
324 _("E375: Unsupported %%%c in format string"), *efmp);
325 EMSG(errmsg);
326 return NULL;
327 }
328
329 *pefmp = efmp;
330
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200331 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200332}
333
334/*
335 * Analyze/parse an errorformat prefix.
336 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200337 static char_u *
338efm_analyze_prefix(char_u *efmp, efm_T *efminfo, char_u *errmsg)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200339{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200340 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200341 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200342 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200343 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200344 else
345 {
346 sprintf((char *)errmsg,
347 _("E376: Invalid %%%c in format string prefix"), *efmp);
348 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200349 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200350 }
351
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200352 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200353}
354
355/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200356 * Converts a 'errorformat' string part in 'efm' to a regular expression
357 * pattern. The resulting regex pattern is returned in "regpat". Additional
358 * information about the 'erroformat' pattern is returned in "fmt_ptr".
359 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200360 */
361 static int
362efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200363 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200364 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200365 efm_T *fmt_ptr,
366 char_u *regpat,
367 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200368{
369 char_u *ptr;
370 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200371 int round;
372 int idx = 0;
373
374 /*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200375 * Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200376 */
377 ptr = regpat;
378 *ptr++ = '^';
379 round = 0;
380 for (efmp = efm; efmp < efm + len; ++efmp)
381 {
382 if (*efmp == '%')
383 {
384 ++efmp;
385 for (idx = 0; idx < FMT_PATTERNS; ++idx)
386 if (fmt_pat[idx].convchar == *efmp)
387 break;
388 if (idx < FMT_PATTERNS)
389 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200390 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200391 errmsg);
392 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200393 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200394 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200395 }
396 else if (*efmp == '*')
397 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200398 ++efmp;
399 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr, errmsg);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200400 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200401 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200402 }
403 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
404 *ptr++ = *efmp; /* regexp magic characters */
405 else if (*efmp == '#')
406 *ptr++ = '*';
407 else if (*efmp == '>')
408 fmt_ptr->conthere = TRUE;
409 else if (efmp == efm + 1) /* analyse prefix */
410 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200411 /*
412 * prefix is allowed only at the beginning of the errorformat
413 * option part
414 */
415 efmp = efm_analyze_prefix(efmp, fmt_ptr, errmsg);
416 if (efmp == NULL)
417 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200418 }
419 else
420 {
421 sprintf((char *)errmsg,
422 _("E377: Invalid %%%c in format string"), *efmp);
423 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200424 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200425 }
426 }
427 else /* copy normal character */
428 {
429 if (*efmp == '\\' && efmp + 1 < efm + len)
430 ++efmp;
431 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
432 *ptr++ = '\\'; /* escape regexp atoms */
433 if (*efmp)
434 *ptr++ = *efmp;
435 }
436 }
437 *ptr++ = '$';
438 *ptr = NUL;
439
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200440 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200441}
442
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200443/*
444 * Free the 'errorformat' information list
445 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200446 static void
447free_efm_list(efm_T **efm_first)
448{
449 efm_T *efm_ptr;
450
451 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
452 {
453 *efm_first = efm_ptr->next;
454 vim_regfree(efm_ptr->prog);
455 vim_free(efm_ptr);
456 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100457 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200458}
459
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200460/*
461 * Compute the size of the buffer used to convert a 'errorformat' pattern into
462 * a regular expression pattern.
463 */
464 static int
465efm_regpat_bufsz(char_u *efm)
466{
467 int sz;
468 int i;
469
470 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
471 for (i = FMT_PATTERNS; i > 0; )
472 sz += (int)STRLEN(fmt_pat[--i].pattern);
473#ifdef BACKSLASH_IN_FILENAME
474 sz += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
475#else
476 sz += 2; /* "%f" can become two chars longer */
477#endif
478
479 return sz;
480}
481
482/*
483 * Return the length of a 'errorformat' option part (separated by ",").
484 */
485 static int
486efm_option_part_len(char_u *efm)
487{
488 int len;
489
490 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
491 if (efm[len] == '\\' && efm[len + 1] != NUL)
492 ++len;
493
494 return len;
495}
496
497/*
498 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
499 * are parsed and converted to regular expressions. Returns information about
500 * the parsed 'errorformat' option.
501 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200502 static efm_T *
503parse_efm_option(char_u *efm)
504{
505 char_u *errmsg = NULL;
506 int errmsglen;
507 efm_T *fmt_ptr = NULL;
508 efm_T *fmt_first = NULL;
509 efm_T *fmt_last = NULL;
510 char_u *fmtstr = NULL;
511 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200512 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200513
514 errmsglen = CMDBUFFSIZE + 1;
515 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
516 if (errmsg == NULL)
517 goto parse_efm_end;
518
519 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200520 * Each part of the format string is copied and modified from errorformat
521 * to regex prog. Only a few % characters are allowed.
522 */
523
524 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200525 * Get some space to modify the format string into.
526 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200527 sz = efm_regpat_bufsz(efm);
528 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200529 goto parse_efm_error;
530
531 while (efm[0] != NUL)
532 {
533 /*
534 * Allocate a new eformat structure and put it at the end of the list
535 */
536 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
537 if (fmt_ptr == NULL)
538 goto parse_efm_error;
539 if (fmt_first == NULL) /* first one */
540 fmt_first = fmt_ptr;
541 else
542 fmt_last->next = fmt_ptr;
543 fmt_last = fmt_ptr;
544
545 /*
546 * Isolate one part in the 'errorformat' option
547 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200548 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200549
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200550 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200551 goto parse_efm_error;
552 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
553 goto parse_efm_error;
554 /*
555 * Advance to next part
556 */
557 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
558 }
559
560 if (fmt_first == NULL) /* nothing found */
561 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 Moolenaare0d37972016-07-15 22:36:01 +0200611 /*
612 * If the line exceeds LINE_MAXLEN exclude the last
613 * byte since it's not a NL character.
614 */
615 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
616 if (state->growbuf == NULL)
617 {
618 state->growbuf = alloc(state->linelen + 1);
619 if (state->growbuf == NULL)
620 return NULL;
621 state->growbufsiz = state->linelen;
622 }
623 else if (state->linelen > state->growbufsiz)
624 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200625 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200626 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200627 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200628 state->growbufsiz = state->linelen;
629 }
630 return state->growbuf;
631}
632
633/*
634 * Get the next string (separated by newline) from state->p_str.
635 */
636 static int
637qf_get_next_str_line(qfstate_T *state)
638{
639 /* Get the next line from the supplied string */
640 char_u *p_str = state->p_str;
641 char_u *p;
642 int len;
643
644 if (*p_str == NUL) /* Reached the end of the string */
645 return QF_END_OF_INPUT;
646
647 p = vim_strchr(p_str, '\n');
648 if (p != NULL)
649 len = (int)(p - p_str) + 1;
650 else
651 len = (int)STRLEN(p_str);
652
653 if (len > IOSIZE - 2)
654 {
655 state->linebuf = qf_grow_linebuf(state, len);
656 if (state->linebuf == NULL)
657 return QF_NOMEM;
658 }
659 else
660 {
661 state->linebuf = IObuff;
662 state->linelen = len;
663 }
664 vim_strncpy(state->linebuf, p_str, state->linelen);
665
666 /*
667 * Increment using len in order to discard the rest of the
668 * line if it exceeds LINE_MAXLEN.
669 */
670 p_str += len;
671 state->p_str = p_str;
672
673 return QF_OK;
674}
675
676/*
677 * Get the next string from state->p_Li.
678 */
679 static int
680qf_get_next_list_line(qfstate_T *state)
681{
682 listitem_T *p_li = state->p_li;
683 int len;
684
685 while (p_li != NULL
686 && (p_li->li_tv.v_type != VAR_STRING
687 || p_li->li_tv.vval.v_string == NULL))
688 p_li = p_li->li_next; /* Skip non-string items */
689
690 if (p_li == NULL) /* End of the list */
691 {
692 state->p_li = NULL;
693 return QF_END_OF_INPUT;
694 }
695
696 len = (int)STRLEN(p_li->li_tv.vval.v_string);
697 if (len > IOSIZE - 2)
698 {
699 state->linebuf = qf_grow_linebuf(state, len);
700 if (state->linebuf == NULL)
701 return QF_NOMEM;
702 }
703 else
704 {
705 state->linebuf = IObuff;
706 state->linelen = len;
707 }
708
709 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
710
711 state->p_li = p_li->li_next; /* next item */
712 return QF_OK;
713}
714
715/*
716 * Get the next string from state->buf.
717 */
718 static int
719qf_get_next_buf_line(qfstate_T *state)
720{
721 char_u *p_buf = NULL;
722 int len;
723
724 /* Get the next line from the supplied buffer */
725 if (state->buflnum > state->lnumlast)
726 return QF_END_OF_INPUT;
727
728 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
729 state->buflnum += 1;
730
731 len = (int)STRLEN(p_buf);
732 if (len > IOSIZE - 2)
733 {
734 state->linebuf = qf_grow_linebuf(state, len);
735 if (state->linebuf == NULL)
736 return QF_NOMEM;
737 }
738 else
739 {
740 state->linebuf = IObuff;
741 state->linelen = len;
742 }
743 vim_strncpy(state->linebuf, p_buf, state->linelen);
744
745 return QF_OK;
746}
747
748/*
749 * Get the next string from file state->fd.
750 */
751 static int
752qf_get_next_file_line(qfstate_T *state)
753{
754 int discard;
755 int growbuflen;
756
757 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
758 return QF_END_OF_INPUT;
759
760 discard = FALSE;
761 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200762 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200763 {
764 /*
765 * The current line exceeds IObuff, continue reading using
766 * growbuf until EOL or LINE_MAXLEN bytes is read.
767 */
768 if (state->growbuf == NULL)
769 {
770 state->growbufsiz = 2 * (IOSIZE - 1);
771 state->growbuf = alloc(state->growbufsiz);
772 if (state->growbuf == NULL)
773 return QF_NOMEM;
774 }
775
776 /* Copy the read part of the line, excluding null-terminator */
777 memcpy(state->growbuf, IObuff, IOSIZE - 1);
778 growbuflen = state->linelen;
779
780 for (;;)
781 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200782 char_u *p;
783
Bram Moolenaare0d37972016-07-15 22:36:01 +0200784 if (fgets((char *)state->growbuf + growbuflen,
785 state->growbufsiz - growbuflen, state->fd) == NULL)
786 break;
787 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
788 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200789 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200790 break;
791 if (state->growbufsiz == LINE_MAXLEN)
792 {
793 discard = TRUE;
794 break;
795 }
796
797 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
798 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200799 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200800 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200801 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200802 }
803
804 while (discard)
805 {
806 /*
807 * The current line is longer than LINE_MAXLEN, continue
808 * reading but discard everything until EOL or EOF is
809 * reached.
810 */
811 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
812 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200813 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200814 break;
815 }
816
817 state->linebuf = state->growbuf;
818 state->linelen = growbuflen;
819 }
820 else
821 state->linebuf = IObuff;
822
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100823#ifdef FEAT_MBYTE
824 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200825 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
826 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100827 char_u *line;
828
829 line = string_convert(&state->vc, state->linebuf, &state->linelen);
830 if (line != NULL)
831 {
832 if (state->linelen < IOSIZE)
833 {
834 STRCPY(state->linebuf, line);
835 vim_free(line);
836 }
837 else
838 {
839 vim_free(state->growbuf);
840 state->linebuf = state->growbuf = line;
841 state->growbufsiz = state->linelen < LINE_MAXLEN
842 ? state->linelen : LINE_MAXLEN;
843 }
844 }
845 }
846#endif
847
Bram Moolenaare0d37972016-07-15 22:36:01 +0200848 return QF_OK;
849}
850
851/*
852 * Get the next string from a file/buffer/list/string.
853 */
854 static int
855qf_get_nextline(qfstate_T *state)
856{
857 int status = QF_FAIL;
858
859 if (state->fd == NULL)
860 {
861 if (state->tv != NULL)
862 {
863 if (state->tv->v_type == VAR_STRING)
864 /* Get the next line from the supplied string */
865 status = qf_get_next_str_line(state);
866 else if (state->tv->v_type == VAR_LIST)
867 /* Get the next line from the supplied list */
868 status = qf_get_next_list_line(state);
869 }
870 else
871 /* Get the next line from the supplied buffer */
872 status = qf_get_next_buf_line(state);
873 }
874 else
875 /* Get the next line from the supplied file */
876 status = qf_get_next_file_line(state);
877
878 if (status != QF_OK)
879 return status;
880
881 /* remove newline/CR from the line */
882 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200883 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200884 state->linebuf[state->linelen - 1] = NUL;
885#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200886 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
887 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200888#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200889 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200890
891#ifdef FEAT_MBYTE
892 remove_bom(state->linebuf);
893#endif
894
895 return QF_OK;
896}
897
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200898typedef struct {
899 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200900 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200901 char_u *errmsg;
902 int errmsglen;
903 long lnum;
904 int col;
905 char_u use_viscol;
906 char_u *pattern;
907 int enr;
908 int type;
909 int valid;
910} qffields_T;
911
912/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200913 * Parse the match for filename ('%f') pattern in regmatch.
914 * Return the matched value in "fields->namebuf".
915 */
916 static int
917qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
918{
919 int c;
920
921 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
922 return QF_FAIL;
923
924 /* Expand ~/file and $HOME/file to full path. */
925 c = *rmp->endp[midx];
926 *rmp->endp[midx] = NUL;
927 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
928 *rmp->endp[midx] = c;
929
930 /*
931 * For separate filename patterns (%O, %P and %Q), the specified file
932 * should exist.
933 */
934 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
935 && mch_getperm(fields->namebuf) == -1)
936 return QF_FAIL;
937
938 return QF_OK;
939}
940
941/*
942 * Parse the match for error number ('%n') pattern in regmatch.
943 * Return the matched value in "fields->enr".
944 */
945 static int
946qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
947{
948 if (rmp->startp[midx] == NULL)
949 return QF_FAIL;
950 fields->enr = (int)atol((char *)rmp->startp[midx]);
951 return QF_OK;
952}
953
954/*
955 * Parse the match for line number (%l') pattern in regmatch.
956 * Return the matched value in "fields->lnum".
957 */
958 static int
959qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
960{
961 if (rmp->startp[midx] == NULL)
962 return QF_FAIL;
963 fields->lnum = atol((char *)rmp->startp[midx]);
964 return QF_OK;
965}
966
967/*
968 * Parse the match for column number ('%c') pattern in regmatch.
969 * Return the matched value in "fields->col".
970 */
971 static int
972qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
973{
974 if (rmp->startp[midx] == NULL)
975 return QF_FAIL;
976 fields->col = (int)atol((char *)rmp->startp[midx]);
977 return QF_OK;
978}
979
980/*
981 * Parse the match for error type ('%t') pattern in regmatch.
982 * Return the matched value in "fields->type".
983 */
984 static int
985qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
986{
987 if (rmp->startp[midx] == NULL)
988 return QF_FAIL;
989 fields->type = *rmp->startp[midx];
990 return QF_OK;
991}
992
993/*
994 * Parse the match for '%+' format pattern. The whole matching line is included
995 * in the error string. Return the matched line in "fields->errmsg".
996 */
997 static int
998qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
999{
1000 char_u *p;
1001
1002 if (linelen >= fields->errmsglen)
1003 {
1004 /* linelen + null terminator */
1005 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1006 return QF_NOMEM;
1007 fields->errmsg = p;
1008 fields->errmsglen = linelen + 1;
1009 }
1010 vim_strncpy(fields->errmsg, linebuf, linelen);
1011 return QF_OK;
1012}
1013
1014/*
1015 * Parse the match for error message ('%m') pattern in regmatch.
1016 * Return the matched value in "fields->errmsg".
1017 */
1018 static int
1019qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1020{
1021 char_u *p;
1022 int len;
1023
1024 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1025 return QF_FAIL;
1026 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1027 if (len >= fields->errmsglen)
1028 {
1029 /* len + null terminator */
1030 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1031 return QF_NOMEM;
1032 fields->errmsg = p;
1033 fields->errmsglen = len + 1;
1034 }
1035 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1036 return QF_OK;
1037}
1038
1039/*
1040 * Parse the match for rest of a single-line file message ('%r') pattern.
1041 * Return the matched value in "tail".
1042 */
1043 static int
1044qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1045{
1046 if (rmp->startp[midx] == NULL)
1047 return QF_FAIL;
1048 *tail = rmp->startp[midx];
1049 return QF_OK;
1050}
1051
1052/*
1053 * Parse the match for the pointer line ('%p') pattern in regmatch.
1054 * Return the matched value in "fields->col".
1055 */
1056 static int
1057qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1058{
1059 char_u *match_ptr;
1060
1061 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1062 return QF_FAIL;
1063 fields->col = 0;
1064 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1065 ++match_ptr)
1066 {
1067 ++fields->col;
1068 if (*match_ptr == TAB)
1069 {
1070 fields->col += 7;
1071 fields->col -= fields->col % 8;
1072 }
1073 }
1074 ++fields->col;
1075 fields->use_viscol = TRUE;
1076 return QF_OK;
1077}
1078
1079/*
1080 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1081 * Return the matched value in "fields->col".
1082 */
1083 static int
1084qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1085{
1086 if (rmp->startp[midx] == NULL)
1087 return QF_FAIL;
1088 fields->col = (int)atol((char *)rmp->startp[midx]);
1089 fields->use_viscol = TRUE;
1090 return QF_OK;
1091}
1092
1093/*
1094 * Parse the match for the search text ('%s') pattern in regmatch.
1095 * Return the matched value in "fields->pattern".
1096 */
1097 static int
1098qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1099{
1100 int len;
1101
1102 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1103 return QF_FAIL;
1104 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1105 if (len > CMDBUFFSIZE - 5)
1106 len = CMDBUFFSIZE - 5;
1107 STRCPY(fields->pattern, "^\\V");
1108 STRNCAT(fields->pattern, rmp->startp[midx], len);
1109 fields->pattern[len + 3] = '\\';
1110 fields->pattern[len + 4] = '$';
1111 fields->pattern[len + 5] = NUL;
1112 return QF_OK;
1113}
1114
1115/*
1116 * Parse the match for the module ('%o') pattern in regmatch.
1117 * Return the matched value in "fields->module".
1118 */
1119 static int
1120qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1121{
1122 int len;
1123
1124 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1125 return QF_FAIL;
1126 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1127 if (len > CMDBUFFSIZE)
1128 len = CMDBUFFSIZE;
1129 STRNCAT(fields->module, rmp->startp[midx], len);
1130 return QF_OK;
1131}
1132
1133/*
1134 * 'errorformat' format pattern parser functions.
1135 * The '%f' and '%r' formats are parsed differently from other formats.
1136 * See qf_parse_match() for details.
1137 */
1138static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1139{
1140 NULL,
1141 qf_parse_fmt_n,
1142 qf_parse_fmt_l,
1143 qf_parse_fmt_c,
1144 qf_parse_fmt_t,
1145 qf_parse_fmt_m,
1146 NULL,
1147 qf_parse_fmt_p,
1148 qf_parse_fmt_v,
1149 qf_parse_fmt_s,
1150 qf_parse_fmt_o
1151};
1152
1153/*
1154 * Parse the error format pattern matches in "regmatch" and set the values in
1155 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1156 * match. Returns QF_OK if all the matches are successfully parsed. On
1157 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001158 */
1159 static int
1160qf_parse_match(
1161 char_u *linebuf,
1162 int linelen,
1163 efm_T *fmt_ptr,
1164 regmatch_T *regmatch,
1165 qffields_T *fields,
1166 int qf_multiline,
1167 int qf_multiscan,
1168 char_u **tail)
1169{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001170 int idx = fmt_ptr->prefix;
1171 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001172 int midx;
1173 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001174
1175 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1176 return QF_FAIL;
1177 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1178 fields->type = idx;
1179 else
1180 fields->type = 0;
1181 /*
1182 * Extract error message data from matched line.
1183 * We check for an actual submatch, because "\[" and "\]" in
1184 * the 'errorformat' may cause the wrong submatch to be used.
1185 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001186 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001187 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001188 status = QF_OK;
1189 midx = (int)fmt_ptr->addr[i];
1190 if (i == 0 && midx > 0) /* %f */
1191 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1192 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001193 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001194 if (fmt_ptr->flags == '+' && !qf_multiscan) /* %+ */
1195 status = qf_parse_fmt_plus(linebuf, linelen, fields);
1196 else if (midx > 0) /* %m */
1197 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001198 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001199 else if (i == 6 && midx > 0) /* %r */
1200 status = qf_parse_fmt_r(regmatch, midx, tail);
1201 else if (midx > 0) /* others */
1202 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001203
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001204 if (status != QF_OK)
1205 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001206 }
1207
1208 return QF_OK;
1209}
1210
1211/*
1212 * Parse an error line in 'linebuf' using a single error format string in
1213 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1214 * Returns QF_OK if the efm format matches completely and the fields are
1215 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1216 */
1217 static int
1218qf_parse_get_fields(
1219 char_u *linebuf,
1220 int linelen,
1221 efm_T *fmt_ptr,
1222 qffields_T *fields,
1223 int qf_multiline,
1224 int qf_multiscan,
1225 char_u **tail)
1226{
1227 regmatch_T regmatch;
1228 int status = QF_FAIL;
1229 int r;
1230
1231 if (qf_multiscan &&
1232 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1233 return QF_FAIL;
1234
1235 fields->namebuf[0] = NUL;
1236 fields->module[0] = NUL;
1237 fields->pattern[0] = NUL;
1238 if (!qf_multiscan)
1239 fields->errmsg[0] = NUL;
1240 fields->lnum = 0;
1241 fields->col = 0;
1242 fields->use_viscol = FALSE;
1243 fields->enr = -1;
1244 fields->type = 0;
1245 *tail = NULL;
1246
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001247 /* Always ignore case when looking for a matching error. */
1248 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001249 regmatch.regprog = fmt_ptr->prog;
1250 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1251 fmt_ptr->prog = regmatch.regprog;
1252 if (r)
1253 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1254 fields, qf_multiline, qf_multiscan, tail);
1255
1256 return status;
1257}
1258
1259/*
1260 * Parse directory error format prefixes (%D and %X).
1261 * Push and pop directories from the directory stack when scanning directory
1262 * names.
1263 */
1264 static int
1265qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1266{
1267 if (idx == 'D') /* enter directory */
1268 {
1269 if (*fields->namebuf == NUL)
1270 {
1271 EMSG(_("E379: Missing or empty directory name"));
1272 return QF_FAIL;
1273 }
1274 qfl->qf_directory =
1275 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1276 if (qfl->qf_directory == NULL)
1277 return QF_FAIL;
1278 }
1279 else if (idx == 'X') /* leave directory */
1280 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1281
1282 return QF_OK;
1283}
1284
1285/*
1286 * Parse global file name error format prefixes (%O, %P and %Q).
1287 */
1288 static int
1289qf_parse_file_pfx(
1290 int idx,
1291 qffields_T *fields,
1292 qf_list_T *qfl,
1293 char_u *tail)
1294{
1295 fields->valid = FALSE;
1296 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1297 {
1298 if (*fields->namebuf && idx == 'P')
1299 qfl->qf_currfile =
1300 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1301 else if (idx == 'Q')
1302 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1303 *fields->namebuf = NUL;
1304 if (tail && *tail)
1305 {
1306 STRMOVE(IObuff, skipwhite(tail));
1307 qfl->qf_multiscan = TRUE;
1308 return QF_MULTISCAN;
1309 }
1310 }
1311
1312 return QF_OK;
1313}
1314
1315/*
1316 * Parse a non-error line (a line which doesn't match any of the error
1317 * format in 'efm').
1318 */
1319 static int
1320qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1321{
1322 char_u *p;
1323
1324 fields->namebuf[0] = NUL; /* no match found, remove file name */
1325 fields->lnum = 0; /* don't jump to this line */
1326 fields->valid = FALSE;
1327 if (linelen >= fields->errmsglen)
1328 {
1329 /* linelen + null terminator */
1330 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1331 return QF_NOMEM;
1332 fields->errmsg = p;
1333 fields->errmsglen = linelen + 1;
1334 }
1335 /* copy whole line to error message */
1336 vim_strncpy(fields->errmsg, linebuf, linelen);
1337
1338 return QF_OK;
1339}
1340
1341/*
1342 * Parse multi-line error format prefixes (%C and %Z)
1343 */
1344 static int
1345qf_parse_multiline_pfx(
1346 qf_info_T *qi,
1347 int qf_idx,
1348 int idx,
1349 qf_list_T *qfl,
1350 qffields_T *fields)
1351{
1352 char_u *ptr;
1353 int len;
1354
1355 if (!qfl->qf_multiignore)
1356 {
1357 qfline_T *qfprev = qfl->qf_last;
1358
1359 if (qfprev == NULL)
1360 return QF_FAIL;
1361 if (*fields->errmsg && !qfl->qf_multiignore)
1362 {
1363 len = (int)STRLEN(qfprev->qf_text);
1364 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1365 == NULL)
1366 return QF_FAIL;
1367 STRCPY(ptr, qfprev->qf_text);
1368 vim_free(qfprev->qf_text);
1369 qfprev->qf_text = ptr;
1370 *(ptr += len) = '\n';
1371 STRCPY(++ptr, fields->errmsg);
1372 }
1373 if (qfprev->qf_nr == -1)
1374 qfprev->qf_nr = fields->enr;
1375 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1376 /* only printable chars allowed */
1377 qfprev->qf_type = fields->type;
1378
1379 if (!qfprev->qf_lnum)
1380 qfprev->qf_lnum = fields->lnum;
1381 if (!qfprev->qf_col)
1382 qfprev->qf_col = fields->col;
1383 qfprev->qf_viscol = fields->use_viscol;
1384 if (!qfprev->qf_fnum)
1385 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1386 qfl->qf_directory,
1387 *fields->namebuf || qfl->qf_directory != NULL
1388 ? fields->namebuf
1389 : qfl->qf_currfile != NULL && fields->valid
1390 ? qfl->qf_currfile : 0);
1391 }
1392 if (idx == 'Z')
1393 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1394 line_breakcheck();
1395
1396 return QF_IGNORE_LINE;
1397}
1398
1399/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001400 * Parse a line and get the quickfix fields.
1401 * Return the QF_ status.
1402 */
1403 static int
1404qf_parse_line(
1405 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001406 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001407 char_u *linebuf,
1408 int linelen,
1409 efm_T *fmt_first,
1410 qffields_T *fields)
1411{
1412 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001413 int idx = 0;
1414 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001415 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001416 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001417
Bram Moolenaare333e792018-04-08 13:27:39 +02001418restofline:
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001419 /* If there was no %> item start at the first pattern */
1420 if (fmt_start == NULL)
1421 fmt_ptr = fmt_first;
1422 else
1423 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001424 /* Otherwise start from the last used pattern */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001425 fmt_ptr = fmt_start;
1426 fmt_start = NULL;
1427 }
1428
1429 /*
1430 * Try to match each part of 'errorformat' until we find a complete
1431 * match or no match.
1432 */
1433 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001434 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1435 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001436 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001437 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1438 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1439 if (status == QF_NOMEM)
1440 return status;
1441 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001442 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001443 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001444 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001445
1446 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1447 {
1448 if (fmt_ptr != NULL)
1449 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001450 /* 'D' and 'X' directory specifiers */
1451 status = qf_parse_dir_pfx(idx, fields, qfl);
1452 if (status != QF_OK)
1453 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001454 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001455
1456 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1457 if (status != QF_OK)
1458 return status;
1459
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001460 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001461 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001462 }
1463 else if (fmt_ptr != NULL)
1464 {
1465 /* honor %> item */
1466 if (fmt_ptr->conthere)
1467 fmt_start = fmt_ptr;
1468
1469 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1470 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001471 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1472 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001473 }
1474 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1475 { /* continuation of multi-line msg */
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001476 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1477 if (status != QF_OK)
1478 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001479 }
1480 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001481 { /* global file names */
1482 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1483 if (status == QF_MULTISCAN)
1484 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001485 }
1486 if (fmt_ptr->flags == '-') /* generally exclude this line */
1487 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001488 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001489 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001490 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001491 return QF_IGNORE_LINE;
1492 }
1493 }
1494
1495 return QF_OK;
1496}
1497
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001498/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001499 * Returns TRUE if the specified quickfix/location list is empty.
1500 */
1501 static int
1502qf_list_empty(qf_info_T *qi, int qf_idx)
1503{
1504 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1505 return TRUE;
1506 return qi->qf_lists[qf_idx].qf_count <= 0;
1507}
1508
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001509/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001510 * Allocate the fields used for parsing lines and populating a quickfix list.
1511 */
1512 static int
1513qf_alloc_fields(qffields_T *pfields)
1514{
1515 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1516 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1517 pfields->errmsglen = CMDBUFFSIZE + 1;
1518 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1519 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1520 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1521 || pfields->pattern == NULL || pfields->module == NULL)
1522 return FAIL;
1523
1524 return OK;
1525}
1526
1527/*
1528 * Free the fields used for parsing lines and populating a quickfix list.
1529 */
1530 static void
1531qf_free_fields(qffields_T *pfields)
1532{
1533 vim_free(pfields->namebuf);
1534 vim_free(pfields->module);
1535 vim_free(pfields->errmsg);
1536 vim_free(pfields->pattern);
1537}
1538
1539/*
1540 * Setup the state information used for parsing lines and populating a
1541 * quickfix list.
1542 */
1543 static int
1544qf_setup_state(
1545 qfstate_T *pstate,
1546 char_u *enc,
1547 char_u *efile,
1548 typval_T *tv,
1549 buf_T *buf,
1550 linenr_T lnumfirst,
1551 linenr_T lnumlast)
1552{
1553#ifdef FEAT_MBYTE
1554 pstate->vc.vc_type = CONV_NONE;
1555 if (enc != NULL && *enc != NUL)
1556 convert_setup(&pstate->vc, enc, p_enc);
1557#endif
1558
1559 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1560 {
1561 EMSG2(_(e_openerrf), efile);
1562 return FAIL;
1563 }
1564
1565 if (tv != NULL)
1566 {
1567 if (tv->v_type == VAR_STRING)
1568 pstate->p_str = tv->vval.v_string;
1569 else if (tv->v_type == VAR_LIST)
1570 pstate->p_li = tv->vval.v_list->lv_first;
1571 pstate->tv = tv;
1572 }
1573 pstate->buf = buf;
1574 pstate->buflnum = lnumfirst;
1575 pstate->lnumlast = lnumlast;
1576
1577 return OK;
1578}
1579
1580/*
1581 * Cleanup the state information used for parsing lines and populating a
1582 * quickfix list.
1583 */
1584 static void
1585qf_cleanup_state(qfstate_T *pstate)
1586{
1587 if (pstate->fd != NULL)
1588 fclose(pstate->fd);
1589
1590 vim_free(pstate->growbuf);
1591#ifdef FEAT_MBYTE
1592 if (pstate->vc.vc_type != CONV_NONE)
1593 convert_setup(&pstate->vc, NULL, NULL);
1594#endif
1595}
1596
1597/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001598 * Read the errorfile "efile" into memory, line by line, building the error
1599 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001600 * Alternative: when "efile" is NULL read errors from buffer "buf".
1601 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001602 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001603 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1604 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001605 * Return -1 for error, number of errors for success.
1606 */
1607 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001608qf_init_ext(
1609 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001610 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001611 char_u *efile,
1612 buf_T *buf,
1613 typval_T *tv,
1614 char_u *errorformat,
1615 int newlist, /* TRUE: start a new error list */
1616 linenr_T lnumfirst, /* first line number to use */
1617 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001618 char_u *qf_title,
1619 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001620{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001621 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001622 qfstate_T state;
1623 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001624 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001625 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001626 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001628 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001630 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001632 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001633 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001634
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001635 vim_memset(&state, 0, sizeof(state));
1636 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001637 if ((qf_alloc_fields(&fields) == FAIL) ||
1638 (qf_setup_state(&state, enc, efile, tv, buf,
1639 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 goto qf_init_end;
1641
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001642 if (newlist || qf_idx == qi->qf_listcount)
1643 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001645 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001646 qf_idx = qi->qf_curlist;
1647 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001648 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001649 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001650 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001651 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001652 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001653 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001656 qfl = &qi->qf_lists[qf_idx];
1657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001659 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001660 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 else
1662 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001664 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001665 * If the errorformat didn't change between calls, then reuse the
1666 * previously parsed values.
1667 */
1668 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1669 {
1670 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001671 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001672 free_efm_list(&fmt_first);
1673
1674 /* parse the current 'efm' */
1675 fmt_first = parse_efm_option(efm);
1676 if (fmt_first != NULL)
1677 last_efm = vim_strsave(efm);
1678 }
1679
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
1683 /*
1684 * got_int is reset here, because it was probably set when killing the
1685 * ":make" command, but we still want to read the errorfile then.
1686 */
1687 got_int = FALSE;
1688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 /*
1690 * Read the lines in the error file one by one.
1691 * Try to recognize one of the error formats in each line.
1692 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001693 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001695 /* Get the next line from a file/buffer/list/string */
1696 status = qf_get_nextline(&state);
1697 if (status == QF_NOMEM) /* memory alloc failure */
1698 goto qf_init_end;
1699 if (status == QF_END_OF_INPUT) /* end of input */
1700 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001702 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1703 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001704 if (status == QF_FAIL)
1705 goto error2;
1706 if (status == QF_NOMEM)
1707 goto qf_init_end;
1708 if (status == QF_IGNORE_LINE)
1709 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001711 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001712 qf_idx,
1713 qfl->qf_directory,
1714 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001715 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001716 : ((qfl->qf_currfile != NULL && fields.valid)
1717 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001718 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001719 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001720 fields.errmsg,
1721 fields.lnum,
1722 fields.col,
1723 fields.use_viscol,
1724 fields.pattern,
1725 fields.enr,
1726 fields.type,
1727 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 goto error2;
1729 line_breakcheck();
1730 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001731 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001733 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001735 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001736 qfl->qf_ptr = qfl->qf_start;
1737 qfl->qf_index = 1;
1738 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
1740 else
1741 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001742 qfl->qf_nonevalid = FALSE;
1743 if (qfl->qf_ptr == NULL)
1744 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001746 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001747 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001748 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
1750 EMSG(_(e_readerrf));
1751error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001752 if (!adding)
1753 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001754 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001755 qf_free(qi, qi->qf_curlist);
1756 qi->qf_listcount--;
1757 if (qi->qf_curlist > 0)
1758 --qi->qf_curlist;
1759 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001760qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001761 if (qf_idx == qi->qf_curlist)
1762 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001763 qf_cleanup_state(&state);
1764 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
1766 return retval;
1767}
1768
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001769/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001770 * Read the errorfile "efile" into memory, line by line, building the error
1771 * list. Set the error list's title to qf_title.
1772 * Return -1 for error, number of errors for success.
1773 */
1774 int
1775qf_init(win_T *wp,
1776 char_u *efile,
1777 char_u *errorformat,
1778 int newlist, /* TRUE: start a new error list */
1779 char_u *qf_title,
1780 char_u *enc)
1781{
1782 qf_info_T *qi = &ql_info;
1783
1784 if (wp != NULL)
1785 {
1786 qi = ll_get_or_alloc_list(wp);
1787 if (qi == NULL)
1788 return FAIL;
1789 }
1790
1791 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1792 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1793}
1794
1795/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001796 * Set the title of the specified quickfix list. Frees the previous title.
1797 * Prepends ':' to the title.
1798 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001799 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001800qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001801{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001802 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001803
Bram Moolenaarfb604092014-07-23 15:55:00 +02001804 if (title != NULL)
1805 {
1806 char_u *p = alloc((int)STRLEN(title) + 2);
1807
Bram Moolenaara3921f42017-06-04 15:30:34 +02001808 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001809 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001810 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001811 }
1812}
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001815 * The title of a quickfix/location list is set, by default, to the command
1816 * that created the quickfix list with the ":" prefix.
1817 * Create a quickfix list title string by prepending ":" to a user command.
1818 * Returns a pointer to a static buffer with the title.
1819 */
1820 static char_u *
1821qf_cmdtitle(char_u *cmd)
1822{
1823 static char_u qftitle_str[IOSIZE];
1824
1825 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1826 return qftitle_str;
1827}
1828
1829/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001830 * Prepare for adding a new quickfix list. If the current list is in the
1831 * middle of the stack, then all the following lists are freed and then
1832 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 */
1834 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001835qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836{
1837 int i;
1838
1839 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001840 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 * the current entry. This makes it possible to browse in a tree-like
1842 * way with ":grep'.
1843 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001844 while (qi->qf_listcount > qi->qf_curlist + 1)
1845 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
1847 /*
1848 * When the stack is full, remove to oldest entry
1849 * Otherwise, add a new entry.
1850 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001851 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001853 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001855 qi->qf_lists[i - 1] = qi->qf_lists[i];
1856 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001859 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001860 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001861 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001862 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863}
1864
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001865/*
1866 * Free a location list
1867 */
1868 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001869ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001870{
1871 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001872 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001873
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001874 qi = *pqi;
1875 if (qi == NULL)
1876 return;
1877 *pqi = NULL; /* Remove reference to this list */
1878
1879 qi->qf_refcount--;
1880 if (qi->qf_refcount < 1)
1881 {
1882 /* No references to this location list */
1883 for (i = 0; i < qi->qf_listcount; ++i)
1884 qf_free(qi, i);
1885 vim_free(qi);
1886 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001887}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001888
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001889/*
1890 * Free all the quickfix/location lists in the stack.
1891 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001892 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001893qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001894{
1895 int i;
1896 qf_info_T *qi = &ql_info;
1897
1898 if (wp != NULL)
1899 {
1900 /* location list */
1901 ll_free_all(&wp->w_llist);
1902 ll_free_all(&wp->w_llist_ref);
1903 }
1904 else
1905 /* quickfix list */
1906 for (i = 0; i < qi->qf_listcount; ++i)
1907 qf_free(qi, i);
1908}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910/*
1911 * Add an entry to the end of the list of errors.
1912 * Returns OK or FAIL.
1913 */
1914 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001915qf_add_entry(
1916 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001917 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001918 char_u *dir, /* optional directory name */
1919 char_u *fname, /* file name or NULL */
Bram Moolenaard76ce852018-05-01 15:02:04 +02001920 char_u *module, /* module name or NULL */
Bram Moolenaar05540972016-01-30 20:31:25 +01001921 int bufnum, /* buffer number or zero */
1922 char_u *mesg, /* message */
1923 long lnum, /* line number */
1924 int col, /* column */
1925 int vis_col, /* using visual column */
1926 char_u *pattern, /* search pattern */
1927 int nr, /* error number */
1928 int type, /* type character */
1929 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001931 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001932 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001934 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001936 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001937 {
1938 buf_T *buf = buflist_findnr(bufnum);
1939
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001940 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001941 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001942 buf->b_has_qf_entry |=
1943 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001944 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001945 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001946 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1948 {
1949 vim_free(qfp);
1950 return FAIL;
1951 }
1952 qfp->qf_lnum = lnum;
1953 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001954 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001955 if (pattern == NULL || *pattern == NUL)
1956 qfp->qf_pattern = NULL;
1957 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1958 {
1959 vim_free(qfp->qf_text);
1960 vim_free(qfp);
1961 return FAIL;
1962 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001963 if (module == NULL || *module == NUL)
1964 qfp->qf_module = NULL;
1965 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
1966 {
1967 vim_free(qfp->qf_text);
1968 vim_free(qfp->qf_pattern);
1969 vim_free(qfp);
1970 return FAIL;
1971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 qfp->qf_nr = nr;
1973 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1974 type = 0;
1975 qfp->qf_type = type;
1976 qfp->qf_valid = valid;
1977
Bram Moolenaara3921f42017-06-04 15:30:34 +02001978 lastp = &qi->qf_lists[qf_idx].qf_last;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001979 if (qf_list_empty(qi, qf_idx)) /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001981 qi->qf_lists[qf_idx].qf_start = qfp;
1982 qi->qf_lists[qf_idx].qf_ptr = qfp;
1983 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001984 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 }
1986 else
1987 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001988 qfp->qf_prev = *lastp;
1989 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001991 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001993 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001994 ++qi->qf_lists[qf_idx].qf_count;
1995 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001996 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001998 qi->qf_lists[qf_idx].qf_index =
1999 qi->qf_lists[qf_idx].qf_count;
2000 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
2002
2003 return OK;
2004}
2005
2006/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002007 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002008 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002009 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002010ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002011{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002012 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002013
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002014 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002015 if (qi != NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002016 qi->qf_refcount++;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002017 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002018}
2019
2020/*
2021 * Return the location list for window 'wp'.
2022 * If not present, allocate a location list
2023 */
2024 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002025ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002026{
2027 if (IS_LL_WINDOW(wp))
2028 /* For a location list window, use the referenced location list */
2029 return wp->w_llist_ref;
2030
2031 /*
2032 * For a non-location list window, w_llist_ref should not point to a
2033 * location list.
2034 */
2035 ll_free_all(&wp->w_llist_ref);
2036
2037 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002038 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002039 return wp->w_llist;
2040}
2041
2042/*
2043 * Copy the location list from window "from" to window "to".
2044 */
2045 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002046copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002047{
2048 qf_info_T *qi;
2049 int idx;
2050 int i;
2051
2052 /*
2053 * When copying from a location list window, copy the referenced
2054 * location list. For other windows, copy the location list for
2055 * that window.
2056 */
2057 if (IS_LL_WINDOW(from))
2058 qi = from->w_llist_ref;
2059 else
2060 qi = from->w_llist;
2061
2062 if (qi == NULL) /* no location list to copy */
2063 return;
2064
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002065 /* allocate a new location list */
2066 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002067 return;
2068
2069 to->w_llist->qf_listcount = qi->qf_listcount;
2070
2071 /* Copy the location lists one at a time */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002072 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002073 {
2074 qf_list_T *from_qfl;
2075 qf_list_T *to_qfl;
2076
2077 to->w_llist->qf_curlist = idx;
2078
2079 from_qfl = &qi->qf_lists[idx];
2080 to_qfl = &to->w_llist->qf_lists[idx];
2081
2082 /* Some of the fields are populated by qf_add_entry() */
2083 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2084 to_qfl->qf_count = 0;
2085 to_qfl->qf_index = 0;
2086 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002087 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002088 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002089 if (from_qfl->qf_title != NULL)
2090 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2091 else
2092 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002093 if (from_qfl->qf_ctx != NULL)
2094 {
2095 to_qfl->qf_ctx = alloc_tv();
2096 if (to_qfl->qf_ctx != NULL)
2097 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2098 }
2099 else
2100 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002101
2102 if (from_qfl->qf_count)
2103 {
2104 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002105 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002106
2107 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002108 for (i = 0, from_qfp = from_qfl->qf_start;
2109 i < from_qfl->qf_count && from_qfp != NULL;
2110 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002111 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002112 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02002113 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002114 NULL,
2115 NULL,
Bram Moolenaard76ce852018-05-01 15:02:04 +02002116 from_qfp->qf_module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002117 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002118 from_qfp->qf_text,
2119 from_qfp->qf_lnum,
2120 from_qfp->qf_col,
2121 from_qfp->qf_viscol,
2122 from_qfp->qf_pattern,
2123 from_qfp->qf_nr,
2124 0,
2125 from_qfp->qf_valid) == FAIL)
2126 {
2127 qf_free_all(to);
2128 return;
2129 }
2130 /*
2131 * qf_add_entry() will not set the qf_num field, as the
2132 * directory and file names are not supplied. So the qf_fnum
2133 * field is copied here.
2134 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002135 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002136 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
2137 prevp->qf_type = from_qfp->qf_type; /* error type */
2138 if (from_qfl->qf_ptr == from_qfp)
2139 to_qfl->qf_ptr = prevp; /* current location */
2140 }
2141 }
2142
2143 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
2144
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002145 /* Assign a new ID for the location list */
2146 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01002147 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002148
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002149 /* When no valid entries are present in the list, qf_ptr points to
2150 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02002151 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002152 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002153 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002154 to_qfl->qf_index = 1;
2155 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002156 }
2157
2158 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
2159}
2160
2161/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002162 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002163 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 */
2165 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002166qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167{
Bram Moolenaar82404332016-07-10 17:00:38 +02002168 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002169 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002170 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002171
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 if (fname == NULL || *fname == NUL) /* no file name */
2173 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
Bram Moolenaare60acc12011-05-10 16:41:25 +02002175#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002176 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002177#endif
2178#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002179 if (directory != NULL)
2180 slash_adjust(directory);
2181 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002182#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002183 if (directory != NULL && !vim_isAbsName(fname)
2184 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2185 {
2186 /*
2187 * Here we check if the file really exists.
2188 * This should normally be true, but if make works without
2189 * "leaving directory"-messages we might have missed a
2190 * directory change.
2191 */
2192 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002195 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002196 if (directory)
2197 ptr = concat_fnames(directory, fname, TRUE);
2198 else
2199 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002201 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02002202 bufname = ptr;
2203 }
2204 else
2205 bufname = fname;
2206
2207 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002208 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002209 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002210 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002211 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002213 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002214 {
2215 vim_free(qf_last_bufname);
2216 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2217 if (bufname == ptr)
2218 qf_last_bufname = bufname;
2219 else
2220 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002221 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002222 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002223 if (buf == NULL)
2224 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002225
Bram Moolenaarc1542742016-07-20 21:44:37 +02002226 buf->b_has_qf_entry =
2227 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002228 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229}
2230
2231/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002232 * Push dirbuf onto the directory stack and return pointer to actual dir or
2233 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 */
2235 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002236qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237{
2238 struct dir_stack_T *ds_new;
2239 struct dir_stack_T *ds_ptr;
2240
2241 /* allocate new stack element and hook it in */
2242 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2243 if (ds_new == NULL)
2244 return NULL;
2245
2246 ds_new->next = *stackptr;
2247 *stackptr = ds_new;
2248
2249 /* store directory on the stack */
2250 if (vim_isAbsName(dirbuf)
2251 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002252 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 (*stackptr)->dirname = vim_strsave(dirbuf);
2254 else
2255 {
2256 /* Okay we don't have an absolute path.
2257 * dirbuf must be a subdir of one of the directories on the stack.
2258 * Let's search...
2259 */
2260 ds_new = (*stackptr)->next;
2261 (*stackptr)->dirname = NULL;
2262 while (ds_new)
2263 {
2264 vim_free((*stackptr)->dirname);
2265 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2266 TRUE);
2267 if (mch_isdir((*stackptr)->dirname) == TRUE)
2268 break;
2269
2270 ds_new = ds_new->next;
2271 }
2272
2273 /* clean up all dirs we already left */
2274 while ((*stackptr)->next != ds_new)
2275 {
2276 ds_ptr = (*stackptr)->next;
2277 (*stackptr)->next = (*stackptr)->next->next;
2278 vim_free(ds_ptr->dirname);
2279 vim_free(ds_ptr);
2280 }
2281
2282 /* Nothing found -> it must be on top level */
2283 if (ds_new == NULL)
2284 {
2285 vim_free((*stackptr)->dirname);
2286 (*stackptr)->dirname = vim_strsave(dirbuf);
2287 }
2288 }
2289
2290 if ((*stackptr)->dirname != NULL)
2291 return (*stackptr)->dirname;
2292 else
2293 {
2294 ds_ptr = *stackptr;
2295 *stackptr = (*stackptr)->next;
2296 vim_free(ds_ptr);
2297 return NULL;
2298 }
2299}
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301/*
2302 * pop dirbuf from the directory stack and return previous directory or NULL if
2303 * stack is empty
2304 */
2305 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002306qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
2308 struct dir_stack_T *ds_ptr;
2309
2310 /* TODO: Should we check if dirbuf is the directory on top of the stack?
2311 * What to do if it isn't? */
2312
2313 /* pop top element and free it */
2314 if (*stackptr != NULL)
2315 {
2316 ds_ptr = *stackptr;
2317 *stackptr = (*stackptr)->next;
2318 vim_free(ds_ptr->dirname);
2319 vim_free(ds_ptr);
2320 }
2321
2322 /* return NEW top element as current dir or NULL if stack is empty*/
2323 return *stackptr ? (*stackptr)->dirname : NULL;
2324}
2325
2326/*
2327 * clean up directory stack
2328 */
2329 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002330qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331{
2332 struct dir_stack_T *ds_ptr;
2333
2334 while ((ds_ptr = *stackptr) != NULL)
2335 {
2336 *stackptr = (*stackptr)->next;
2337 vim_free(ds_ptr->dirname);
2338 vim_free(ds_ptr);
2339 }
2340}
2341
2342/*
2343 * Check in which directory of the directory stack the given file can be
2344 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002345 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 * Cleans up intermediate directory entries.
2347 *
2348 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002349 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 * ./
2351 * ./aa
2352 * ./aa/bb
2353 * ./bb
2354 * ./bb/x.c
2355 * and make says:
2356 * making all in aa
2357 * making all in bb
2358 * x.c:9: Error
2359 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2360 * qf_guess_filepath will return NULL.
2361 */
2362 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002363qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364{
2365 struct dir_stack_T *ds_ptr;
2366 struct dir_stack_T *ds_tmp;
2367 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002368 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002371 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 return NULL;
2373
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002374 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 fullname = NULL;
2376 while (ds_ptr)
2377 {
2378 vim_free(fullname);
2379 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2380
2381 /* If concat_fnames failed, just go on. The worst thing that can happen
2382 * is that we delete the entire stack.
2383 */
2384 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2385 break;
2386
2387 ds_ptr = ds_ptr->next;
2388 }
2389
2390 vim_free(fullname);
2391
2392 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002393 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002395 ds_tmp = qfl->qf_dir_stack->next;
2396 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 vim_free(ds_tmp->dirname);
2398 vim_free(ds_tmp);
2399 }
2400
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002401 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402}
2403
2404/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002405 * Returns TRUE if a quickfix/location list with the given identifier exists.
2406 */
2407 static int
2408qflist_valid (win_T *wp, int_u qf_id)
2409{
2410 qf_info_T *qi = &ql_info;
2411 int i;
2412
2413 if (wp != NULL)
2414 {
2415 qi = GET_LOC_LIST(wp); /* Location list */
2416 if (qi == NULL)
2417 return FALSE;
2418 }
2419
2420 for (i = 0; i < qi->qf_listcount; ++i)
2421 if (qi->qf_lists[i].qf_id == qf_id)
2422 return TRUE;
2423
2424 return FALSE;
2425}
2426
2427/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002428 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002429 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002430 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002431 * Similar to location list.
2432 */
2433 static int
2434is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
2435{
2436 qf_list_T *qfl;
2437 qfline_T *qfp;
2438 int i;
2439
2440 qfl = &qi->qf_lists[qi->qf_curlist];
2441
2442 /* Search for the entry in the current list */
2443 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2444 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002445 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002446 break;
2447
2448 if (i == qfl->qf_count) /* Entry is not found */
2449 return FALSE;
2450
2451 return TRUE;
2452}
2453
2454/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002455 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002456 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002457 */
2458 static qfline_T *
2459get_next_valid_entry(
2460 qf_info_T *qi,
2461 qfline_T *qf_ptr,
2462 int *qf_index,
2463 int dir)
2464{
2465 int idx;
2466 int old_qf_fnum;
2467
2468 idx = *qf_index;
2469 old_qf_fnum = qf_ptr->qf_fnum;
2470
2471 do
2472 {
2473 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2474 || qf_ptr->qf_next == NULL)
2475 return NULL;
2476 ++idx;
2477 qf_ptr = qf_ptr->qf_next;
2478 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2479 && !qf_ptr->qf_valid)
2480 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2481
2482 *qf_index = idx;
2483 return qf_ptr;
2484}
2485
2486/*
2487 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002488 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002489 */
2490 static qfline_T *
2491get_prev_valid_entry(
2492 qf_info_T *qi,
2493 qfline_T *qf_ptr,
2494 int *qf_index,
2495 int dir)
2496{
2497 int idx;
2498 int old_qf_fnum;
2499
2500 idx = *qf_index;
2501 old_qf_fnum = qf_ptr->qf_fnum;
2502
2503 do
2504 {
2505 if (idx == 1 || qf_ptr->qf_prev == NULL)
2506 return NULL;
2507 --idx;
2508 qf_ptr = qf_ptr->qf_prev;
2509 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2510 && !qf_ptr->qf_valid)
2511 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2512
2513 *qf_index = idx;
2514 return qf_ptr;
2515}
2516
2517/*
2518 * Get the n'th (errornr) previous/next valid entry from the current entry in
2519 * the quickfix list.
2520 * dir == FORWARD or FORWARD_FILE: next valid entry
2521 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2522 */
2523 static qfline_T *
2524get_nth_valid_entry(
2525 qf_info_T *qi,
2526 int errornr,
2527 qfline_T *qf_ptr,
2528 int *qf_index,
2529 int dir)
2530{
2531 qfline_T *prev_qf_ptr;
2532 int prev_index;
2533 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2534 char_u *err = e_no_more_items;
2535
2536 while (errornr--)
2537 {
2538 prev_qf_ptr = qf_ptr;
2539 prev_index = *qf_index;
2540
2541 if (dir == FORWARD || dir == FORWARD_FILE)
2542 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2543 else
2544 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2545 if (qf_ptr == NULL)
2546 {
2547 qf_ptr = prev_qf_ptr;
2548 *qf_index = prev_index;
2549 if (err != NULL)
2550 {
2551 EMSG(_(err));
2552 return NULL;
2553 }
2554 break;
2555 }
2556
2557 err = NULL;
2558 }
2559
2560 return qf_ptr;
2561}
2562
2563/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002564 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002565 */
2566 static qfline_T *
2567get_nth_entry(
2568 qf_info_T *qi,
2569 int errornr,
2570 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002571 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002572{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002573 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002574
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002575 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002576 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2577 {
2578 --qf_idx;
2579 qf_ptr = qf_ptr->qf_prev;
2580 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002581 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002582 while (errornr > qf_idx &&
2583 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2584 qf_ptr->qf_next != NULL)
2585 {
2586 ++qf_idx;
2587 qf_ptr = qf_ptr->qf_next;
2588 }
2589
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002590 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002591 return qf_ptr;
2592}
2593
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002594/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002595 * Find a window displaying a Vim help file.
2596 */
2597 static win_T *
2598qf_find_help_win(void)
2599{
2600 win_T *wp;
2601
2602 FOR_ALL_WINDOWS(wp)
2603 if (bt_help(wp->w_buffer))
2604 return wp;
2605
2606 return NULL;
2607}
2608
2609/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002610 * Find a help window or open one.
2611 */
2612 static int
2613jump_to_help_window(qf_info_T *qi, int *opened_window)
2614{
2615 win_T *wp;
2616 int flags;
2617
2618 if (cmdmod.tab != 0)
2619 wp = NULL;
2620 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002621 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002622 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2623 win_enter(wp, TRUE);
2624 else
2625 {
2626 /*
2627 * Split off help window; put it at far top if no position
2628 * specified, the current window is vertically split and narrow.
2629 */
2630 flags = WSP_HELP;
2631 if (cmdmod.split == 0 && curwin->w_width != Columns
2632 && curwin->w_width < 80)
2633 flags |= WSP_TOP;
2634 if (qi != &ql_info)
2635 flags |= WSP_NEWLOC; /* don't copy the location list */
2636
2637 if (win_split(0, flags) == FAIL)
2638 return FAIL;
2639
2640 *opened_window = TRUE;
2641
2642 if (curwin->w_height < p_hh)
2643 win_setheight((int)p_hh);
2644
2645 if (qi != &ql_info) /* not a quickfix list */
2646 {
2647 /* The new window should use the supplied location list */
2648 curwin->w_llist = qi;
2649 qi->qf_refcount++;
2650 }
2651 }
2652
2653 if (!p_im)
2654 restart_edit = 0; /* don't want insert mode in help file */
2655
2656 return OK;
2657}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002658
2659/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002660 * Find a non-quickfix window using the given location list.
2661 * Returns NULL if a matching window is not found.
2662 */
2663 static win_T *
2664qf_find_win_with_loclist(qf_info_T *ll)
2665{
2666 win_T *wp;
2667
2668 FOR_ALL_WINDOWS(wp)
2669 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2670 return wp;
2671
2672 return NULL;
2673}
2674
2675/*
2676 * Find a window containing a normal buffer
2677 */
2678 static win_T *
2679qf_find_win_with_normal_buf(void)
2680{
2681 win_T *wp;
2682
2683 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002684 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002685 return wp;
2686
2687 return NULL;
2688}
2689
2690/*
2691 * Go to a window in any tabpage containing the specified file. Returns TRUE
2692 * if successfully jumped to the window. Otherwise returns FALSE.
2693 */
2694 static int
2695qf_goto_tabwin_with_file(int fnum)
2696{
2697 tabpage_T *tp;
2698 win_T *wp;
2699
2700 FOR_ALL_TAB_WINDOWS(tp, wp)
2701 if (wp->w_buffer->b_fnum == fnum)
2702 {
2703 goto_tabpage_win(tp, wp);
2704 return TRUE;
2705 }
2706
2707 return FALSE;
2708}
2709
2710/*
2711 * Create a new window to show a file above the quickfix window. Called when
2712 * only the quickfix window is present.
2713 */
2714 static int
2715qf_open_new_file_win(qf_info_T *ll_ref)
2716{
2717 int flags;
2718
2719 flags = WSP_ABOVE;
2720 if (ll_ref != NULL)
2721 flags |= WSP_NEWLOC;
2722 if (win_split(0, flags) == FAIL)
2723 return FAIL; /* not enough room for window */
2724 p_swb = empty_option; /* don't split again */
2725 swb_flags = 0;
2726 RESET_BINDING(curwin);
2727 if (ll_ref != NULL)
2728 {
2729 /* The new window should use the location list from the
2730 * location list window */
2731 curwin->w_llist = ll_ref;
2732 ll_ref->qf_refcount++;
2733 }
2734 return OK;
2735}
2736
2737/*
2738 * Go to a window that shows the right buffer. If the window is not found, go
2739 * to the window just above the location list window. This is used for opening
2740 * a file from a location window and not from a quickfix window. If some usable
2741 * window is previously found, then it is supplied in 'use_win'.
2742 */
2743 static void
2744qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2745{
2746 win_T *win = use_win;
2747
2748 if (win == NULL)
2749 {
2750 /* Find the window showing the selected file */
2751 FOR_ALL_WINDOWS(win)
2752 if (win->w_buffer->b_fnum == qf_fnum)
2753 break;
2754 if (win == NULL)
2755 {
2756 /* Find a previous usable window */
2757 win = curwin;
2758 do
2759 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002760 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002761 break;
2762 if (win->w_prev == NULL)
2763 win = lastwin; /* wrap around the top */
2764 else
2765 win = win->w_prev; /* go to previous window */
2766 } while (win != curwin);
2767 }
2768 }
2769 win_goto(win);
2770
2771 /* If the location list for the window is not set, then set it
2772 * to the location list from the location window */
2773 if (win->w_llist == NULL)
2774 {
2775 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002776 if (ll_ref != NULL)
2777 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002778 }
2779}
2780
2781/*
2782 * Go to a window that shows the specified file. If a window is not found, go
2783 * to the window just above the quickfix window. This is used for opening a
2784 * file from a quickfix window and not from a location window.
2785 */
2786 static void
2787qf_goto_win_with_qfl_file(int qf_fnum)
2788{
2789 win_T *win;
2790 win_T *altwin;
2791
2792 win = curwin;
2793 altwin = NULL;
2794 for (;;)
2795 {
2796 if (win->w_buffer->b_fnum == qf_fnum)
2797 break;
2798 if (win->w_prev == NULL)
2799 win = lastwin; /* wrap around the top */
2800 else
2801 win = win->w_prev; /* go to previous window */
2802
2803 if (IS_QF_WINDOW(win))
2804 {
2805 /* Didn't find it, go to the window before the quickfix
2806 * window. */
2807 if (altwin != NULL)
2808 win = altwin;
2809 else if (curwin->w_prev != NULL)
2810 win = curwin->w_prev;
2811 else
2812 win = curwin->w_next;
2813 break;
2814 }
2815
2816 /* Remember a usable window. */
Bram Moolenaar91335e52018-08-01 17:53:12 +02002817 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002818 altwin = win;
2819 }
2820
2821 win_goto(win);
2822}
2823
2824/*
2825 * Find a suitable window for opening a file (qf_fnum) from the
2826 * quickfix/location list and jump to it. If the file is already opened in a
2827 * window, jump to it. Otherwise open a new window to display the file. This is
2828 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002829 */
2830 static int
2831qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2832{
2833 win_T *usable_win_ptr = NULL;
2834 int usable_win;
2835 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002836 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002837
2838 usable_win = 0;
2839
2840 ll_ref = curwin->w_llist_ref;
2841 if (ll_ref != NULL)
2842 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002843 /* Find a non-quickfix window with this location list */
2844 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2845 if (usable_win_ptr != NULL)
2846 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002847 }
2848
2849 if (!usable_win)
2850 {
2851 /* Locate a window showing a normal buffer */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002852 win = qf_find_win_with_normal_buf();
2853 if (win != NULL)
2854 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002855 }
2856
2857 /*
2858 * If no usable window is found and 'switchbuf' contains "usetab"
2859 * then search in other tabs.
2860 */
2861 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002862 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002863
2864 /*
2865 * If there is only one window and it is the quickfix window, create a
2866 * new one above the quickfix window.
2867 */
2868 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2869 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002870 if (qf_open_new_file_win(ll_ref) != OK)
2871 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002872 *opened_window = TRUE; /* close it when fail */
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002873 }
2874 else
2875 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002876 if (curwin->w_llist_ref != NULL) /* In a location window */
2877 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
2878 else /* In a quickfix window */
2879 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002880 }
2881
2882 return OK;
2883}
2884
2885/*
2886 * Edit the selected file or help file.
2887 */
2888 static int
2889qf_jump_edit_buffer(
2890 qf_info_T *qi,
2891 qfline_T *qf_ptr,
2892 int forceit,
2893 win_T *oldwin,
2894 int *opened_window,
2895 int *abort)
2896{
2897 int retval = OK;
2898
2899 if (qf_ptr->qf_type == 1)
2900 {
2901 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2902 * set b_p_ro flag). */
2903 if (!can_abandon(curbuf, forceit))
2904 {
2905 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002906 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002907 }
2908 else
2909 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2910 ECMD_HIDE + ECMD_SET_HELP,
2911 oldwin == curwin ? curwin : NULL);
2912 }
2913 else
2914 {
2915 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002916 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002917
2918 retval = buflist_getfile(qf_ptr->qf_fnum,
2919 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002920
2921 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002922 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002923 /*
2924 * Location list. Check whether the associated window is still
2925 * present and the list is still valid.
2926 */
2927 if (!win_valid_any_tab(oldwin))
2928 {
2929 EMSG(_("E924: Current window was closed"));
2930 *abort = TRUE;
2931 *opened_window = FALSE;
2932 }
2933 else if (!qflist_valid(oldwin, save_qfid))
2934 {
2935 EMSG(_(e_loc_list_changed));
2936 *abort = TRUE;
2937 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002938 }
2939 else if (old_qf_curlist != qi->qf_curlist
2940 || !is_qf_entry_present(qi, qf_ptr))
2941 {
2942 if (qi == &ql_info)
2943 EMSG(_("E925: Current quickfix was changed"));
2944 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002945 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002946 *abort = TRUE;
2947 }
2948
2949 if (*abort)
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002950 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002951 }
2952
2953 return retval;
2954}
2955
2956/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002957 * Go to the error line in the current file using either line/column number or
2958 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002959 */
2960 static void
2961qf_jump_goto_line(
2962 linenr_T qf_lnum,
2963 int qf_col,
2964 char_u qf_viscol,
2965 char_u *qf_pattern)
2966{
2967 linenr_T i;
2968 char_u *line;
2969 colnr_T screen_col;
2970 colnr_T char_col;
2971
2972 if (qf_pattern == NULL)
2973 {
2974 /*
2975 * Go to line with error, unless qf_lnum is 0.
2976 */
2977 i = qf_lnum;
2978 if (i > 0)
2979 {
2980 if (i > curbuf->b_ml.ml_line_count)
2981 i = curbuf->b_ml.ml_line_count;
2982 curwin->w_cursor.lnum = i;
2983 }
2984 if (qf_col > 0)
2985 {
2986 curwin->w_cursor.col = qf_col - 1;
2987#ifdef FEAT_VIRTUALEDIT
2988 curwin->w_cursor.coladd = 0;
2989#endif
2990 if (qf_viscol == TRUE)
2991 {
2992 /*
2993 * Check each character from the beginning of the error
2994 * line up to the error column. For each tab character
2995 * found, reduce the error column value by the length of
2996 * a tab character.
2997 */
2998 line = ml_get_curline();
2999 screen_col = 0;
3000 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3001 {
3002 if (*line == NUL)
3003 break;
3004 if (*line++ == '\t')
3005 {
3006 curwin->w_cursor.col -= 7 - (screen_col % 8);
3007 screen_col += 8 - (screen_col % 8);
3008 }
3009 else
3010 ++screen_col;
3011 }
3012 }
3013 check_cursor();
3014 }
3015 else
3016 beginline(BL_WHITE | BL_FIX);
3017 }
3018 else
3019 {
3020 pos_T save_cursor;
3021
3022 /* Move the cursor to the first line in the buffer */
3023 save_cursor = curwin->w_cursor;
3024 curwin->w_cursor.lnum = 0;
3025 if (!do_search(NULL, '/', qf_pattern, (long)1,
3026 SEARCH_KEEP, NULL, NULL))
3027 curwin->w_cursor = save_cursor;
3028 }
3029}
3030
3031/*
3032 * Display quickfix list index and size message
3033 */
3034 static void
3035qf_jump_print_msg(
3036 qf_info_T *qi,
3037 int qf_index,
3038 qfline_T *qf_ptr,
3039 buf_T *old_curbuf,
3040 linenr_T old_lnum)
3041{
3042 linenr_T i;
3043 int len;
3044
3045 /* Update the screen before showing the message, unless the screen
3046 * scrolled up. */
3047 if (!msg_scrolled)
3048 update_topline_redraw();
3049 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3050 qi->qf_lists[qi->qf_curlist].qf_count,
3051 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3052 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
3053 /* Add the message, skipping leading whitespace and newlines. */
3054 len = (int)STRLEN(IObuff);
3055 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3056
3057 /* Output the message. Overwrite to avoid scrolling when the 'O'
3058 * flag is present in 'shortmess'; But when not jumping, print the
3059 * whole message. */
3060 i = msg_scroll;
3061 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3062 msg_scroll = TRUE;
3063 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3064 msg_scroll = FALSE;
3065 msg_attr_keep(IObuff, 0, TRUE);
3066 msg_scroll = i;
3067}
3068
3069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 * jump to a quickfix line
3071 * if dir == FORWARD go "errornr" valid entries forward
3072 * if dir == BACKWARD go "errornr" valid entries backward
3073 * if dir == FORWARD_FILE go "errornr" valid entries files backward
3074 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
3075 * else if "errornr" is zero, redisplay the same line
3076 * else go to entry "errornr"
3077 */
3078 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003079qf_jump(qf_info_T *qi,
3080 int dir,
3081 int errornr,
3082 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003084 qfline_T *qf_ptr;
3085 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 buf_T *old_curbuf;
3089 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003090 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003091 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003093 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#ifdef FEAT_FOLDING
3096 int old_KeyTyped = KeyTyped; /* getting file may reset it */
3097#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003098 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003100 if (qi == NULL)
3101 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003102
3103 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003104 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 {
3106 EMSG(_(e_quickfix));
3107 return;
3108 }
3109
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003110 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003112 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02003114 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003116 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
3117 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003119 qf_ptr = old_qf_ptr;
3120 qf_index = old_qf_index;
3121 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 }
3123 }
3124 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003125 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003127 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
3128 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 /* No need to print the error message if it's visible in the error
3130 * window */
3131 print_message = FALSE;
3132
3133 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003134 * For ":helpgrep" find a help window or open one.
3135 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003136 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003137 if (jump_to_help_window(qi, &opened_window) == FAIL)
3138 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003139
3140 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 * If currently in the quickfix window, find another window to show the
3142 * file in.
3143 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003144 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 {
3146 /*
3147 * If there is no file specified, we don't know where to go.
3148 * But do advance, otherwise ":cn" gets stuck.
3149 */
3150 if (qf_ptr->qf_fnum == 0)
3151 goto theend;
3152
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003153 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
3154 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
3157 /*
3158 * If there is a file name,
3159 * read the wanted file if needed, and check autowrite etc.
3160 */
3161 old_curbuf = curbuf;
3162 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003163
3164 if (qf_ptr->qf_fnum != 0)
3165 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003166 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003167
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003168 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3169 &opened_window, &abort);
3170 if (abort)
3171 {
3172 qi = NULL;
3173 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003174 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003175 }
3176
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003177 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 {
3179 /* When not switched to another buffer, still need to set pc mark */
3180 if (curbuf == old_curbuf)
3181 setpcmark();
3182
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003183 if (qf_ptr != NULL)
3184 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col,
3185 qf_ptr->qf_viscol, qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
3187#ifdef FEAT_FOLDING
3188 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
3189 foldOpenCursor();
3190#endif
3191 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003192 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 }
3194 else
3195 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 if (opened_window)
3197 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01003198 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 {
3200 /*
3201 * Couldn't open file, so put index back where it was. This could
3202 * happen if the file was readonly and we changed something.
3203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 qf_ptr = old_qf_ptr;
3206 qf_index = old_qf_index;
3207 }
3208 }
3209theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003210 if (qi != NULL)
3211 {
3212 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
3213 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
3214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 if (p_swb != old_swb && opened_window)
3216 {
3217 /* Restore old 'switchbuf' value, but not when an autocommand or
3218 * modeline has changed the value. */
3219 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003222 swb_flags = old_swb_flags;
3223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 else
3225 free_string_option(old_swb);
3226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227}
3228
3229/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003230 * Highlight attributes used for displaying entries from the quickfix list.
3231 */
3232static int qfFileAttr;
3233static int qfSepAttr;
3234static int qfLineAttr;
3235
3236/*
3237 * Display information about a single entry from the quickfix/location list.
3238 * Used by ":clist/:llist" commands.
3239 */
3240 static void
3241qf_list_entry(qf_info_T *qi, qfline_T *qfp, int qf_idx)
3242{
3243 char_u *fname;
3244 buf_T *buf;
3245 int filter_entry;
3246
3247 fname = NULL;
3248 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3249 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3250 (char *)qfp->qf_module);
3251 else {
3252 if (qfp->qf_fnum != 0
3253 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3254 {
3255 fname = buf->b_fname;
3256 if (qfp->qf_type == 1) /* :helpgrep */
3257 fname = gettail(fname);
3258 }
3259 if (fname == NULL)
3260 sprintf((char *)IObuff, "%2d", qf_idx);
3261 else
3262 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3263 qf_idx, (char *)fname);
3264 }
3265
3266 // Support for filtering entries using :filter /pat/ clist
3267 // Match against the module name, file name, search pattern and
3268 // text of the entry.
3269 filter_entry = TRUE;
3270 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3271 filter_entry &= message_filtered(qfp->qf_module);
3272 if (filter_entry && fname != NULL)
3273 filter_entry &= message_filtered(fname);
3274 if (filter_entry && qfp->qf_pattern != NULL)
3275 filter_entry &= message_filtered(qfp->qf_pattern);
3276 if (filter_entry)
3277 filter_entry &= message_filtered(qfp->qf_text);
3278 if (filter_entry)
3279 return;
3280
3281 msg_putchar('\n');
3282 msg_outtrans_attr(IObuff, qf_idx == qi->qf_lists[qi->qf_curlist].qf_index
3283 ? HL_ATTR(HLF_QFL) : qfFileAttr);
3284
3285 if (qfp->qf_lnum != 0)
3286 msg_puts_attr((char_u *)":", qfSepAttr);
3287 if (qfp->qf_lnum == 0)
3288 IObuff[0] = NUL;
3289 else if (qfp->qf_col == 0)
3290 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3291 else
3292 sprintf((char *)IObuff, "%ld col %d",
3293 qfp->qf_lnum, qfp->qf_col);
3294 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3295 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3296 msg_puts_attr(IObuff, qfLineAttr);
3297 msg_puts_attr((char_u *)":", qfSepAttr);
3298 if (qfp->qf_pattern != NULL)
3299 {
3300 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3301 msg_puts(IObuff);
3302 msg_puts_attr((char_u *)":", qfSepAttr);
3303 }
3304 msg_puts((char_u *)" ");
3305
3306 /* Remove newlines and leading whitespace from the text. For an
3307 * unrecognized line keep the indent, the compiler may mark a word
3308 * with ^^^^. */
3309 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3310 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3311 IObuff, IOSIZE);
3312 msg_prt_line(IObuff, FALSE);
3313 out_flush(); /* show one line at a time */
3314}
3315
3316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003318 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 */
3320 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003321qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003323 qfline_T *qfp;
3324 int i;
3325 int idx1 = 1;
3326 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003327 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003328 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003329 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003331 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003333 if (eap->cmdidx == CMD_llist)
3334 {
3335 qi = GET_LOC_LIST(curwin);
3336 if (qi == NULL)
3337 {
3338 EMSG(_(e_loclist));
3339 return;
3340 }
3341 }
3342
3343 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003344 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 {
3346 EMSG(_(e_quickfix));
3347 return;
3348 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003349 if (*arg == '+')
3350 {
3351 ++arg;
3352 plus = TRUE;
3353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3355 {
3356 EMSG(_(e_trailing));
3357 return;
3358 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003359 if (plus)
3360 {
3361 i = qi->qf_lists[qi->qf_curlist].qf_index;
3362 idx2 = i + idx1;
3363 idx1 = i;
3364 }
3365 else
3366 {
3367 i = qi->qf_lists[qi->qf_curlist].qf_count;
3368 if (idx1 < 0)
3369 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3370 if (idx2 < 0)
3371 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
Bram Moolenaara796d462018-05-01 14:30:36 +02003374 /* Shorten all the file names, so that it is easy to read */
3375 shorten_fnames(FALSE);
3376
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003377 /*
3378 * Get the attributes for the different quickfix highlight items. Note
3379 * that this depends on syntax items defined in the qf.vim syntax file
3380 */
3381 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3382 if (qfFileAttr == 0)
3383 qfFileAttr = HL_ATTR(HLF_D);
3384 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3385 if (qfSepAttr == 0)
3386 qfSepAttr = HL_ATTR(HLF_D);
3387 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3388 if (qfLineAttr == 0)
3389 qfLineAttr = HL_ATTR(HLF_N);
3390
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003391 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003393 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3394 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 {
3396 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3397 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003398 if (got_int)
3399 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003400
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003401 qf_list_entry(qi, qfp, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003403
3404 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003405 if (qfp == NULL)
3406 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003407 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 ui_breakcheck();
3409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410}
3411
3412/*
3413 * Remove newlines and leading whitespace from an error message.
3414 * Put the result in "buf[bufsize]".
3415 */
3416 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003417qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 int i;
3420 char_u *p = text;
3421
3422 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3423 {
3424 if (*p == '\n')
3425 {
3426 buf[i] = ' ';
3427 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003428 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 break;
3430 }
3431 else
3432 buf[i] = *p++;
3433 }
3434 buf[i] = NUL;
3435}
3436
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003437/*
3438 * Display information (list number, list size and the title) about a
3439 * quickfix/location list.
3440 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003441 static void
3442qf_msg(qf_info_T *qi, int which, char *lead)
3443{
3444 char *title = (char *)qi->qf_lists[which].qf_title;
3445 int count = qi->qf_lists[which].qf_count;
3446 char_u buf[IOSIZE];
3447
3448 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3449 lead,
3450 which + 1,
3451 qi->qf_listcount,
3452 count);
3453
3454 if (title != NULL)
3455 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003456 size_t len = STRLEN(buf);
3457
3458 if (len < 34)
3459 {
3460 vim_memset(buf + len, ' ', 34 - len);
3461 buf[34] = NUL;
3462 }
3463 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003464 }
3465 trunc_string(buf, buf, Columns - 1, IOSIZE);
3466 msg(buf);
3467}
3468
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469/*
3470 * ":colder [count]": Up in the quickfix stack.
3471 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003472 * ":lolder [count]": Up in the location list stack.
3473 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 */
3475 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003476qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003478 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 int count;
3480
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003481 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
3482 {
3483 qi = GET_LOC_LIST(curwin);
3484 if (qi == NULL)
3485 {
3486 EMSG(_(e_loclist));
3487 return;
3488 }
3489 }
3490
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (eap->addr_count != 0)
3492 count = eap->line2;
3493 else
3494 count = 1;
3495 while (count--)
3496 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003497 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003499 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
3501 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003502 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003504 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
3506 else
3507 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003508 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 {
3510 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003511 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003513 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
3515 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003516 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003517 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518}
3519
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003520/*
3521 * Display the information about all the quickfix/location lists in the stack
3522 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003523 void
3524qf_history(exarg_T *eap)
3525{
3526 qf_info_T *qi = &ql_info;
3527 int i;
3528
3529 if (eap->cmdidx == CMD_lhistory)
3530 qi = GET_LOC_LIST(curwin);
3531 if (qi == NULL || (qi->qf_listcount == 0
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003532 && qf_list_empty(qi, qi->qf_curlist)))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003533 MSG(_("No entries"));
3534 else
3535 for (i = 0; i < qi->qf_listcount; ++i)
3536 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3537}
3538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003540 * Free all the entries in the error list "idx". Note that other information
3541 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 */
3543 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003544qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003546 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003547 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003548 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003549 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003551 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003553 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003554 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003555 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003556 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003557 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003558 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003559 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003560 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003561 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003562 if (stop)
3563 /* Somehow qf_count may have an incorrect value, set it to 1
3564 * to avoid crashing when it's wrong.
3565 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003566 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003567 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003568 qfl->qf_start = qfpnext;
3569 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003571
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003572 qfl->qf_index = 0;
3573 qfl->qf_start = NULL;
3574 qfl->qf_last = NULL;
3575 qfl->qf_ptr = NULL;
3576 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003577
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003578 qf_clean_dir_stack(&qfl->qf_dir_stack);
3579 qfl->qf_directory = NULL;
3580 qf_clean_dir_stack(&qfl->qf_file_stack);
3581 qfl->qf_currfile = NULL;
3582 qfl->qf_multiline = FALSE;
3583 qfl->qf_multiignore = FALSE;
3584 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585}
3586
3587/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003588 * Free error list "idx". Frees all the entries in the quickfix list,
3589 * associated context information and the title.
3590 */
3591 static void
3592qf_free(qf_info_T *qi, int idx)
3593{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003594 qf_list_T *qfl = &qi->qf_lists[idx];
3595
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003596 qf_free_items(qi, idx);
3597
Bram Moolenaard23a8232018-02-10 18:45:26 +01003598 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003599 free_tv(qfl->qf_ctx);
3600 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003601 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003602 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003603}
3604
3605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 * qf_mark_adjust: adjust marks
3607 */
3608 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003609qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003610 win_T *wp,
3611 linenr_T line1,
3612 linenr_T line2,
3613 long amount,
3614 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003616 int i;
3617 qfline_T *qfp;
3618 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003619 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003620 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003621 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
Bram Moolenaarc1542742016-07-20 21:44:37 +02003623 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003624 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003625 if (wp != NULL)
3626 {
3627 if (wp->w_llist == NULL)
3628 return;
3629 qi = wp->w_llist;
3630 }
3631
3632 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003633 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003634 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003635 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3636 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 if (qfp->qf_fnum == curbuf->b_fnum)
3638 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003639 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3641 {
3642 if (amount == MAXLNUM)
3643 qfp->qf_cleared = TRUE;
3644 else
3645 qfp->qf_lnum += amount;
3646 }
3647 else if (amount_after && qfp->qf_lnum > line2)
3648 qfp->qf_lnum += amount_after;
3649 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003650
3651 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003652 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653}
3654
3655/*
3656 * Make a nice message out of the error character and the error number:
3657 * char number message
3658 * e or E 0 " error"
3659 * w or W 0 " warning"
3660 * i or I 0 " info"
3661 * 0 0 ""
3662 * other 0 " c"
3663 * e or E n " error n"
3664 * w or W n " warning n"
3665 * i or I n " info n"
3666 * 0 n " error n"
3667 * other n " c n"
3668 * 1 x "" :helpgrep
3669 */
3670 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003671qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672{
3673 static char_u buf[20];
3674 static char_u cc[3];
3675 char_u *p;
3676
3677 if (c == 'W' || c == 'w')
3678 p = (char_u *)" warning";
3679 else if (c == 'I' || c == 'i')
3680 p = (char_u *)" info";
3681 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3682 p = (char_u *)" error";
3683 else if (c == 0 || c == 1)
3684 p = (char_u *)"";
3685 else
3686 {
3687 cc[0] = ' ';
3688 cc[1] = c;
3689 cc[2] = NUL;
3690 p = cc;
3691 }
3692
3693 if (nr <= 0)
3694 return p;
3695
3696 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3697 return buf;
3698}
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003701 * When "split" is FALSE: Open the entry/result under the cursor.
3702 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3703 */
3704 void
3705qf_view_result(int split)
3706{
3707 qf_info_T *qi = &ql_info;
3708
3709 if (!bt_quickfix(curbuf))
3710 return;
3711
3712 if (IS_LL_WINDOW(curwin))
3713 qi = GET_LOC_LIST(curwin);
3714
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003715 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003716 {
3717 EMSG(_(e_quickfix));
3718 return;
3719 }
3720
3721 if (split)
3722 {
3723 char_u cmd[32];
3724
3725 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3726 (long)curwin->w_cursor.lnum,
3727 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3728 if (do_cmdline_cmd(cmd) == OK)
3729 do_cmdline_cmd((char_u *) "clearjumps");
3730 return;
3731 }
3732
3733 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3734}
3735
3736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 * ":cwindow": open the quickfix window if we have errors to display,
3738 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003739 * ":lwindow": open the location list window if we have locations to display,
3740 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 */
3742 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003743ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003745 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 win_T *win;
3747
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003748 if (eap->cmdidx == CMD_lwindow)
3749 {
3750 qi = GET_LOC_LIST(curwin);
3751 if (qi == NULL)
3752 return;
3753 }
3754
3755 /* Look for an existing quickfix window. */
3756 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
3758 /*
3759 * If a quickfix window is open but we have no errors to display,
3760 * close the window. If a quickfix window is not open, then open
3761 * it if we have errors; otherwise, leave it closed.
3762 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003763 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003764 || qf_list_empty(qi, qi->qf_curlist)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003765 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 {
3767 if (win != NULL)
3768 ex_cclose(eap);
3769 }
3770 else if (win == NULL)
3771 ex_copen(eap);
3772}
3773
3774/*
3775 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003776 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003779ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003781 win_T *win = NULL;
3782 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003784 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3785 {
3786 qi = GET_LOC_LIST(curwin);
3787 if (qi == NULL)
3788 return;
3789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003791 /* Find existing quickfix window and close it. */
3792 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 if (win != NULL)
3794 win_close(win, FALSE);
3795}
3796
3797/*
3798 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003799 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 */
3801 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003802ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003804 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003807 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003808 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003809 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003811 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3812 {
3813 qi = GET_LOC_LIST(curwin);
3814 if (qi == NULL)
3815 {
3816 EMSG(_(e_loclist));
3817 return;
3818 }
3819 }
3820
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 if (eap->addr_count != 0)
3822 height = eap->line2;
3823 else
3824 height = QF_WINHEIGHT;
3825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827#ifdef FEAT_GUI
3828 need_mouse_correct = TRUE;
3829#endif
3830
3831 /*
3832 * Find existing quickfix window, or open a new one.
3833 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003834 win = qf_find_win(qi);
3835
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003836 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003837 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003839 if (eap->addr_count != 0)
3840 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003841 if (cmdmod.split & WSP_VERT)
3842 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003843 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003844 win_setwidth(height);
3845 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003846 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003847 win_setheight(height);
3848 }
3849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 else
3851 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003852 int flags = 0;
3853
Bram Moolenaar9c102382006-05-03 21:26:49 +00003854 qf_buf = qf_find_buf(qi);
3855
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 /* The current window becomes the previous window afterwards. */
3857 win = curwin;
3858
Bram Moolenaar77642c02012-11-20 17:55:10 +01003859 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3860 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003861 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003862 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003863 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003864 /* Default is to open the window below the current window */
3865 if (cmdmod.split == 0)
3866 flags = WSP_BELOW;
3867 flags |= WSP_NEWLOC;
3868 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003870 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003872 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003874 /*
3875 * For the location list window, create a reference to the
3876 * location list from the window 'win'.
3877 */
3878 curwin->w_llist_ref = win->w_llist;
3879 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003881
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003882 if (oldwin != curwin)
3883 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003884 if (qf_buf != NULL)
3885 /* Use the existing quickfix buffer */
3886 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003887 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003888 else
3889 {
3890 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003891 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003892 /* switch off 'swapfile' */
3893 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3894 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003895 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003896 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003897 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003898#ifdef FEAT_DIFF
3899 curwin->w_p_diff = FALSE;
3900#endif
3901#ifdef FEAT_FOLDING
3902 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3903 OPT_LOCAL);
3904#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003907 /* Only set the height when still in the same tab page and there is no
3908 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003909 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 win_setheight(height);
3911 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3912 if (win_valid(win))
3913 prevwin = win;
3914 }
3915
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003916 qf_set_title_var(qi);
3917
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 /*
3919 * Fill the buffer with the quickfix list.
3920 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003921 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003923 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 curwin->w_cursor.col = 0;
3925 check_cursor();
3926 update_topline(); /* scroll to show the line */
3927}
3928
3929/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003930 * Move the cursor in the quickfix window to "lnum".
3931 */
3932 static void
3933qf_win_goto(win_T *win, linenr_T lnum)
3934{
3935 win_T *old_curwin = curwin;
3936
3937 curwin = win;
3938 curbuf = win->w_buffer;
3939 curwin->w_cursor.lnum = lnum;
3940 curwin->w_cursor.col = 0;
3941#ifdef FEAT_VIRTUALEDIT
3942 curwin->w_cursor.coladd = 0;
3943#endif
3944 curwin->w_curswant = 0;
3945 update_topline(); /* scroll to show the line */
3946 redraw_later(VALID);
3947 curwin->w_redr_status = TRUE; /* update ruler */
3948 curwin = old_curwin;
3949 curbuf = curwin->w_buffer;
3950}
3951
3952/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003953 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003954 */
3955 void
3956ex_cbottom(exarg_T *eap UNUSED)
3957{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003958 qf_info_T *qi = &ql_info;
3959 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003960
Bram Moolenaar537ef082016-07-09 17:56:19 +02003961 if (eap->cmdidx == CMD_lbottom)
3962 {
3963 qi = GET_LOC_LIST(curwin);
3964 if (qi == NULL)
3965 {
3966 EMSG(_(e_loclist));
3967 return;
3968 }
3969 }
3970
3971 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003972 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3973 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3974}
3975
3976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 * Return the number of the current entry (line number in the quickfix
3978 * window).
3979 */
3980 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003981qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003983 qf_info_T *qi = &ql_info;
3984
3985 if (IS_LL_WINDOW(wp))
3986 /* In the location list window, use the referenced location list */
3987 qi = wp->w_llist_ref;
3988
3989 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990}
3991
3992/*
3993 * Update the cursor position in the quickfix window to the current error.
3994 * Return TRUE if there is a quickfix window.
3995 */
3996 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003997qf_win_pos_update(
3998 qf_info_T *qi,
3999 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
4001 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004002 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003
4004 /*
4005 * Put the cursor on the current error in the quickfix window, so that
4006 * it's viewable.
4007 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004008 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 if (win != NULL
4010 && qf_index <= win->w_buffer->b_ml.ml_line_count
4011 && old_qf_index != qf_index)
4012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 if (qf_index > old_qf_index)
4014 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004015 win->w_redraw_top = old_qf_index;
4016 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018 else
4019 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004020 win->w_redraw_top = qf_index;
4021 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004023 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 }
4025 return win != NULL;
4026}
4027
4028/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004029 * Check whether the given window is displaying the specified quickfix/location
4030 * list buffer
4031 */
4032 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004033is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004034{
4035 /*
4036 * A window displaying the quickfix buffer will have the w_llist_ref field
4037 * set to NULL.
4038 * A window displaying a location list buffer will have the w_llist_ref
4039 * pointing to the location list.
4040 */
4041 if (bt_quickfix(win->w_buffer))
4042 if ((qi == &ql_info && win->w_llist_ref == NULL)
4043 || (qi != &ql_info && win->w_llist_ref == qi))
4044 return TRUE;
4045
4046 return FALSE;
4047}
4048
4049/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004050 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004051 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004052 */
4053 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004054qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004055{
4056 win_T *win;
4057
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004058 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004059 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004060 return win;
4061 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004062}
4063
4064/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004065 * Find a quickfix buffer.
4066 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 */
4068 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004069qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004071 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004072 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073
Bram Moolenaar9c102382006-05-03 21:26:49 +00004074 FOR_ALL_TAB_WINDOWS(tp, win)
4075 if (is_qf_win(win, qi))
4076 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004077
Bram Moolenaar9c102382006-05-03 21:26:49 +00004078 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079}
4080
4081/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004082 * Update the w:quickfix_title variable in the quickfix/location list window
4083 */
4084 static void
4085qf_update_win_titlevar(qf_info_T *qi)
4086{
4087 win_T *win;
4088 win_T *curwin_save;
4089
4090 if ((win = qf_find_win(qi)) != NULL)
4091 {
4092 curwin_save = curwin;
4093 curwin = win;
4094 qf_set_title_var(qi);
4095 curwin = curwin_save;
4096 }
4097}
4098
4099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 * Find the quickfix buffer. If it exists, update the contents.
4101 */
4102 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004103qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
4105 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004106 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108
4109 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004110 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 if (buf != NULL)
4112 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004113 linenr_T old_line_count = buf->b_ml.ml_line_count;
4114
4115 if (old_last == NULL)
4116 /* set curwin/curbuf to buf and save a few things */
4117 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
Bram Moolenaard823fa92016-08-12 16:29:27 +02004119 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004120
Bram Moolenaar864293a2016-06-02 13:40:04 +02004121 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004122 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004123
Bram Moolenaar864293a2016-06-02 13:40:04 +02004124 if (old_last == NULL)
4125 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004126 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004127
4128 /* restore curwin/curbuf and a few other things */
4129 aucmd_restbuf(&aco);
4130 }
4131
4132 /* Only redraw when added lines are visible. This avoids flickering
4133 * when the added lines are not visible. */
4134 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4135 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
4137}
4138
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004139/*
4140 * Set "w:quickfix_title" if "qi" has a title.
4141 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004142 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004143qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004144{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004145 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
4146 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004147 qi->qf_lists[qi->qf_curlist].qf_title);
4148}
4149
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004151 * Add an error line to the quickfix buffer.
4152 */
4153 static int
4154qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4155{
4156 int len;
4157 buf_T *errbuf;
4158
4159 if (qfp->qf_module != NULL)
4160 {
4161 STRCPY(IObuff, qfp->qf_module);
4162 len = (int)STRLEN(IObuff);
4163 }
4164 else if (qfp->qf_fnum != 0
4165 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4166 && errbuf->b_fname != NULL)
4167 {
4168 if (qfp->qf_type == 1) /* :helpgrep */
4169 STRCPY(IObuff, gettail(errbuf->b_fname));
4170 else
4171 {
4172 /* shorten the file name if not done already */
4173 if (errbuf->b_sfname == NULL
4174 || mch_isFullName(errbuf->b_sfname))
4175 {
4176 if (*dirname == NUL)
4177 mch_dirname(dirname, MAXPATHL);
4178 shorten_buf_fname(errbuf, dirname, FALSE);
4179 }
4180 STRCPY(IObuff, errbuf->b_fname);
4181 }
4182 len = (int)STRLEN(IObuff);
4183 }
4184 else
4185 len = 0;
4186 IObuff[len++] = '|';
4187
4188 if (qfp->qf_lnum > 0)
4189 {
4190 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4191 len += (int)STRLEN(IObuff + len);
4192
4193 if (qfp->qf_col > 0)
4194 {
4195 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4196 len += (int)STRLEN(IObuff + len);
4197 }
4198
4199 sprintf((char *)IObuff + len, "%s",
4200 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4201 len += (int)STRLEN(IObuff + len);
4202 }
4203 else if (qfp->qf_pattern != NULL)
4204 {
4205 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4206 len += (int)STRLEN(IObuff + len);
4207 }
4208 IObuff[len++] = '|';
4209 IObuff[len++] = ' ';
4210
4211 /* Remove newlines and leading whitespace from the text.
4212 * For an unrecognized line keep the indent, the compiler may
4213 * mark a word with ^^^^. */
4214 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4215 IObuff + len, IOSIZE - len);
4216
4217 if (ml_append_buf(buf, lnum, IObuff,
4218 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4219 return FAIL;
4220
4221 return OK;
4222}
4223
4224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 * Fill current buffer with quickfix errors, replacing any previous contents.
4226 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004227 * If "old_last" is not NULL append the items after this one.
4228 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4229 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 */
4231 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004232qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004234 linenr_T lnum;
4235 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004236 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
Bram Moolenaar864293a2016-06-02 13:40:04 +02004238 if (old_last == NULL)
4239 {
4240 if (buf != curbuf)
4241 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004242 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004243 return;
4244 }
4245
4246 /* delete all existing lines */
4247 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4248 (void)ml_delete((linenr_T)1, FALSE);
4249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004252 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
Bram Moolenaara796d462018-05-01 14:30:36 +02004254 char_u dirname[MAXPATHL];
4255
4256 *dirname = NUL;
4257
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004259 if (old_last == NULL)
4260 {
4261 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4262 lnum = 0;
4263 }
4264 else
4265 {
4266 qfp = old_last->qf_next;
4267 lnum = buf->b_ml.ml_line_count;
4268 }
4269 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004271 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004273
Bram Moolenaar864293a2016-06-02 13:40:04 +02004274 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004276 if (qfp == NULL)
4277 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004279
4280 if (old_last == NULL)
4281 /* Delete the empty line which is now at the end */
4282 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284
4285 /* correct cursor position */
4286 check_lnums(TRUE);
4287
Bram Moolenaar864293a2016-06-02 13:40:04 +02004288 if (old_last == NULL)
4289 {
4290 /* Set the 'filetype' to "qf" each time after filling the buffer.
4291 * This resembles reading a file into a buffer, it's more logical when
4292 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02004293 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004294 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4295 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
Bram Moolenaar864293a2016-06-02 13:40:04 +02004297 keep_filetype = TRUE; /* don't detect 'filetype' */
4298 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004300 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004302 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004303 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004304
Bram Moolenaar864293a2016-06-02 13:40:04 +02004305 /* make sure it will be redrawn */
4306 redraw_curbuf_later(NOT_VALID);
4307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
4309 /* Restore KeyTyped, setting 'filetype' may reset it. */
4310 KeyTyped = old_KeyTyped;
4311}
4312
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004313/*
4314 * For every change made to the quickfix list, update the changed tick.
4315 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004316 static void
4317qf_list_changed(qf_info_T *qi, int qf_idx)
4318{
4319 qi->qf_lists[qf_idx].qf_changedtick++;
4320}
4321
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004323 * Return the quickfix/location list number with the given identifier.
4324 * Returns -1 if list is not found.
4325 */
4326 static int
4327qf_id2nr(qf_info_T *qi, int_u qfid)
4328{
4329 int qf_idx;
4330
4331 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4332 if (qi->qf_lists[qf_idx].qf_id == qfid)
4333 return qf_idx;
4334 return INVALID_QFIDX;
4335}
4336
4337/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004338 * If the current list is not "save_qfid" and we can find the list with that ID
4339 * then make it the current list.
4340 * This is used when autocommands may have changed the current list.
4341 */
4342 static void
4343qf_restore_list(qf_info_T *qi, int_u save_qfid)
4344{
4345 int curlist;
4346
4347 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4348 {
4349 curlist = qf_id2nr(qi, save_qfid);
4350 if (curlist >= 0)
4351 qi->qf_curlist = curlist;
4352 // else: what if the list can't be found?
4353 }
4354}
4355
4356/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004357 * Jump to the first entry if there is one.
4358 */
4359 static void
4360qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4361{
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004362 qf_restore_list(qi, save_qfid);
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004363
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004364 // Autocommands might have cleared the list, check for it
4365 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004366 qf_jump(qi, 0, 0, forceit);
4367}
4368
4369/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004370 * Return TRUE when using ":vimgrep" for ":grep".
4371 */
4372 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004373grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004374{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004375 return ((cmdidx == CMD_grep
4376 || cmdidx == CMD_lgrep
4377 || cmdidx == CMD_grepadd
4378 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004379 && STRCMP("internal",
4380 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4381}
4382
4383/*
Bram Moolenaara6557602006-02-04 22:43:20 +00004384 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 */
4386 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004387ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388{
Bram Moolenaar7c626922005-02-07 22:01:03 +00004389 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004391 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00004393 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004394 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004395 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004396 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004397 int_u save_qfid;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004398
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004399 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
4400 if (grep_internal(eap->cmdidx))
4401 {
4402 ex_vimgrep(eap);
4403 return;
4404 }
4405
Bram Moolenaar7c626922005-02-07 22:01:03 +00004406 switch (eap->cmdidx)
4407 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00004408 case CMD_make: au_name = (char_u *)"make"; break;
4409 case CMD_lmake: au_name = (char_u *)"lmake"; break;
4410 case CMD_grep: au_name = (char_u *)"grep"; break;
4411 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4412 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4413 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004414 default: break;
4415 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004416 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4417 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004418 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004419#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004420 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004421 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004422#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004423 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004424#ifdef FEAT_MBYTE
4425 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
Bram Moolenaara6557602006-02-04 22:43:20 +00004428 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
4429 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004430 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004431
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00004433 fname = get_mef_name();
4434 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004436 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438 /*
4439 * If 'shellpipe' empty: don't redirect to 'errorfile'.
4440 */
4441 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
4442 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00004443 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 cmd = alloc(len);
4445 if (cmd == NULL)
4446 return;
4447 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
4448 (char *)p_shq);
4449 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004450 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 /*
4452 * Output a newline if there's something else than the :make command that
4453 * was typed (in which case the cursor is in column 0).
4454 */
4455 if (msg_col == 0)
4456 msg_didout = FALSE;
4457 msg_start();
4458 MSG_PUTS(":!");
4459 msg_outtrans(cmd); /* show what we are doing */
4460
4461 /* let the shell know if we are redirecting output or not */
4462 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4463
4464#ifdef AMIGA
4465 out_flush();
4466 /* read window status report and redraw before message */
4467 (void)char_avail();
4468#endif
4469
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004470 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00004471 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4472 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004473 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004474 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004475 if (wp != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004476 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004477 qi = GET_LOC_LIST(wp);
4478 if (qi == NULL)
4479 goto cleanup;
4480 }
4481 if (res >= 0)
4482 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004483
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004484 // Remember the current quickfix list identifier, so that we can
4485 // check for autocommands changing the current quickfix list.
4486 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4487 if (au_name != NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004488 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4489 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004490 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004491 // display the first error
4492 qf_jump_first(qi, save_qfid, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004494cleanup:
Bram Moolenaar7c626922005-02-07 22:01:03 +00004495 mch_remove(fname);
4496 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 vim_free(cmd);
4498}
4499
4500/*
4501 * Return the name for the errorfile, in allocated memory.
4502 * Find a new unique name when 'makeef' contains "##".
4503 * Returns NULL for error.
4504 */
4505 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004506get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507{
4508 char_u *p;
4509 char_u *name;
4510 static int start = -1;
4511 static int off = 0;
4512#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004513 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514#endif
4515
4516 if (*p_mef == NUL)
4517 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004518 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 if (name == NULL)
4520 EMSG(_(e_notmp));
4521 return name;
4522 }
4523
4524 for (p = p_mef; *p; ++p)
4525 if (p[0] == '#' && p[1] == '#')
4526 break;
4527
4528 if (*p == NUL)
4529 return vim_strsave(p_mef);
4530
4531 /* Keep trying until the name doesn't exist yet. */
4532 for (;;)
4533 {
4534 if (start == -1)
4535 start = mch_get_pid();
4536 else
4537 off += 19;
4538
4539 name = alloc((unsigned)STRLEN(p_mef) + 30);
4540 if (name == NULL)
4541 break;
4542 STRCPY(name, p_mef);
4543 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4544 STRCAT(name, p + 2);
4545 if (mch_getperm(name) < 0
4546#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02004547 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 && mch_lstat((char *)name, &sb) < 0
4549#endif
4550 )
4551 break;
4552 vim_free(name);
4553 }
4554 return name;
4555}
4556
4557/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004558 * Returns the number of valid entries in the current quickfix/location list.
4559 */
4560 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004561qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004562{
4563 qf_info_T *qi = &ql_info;
4564 qfline_T *qfp;
4565 int i, sz = 0;
4566 int prev_fnum = 0;
4567
4568 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4569 {
4570 /* Location list */
4571 qi = GET_LOC_LIST(curwin);
4572 if (qi == NULL)
4573 return 0;
4574 }
4575
4576 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004577 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004578 ++i, qfp = qfp->qf_next)
4579 {
4580 if (qfp->qf_valid)
4581 {
4582 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4583 sz++; /* Count all valid entries */
4584 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4585 {
4586 /* Count the number of files */
4587 sz++;
4588 prev_fnum = qfp->qf_fnum;
4589 }
4590 }
4591 }
4592
4593 return sz;
4594}
4595
4596/*
4597 * Returns the current index of the quickfix/location list.
4598 * Returns 0 if there is an error.
4599 */
4600 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004601qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004602{
4603 qf_info_T *qi = &ql_info;
4604
4605 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4606 {
4607 /* Location list */
4608 qi = GET_LOC_LIST(curwin);
4609 if (qi == NULL)
4610 return 0;
4611 }
4612
4613 return qi->qf_lists[qi->qf_curlist].qf_index;
4614}
4615
4616/*
4617 * Returns the current index in the quickfix/location list (counting only valid
4618 * entries). If no valid entries are in the list, then returns 1.
4619 */
4620 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004621qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004622{
4623 qf_info_T *qi = &ql_info;
4624 qf_list_T *qfl;
4625 qfline_T *qfp;
4626 int i, eidx = 0;
4627 int prev_fnum = 0;
4628
4629 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4630 {
4631 /* Location list */
4632 qi = GET_LOC_LIST(curwin);
4633 if (qi == NULL)
4634 return 1;
4635 }
4636
4637 qfl = &qi->qf_lists[qi->qf_curlist];
4638 qfp = qfl->qf_start;
4639
4640 /* check if the list has valid errors */
4641 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4642 return 1;
4643
4644 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4645 {
4646 if (qfp->qf_valid)
4647 {
4648 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4649 {
4650 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4651 {
4652 /* Count the number of files */
4653 eidx++;
4654 prev_fnum = qfp->qf_fnum;
4655 }
4656 }
4657 else
4658 eidx++;
4659 }
4660 }
4661
4662 return eidx ? eidx : 1;
4663}
4664
4665/*
4666 * Get the 'n'th valid error entry in the quickfix or location list.
4667 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4668 * For :cdo and :ldo returns the 'n'th valid error entry.
4669 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4670 */
4671 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004672qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004673{
4674 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4675 qfline_T *qfp = qfl->qf_start;
4676 int i, eidx;
4677 int prev_fnum = 0;
4678
4679 /* check if the list has valid errors */
4680 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4681 return 1;
4682
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004683 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004684 i++, qfp = qfp->qf_next)
4685 {
4686 if (qfp->qf_valid)
4687 {
4688 if (fdo)
4689 {
4690 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4691 {
4692 /* Count the number of files */
4693 eidx++;
4694 prev_fnum = qfp->qf_fnum;
4695 }
4696 }
4697 else
4698 eidx++;
4699 }
4700
4701 if (eidx == n)
4702 break;
4703 }
4704
4705 if (i <= qfl->qf_count)
4706 return i;
4707 else
4708 return 1;
4709}
4710
4711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004713 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004714 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 */
4716 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004717ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004719 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004720 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004721
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004722 if (eap->cmdidx == CMD_ll
4723 || eap->cmdidx == CMD_lrewind
4724 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004725 || eap->cmdidx == CMD_llast
4726 || eap->cmdidx == CMD_ldo
4727 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004728 {
4729 qi = GET_LOC_LIST(curwin);
4730 if (qi == NULL)
4731 {
4732 EMSG(_(e_loclist));
4733 return;
4734 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004735 }
4736
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004737 if (eap->addr_count > 0)
4738 errornr = (int)eap->line2;
4739 else
4740 {
4741 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4742 errornr = 0;
4743 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4744 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4745 errornr = 1;
4746 else
4747 errornr = 32767;
4748 }
4749
4750 /* For cdo and ldo commands, jump to the nth valid error.
4751 * For cfdo and lfdo commands, jump to the nth valid file entry.
4752 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004753 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4754 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004755 errornr = qf_get_nth_valid_entry(qi,
4756 eap->addr_count > 0 ? (int)eap->line1 : 1,
4757 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4758
4759 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760}
4761
4762/*
4763 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004764 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004765 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 */
4767 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004768ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004770 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004771 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004772
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004773 if (eap->cmdidx == CMD_lnext
4774 || eap->cmdidx == CMD_lNext
4775 || eap->cmdidx == CMD_lprevious
4776 || eap->cmdidx == CMD_lnfile
4777 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004778 || eap->cmdidx == CMD_lpfile
4779 || eap->cmdidx == CMD_ldo
4780 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004781 {
4782 qi = GET_LOC_LIST(curwin);
4783 if (qi == NULL)
4784 {
4785 EMSG(_(e_loclist));
4786 return;
4787 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004788 }
4789
Bram Moolenaar55b69262017-08-13 13:42:01 +02004790 if (eap->addr_count > 0
4791 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4792 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004793 errornr = (int)eap->line2;
4794 else
4795 errornr = 1;
4796
4797 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4798 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004800 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4801 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004803 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4804 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 ? BACKWARD_FILE
4806 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004807 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808}
4809
4810/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004811 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004812 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 */
4814 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004815ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004817 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004818 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004819 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004820 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004821 int_u save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004822 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004823
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004824 switch (eap->cmdidx)
4825 {
4826 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4827 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4828 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4829 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4830 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4831 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4832 default: break;
4833 }
4834 if (au_name != NULL)
4835 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004836#ifdef FEAT_MBYTE
4837 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4838#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004839#ifdef FEAT_BROWSE
4840 if (cmdmod.browse)
4841 {
4842 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004843 NULL, NULL,
4844 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004845 if (browse_file == NULL)
4846 return;
4847 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4848 vim_free(browse_file);
4849 }
4850 else
4851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004853 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004854
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004855 if (eap->cmdidx == CMD_lfile
4856 || eap->cmdidx == CMD_lgetfile
4857 || eap->cmdidx == CMD_laddfile)
4858 wp = curwin;
4859
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004860 /*
4861 * This function is used by the :cfile, :cgetfile and :caddfile
4862 * commands.
4863 * :cfile always creates a new quickfix list and jumps to the
4864 * first error.
4865 * :cgetfile creates a new quickfix list but doesn't jump to the
4866 * first error.
4867 * :caddfile adds to an existing quickfix list. If there is no
4868 * quickfix list then a new list is created.
4869 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004870 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004871 && eap->cmdidx != CMD_laddfile),
4872 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004873 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004874 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01004875 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004876 if (qi == NULL)
4877 return;
4878 }
4879 if (res >= 0)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004880 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004881 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004882 if (au_name != NULL)
4883 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004884
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004885 // Jump to the first error for a new list and if autocmds didn't
4886 // free the list.
4887 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
4888 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004889 // display the first error
4890 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004891}
4892
4893/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004894 * Return the vimgrep autocmd name.
4895 */
4896 static char_u *
4897vgr_get_auname(cmdidx_T cmdidx)
4898{
4899 switch (cmdidx)
4900 {
4901 case CMD_vimgrep: return (char_u *)"vimgrep";
4902 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4903 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4904 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4905 case CMD_grep: return (char_u *)"grep";
4906 case CMD_lgrep: return (char_u *)"lgrep";
4907 case CMD_grepadd: return (char_u *)"grepadd";
4908 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4909 default: return NULL;
4910 }
4911}
4912
4913/*
4914 * Initialize the regmatch used by vimgrep for pattern "s".
4915 */
4916 static void
4917vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4918{
4919 /* Get the search pattern: either white-separated or enclosed in // */
4920 regmatch->regprog = NULL;
4921
4922 if (s == NULL || *s == NUL)
4923 {
4924 /* Pattern is empty, use last search pattern. */
4925 if (last_search_pat() == NULL)
4926 {
4927 EMSG(_(e_noprevre));
4928 return;
4929 }
4930 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4931 }
4932 else
4933 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4934
4935 regmatch->rmm_ic = p_ic;
4936 regmatch->rmm_maxcol = 0;
4937}
4938
4939/*
4940 * Display a file name when vimgrep is running.
4941 */
4942 static void
4943vgr_display_fname(char_u *fname)
4944{
4945 char_u *p;
4946
4947 msg_start();
4948 p = msg_strtrunc(fname, TRUE);
4949 if (p == NULL)
4950 msg_outtrans(fname);
4951 else
4952 {
4953 msg_outtrans(p);
4954 vim_free(p);
4955 }
4956 msg_clr_eos();
4957 msg_didout = FALSE; /* overwrite this message */
4958 msg_nowait = TRUE; /* don't wait for this message */
4959 msg_col = 0;
4960 out_flush();
4961}
4962
4963/*
4964 * Load a dummy buffer to search for a pattern using vimgrep.
4965 */
4966 static buf_T *
4967vgr_load_dummy_buf(
4968 char_u *fname,
4969 char_u *dirname_start,
4970 char_u *dirname_now)
4971{
4972 int save_mls;
4973#if defined(FEAT_SYN_HL)
4974 char_u *save_ei = NULL;
4975#endif
4976 buf_T *buf;
4977
4978#if defined(FEAT_SYN_HL)
4979 /* Don't do Filetype autocommands to avoid loading syntax and
4980 * indent scripts, a great speed improvement. */
4981 save_ei = au_event_disable(",Filetype");
4982#endif
4983 /* Don't use modelines here, it's useless. */
4984 save_mls = p_mls;
4985 p_mls = 0;
4986
4987 /* Load file into a buffer, so that 'fileencoding' is detected,
4988 * autocommands applied, etc. */
4989 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
4990
4991 p_mls = save_mls;
4992#if defined(FEAT_SYN_HL)
4993 au_event_restore(save_ei);
4994#endif
4995
4996 return buf;
4997}
4998
4999/*
5000 * Check whether a quickfix/location list valid. Autocmds may remove or change
5001 * a quickfix list when vimgrep is running. If the list is not found, create a
5002 * new list.
5003 */
5004 static int
5005vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005006 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005007 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005008 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005009 char_u *title)
5010{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005011 /* Verify that the quickfix/location list was not freed by an autocmd */
5012 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005013 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005014 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005015 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005016 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005017 EMSG(_(e_loc_list_changed));
5018 return FALSE;
5019 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005020 else
5021 {
5022 /* Quickfix list is not found, create a new one. */
5023 qf_new_list(qi, title);
5024 return TRUE;
5025 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005026 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005027
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005028 qf_restore_list(qi, qfid);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005029
5030 return TRUE;
5031}
5032
5033/*
5034 * Search for a pattern in all the lines in a buffer and add the matching lines
5035 * to a quickfix list.
5036 */
5037 static int
5038vgr_match_buflines(
5039 qf_info_T *qi,
5040 char_u *fname,
5041 buf_T *buf,
5042 regmmatch_T *regmatch,
5043 long tomatch,
5044 int duplicate_name,
5045 int flags)
5046{
5047 int found_match = FALSE;
5048 long lnum;
5049 colnr_T col;
5050
5051 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5052 {
5053 col = 0;
5054 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5055 col, NULL, NULL) > 0)
5056 {
5057 /* Pass the buffer number so that it gets used even for a
5058 * dummy buffer, unless duplicate_name is set, then the
5059 * buffer will be wiped out below. */
5060 if (qf_add_entry(qi,
5061 qi->qf_curlist,
5062 NULL, /* dir */
5063 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005064 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005065 duplicate_name ? 0 : buf->b_fnum,
5066 ml_get_buf(buf,
5067 regmatch->startpos[0].lnum + lnum, FALSE),
5068 regmatch->startpos[0].lnum + lnum,
5069 regmatch->startpos[0].col + 1,
5070 FALSE, /* vis_col */
5071 NULL, /* search pattern */
5072 0, /* nr */
5073 0, /* type */
5074 TRUE /* valid */
5075 ) == FAIL)
5076 {
5077 got_int = TRUE;
5078 break;
5079 }
5080 found_match = TRUE;
5081 if (--tomatch == 0)
5082 break;
5083 if ((flags & VGR_GLOBAL) == 0
5084 || regmatch->endpos[0].lnum > 0)
5085 break;
5086 col = regmatch->endpos[0].col
5087 + (col == regmatch->endpos[0].col);
5088 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5089 break;
5090 }
5091 line_breakcheck();
5092 if (got_int)
5093 break;
5094 }
5095
5096 return found_match;
5097}
5098
5099/*
5100 * Jump to the first match and update the directory.
5101 */
5102 static void
5103vgr_jump_to_match(
5104 qf_info_T *qi,
5105 int forceit,
5106 int *redraw_for_dummy,
5107 buf_T *first_match_buf,
5108 char_u *target_dir)
5109{
5110 buf_T *buf;
5111
5112 buf = curbuf;
5113 qf_jump(qi, 0, 0, forceit);
5114 if (buf != curbuf)
5115 /* If we jumped to another buffer redrawing will already be
5116 * taken care of. */
5117 *redraw_for_dummy = FALSE;
5118
5119 /* Jump to the directory used after loading the buffer. */
5120 if (curbuf == first_match_buf && target_dir != NULL)
5121 {
5122 exarg_T ea;
5123
5124 ea.arg = target_dir;
5125 ea.cmdidx = CMD_lcd;
5126 ex_cd(&ea);
5127 }
5128}
5129
5130/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005131 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005132 * ":vimgrepadd {pattern} file(s)"
5133 * ":lvimgrep {pattern} file(s)"
5134 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005135 */
5136 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005137ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005138{
Bram Moolenaar81695252004-12-29 20:58:21 +00005139 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005140 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005141 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005142 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005143 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005144 char_u *s;
5145 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005146 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005147 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005148 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005149 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005150 buf_T *buf;
5151 int duplicate_name = FALSE;
5152 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005153 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005154 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005155 buf_T *first_match_buf = NULL;
5156 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005157 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005158 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005159 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005160 char_u *dirname_start = NULL;
5161 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005162 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005163 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005164
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005165 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005166 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5167 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005168 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005169#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005170 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005171 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005172#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005173 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005174
Bram Moolenaar754b5602006-02-09 23:53:20 +00005175 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005176 || eap->cmdidx == CMD_lvimgrep
5177 || eap->cmdidx == CMD_lgrepadd
5178 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00005179 {
5180 qi = ll_get_or_alloc_list(curwin);
5181 if (qi == NULL)
5182 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005183 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005184 }
5185
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005186 if (eap->addr_count > 0)
5187 tomatch = eap->line2;
5188 else
5189 tomatch = MAXLNUM;
5190
Bram Moolenaar81695252004-12-29 20:58:21 +00005191 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005192 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005193 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005194 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005195 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005196 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005197 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005198 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005199 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005200
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005201 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005202 if (regmatch.regprog == NULL)
5203 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005204
5205 p = skipwhite(p);
5206 if (*p == NUL)
5207 {
5208 EMSG(_("E683: File name missing or invalid pattern"));
5209 goto theend;
5210 }
5211
Bram Moolenaar55b69262017-08-13 13:42:01 +02005212 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005213 && eap->cmdidx != CMD_vimgrepadd
5214 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005215 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005216 /* make place for a new list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005217 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005218
5219 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005220 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005221 goto theend;
5222 if (fcount == 0)
5223 {
5224 EMSG(_(e_nomatch));
5225 goto theend;
5226 }
5227
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005228 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5229 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005230 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005231 {
5232 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005233 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005234 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005235
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005236 /* Remember the current directory, because a BufRead autocommand that does
5237 * ":lcd %:p:h" changes the meaning of short path names. */
5238 mch_dirname(dirname_start, MAXPATHL);
5239
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005240 /* Remember the current quickfix list identifier, so that we can check for
5241 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005242 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005243
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005244 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005245 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005246 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005247 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005248 if (time(NULL) > seconds)
5249 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005250 /* Display the file name every second or so, show the user we are
5251 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005252 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005253 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005254 }
5255
Bram Moolenaar81695252004-12-29 20:58:21 +00005256 buf = buflist_findname_exp(fnames[fi]);
5257 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5258 {
5259 /* Remember that a buffer with this name already exists. */
5260 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005261 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005262 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005263
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005264 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005265 }
5266 else
5267 /* Use existing, loaded buffer. */
5268 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005269
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005270 /* Check whether the quickfix list is still valid. When loading a
5271 * buffer above, autocommands might have changed the quickfix list. */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005272 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005273 {
5274 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005275 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005276 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005277 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005278
Bram Moolenaar81695252004-12-29 20:58:21 +00005279 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005280 {
5281 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005282 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005283 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005284 else
5285 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005286 /* Try for a match in all lines of the buffer.
5287 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005288 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5289 tomatch, duplicate_name, flags);
5290
Bram Moolenaar81695252004-12-29 20:58:21 +00005291 if (using_dummy)
5292 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005293 if (found_match && first_match_buf == NULL)
5294 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005295 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005296 {
Bram Moolenaar81695252004-12-29 20:58:21 +00005297 /* Never keep a dummy buffer if there is another buffer
5298 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005299 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005300 buf = NULL;
5301 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005302 else if (!cmdmod.hide
5303 || buf->b_p_bh[0] == 'u' /* "unload" */
5304 || buf->b_p_bh[0] == 'w' /* "wipe" */
5305 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00005306 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005307 /* When no match was found we don't need to remember the
5308 * buffer, wipe it out. If there was a match and it
5309 * wasn't the first one or we won't jump there: only
5310 * unload the buffer.
5311 * Ignore 'hidden' here, because it may lead to having too
5312 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00005313 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005314 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005315 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005316 buf = NULL;
5317 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005318 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005319 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005320 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02005321 /* Keeping the buffer, remove the dummy flag. */
5322 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005323 buf = NULL;
5324 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005325 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005326
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005327 if (buf != NULL)
5328 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02005329 /* Keeping the buffer, remove the dummy flag. */
5330 buf->b_flags &= ~BF_DUMMY;
5331
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005332 /* If the buffer is still loaded we need to use the
5333 * directory we jumped to below. */
5334 if (buf == first_match_buf
5335 && target_dir == NULL
5336 && STRCMP(dirname_start, dirname_now) != 0)
5337 target_dir = vim_strsave(dirname_now);
5338
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005339 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005340 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00005341 * need to be done (again). But not the window-local
5342 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005343 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005344#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005345 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5346 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005347#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005348 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005349 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005350 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005351 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005352 }
5353 }
5354
5355 FreeWild(fcount, fnames);
5356
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005357 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5358 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
5359 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005360 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005361
Bram Moolenaar864293a2016-06-02 13:40:04 +02005362 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005363
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005364 if (au_name != NULL)
5365 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5366 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01005367 /*
5368 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5369 * is still valid.
5370 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005371 if (!qflist_valid(wp, save_qfid))
5372 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005373
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005374 qf_restore_list(qi, save_qfid);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005375
Bram Moolenaar86b68352004-12-27 21:59:20 +00005376 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005377 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005378 {
5379 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005380 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5381 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005382 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005383 else
5384 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005385
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005386 /* If we loaded a dummy buffer into the current window, the autocommands
5387 * may have messed up things, need to redraw and recompute folds. */
5388 if (redraw_for_dummy)
5389 {
5390#ifdef FEAT_FOLDING
5391 foldUpdateAll(curwin);
5392#else
5393 redraw_later(NOT_VALID);
5394#endif
5395 }
5396
Bram Moolenaar86b68352004-12-27 21:59:20 +00005397theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005398 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005399 vim_free(dirname_now);
5400 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005401 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005402 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005403}
5404
5405/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005406 * Restore current working directory to "dirname_start" if they differ, taking
5407 * into account whether it is set locally or globally.
5408 */
5409 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005410restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005411{
5412 char_u *dirname_now = alloc(MAXPATHL);
5413
5414 if (NULL != dirname_now)
5415 {
5416 mch_dirname(dirname_now, MAXPATHL);
5417 if (STRCMP(dirname_start, dirname_now) != 0)
5418 {
5419 /* If the directory has changed, change it back by building up an
5420 * appropriate ex command and executing it. */
5421 exarg_T ea;
5422
5423 ea.arg = dirname_start;
5424 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5425 ex_cd(&ea);
5426 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005427 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005428 }
5429}
5430
5431/*
5432 * Load file "fname" into a dummy buffer and return the buffer pointer,
5433 * placing the directory resulting from the buffer load into the
5434 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5435 * prior to calling this function. Restores directory to "dirname_start" prior
5436 * to returning, if autocmds or the 'autochdir' option have changed it.
5437 *
5438 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5439 * or wipe_dummy_buffer() later!
5440 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005441 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005442 */
5443 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005444load_dummy_buffer(
5445 char_u *fname,
5446 char_u *dirname_start, /* in: old directory */
5447 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005448{
5449 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005450 bufref_T newbufref;
5451 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005452 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005453 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005454 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005455
5456 /* Allocate a buffer without putting it in the buffer list. */
5457 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5458 if (newbuf == NULL)
5459 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005460 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005461
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005462 /* Init the options. */
5463 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5464
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005465 /* need to open the memfile before putting the buffer in a window */
5466 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005467 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005468 /* Make sure this buffer isn't wiped out by autocommands. */
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005469 ++newbuf->b_locked;
5470
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005471 /* set curwin/curbuf to buf and save a few things */
5472 aucmd_prepbuf(&aco, newbuf);
5473
5474 /* Need to set the filename for autocommands. */
5475 (void)setfname(curbuf, fname, NULL, FALSE);
5476
Bram Moolenaar81695252004-12-29 20:58:21 +00005477 /* Create swap file now to avoid the ATTENTION message. */
5478 check_need_swap(TRUE);
5479
5480 /* Remove the "dummy" flag, otherwise autocommands may not
5481 * work. */
5482 curbuf->b_flags &= ~BF_DUMMY;
5483
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005484 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005485 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005486 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005487 NULL, READ_NEW | READ_DUMMY);
5488 --newbuf->b_locked;
5489 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005490 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005491 && !(curbuf->b_flags & BF_NEW))
5492 {
5493 failed = FALSE;
5494 if (curbuf != newbuf)
5495 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005496 /* Bloody autocommands changed the buffer! Can happen when
5497 * using netrw and editing a remote file. Use the current
5498 * buffer instead, delete the dummy one after restoring the
5499 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005500 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005501 newbuf = curbuf;
5502 }
5503 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005504
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005505 /* restore curwin/curbuf and a few other things */
5506 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005507 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5508 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005509
5510 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5511 * skip it. */
5512 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005513 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005514
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005515 /*
5516 * When autocommands/'autochdir' option changed directory: go back.
5517 * Let the caller know what the resulting dir was first, in case it is
5518 * important.
5519 */
5520 mch_dirname(resulting_dir, MAXPATHL);
5521 restore_start_dir(dirname_start);
5522
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005523 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005524 return NULL;
5525 if (failed)
5526 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005527 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005528 return NULL;
5529 }
5530 return newbuf;
5531}
5532
5533/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005534 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5535 * directory to "dirname_start" prior to returning, if autocmds or the
5536 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005537 */
5538 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005539wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005540{
5541 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005542 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005543#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005544 cleanup_T cs;
5545
5546 /* Reset the error/interrupt/exception state here so that aborting()
5547 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5548 * work when got_int is set. */
5549 enter_cleanup(&cs);
5550#endif
5551
Bram Moolenaar81695252004-12-29 20:58:21 +00005552 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005553
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005554#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005555 /* Restore the error/interrupt/exception state if not discarded by a
5556 * new aborting error, interrupt, or uncaught exception. */
5557 leave_cleanup(&cs);
5558#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005559 /* When autocommands/'autochdir' option changed directory: go back. */
5560 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005561 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005562}
5563
5564/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005565 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5566 * directory to "dirname_start" prior to returning, if autocmds or the
5567 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005568 */
5569 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005570unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005571{
5572 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005573 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005574 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005575
5576 /* When autocommands/'autochdir' option changed directory: go back. */
5577 restore_start_dir(dirname_start);
5578 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005579}
5580
Bram Moolenaar05159a02005-02-26 23:04:13 +00005581#if defined(FEAT_EVAL) || defined(PROTO)
5582/*
5583 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005584 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005585 */
5586 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005587get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005588{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005589 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005590 dict_T *dict;
5591 char_u buf[2];
5592 qfline_T *qfp;
5593 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005594 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005595
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005596 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005597 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005598 qi = &ql_info;
5599 if (wp != NULL)
5600 {
5601 qi = GET_LOC_LIST(wp);
5602 if (qi == NULL)
5603 return FAIL;
5604 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005605 }
5606
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005607 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005608 qf_idx = qi->qf_curlist;
5609
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005610 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005611 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005612
Bram Moolenaard823fa92016-08-12 16:29:27 +02005613 qfp = qi->qf_lists[qf_idx].qf_start;
5614 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005615 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005616 /* Handle entries with a non-existing buffer number. */
5617 bufnum = qfp->qf_fnum;
5618 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5619 bufnum = 0;
5620
Bram Moolenaar05159a02005-02-26 23:04:13 +00005621 if ((dict = dict_alloc()) == NULL)
5622 return FAIL;
5623 if (list_append_dict(list, dict) == FAIL)
5624 return FAIL;
5625
5626 buf[0] = qfp->qf_type;
5627 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005628 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5629 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5630 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5631 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5632 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5633 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5634 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5635 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5636 || dict_add_string(dict, "type", buf) == FAIL
5637 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005638 return FAIL;
5639
5640 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005641 if (qfp == NULL)
5642 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005643 }
5644 return OK;
5645}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005646
5647/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005648 * Flags used by getqflist()/getloclist() to determine which fields to return.
5649 */
5650enum {
5651 QF_GETLIST_NONE = 0x0,
5652 QF_GETLIST_TITLE = 0x1,
5653 QF_GETLIST_ITEMS = 0x2,
5654 QF_GETLIST_NR = 0x4,
5655 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005656 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005657 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005658 QF_GETLIST_IDX = 0x40,
5659 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005660 QF_GETLIST_TICK = 0x100,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005661 QF_GETLIST_ALL = 0x1FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005662};
5663
5664/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005665 * Parse text from 'di' and return the quickfix list items.
5666 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005667 */
5668 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005669qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005670{
5671 int status = FAIL;
5672 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005673 char_u *errorformat = p_efm;
5674 dictitem_T *efm_di;
5675 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005676
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005677 /* Only a List value is supported */
5678 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005679 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005680 /* If errorformat is supplied then use it, otherwise use the 'efm'
5681 * option setting
5682 */
5683 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5684 {
5685 if (efm_di->di_tv.v_type != VAR_STRING ||
5686 efm_di->di_tv.vval.v_string == NULL)
5687 return FAIL;
5688 errorformat = efm_di->di_tv.vval.v_string;
5689 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005690
Bram Moolenaar36538222017-09-02 19:51:44 +02005691 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005692 if (l == NULL)
5693 return FAIL;
5694
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005695 qi = ll_new_list();
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005696 if (qi != NULL)
5697 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005698 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005699 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5700 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005701 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005702 qf_free(qi, 0);
5703 }
5704 free(qi);
5705 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005706 dict_add_list(retdict, "items", l);
5707 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005708 }
5709
5710 return status;
5711}
5712
5713/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005714 * Return the quickfix/location list window identifier in the current tabpage.
5715 */
5716 static int
5717qf_winid(qf_info_T *qi)
5718{
5719 win_T *win;
5720
5721 /* The quickfix window can be opened even if the quickfix list is not set
5722 * using ":copen". This is not true for location lists. */
5723 if (qi == NULL)
5724 return 0;
5725 win = qf_find_win(qi);
5726 if (win != NULL)
5727 return win->w_id;
5728 return 0;
5729}
5730
5731/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005732 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005733 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005734 static int
5735qf_getprop_keys2flags(dict_T *what)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005736{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005737 int flags = QF_GETLIST_NONE;
5738
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005739 if (dict_find(what, (char_u *)"all", -1) != NULL)
5740 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005741
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005742 if (dict_find(what, (char_u *)"title", -1) != NULL)
5743 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005744
Bram Moolenaara6d48492017-12-12 22:45:31 +01005745 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5746 flags |= QF_GETLIST_NR;
5747
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005748 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5749 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005750
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005751 if (dict_find(what, (char_u *)"context", -1) != NULL)
5752 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005753
Bram Moolenaara6d48492017-12-12 22:45:31 +01005754 if (dict_find(what, (char_u *)"id", -1) != NULL)
5755 flags |= QF_GETLIST_ID;
5756
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005757 if (dict_find(what, (char_u *)"items", -1) != NULL)
5758 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005759
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005760 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5761 flags |= QF_GETLIST_IDX;
5762
5763 if (dict_find(what, (char_u *)"size", -1) != NULL)
5764 flags |= QF_GETLIST_SIZE;
5765
Bram Moolenaarb254af32017-12-18 19:48:58 +01005766 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5767 flags |= QF_GETLIST_TICK;
5768
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005769 return flags;
5770}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005771
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005772/*
5773 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5774 * If 'nr' and 'id' are not present in 'what' then return the current
5775 * quickfix list index.
5776 * If 'nr' is zero then return the current quickfix list index.
5777 * If 'nr' is '$' then return the last quickfix list index.
5778 * If 'id' is present then return the index of the quickfix list with that id.
5779 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5780 * Return -1, if quickfix list is not present or if the stack is empty.
5781 */
5782 static int
5783qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5784{
5785 int qf_idx;
5786 dictitem_T *di;
5787
5788 qf_idx = qi->qf_curlist; /* default is the current list */
5789 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5790 {
5791 /* Use the specified quickfix/location list */
5792 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005793 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005794 /* for zero use the current list */
5795 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005796 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005797 qf_idx = di->di_tv.vval.v_number - 1;
5798 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005799 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005800 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005801 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005802 else if (di->di_tv.v_type == VAR_STRING
5803 && di->di_tv.vval.v_string != NULL
5804 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5805 /* Get the last quickfix list number */
5806 qf_idx = qi->qf_listcount - 1;
5807 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005808 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005809 }
5810
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005811 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5812 {
5813 /* Look for a list with the specified id */
5814 if (di->di_tv.v_type == VAR_NUMBER)
5815 {
5816 /*
5817 * For zero, use the current list or the list specified by 'nr'
5818 */
5819 if (di->di_tv.vval.v_number != 0)
5820 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5821 }
5822 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005823 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005824 }
5825
5826 return qf_idx;
5827}
5828
5829/*
5830 * Return default values for quickfix list properties in retdict.
5831 */
5832 static int
5833qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5834{
5835 int status = OK;
5836
5837 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005838 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005839 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5840 {
5841 list_T *l = list_alloc();
5842 if (l != NULL)
5843 status = dict_add_list(retdict, "items", l);
5844 else
5845 status = FAIL;
5846 }
5847 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005848 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005849 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005850 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005851 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005852 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005853 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005854 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005855 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005856 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005857 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005858 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005859 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005860 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005861
5862 return status;
5863}
5864
5865/*
5866 * Return the quickfix list title as 'title' in retdict
5867 */
5868 static int
5869qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5870{
Bram Moolenaare0be1672018-07-08 16:50:37 +02005871 return dict_add_string(retdict, "title", qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005872}
5873
5874/*
5875 * Return the quickfix list items/entries as 'items' in retdict
5876 */
5877 static int
5878qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5879{
5880 int status = OK;
5881 list_T *l = list_alloc();
5882 if (l != NULL)
5883 {
5884 (void)get_errorlist(qi, NULL, qf_idx, l);
5885 dict_add_list(retdict, "items", l);
5886 }
5887 else
5888 status = FAIL;
5889
5890 return status;
5891}
5892
5893/*
5894 * Return the quickfix list context (if any) as 'context' in retdict.
5895 */
5896 static int
5897qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5898{
5899 int status;
5900 dictitem_T *di;
5901
5902 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5903 {
5904 di = dictitem_alloc((char_u *)"context");
5905 if (di != NULL)
5906 {
5907 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5908 status = dict_add(retdict, di);
5909 if (status == FAIL)
5910 dictitem_free(di);
5911 }
5912 else
5913 status = FAIL;
5914 }
5915 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02005916 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005917
5918 return status;
5919}
5920
5921/*
5922 * Return the quickfix list index as 'idx' in retdict
5923 */
5924 static int
5925qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5926{
5927 int idx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005928 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005929 /* For empty lists, qf_index is set to 1 */
5930 idx = 0;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005931 return dict_add_number(retdict, "idx", idx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005932}
5933
5934/*
5935 * Return quickfix/location list details (title) as a
5936 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5937 * then current list is used. Otherwise the specified list is used.
5938 */
5939 int
5940qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5941{
5942 qf_info_T *qi = &ql_info;
5943 int status = OK;
5944 int qf_idx;
5945 dictitem_T *di;
5946 int flags = QF_GETLIST_NONE;
5947
5948 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5949 return qf_get_list_from_lines(what, di, retdict);
5950
5951 if (wp != NULL)
5952 qi = GET_LOC_LIST(wp);
5953
5954 flags = qf_getprop_keys2flags(what);
5955
5956 if (qi != NULL && qi->qf_listcount != 0)
5957 qf_idx = qf_getprop_qfidx(qi, what);
5958
Bram Moolenaara6d48492017-12-12 22:45:31 +01005959 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005960 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005961 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005962
Bram Moolenaard823fa92016-08-12 16:29:27 +02005963 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005964 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005965 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005966 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005967 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005968 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005969 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005970 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005971 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005972 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005973 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005974 status = dict_add_number(retdict, "id", qi->qf_lists[qf_idx].qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005975 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005976 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005977 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005978 status = dict_add_number(retdict, "size",
5979 qi->qf_lists[qf_idx].qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005980 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005981 status = dict_add_number(retdict, "changedtick",
5982 qi->qf_lists[qf_idx].qf_changedtick);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005983
Bram Moolenaard823fa92016-08-12 16:29:27 +02005984 return status;
5985}
5986
5987/*
5988 * Add list of entries to quickfix/location list. Each list entry is
5989 * a dictionary with item information.
5990 */
5991 static int
5992qf_add_entries(
5993 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005994 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005995 list_T *list,
5996 char_u *title,
5997 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005998{
5999 listitem_T *li;
6000 dict_T *d;
Bram Moolenaard76ce852018-05-01 15:02:04 +02006001 char_u *filename, *module, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006002 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006003 long lnum;
6004 int col, nr;
6005 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006006 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006007 int valid, status;
6008 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006009 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006010
Bram Moolenaara3921f42017-06-04 15:30:34 +02006011 if (action == ' ' || qf_idx == qi->qf_listcount)
6012 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006013 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006014 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006015 qf_idx = qi->qf_curlist;
6016 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006017 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006018 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02006019 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006020 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006021 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006022 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006023 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006024 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006025
6026 for (li = list->lv_first; li != NULL; li = li->li_next)
6027 {
6028 if (li->li_tv.v_type != VAR_DICT)
6029 continue; /* Skip non-dict items */
6030
6031 d = li->li_tv.vval.v_dict;
6032 if (d == NULL)
6033 continue;
6034
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006035 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaard76ce852018-05-01 15:02:04 +02006036 module = get_dict_string(d, (char_u *)"module", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006037 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6038 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6039 col = (int)get_dict_number(d, (char_u *)"col");
6040 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6041 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006042 type = get_dict_string(d, (char_u *)"type", TRUE);
6043 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6044 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006045 if (text == NULL)
6046 text = vim_strsave((char_u *)"");
6047
6048 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006049 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006050 valid = FALSE;
6051
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006052 /* Mark entries with non-existing buffer number as not valid. Give the
6053 * error message only once. */
6054 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6055 {
6056 if (!did_bufnr_emsg)
6057 {
6058 did_bufnr_emsg = TRUE;
6059 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6060 }
6061 valid = FALSE;
6062 bufnum = 0;
6063 }
6064
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02006065 /* If the 'valid' field is present it overrules the detected value. */
6066 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6067 valid = (int)get_dict_number(d, (char_u *)"valid");
6068
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006069 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006070 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006071 NULL, /* dir */
6072 filename,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006073 module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006074 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006075 text,
6076 lnum,
6077 col,
6078 vcol, /* vis_col */
6079 pattern, /* search pattern */
6080 nr,
6081 type == NULL ? NUL : *type,
6082 valid);
6083
6084 vim_free(filename);
Bram Moolenaard76ce852018-05-01 15:02:04 +02006085 vim_free(module);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006086 vim_free(pattern);
6087 vim_free(text);
6088 vim_free(type);
6089
6090 if (status == FAIL)
6091 {
6092 retval = FAIL;
6093 break;
6094 }
6095 }
6096
Bram Moolenaara3921f42017-06-04 15:30:34 +02006097 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02006098 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02006099 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006100 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02006101 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006102 if (action != 'a')
6103 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006104 qi->qf_lists[qf_idx].qf_ptr =
6105 qi->qf_lists[qf_idx].qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006106 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara3921f42017-06-04 15:30:34 +02006107 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006108 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006109
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006110 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02006111 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006112
6113 return retval;
6114}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006115
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006116/*
6117 * Get the quickfix list index from 'nr' or 'id'
6118 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006119 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006120qf_setprop_get_qfidx(
6121 qf_info_T *qi,
6122 dict_T *what,
6123 int action,
6124 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006125{
6126 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006127 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006128
Bram Moolenaard823fa92016-08-12 16:29:27 +02006129 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6130 {
6131 /* Use the specified quickfix/location list */
6132 if (di->di_tv.v_type == VAR_NUMBER)
6133 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006134 /* for zero use the current list */
6135 if (di->di_tv.vval.v_number != 0)
6136 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006137
Bram Moolenaar55b69262017-08-13 13:42:01 +02006138 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6139 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006140 /*
6141 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02006142 * non-available list and add the new list at the end of the
6143 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006144 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006145 *newlist = TRUE;
6146 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006147 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006148 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006149 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006150 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006151 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02006152 }
6153 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006154 && di->di_tv.vval.v_string != NULL
6155 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006156 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006157 if (qi->qf_listcount > 0)
6158 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006159 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006160 qf_idx = 0;
6161 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006162 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006163 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006164 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006165 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006166 }
6167
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006168 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006169 {
6170 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006171 if (di->di_tv.v_type != VAR_NUMBER)
6172 return INVALID_QFIDX;
6173
6174 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006175 }
6176
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006177 return qf_idx;
6178}
6179
6180/*
6181 * Set the quickfix list title.
6182 */
6183 static int
6184qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6185{
6186 if (di->di_tv.v_type != VAR_STRING)
6187 return FAIL;
6188
6189 vim_free(qi->qf_lists[qf_idx].qf_title);
6190 qi->qf_lists[qf_idx].qf_title =
6191 get_dict_string(what, (char_u *)"title", TRUE);
6192 if (qf_idx == qi->qf_curlist)
6193 qf_update_win_titlevar(qi);
6194
6195 return OK;
6196}
6197
6198/*
6199 * Set quickfix list items/entries.
6200 */
6201 static int
6202qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6203{
6204 int retval = FAIL;
6205 char_u *title_save;
6206
6207 if (di->di_tv.v_type != VAR_LIST)
6208 return FAIL;
6209
6210 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6211 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6212 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006213 vim_free(title_save);
6214
6215 return retval;
6216}
6217
6218/*
6219 * Set quickfix list items/entries from a list of lines.
6220 */
6221 static int
6222qf_setprop_items_from_lines(
6223 qf_info_T *qi,
6224 int qf_idx,
6225 dict_T *what,
6226 dictitem_T *di,
6227 int action)
6228{
6229 char_u *errorformat = p_efm;
6230 dictitem_T *efm_di;
6231 int retval = FAIL;
6232
6233 /* Use the user supplied errorformat settings (if present) */
6234 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6235 {
6236 if (efm_di->di_tv.v_type != VAR_STRING ||
6237 efm_di->di_tv.vval.v_string == NULL)
6238 return FAIL;
6239 errorformat = efm_di->di_tv.vval.v_string;
6240 }
6241
6242 /* Only a List value is supported */
6243 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6244 return FAIL;
6245
6246 if (action == 'r')
6247 qf_free_items(qi, qf_idx);
6248 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6249 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6250 retval = OK;
6251
6252 return retval;
6253}
6254
6255/*
6256 * Set quickfix list context.
6257 */
6258 static int
6259qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di)
6260{
6261 typval_T *ctx;
6262
6263 free_tv(qi->qf_lists[qf_idx].qf_ctx);
6264 ctx = alloc_tv();
6265 if (ctx != NULL)
6266 copy_tv(&di->di_tv, ctx);
6267 qi->qf_lists[qf_idx].qf_ctx = ctx;
6268
6269 return OK;
6270}
6271
6272/*
6273 * Set quickfix/location list properties (title, items, context).
6274 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006275 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006276 */
6277 static int
6278qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6279{
6280 dictitem_T *di;
6281 int retval = FAIL;
6282 int qf_idx;
6283 int newlist = FALSE;
6284
6285 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
6286 newlist = TRUE;
6287
6288 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
6289 if (qf_idx == INVALID_QFIDX) /* List not found */
6290 return FAIL;
6291
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006292 if (newlist)
6293 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006294 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006295 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006296 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006297 }
6298
6299 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006300 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006301 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006302 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006303 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006304 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006305 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006306 retval = qf_setprop_context(qi, qf_idx, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006307
Bram Moolenaarb254af32017-12-18 19:48:58 +01006308 if (retval == OK)
6309 qf_list_changed(qi, qf_idx);
6310
Bram Moolenaard823fa92016-08-12 16:29:27 +02006311 return retval;
6312}
6313
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006314/*
6315 * Find the non-location list window with the specified location list.
6316 */
6317 static win_T *
6318find_win_with_ll(qf_info_T *qi)
6319{
6320 win_T *wp = NULL;
6321
6322 FOR_ALL_WINDOWS(wp)
6323 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6324 return wp;
6325
6326 return NULL;
6327}
6328
6329/*
6330 * Free the entire quickfix/location list stack.
6331 * If the quickfix/location list window is open, then clear it.
6332 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006333 static void
6334qf_free_stack(win_T *wp, qf_info_T *qi)
6335{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006336 win_T *qfwin = qf_find_win(qi);
6337 win_T *llwin = NULL;
6338 win_T *orig_wp = wp;
6339
6340 if (qfwin != NULL)
6341 {
6342 /* If the quickfix/location list window is open, then clear it */
6343 if (qi->qf_curlist < qi->qf_listcount)
6344 qf_free(qi, qi->qf_curlist);
6345 qf_update_buffer(qi, NULL);
6346 }
6347
6348 if (wp != NULL && IS_LL_WINDOW(wp))
6349 {
6350 /* If in the location list window, then use the non-location list
6351 * window with this location list (if present)
6352 */
6353 llwin = find_win_with_ll(qi);
6354 if (llwin != NULL)
6355 wp = llwin;
6356 }
6357
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006358 qf_free_all(wp);
6359 if (wp == NULL)
6360 {
6361 /* quickfix list */
6362 qi->qf_curlist = 0;
6363 qi->qf_listcount = 0;
6364 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006365 else if (IS_LL_WINDOW(orig_wp))
6366 {
6367 /* If the location list window is open, then create a new empty
6368 * location list */
6369 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006370
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006371 /* first free the list reference in the location list window */
6372 ll_free_all(&orig_wp->w_llist_ref);
6373
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006374 orig_wp->w_llist_ref = new_ll;
6375 if (llwin != NULL)
6376 {
6377 llwin->w_llist = new_ll;
6378 new_ll->qf_refcount++;
6379 }
6380 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006381}
6382
Bram Moolenaard823fa92016-08-12 16:29:27 +02006383/*
6384 * Populate the quickfix list with the items supplied in the list
6385 * of dictionaries. "title" will be copied to w:quickfix_title.
6386 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6387 */
6388 int
6389set_errorlist(
6390 win_T *wp,
6391 list_T *list,
6392 int action,
6393 char_u *title,
6394 dict_T *what)
6395{
6396 qf_info_T *qi = &ql_info;
6397 int retval = OK;
6398
6399 if (wp != NULL)
6400 {
6401 qi = ll_get_or_alloc_list(wp);
6402 if (qi == NULL)
6403 return FAIL;
6404 }
6405
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006406 if (action == 'f')
6407 {
6408 /* Free the entire quickfix or location list stack */
6409 qf_free_stack(wp, qi);
6410 }
6411 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006412 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006413 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006414 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006415 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006416 if (retval == OK)
6417 qf_list_changed(qi, qi->qf_curlist);
6418 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006419
6420 return retval;
6421}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006422
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006423/*
6424 * Mark the context as in use for all the lists in a quickfix stack.
6425 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006426 static int
6427mark_quickfix_ctx(qf_info_T *qi, int copyID)
6428{
6429 int i;
6430 int abort = FALSE;
6431 typval_T *ctx;
6432
6433 for (i = 0; i < LISTCOUNT && !abort; ++i)
6434 {
6435 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006436 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6437 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006438 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6439 }
6440
6441 return abort;
6442}
6443
6444/*
6445 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006446 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006447 */
6448 int
6449set_ref_in_quickfix(int copyID)
6450{
6451 int abort = FALSE;
6452 tabpage_T *tp;
6453 win_T *win;
6454
6455 abort = mark_quickfix_ctx(&ql_info, copyID);
6456 if (abort)
6457 return abort;
6458
6459 FOR_ALL_TAB_WINDOWS(tp, win)
6460 {
6461 if (win->w_llist != NULL)
6462 {
6463 abort = mark_quickfix_ctx(win->w_llist, copyID);
6464 if (abort)
6465 return abort;
6466 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006467 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6468 {
6469 /* In a location list window and none of the other windows is
6470 * referring to this location list. Mark the location list
6471 * context as still in use.
6472 */
6473 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6474 if (abort)
6475 return abort;
6476 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006477 }
6478
6479 return abort;
6480}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006481#endif
6482
Bram Moolenaar81695252004-12-29 20:58:21 +00006483/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006484 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006485 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006486 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006487 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006488 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006489 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006490 */
6491 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006492ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006493{
6494 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006495 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006496 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006497 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006498 int_u save_qfid;
6499 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006500
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006501 switch (eap->cmdidx)
6502 {
6503 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6504 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6505 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6506 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6507 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6508 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6509 default: break;
6510 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006511 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6512 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006513 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006514#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006515 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006516 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006517#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006518 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006519
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006520 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01006521 if (eap->cmdidx == CMD_lbuffer
6522 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006523 || eap->cmdidx == CMD_laddbuffer)
6524 {
6525 qi = ll_get_or_alloc_list(curwin);
6526 if (qi == NULL)
6527 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006528 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006529 }
6530
Bram Moolenaar86b68352004-12-27 21:59:20 +00006531 if (*eap->arg == NUL)
6532 buf = curbuf;
6533 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6534 buf = buflist_findnr(atoi((char *)eap->arg));
6535 if (buf == NULL)
6536 EMSG(_(e_invarg));
6537 else if (buf->b_ml.ml_mfp == NULL)
6538 EMSG(_("E681: Buffer is not loaded"));
6539 else
6540 {
6541 if (eap->addr_count == 0)
6542 {
6543 eap->line1 = 1;
6544 eap->line2 = buf->b_ml.ml_line_count;
6545 }
6546 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6547 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6548 EMSG(_(e_invrange));
6549 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006550 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006551 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006552
6553 if (buf->b_sfname)
6554 {
6555 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6556 (char *)qf_title, (char *)buf->b_sfname);
6557 qf_title = IObuff;
6558 }
6559
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006560 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006561 (eap->cmdidx != CMD_caddbuffer
6562 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006563 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006564 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006565 if (res >= 0)
6566 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006567
6568 // Remember the current quickfix list identifier, so that we can
6569 // check for autocommands changing the current quickfix list.
6570 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006571 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006572 {
6573 buf_T *curbuf_old = curbuf;
6574
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006575 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6576 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006577 if (curbuf != curbuf_old)
6578 // Autocommands changed buffer, don't jump now, "qi" may
6579 // be invalid.
6580 res = 0;
6581 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006582 // Jump to the first error for a new list and if autocmds didn't
6583 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006584 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006585 eap->cmdidx == CMD_lbuffer)
6586 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006587 // display the first error
6588 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar754b5602006-02-09 23:53:20 +00006589 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006590 }
6591}
6592
Bram Moolenaar1e015462005-09-25 22:16:38 +00006593#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006594/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006595 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6596 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006597 */
6598 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006599ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006600{
6601 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006602 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006603 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006604 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006605 int_u save_qfid;
6606 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006607
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006608 switch (eap->cmdidx)
6609 {
6610 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6611 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6612 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6613 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6614 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6615 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6616 default: break;
6617 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006618 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6619 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006620 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006621#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006622 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006623 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006624#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006625 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006626
Bram Moolenaar3c097222017-12-21 20:54:49 +01006627 if (eap->cmdidx == CMD_lexpr
6628 || eap->cmdidx == CMD_lgetexpr
6629 || eap->cmdidx == CMD_laddexpr)
6630 {
6631 qi = ll_get_or_alloc_list(curwin);
6632 if (qi == NULL)
6633 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006634 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006635 }
6636
Bram Moolenaar4770d092006-01-12 23:22:24 +00006637 /* Evaluate the expression. When the result is a string or a list we can
6638 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006639 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006640 if (tv != NULL)
6641 {
6642 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6643 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6644 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006645 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006646 (eap->cmdidx != CMD_caddexpr
6647 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006648 (linenr_T)0, (linenr_T)0,
6649 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006650 if (res >= 0)
6651 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006652
6653 // Remember the current quickfix list identifier, so that we can
6654 // check for autocommands changing the current quickfix list.
6655 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006656 if (au_name != NULL)
6657 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6658 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006659
6660 // Jump to the first error for a new list and if autocmds didn't
6661 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006662 if (res > 0 && (eap->cmdidx == CMD_cexpr
6663 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006664 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006665 // display the first error
6666 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006667 }
6668 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006669 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006670 free_tv(tv);
6671 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006672}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006673#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006674
6675/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006676 * Get the location list for ":lhelpgrep"
6677 */
6678 static qf_info_T *
6679hgr_get_ll(int *new_ll)
6680{
6681 win_T *wp;
6682 qf_info_T *qi;
6683
6684 /* If the current window is a help window, then use it */
6685 if (bt_help(curwin->w_buffer))
6686 wp = curwin;
6687 else
6688 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006689 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006690
6691 if (wp == NULL) /* Help window not found */
6692 qi = NULL;
6693 else
6694 qi = wp->w_llist;
6695
6696 if (qi == NULL)
6697 {
6698 /* Allocate a new location list for help text matches */
6699 if ((qi = ll_new_list()) == NULL)
6700 return NULL;
6701 *new_ll = TRUE;
6702 }
6703
6704 return qi;
6705}
6706
6707/*
6708 * Search for a pattern in a help file.
6709 */
6710 static void
6711hgr_search_file(
6712 qf_info_T *qi,
6713 char_u *fname,
6714#ifdef FEAT_MBYTE
6715 vimconv_T *p_vc,
6716#endif
6717 regmatch_T *p_regmatch)
6718{
6719 FILE *fd;
6720 long lnum;
6721
6722 fd = mch_fopen((char *)fname, "r");
6723 if (fd == NULL)
6724 return;
6725
6726 lnum = 1;
6727 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6728 {
6729 char_u *line = IObuff;
6730#ifdef FEAT_MBYTE
6731 /* Convert a line if 'encoding' is not utf-8 and
6732 * the line contains a non-ASCII character. */
6733 if (p_vc->vc_type != CONV_NONE
6734 && has_non_ascii(IObuff))
6735 {
6736 line = string_convert(p_vc, IObuff, NULL);
6737 if (line == NULL)
6738 line = IObuff;
6739 }
6740#endif
6741
6742 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6743 {
6744 int l = (int)STRLEN(line);
6745
6746 /* remove trailing CR, LF, spaces, etc. */
6747 while (l > 0 && line[l - 1] <= ' ')
6748 line[--l] = NUL;
6749
6750 if (qf_add_entry(qi,
6751 qi->qf_curlist,
6752 NULL, /* dir */
6753 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006754 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006755 0,
6756 line,
6757 lnum,
6758 (int)(p_regmatch->startp[0] - line)
6759 + 1, /* col */
6760 FALSE, /* vis_col */
6761 NULL, /* search pattern */
6762 0, /* nr */
6763 1, /* type */
6764 TRUE /* valid */
6765 ) == FAIL)
6766 {
6767 got_int = TRUE;
6768#ifdef FEAT_MBYTE
6769 if (line != IObuff)
6770 vim_free(line);
6771#endif
6772 break;
6773 }
6774 }
6775#ifdef FEAT_MBYTE
6776 if (line != IObuff)
6777 vim_free(line);
6778#endif
6779 ++lnum;
6780 line_breakcheck();
6781 }
6782 fclose(fd);
6783}
6784
6785/*
6786 * Search for a pattern in all the help files in the doc directory under
6787 * the given directory.
6788 */
6789 static void
6790hgr_search_files_in_dir(
6791 qf_info_T *qi,
6792 char_u *dirname,
6793 regmatch_T *p_regmatch
6794#ifdef FEAT_MBYTE
6795 , vimconv_T *p_vc
6796#endif
6797#ifdef FEAT_MULTI_LANG
6798 , char_u *lang
6799#endif
6800 )
6801{
6802 int fcount;
6803 char_u **fnames;
6804 int fi;
6805
6806 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6807 add_pathsep(dirname);
6808 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6809 if (gen_expand_wildcards(1, &dirname, &fcount,
6810 &fnames, EW_FILE|EW_SILENT) == OK
6811 && fcount > 0)
6812 {
6813 for (fi = 0; fi < fcount && !got_int; ++fi)
6814 {
6815#ifdef FEAT_MULTI_LANG
6816 /* Skip files for a different language. */
6817 if (lang != NULL
6818 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006819 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006820 && !(STRNICMP(lang, "en", 2) == 0
6821 && STRNICMP("txt", fnames[fi]
6822 + STRLEN(fnames[fi]) - 3, 3) == 0))
6823 continue;
6824#endif
6825
6826 hgr_search_file(qi, fnames[fi],
6827#ifdef FEAT_MBYTE
6828 p_vc,
6829#endif
6830 p_regmatch);
6831 }
6832 FreeWild(fcount, fnames);
6833 }
6834}
6835
6836/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006837 * Search for a pattern in all the help files in the 'runtimepath'
6838 * and add the matches to a quickfix list.
6839 * 'arg' is the language specifier. If supplied, then only matches in the
6840 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006841 */
6842 static void
6843hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *arg)
6844{
6845 char_u *p;
6846#ifdef FEAT_MULTI_LANG
6847 char_u *lang;
6848#endif
6849
6850#ifdef FEAT_MBYTE
6851 vimconv_T vc;
6852
6853 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6854 * differs. */
6855 vc.vc_type = CONV_NONE;
6856 if (!enc_utf8)
6857 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6858#endif
6859
6860#ifdef FEAT_MULTI_LANG
6861 /* Check for a specified language */
6862 lang = check_help_lang(arg);
6863#endif
6864
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006865 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006866 p = p_rtp;
6867 while (*p != NUL && !got_int)
6868 {
6869 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6870
6871 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6872#ifdef FEAT_MBYTE
6873 , &vc
6874#endif
6875#ifdef FEAT_MULTI_LANG
6876 , lang
6877#endif
6878 );
6879 }
6880
6881#ifdef FEAT_MBYTE
6882 if (vc.vc_type != CONV_NONE)
6883 convert_setup(&vc, NULL, NULL);
6884#endif
6885}
6886
6887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 * ":helpgrep {pattern}"
6889 */
6890 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006891ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892{
6893 regmatch_T regmatch;
6894 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006895 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006896 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006897 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898
Bram Moolenaar73633f82012-01-20 13:39:07 +01006899 switch (eap->cmdidx)
6900 {
6901 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6902 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6903 default: break;
6904 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006905 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6906 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006907 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006908#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006909 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006910 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006911#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006912 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006913
6914 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6915 save_cpo = p_cpo;
6916 p_cpo = empty_option;
6917
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006918 if (eap->cmdidx == CMD_lhelpgrep)
6919 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006920 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006921 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006922 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006923 }
6924
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6926 regmatch.rm_ic = FALSE;
6927 if (regmatch.regprog != NULL)
6928 {
6929 /* create a new quickfix list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006930 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006932 hgr_search_in_rtp(qi, &regmatch, eap->arg);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006933
Bram Moolenaar473de612013-06-08 18:19:48 +02006934 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006936 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6937 qi->qf_lists[qi->qf_curlist].qf_ptr =
6938 qi->qf_lists[qi->qf_curlist].qf_start;
6939 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 }
6941
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006942 if (p_cpo == empty_option)
6943 p_cpo = save_cpo;
6944 else
6945 /* Darn, some plugin changed the value. */
6946 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
Bram Moolenaarb254af32017-12-18 19:48:58 +01006948 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006949 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950
Bram Moolenaar73633f82012-01-20 13:39:07 +01006951 if (au_name != NULL)
6952 {
6953 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6954 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02006955 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006956 /* autocommands made "qi" invalid */
6957 return;
6958 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006961 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006962 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006963 else
6964 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006965
6966 if (eap->cmdidx == CMD_lhelpgrep)
6967 {
6968 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00006969 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006970 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006971 {
6972 if (new_qi)
6973 ll_free_all(&qi);
6974 }
6975 else if (curwin->w_llist == NULL)
6976 curwin->w_llist = qi;
6977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978}
6979
6980#endif /* FEAT_QUICKFIX */