blob: ac40b24dd369016b29dc8a1b104600fb307b5d3d [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.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004341 * Returns OK if successfully restored the list. Returns FAIL if the list with
4342 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004343 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004344 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004345qf_restore_list(qf_info_T *qi, int_u save_qfid)
4346{
4347 int curlist;
4348
4349 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4350 {
4351 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004352 if (curlist < 0)
4353 // list is not present
4354 return FAIL;
4355 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004356 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004357 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004358}
4359
4360/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004361 * Jump to the first entry if there is one.
4362 */
4363 static void
4364qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4365{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004366 if (qf_restore_list(qi, save_qfid) == FAIL)
4367 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004368
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004369 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004370 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004371 qf_jump(qi, 0, 0, forceit);
4372}
4373
4374/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004375 * Return TRUE when using ":vimgrep" for ":grep".
4376 */
4377 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004378grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004379{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004380 return ((cmdidx == CMD_grep
4381 || cmdidx == CMD_lgrep
4382 || cmdidx == CMD_grepadd
4383 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004384 && STRCMP("internal",
4385 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4386}
4387
4388/*
Bram Moolenaara6557602006-02-04 22:43:20 +00004389 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 */
4391 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004392ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393{
Bram Moolenaar7c626922005-02-07 22:01:03 +00004394 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004396 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00004398 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004399 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004400 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004401 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004402 int_u save_qfid;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004403
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004404 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
4405 if (grep_internal(eap->cmdidx))
4406 {
4407 ex_vimgrep(eap);
4408 return;
4409 }
4410
Bram Moolenaar7c626922005-02-07 22:01:03 +00004411 switch (eap->cmdidx)
4412 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00004413 case CMD_make: au_name = (char_u *)"make"; break;
4414 case CMD_lmake: au_name = (char_u *)"lmake"; break;
4415 case CMD_grep: au_name = (char_u *)"grep"; break;
4416 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4417 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4418 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004419 default: break;
4420 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004421 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4422 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004423 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004424#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004425 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004426 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004427#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004428 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004429#ifdef FEAT_MBYTE
4430 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
Bram Moolenaara6557602006-02-04 22:43:20 +00004433 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
4434 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004435 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004436
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00004438 fname = get_mef_name();
4439 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004441 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442
4443 /*
4444 * If 'shellpipe' empty: don't redirect to 'errorfile'.
4445 */
4446 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
4447 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00004448 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 cmd = alloc(len);
4450 if (cmd == NULL)
4451 return;
4452 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
4453 (char *)p_shq);
4454 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004455 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 /*
4457 * Output a newline if there's something else than the :make command that
4458 * was typed (in which case the cursor is in column 0).
4459 */
4460 if (msg_col == 0)
4461 msg_didout = FALSE;
4462 msg_start();
4463 MSG_PUTS(":!");
4464 msg_outtrans(cmd); /* show what we are doing */
4465
4466 /* let the shell know if we are redirecting output or not */
4467 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4468
4469#ifdef AMIGA
4470 out_flush();
4471 /* read window status report and redraw before message */
4472 (void)char_avail();
4473#endif
4474
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004475 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00004476 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4477 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004478 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004479 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004480 if (wp != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004481 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004482 qi = GET_LOC_LIST(wp);
4483 if (qi == NULL)
4484 goto cleanup;
4485 }
4486 if (res >= 0)
4487 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004488
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004489 // Remember the current quickfix list identifier, so that we can
4490 // check for autocommands changing the current quickfix list.
4491 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4492 if (au_name != NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004493 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4494 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004495 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004496 // display the first error
4497 qf_jump_first(qi, save_qfid, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004499cleanup:
Bram Moolenaar7c626922005-02-07 22:01:03 +00004500 mch_remove(fname);
4501 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 vim_free(cmd);
4503}
4504
4505/*
4506 * Return the name for the errorfile, in allocated memory.
4507 * Find a new unique name when 'makeef' contains "##".
4508 * Returns NULL for error.
4509 */
4510 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004511get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512{
4513 char_u *p;
4514 char_u *name;
4515 static int start = -1;
4516 static int off = 0;
4517#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004518 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519#endif
4520
4521 if (*p_mef == NUL)
4522 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004523 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 if (name == NULL)
4525 EMSG(_(e_notmp));
4526 return name;
4527 }
4528
4529 for (p = p_mef; *p; ++p)
4530 if (p[0] == '#' && p[1] == '#')
4531 break;
4532
4533 if (*p == NUL)
4534 return vim_strsave(p_mef);
4535
4536 /* Keep trying until the name doesn't exist yet. */
4537 for (;;)
4538 {
4539 if (start == -1)
4540 start = mch_get_pid();
4541 else
4542 off += 19;
4543
4544 name = alloc((unsigned)STRLEN(p_mef) + 30);
4545 if (name == NULL)
4546 break;
4547 STRCPY(name, p_mef);
4548 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4549 STRCAT(name, p + 2);
4550 if (mch_getperm(name) < 0
4551#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02004552 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 && mch_lstat((char *)name, &sb) < 0
4554#endif
4555 )
4556 break;
4557 vim_free(name);
4558 }
4559 return name;
4560}
4561
4562/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004563 * Returns the number of valid entries in the current quickfix/location list.
4564 */
4565 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004566qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004567{
4568 qf_info_T *qi = &ql_info;
4569 qfline_T *qfp;
4570 int i, sz = 0;
4571 int prev_fnum = 0;
4572
4573 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4574 {
4575 /* Location list */
4576 qi = GET_LOC_LIST(curwin);
4577 if (qi == NULL)
4578 return 0;
4579 }
4580
4581 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004582 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004583 ++i, qfp = qfp->qf_next)
4584 {
4585 if (qfp->qf_valid)
4586 {
4587 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4588 sz++; /* Count all valid entries */
4589 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4590 {
4591 /* Count the number of files */
4592 sz++;
4593 prev_fnum = qfp->qf_fnum;
4594 }
4595 }
4596 }
4597
4598 return sz;
4599}
4600
4601/*
4602 * Returns the current index of the quickfix/location list.
4603 * Returns 0 if there is an error.
4604 */
4605 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004606qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004607{
4608 qf_info_T *qi = &ql_info;
4609
4610 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4611 {
4612 /* Location list */
4613 qi = GET_LOC_LIST(curwin);
4614 if (qi == NULL)
4615 return 0;
4616 }
4617
4618 return qi->qf_lists[qi->qf_curlist].qf_index;
4619}
4620
4621/*
4622 * Returns the current index in the quickfix/location list (counting only valid
4623 * entries). If no valid entries are in the list, then returns 1.
4624 */
4625 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004626qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004627{
4628 qf_info_T *qi = &ql_info;
4629 qf_list_T *qfl;
4630 qfline_T *qfp;
4631 int i, eidx = 0;
4632 int prev_fnum = 0;
4633
4634 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4635 {
4636 /* Location list */
4637 qi = GET_LOC_LIST(curwin);
4638 if (qi == NULL)
4639 return 1;
4640 }
4641
4642 qfl = &qi->qf_lists[qi->qf_curlist];
4643 qfp = qfl->qf_start;
4644
4645 /* check if the list has valid errors */
4646 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4647 return 1;
4648
4649 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4650 {
4651 if (qfp->qf_valid)
4652 {
4653 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4654 {
4655 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4656 {
4657 /* Count the number of files */
4658 eidx++;
4659 prev_fnum = qfp->qf_fnum;
4660 }
4661 }
4662 else
4663 eidx++;
4664 }
4665 }
4666
4667 return eidx ? eidx : 1;
4668}
4669
4670/*
4671 * Get the 'n'th valid error entry in the quickfix or location list.
4672 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4673 * For :cdo and :ldo returns the 'n'th valid error entry.
4674 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4675 */
4676 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004677qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004678{
4679 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4680 qfline_T *qfp = qfl->qf_start;
4681 int i, eidx;
4682 int prev_fnum = 0;
4683
4684 /* check if the list has valid errors */
4685 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4686 return 1;
4687
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004688 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004689 i++, qfp = qfp->qf_next)
4690 {
4691 if (qfp->qf_valid)
4692 {
4693 if (fdo)
4694 {
4695 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4696 {
4697 /* Count the number of files */
4698 eidx++;
4699 prev_fnum = qfp->qf_fnum;
4700 }
4701 }
4702 else
4703 eidx++;
4704 }
4705
4706 if (eidx == n)
4707 break;
4708 }
4709
4710 if (i <= qfl->qf_count)
4711 return i;
4712 else
4713 return 1;
4714}
4715
4716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004718 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004719 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 */
4721 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004722ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004724 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004725 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004726
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004727 if (eap->cmdidx == CMD_ll
4728 || eap->cmdidx == CMD_lrewind
4729 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004730 || eap->cmdidx == CMD_llast
4731 || eap->cmdidx == CMD_ldo
4732 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004733 {
4734 qi = GET_LOC_LIST(curwin);
4735 if (qi == NULL)
4736 {
4737 EMSG(_(e_loclist));
4738 return;
4739 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004740 }
4741
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004742 if (eap->addr_count > 0)
4743 errornr = (int)eap->line2;
4744 else
4745 {
4746 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4747 errornr = 0;
4748 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4749 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4750 errornr = 1;
4751 else
4752 errornr = 32767;
4753 }
4754
4755 /* For cdo and ldo commands, jump to the nth valid error.
4756 * For cfdo and lfdo commands, jump to the nth valid file entry.
4757 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004758 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4759 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004760 errornr = qf_get_nth_valid_entry(qi,
4761 eap->addr_count > 0 ? (int)eap->line1 : 1,
4762 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4763
4764 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765}
4766
4767/*
4768 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004769 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004770 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 */
4772 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004773ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004775 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004776 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004777
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004778 if (eap->cmdidx == CMD_lnext
4779 || eap->cmdidx == CMD_lNext
4780 || eap->cmdidx == CMD_lprevious
4781 || eap->cmdidx == CMD_lnfile
4782 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004783 || eap->cmdidx == CMD_lpfile
4784 || eap->cmdidx == CMD_ldo
4785 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004786 {
4787 qi = GET_LOC_LIST(curwin);
4788 if (qi == NULL)
4789 {
4790 EMSG(_(e_loclist));
4791 return;
4792 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004793 }
4794
Bram Moolenaar55b69262017-08-13 13:42:01 +02004795 if (eap->addr_count > 0
4796 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4797 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004798 errornr = (int)eap->line2;
4799 else
4800 errornr = 1;
4801
4802 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4803 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004805 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4806 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004808 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4809 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 ? BACKWARD_FILE
4811 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004812 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813}
4814
4815/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004816 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004817 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 */
4819 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004820ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004822 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004823 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004824 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004825 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004826 int_u save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004827 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004828
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004829 switch (eap->cmdidx)
4830 {
4831 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4832 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4833 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4834 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4835 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4836 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4837 default: break;
4838 }
4839 if (au_name != NULL)
4840 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004841#ifdef FEAT_MBYTE
4842 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4843#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004844#ifdef FEAT_BROWSE
4845 if (cmdmod.browse)
4846 {
4847 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004848 NULL, NULL,
4849 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004850 if (browse_file == NULL)
4851 return;
4852 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4853 vim_free(browse_file);
4854 }
4855 else
4856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004858 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004859
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004860 if (eap->cmdidx == CMD_lfile
4861 || eap->cmdidx == CMD_lgetfile
4862 || eap->cmdidx == CMD_laddfile)
4863 wp = curwin;
4864
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004865 /*
4866 * This function is used by the :cfile, :cgetfile and :caddfile
4867 * commands.
4868 * :cfile always creates a new quickfix list and jumps to the
4869 * first error.
4870 * :cgetfile creates a new quickfix list but doesn't jump to the
4871 * first error.
4872 * :caddfile adds to an existing quickfix list. If there is no
4873 * quickfix list then a new list is created.
4874 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004875 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004876 && eap->cmdidx != CMD_laddfile),
4877 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004878 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004879 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01004880 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004881 if (qi == NULL)
4882 return;
4883 }
4884 if (res >= 0)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004885 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004886 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004887 if (au_name != NULL)
4888 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004889
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004890 // Jump to the first error for a new list and if autocmds didn't
4891 // free the list.
4892 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
4893 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004894 // display the first error
4895 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004896}
4897
4898/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004899 * Return the vimgrep autocmd name.
4900 */
4901 static char_u *
4902vgr_get_auname(cmdidx_T cmdidx)
4903{
4904 switch (cmdidx)
4905 {
4906 case CMD_vimgrep: return (char_u *)"vimgrep";
4907 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4908 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4909 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4910 case CMD_grep: return (char_u *)"grep";
4911 case CMD_lgrep: return (char_u *)"lgrep";
4912 case CMD_grepadd: return (char_u *)"grepadd";
4913 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4914 default: return NULL;
4915 }
4916}
4917
4918/*
4919 * Initialize the regmatch used by vimgrep for pattern "s".
4920 */
4921 static void
4922vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4923{
4924 /* Get the search pattern: either white-separated or enclosed in // */
4925 regmatch->regprog = NULL;
4926
4927 if (s == NULL || *s == NUL)
4928 {
4929 /* Pattern is empty, use last search pattern. */
4930 if (last_search_pat() == NULL)
4931 {
4932 EMSG(_(e_noprevre));
4933 return;
4934 }
4935 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4936 }
4937 else
4938 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4939
4940 regmatch->rmm_ic = p_ic;
4941 regmatch->rmm_maxcol = 0;
4942}
4943
4944/*
4945 * Display a file name when vimgrep is running.
4946 */
4947 static void
4948vgr_display_fname(char_u *fname)
4949{
4950 char_u *p;
4951
4952 msg_start();
4953 p = msg_strtrunc(fname, TRUE);
4954 if (p == NULL)
4955 msg_outtrans(fname);
4956 else
4957 {
4958 msg_outtrans(p);
4959 vim_free(p);
4960 }
4961 msg_clr_eos();
4962 msg_didout = FALSE; /* overwrite this message */
4963 msg_nowait = TRUE; /* don't wait for this message */
4964 msg_col = 0;
4965 out_flush();
4966}
4967
4968/*
4969 * Load a dummy buffer to search for a pattern using vimgrep.
4970 */
4971 static buf_T *
4972vgr_load_dummy_buf(
4973 char_u *fname,
4974 char_u *dirname_start,
4975 char_u *dirname_now)
4976{
4977 int save_mls;
4978#if defined(FEAT_SYN_HL)
4979 char_u *save_ei = NULL;
4980#endif
4981 buf_T *buf;
4982
4983#if defined(FEAT_SYN_HL)
4984 /* Don't do Filetype autocommands to avoid loading syntax and
4985 * indent scripts, a great speed improvement. */
4986 save_ei = au_event_disable(",Filetype");
4987#endif
4988 /* Don't use modelines here, it's useless. */
4989 save_mls = p_mls;
4990 p_mls = 0;
4991
4992 /* Load file into a buffer, so that 'fileencoding' is detected,
4993 * autocommands applied, etc. */
4994 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
4995
4996 p_mls = save_mls;
4997#if defined(FEAT_SYN_HL)
4998 au_event_restore(save_ei);
4999#endif
5000
5001 return buf;
5002}
5003
5004/*
5005 * Check whether a quickfix/location list valid. Autocmds may remove or change
5006 * a quickfix list when vimgrep is running. If the list is not found, create a
5007 * new list.
5008 */
5009 static int
5010vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005011 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005012 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005013 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005014 char_u *title)
5015{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005016 /* Verify that the quickfix/location list was not freed by an autocmd */
5017 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005018 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005019 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005020 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005021 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005022 EMSG(_(e_loc_list_changed));
5023 return FALSE;
5024 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005025 else
5026 {
5027 /* Quickfix list is not found, create a new one. */
5028 qf_new_list(qi, title);
5029 return TRUE;
5030 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005031 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005032
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005033 if (qf_restore_list(qi, qfid) == FAIL)
5034 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005035
5036 return TRUE;
5037}
5038
5039/*
5040 * Search for a pattern in all the lines in a buffer and add the matching lines
5041 * to a quickfix list.
5042 */
5043 static int
5044vgr_match_buflines(
5045 qf_info_T *qi,
5046 char_u *fname,
5047 buf_T *buf,
5048 regmmatch_T *regmatch,
5049 long tomatch,
5050 int duplicate_name,
5051 int flags)
5052{
5053 int found_match = FALSE;
5054 long lnum;
5055 colnr_T col;
5056
5057 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5058 {
5059 col = 0;
5060 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5061 col, NULL, NULL) > 0)
5062 {
5063 /* Pass the buffer number so that it gets used even for a
5064 * dummy buffer, unless duplicate_name is set, then the
5065 * buffer will be wiped out below. */
5066 if (qf_add_entry(qi,
5067 qi->qf_curlist,
5068 NULL, /* dir */
5069 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005070 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005071 duplicate_name ? 0 : buf->b_fnum,
5072 ml_get_buf(buf,
5073 regmatch->startpos[0].lnum + lnum, FALSE),
5074 regmatch->startpos[0].lnum + lnum,
5075 regmatch->startpos[0].col + 1,
5076 FALSE, /* vis_col */
5077 NULL, /* search pattern */
5078 0, /* nr */
5079 0, /* type */
5080 TRUE /* valid */
5081 ) == FAIL)
5082 {
5083 got_int = TRUE;
5084 break;
5085 }
5086 found_match = TRUE;
5087 if (--tomatch == 0)
5088 break;
5089 if ((flags & VGR_GLOBAL) == 0
5090 || regmatch->endpos[0].lnum > 0)
5091 break;
5092 col = regmatch->endpos[0].col
5093 + (col == regmatch->endpos[0].col);
5094 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5095 break;
5096 }
5097 line_breakcheck();
5098 if (got_int)
5099 break;
5100 }
5101
5102 return found_match;
5103}
5104
5105/*
5106 * Jump to the first match and update the directory.
5107 */
5108 static void
5109vgr_jump_to_match(
5110 qf_info_T *qi,
5111 int forceit,
5112 int *redraw_for_dummy,
5113 buf_T *first_match_buf,
5114 char_u *target_dir)
5115{
5116 buf_T *buf;
5117
5118 buf = curbuf;
5119 qf_jump(qi, 0, 0, forceit);
5120 if (buf != curbuf)
5121 /* If we jumped to another buffer redrawing will already be
5122 * taken care of. */
5123 *redraw_for_dummy = FALSE;
5124
5125 /* Jump to the directory used after loading the buffer. */
5126 if (curbuf == first_match_buf && target_dir != NULL)
5127 {
5128 exarg_T ea;
5129
5130 ea.arg = target_dir;
5131 ea.cmdidx = CMD_lcd;
5132 ex_cd(&ea);
5133 }
5134}
5135
5136/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005137 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005138 * ":vimgrepadd {pattern} file(s)"
5139 * ":lvimgrep {pattern} file(s)"
5140 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005141 */
5142 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005143ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005144{
Bram Moolenaar81695252004-12-29 20:58:21 +00005145 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005146 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005147 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005148 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005149 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005150 char_u *s;
5151 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005152 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005153 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005154 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005155 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005156 buf_T *buf;
5157 int duplicate_name = FALSE;
5158 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005159 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005160 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005161 buf_T *first_match_buf = NULL;
5162 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005163 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005164 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005165 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005166 char_u *dirname_start = NULL;
5167 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005168 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005169 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005170
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005171 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005172 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5173 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005174 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005175#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005176 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005177 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005178#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005179 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005180
Bram Moolenaar754b5602006-02-09 23:53:20 +00005181 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005182 || eap->cmdidx == CMD_lvimgrep
5183 || eap->cmdidx == CMD_lgrepadd
5184 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00005185 {
5186 qi = ll_get_or_alloc_list(curwin);
5187 if (qi == NULL)
5188 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005189 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005190 }
5191
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005192 if (eap->addr_count > 0)
5193 tomatch = eap->line2;
5194 else
5195 tomatch = MAXLNUM;
5196
Bram Moolenaar81695252004-12-29 20:58:21 +00005197 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005198 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005199 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005200 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005201 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005202 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005203 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005204 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005205 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005206
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005207 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005208 if (regmatch.regprog == NULL)
5209 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005210
5211 p = skipwhite(p);
5212 if (*p == NUL)
5213 {
5214 EMSG(_("E683: File name missing or invalid pattern"));
5215 goto theend;
5216 }
5217
Bram Moolenaar55b69262017-08-13 13:42:01 +02005218 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005219 && eap->cmdidx != CMD_vimgrepadd
5220 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005221 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005222 /* make place for a new list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005223 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005224
5225 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005226 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005227 goto theend;
5228 if (fcount == 0)
5229 {
5230 EMSG(_(e_nomatch));
5231 goto theend;
5232 }
5233
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005234 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5235 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005236 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005237 {
5238 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005239 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005240 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005241
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005242 /* Remember the current directory, because a BufRead autocommand that does
5243 * ":lcd %:p:h" changes the meaning of short path names. */
5244 mch_dirname(dirname_start, MAXPATHL);
5245
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005246 /* Remember the current quickfix list identifier, so that we can check for
5247 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005248 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005249
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005250 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005251 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005252 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005253 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005254 if (time(NULL) > seconds)
5255 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005256 /* Display the file name every second or so, show the user we are
5257 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005258 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005259 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005260 }
5261
Bram Moolenaar81695252004-12-29 20:58:21 +00005262 buf = buflist_findname_exp(fnames[fi]);
5263 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5264 {
5265 /* Remember that a buffer with this name already exists. */
5266 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005267 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005268 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005269
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005270 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005271 }
5272 else
5273 /* Use existing, loaded buffer. */
5274 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005275
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005276 /* Check whether the quickfix list is still valid. When loading a
5277 * buffer above, autocommands might have changed the quickfix list. */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005278 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005279 {
5280 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005281 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005282 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005283 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005284
Bram Moolenaar81695252004-12-29 20:58:21 +00005285 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005286 {
5287 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005288 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005289 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005290 else
5291 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005292 /* Try for a match in all lines of the buffer.
5293 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005294 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5295 tomatch, duplicate_name, flags);
5296
Bram Moolenaar81695252004-12-29 20:58:21 +00005297 if (using_dummy)
5298 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005299 if (found_match && first_match_buf == NULL)
5300 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005301 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005302 {
Bram Moolenaar81695252004-12-29 20:58:21 +00005303 /* Never keep a dummy buffer if there is another buffer
5304 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005305 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005306 buf = NULL;
5307 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005308 else if (!cmdmod.hide
5309 || buf->b_p_bh[0] == 'u' /* "unload" */
5310 || buf->b_p_bh[0] == 'w' /* "wipe" */
5311 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00005312 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005313 /* When no match was found we don't need to remember the
5314 * buffer, wipe it out. If there was a match and it
5315 * wasn't the first one or we won't jump there: only
5316 * unload the buffer.
5317 * Ignore 'hidden' here, because it may lead to having too
5318 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00005319 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005320 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005321 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005322 buf = NULL;
5323 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005324 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005325 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005326 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02005327 /* Keeping the buffer, remove the dummy flag. */
5328 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005329 buf = NULL;
5330 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005331 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005332
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005333 if (buf != NULL)
5334 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02005335 /* Keeping the buffer, remove the dummy flag. */
5336 buf->b_flags &= ~BF_DUMMY;
5337
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005338 /* If the buffer is still loaded we need to use the
5339 * directory we jumped to below. */
5340 if (buf == first_match_buf
5341 && target_dir == NULL
5342 && STRCMP(dirname_start, dirname_now) != 0)
5343 target_dir = vim_strsave(dirname_now);
5344
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005345 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005346 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00005347 * need to be done (again). But not the window-local
5348 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005349 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005350#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005351 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5352 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005353#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005354 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005355 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005356 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005357 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005358 }
5359 }
5360
5361 FreeWild(fcount, fnames);
5362
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005363 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5364 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
5365 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005366 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005367
Bram Moolenaar864293a2016-06-02 13:40:04 +02005368 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005369
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005370 if (au_name != NULL)
5371 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5372 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01005373 /*
5374 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5375 * is still valid.
5376 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005377 if (!qflist_valid(wp, save_qfid))
5378 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005379
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005380 if (qf_restore_list(qi, save_qfid) == FAIL)
5381 goto theend;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005382
Bram Moolenaar86b68352004-12-27 21:59:20 +00005383 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005384 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005385 {
5386 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005387 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5388 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005389 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005390 else
5391 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005392
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005393 /* If we loaded a dummy buffer into the current window, the autocommands
5394 * may have messed up things, need to redraw and recompute folds. */
5395 if (redraw_for_dummy)
5396 {
5397#ifdef FEAT_FOLDING
5398 foldUpdateAll(curwin);
5399#else
5400 redraw_later(NOT_VALID);
5401#endif
5402 }
5403
Bram Moolenaar86b68352004-12-27 21:59:20 +00005404theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005405 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005406 vim_free(dirname_now);
5407 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005408 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005409 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005410}
5411
5412/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005413 * Restore current working directory to "dirname_start" if they differ, taking
5414 * into account whether it is set locally or globally.
5415 */
5416 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005417restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005418{
5419 char_u *dirname_now = alloc(MAXPATHL);
5420
5421 if (NULL != dirname_now)
5422 {
5423 mch_dirname(dirname_now, MAXPATHL);
5424 if (STRCMP(dirname_start, dirname_now) != 0)
5425 {
5426 /* If the directory has changed, change it back by building up an
5427 * appropriate ex command and executing it. */
5428 exarg_T ea;
5429
5430 ea.arg = dirname_start;
5431 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5432 ex_cd(&ea);
5433 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005434 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005435 }
5436}
5437
5438/*
5439 * Load file "fname" into a dummy buffer and return the buffer pointer,
5440 * placing the directory resulting from the buffer load into the
5441 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5442 * prior to calling this function. Restores directory to "dirname_start" prior
5443 * to returning, if autocmds or the 'autochdir' option have changed it.
5444 *
5445 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5446 * or wipe_dummy_buffer() later!
5447 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005448 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005449 */
5450 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005451load_dummy_buffer(
5452 char_u *fname,
5453 char_u *dirname_start, /* in: old directory */
5454 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005455{
5456 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005457 bufref_T newbufref;
5458 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005459 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005460 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005461 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005462
5463 /* Allocate a buffer without putting it in the buffer list. */
5464 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5465 if (newbuf == NULL)
5466 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005467 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005468
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005469 /* Init the options. */
5470 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5471
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005472 /* need to open the memfile before putting the buffer in a window */
5473 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005474 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005475 /* Make sure this buffer isn't wiped out by autocommands. */
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005476 ++newbuf->b_locked;
5477
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005478 /* set curwin/curbuf to buf and save a few things */
5479 aucmd_prepbuf(&aco, newbuf);
5480
5481 /* Need to set the filename for autocommands. */
5482 (void)setfname(curbuf, fname, NULL, FALSE);
5483
Bram Moolenaar81695252004-12-29 20:58:21 +00005484 /* Create swap file now to avoid the ATTENTION message. */
5485 check_need_swap(TRUE);
5486
5487 /* Remove the "dummy" flag, otherwise autocommands may not
5488 * work. */
5489 curbuf->b_flags &= ~BF_DUMMY;
5490
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005491 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005492 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005493 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005494 NULL, READ_NEW | READ_DUMMY);
5495 --newbuf->b_locked;
5496 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005497 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005498 && !(curbuf->b_flags & BF_NEW))
5499 {
5500 failed = FALSE;
5501 if (curbuf != newbuf)
5502 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005503 /* Bloody autocommands changed the buffer! Can happen when
5504 * using netrw and editing a remote file. Use the current
5505 * buffer instead, delete the dummy one after restoring the
5506 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005507 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005508 newbuf = curbuf;
5509 }
5510 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005511
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005512 /* restore curwin/curbuf and a few other things */
5513 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005514 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5515 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005516
5517 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5518 * skip it. */
5519 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005520 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005521
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005522 /*
5523 * When autocommands/'autochdir' option changed directory: go back.
5524 * Let the caller know what the resulting dir was first, in case it is
5525 * important.
5526 */
5527 mch_dirname(resulting_dir, MAXPATHL);
5528 restore_start_dir(dirname_start);
5529
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005530 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005531 return NULL;
5532 if (failed)
5533 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005534 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005535 return NULL;
5536 }
5537 return newbuf;
5538}
5539
5540/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005541 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5542 * directory to "dirname_start" prior to returning, if autocmds or the
5543 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005544 */
5545 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005546wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005547{
5548 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005549 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005550#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005551 cleanup_T cs;
5552
5553 /* Reset the error/interrupt/exception state here so that aborting()
5554 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5555 * work when got_int is set. */
5556 enter_cleanup(&cs);
5557#endif
5558
Bram Moolenaar81695252004-12-29 20:58:21 +00005559 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005560
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005561#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005562 /* Restore the error/interrupt/exception state if not discarded by a
5563 * new aborting error, interrupt, or uncaught exception. */
5564 leave_cleanup(&cs);
5565#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005566 /* When autocommands/'autochdir' option changed directory: go back. */
5567 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005568 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005569}
5570
5571/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005572 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5573 * directory to "dirname_start" prior to returning, if autocmds or the
5574 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005575 */
5576 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005577unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005578{
5579 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005580 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005581 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005582
5583 /* When autocommands/'autochdir' option changed directory: go back. */
5584 restore_start_dir(dirname_start);
5585 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005586}
5587
Bram Moolenaar05159a02005-02-26 23:04:13 +00005588#if defined(FEAT_EVAL) || defined(PROTO)
5589/*
5590 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005591 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005592 */
5593 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005594get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005595{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005596 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005597 dict_T *dict;
5598 char_u buf[2];
5599 qfline_T *qfp;
5600 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005601 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005602
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005603 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005604 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005605 qi = &ql_info;
5606 if (wp != NULL)
5607 {
5608 qi = GET_LOC_LIST(wp);
5609 if (qi == NULL)
5610 return FAIL;
5611 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005612 }
5613
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005614 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005615 qf_idx = qi->qf_curlist;
5616
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005617 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005618 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005619
Bram Moolenaard823fa92016-08-12 16:29:27 +02005620 qfp = qi->qf_lists[qf_idx].qf_start;
5621 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005622 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005623 /* Handle entries with a non-existing buffer number. */
5624 bufnum = qfp->qf_fnum;
5625 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5626 bufnum = 0;
5627
Bram Moolenaar05159a02005-02-26 23:04:13 +00005628 if ((dict = dict_alloc()) == NULL)
5629 return FAIL;
5630 if (list_append_dict(list, dict) == FAIL)
5631 return FAIL;
5632
5633 buf[0] = qfp->qf_type;
5634 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005635 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5636 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5637 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5638 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5639 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5640 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5641 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5642 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5643 || dict_add_string(dict, "type", buf) == FAIL
5644 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005645 return FAIL;
5646
5647 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005648 if (qfp == NULL)
5649 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005650 }
5651 return OK;
5652}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005653
5654/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005655 * Flags used by getqflist()/getloclist() to determine which fields to return.
5656 */
5657enum {
5658 QF_GETLIST_NONE = 0x0,
5659 QF_GETLIST_TITLE = 0x1,
5660 QF_GETLIST_ITEMS = 0x2,
5661 QF_GETLIST_NR = 0x4,
5662 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005663 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005664 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005665 QF_GETLIST_IDX = 0x40,
5666 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005667 QF_GETLIST_TICK = 0x100,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005668 QF_GETLIST_ALL = 0x1FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005669};
5670
5671/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005672 * Parse text from 'di' and return the quickfix list items.
5673 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005674 */
5675 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005676qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005677{
5678 int status = FAIL;
5679 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005680 char_u *errorformat = p_efm;
5681 dictitem_T *efm_di;
5682 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005683
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005684 /* Only a List value is supported */
5685 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005686 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005687 /* If errorformat is supplied then use it, otherwise use the 'efm'
5688 * option setting
5689 */
5690 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5691 {
5692 if (efm_di->di_tv.v_type != VAR_STRING ||
5693 efm_di->di_tv.vval.v_string == NULL)
5694 return FAIL;
5695 errorformat = efm_di->di_tv.vval.v_string;
5696 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005697
Bram Moolenaar36538222017-09-02 19:51:44 +02005698 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005699 if (l == NULL)
5700 return FAIL;
5701
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005702 qi = ll_new_list();
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005703 if (qi != NULL)
5704 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005705 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005706 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5707 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005708 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005709 qf_free(qi, 0);
5710 }
5711 free(qi);
5712 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005713 dict_add_list(retdict, "items", l);
5714 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005715 }
5716
5717 return status;
5718}
5719
5720/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005721 * Return the quickfix/location list window identifier in the current tabpage.
5722 */
5723 static int
5724qf_winid(qf_info_T *qi)
5725{
5726 win_T *win;
5727
5728 /* The quickfix window can be opened even if the quickfix list is not set
5729 * using ":copen". This is not true for location lists. */
5730 if (qi == NULL)
5731 return 0;
5732 win = qf_find_win(qi);
5733 if (win != NULL)
5734 return win->w_id;
5735 return 0;
5736}
5737
5738/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005739 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005740 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005741 static int
5742qf_getprop_keys2flags(dict_T *what)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005743{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005744 int flags = QF_GETLIST_NONE;
5745
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005746 if (dict_find(what, (char_u *)"all", -1) != NULL)
5747 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005748
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005749 if (dict_find(what, (char_u *)"title", -1) != NULL)
5750 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005751
Bram Moolenaara6d48492017-12-12 22:45:31 +01005752 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5753 flags |= QF_GETLIST_NR;
5754
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005755 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5756 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005757
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005758 if (dict_find(what, (char_u *)"context", -1) != NULL)
5759 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005760
Bram Moolenaara6d48492017-12-12 22:45:31 +01005761 if (dict_find(what, (char_u *)"id", -1) != NULL)
5762 flags |= QF_GETLIST_ID;
5763
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005764 if (dict_find(what, (char_u *)"items", -1) != NULL)
5765 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005766
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005767 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5768 flags |= QF_GETLIST_IDX;
5769
5770 if (dict_find(what, (char_u *)"size", -1) != NULL)
5771 flags |= QF_GETLIST_SIZE;
5772
Bram Moolenaarb254af32017-12-18 19:48:58 +01005773 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5774 flags |= QF_GETLIST_TICK;
5775
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005776 return flags;
5777}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005778
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005779/*
5780 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5781 * If 'nr' and 'id' are not present in 'what' then return the current
5782 * quickfix list index.
5783 * If 'nr' is zero then return the current quickfix list index.
5784 * If 'nr' is '$' then return the last quickfix list index.
5785 * If 'id' is present then return the index of the quickfix list with that id.
5786 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5787 * Return -1, if quickfix list is not present or if the stack is empty.
5788 */
5789 static int
5790qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5791{
5792 int qf_idx;
5793 dictitem_T *di;
5794
5795 qf_idx = qi->qf_curlist; /* default is the current list */
5796 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5797 {
5798 /* Use the specified quickfix/location list */
5799 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005800 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005801 /* for zero use the current list */
5802 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005803 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005804 qf_idx = di->di_tv.vval.v_number - 1;
5805 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005806 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005807 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005808 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005809 else if (di->di_tv.v_type == VAR_STRING
5810 && di->di_tv.vval.v_string != NULL
5811 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5812 /* Get the last quickfix list number */
5813 qf_idx = qi->qf_listcount - 1;
5814 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005815 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005816 }
5817
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005818 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5819 {
5820 /* Look for a list with the specified id */
5821 if (di->di_tv.v_type == VAR_NUMBER)
5822 {
5823 /*
5824 * For zero, use the current list or the list specified by 'nr'
5825 */
5826 if (di->di_tv.vval.v_number != 0)
5827 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5828 }
5829 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005830 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005831 }
5832
5833 return qf_idx;
5834}
5835
5836/*
5837 * Return default values for quickfix list properties in retdict.
5838 */
5839 static int
5840qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5841{
5842 int status = OK;
5843
5844 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005845 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005846 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5847 {
5848 list_T *l = list_alloc();
5849 if (l != NULL)
5850 status = dict_add_list(retdict, "items", l);
5851 else
5852 status = FAIL;
5853 }
5854 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005855 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005856 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005857 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005858 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005859 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005860 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005861 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005862 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005863 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005864 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005865 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005866 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005867 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005868
5869 return status;
5870}
5871
5872/*
5873 * Return the quickfix list title as 'title' in retdict
5874 */
5875 static int
5876qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5877{
Bram Moolenaare0be1672018-07-08 16:50:37 +02005878 return dict_add_string(retdict, "title", qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005879}
5880
5881/*
5882 * Return the quickfix list items/entries as 'items' in retdict
5883 */
5884 static int
5885qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5886{
5887 int status = OK;
5888 list_T *l = list_alloc();
5889 if (l != NULL)
5890 {
5891 (void)get_errorlist(qi, NULL, qf_idx, l);
5892 dict_add_list(retdict, "items", l);
5893 }
5894 else
5895 status = FAIL;
5896
5897 return status;
5898}
5899
5900/*
5901 * Return the quickfix list context (if any) as 'context' in retdict.
5902 */
5903 static int
5904qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5905{
5906 int status;
5907 dictitem_T *di;
5908
5909 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5910 {
5911 di = dictitem_alloc((char_u *)"context");
5912 if (di != NULL)
5913 {
5914 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5915 status = dict_add(retdict, di);
5916 if (status == FAIL)
5917 dictitem_free(di);
5918 }
5919 else
5920 status = FAIL;
5921 }
5922 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02005923 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005924
5925 return status;
5926}
5927
5928/*
5929 * Return the quickfix list index as 'idx' in retdict
5930 */
5931 static int
5932qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5933{
5934 int idx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005935 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005936 /* For empty lists, qf_index is set to 1 */
5937 idx = 0;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005938 return dict_add_number(retdict, "idx", idx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005939}
5940
5941/*
5942 * Return quickfix/location list details (title) as a
5943 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5944 * then current list is used. Otherwise the specified list is used.
5945 */
5946 int
5947qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5948{
5949 qf_info_T *qi = &ql_info;
5950 int status = OK;
5951 int qf_idx;
5952 dictitem_T *di;
5953 int flags = QF_GETLIST_NONE;
5954
5955 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5956 return qf_get_list_from_lines(what, di, retdict);
5957
5958 if (wp != NULL)
5959 qi = GET_LOC_LIST(wp);
5960
5961 flags = qf_getprop_keys2flags(what);
5962
5963 if (qi != NULL && qi->qf_listcount != 0)
5964 qf_idx = qf_getprop_qfidx(qi, what);
5965
Bram Moolenaara6d48492017-12-12 22:45:31 +01005966 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005967 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005968 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005969
Bram Moolenaard823fa92016-08-12 16:29:27 +02005970 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005971 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005972 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005973 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005974 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005975 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005976 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005977 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005978 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005979 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005980 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005981 status = dict_add_number(retdict, "id", qi->qf_lists[qf_idx].qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005982 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005983 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005984 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005985 status = dict_add_number(retdict, "size",
5986 qi->qf_lists[qf_idx].qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005987 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005988 status = dict_add_number(retdict, "changedtick",
5989 qi->qf_lists[qf_idx].qf_changedtick);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005990
Bram Moolenaard823fa92016-08-12 16:29:27 +02005991 return status;
5992}
5993
5994/*
5995 * Add list of entries to quickfix/location list. Each list entry is
5996 * a dictionary with item information.
5997 */
5998 static int
5999qf_add_entries(
6000 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006001 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006002 list_T *list,
6003 char_u *title,
6004 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006005{
6006 listitem_T *li;
6007 dict_T *d;
Bram Moolenaard76ce852018-05-01 15:02:04 +02006008 char_u *filename, *module, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006009 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006010 long lnum;
6011 int col, nr;
6012 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006013 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006014 int valid, status;
6015 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006016 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006017
Bram Moolenaara3921f42017-06-04 15:30:34 +02006018 if (action == ' ' || qf_idx == qi->qf_listcount)
6019 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006020 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006021 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006022 qf_idx = qi->qf_curlist;
6023 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006024 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006025 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02006026 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006027 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006028 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006029 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006030 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006031 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006032
6033 for (li = list->lv_first; li != NULL; li = li->li_next)
6034 {
6035 if (li->li_tv.v_type != VAR_DICT)
6036 continue; /* Skip non-dict items */
6037
6038 d = li->li_tv.vval.v_dict;
6039 if (d == NULL)
6040 continue;
6041
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006042 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaard76ce852018-05-01 15:02:04 +02006043 module = get_dict_string(d, (char_u *)"module", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006044 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6045 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6046 col = (int)get_dict_number(d, (char_u *)"col");
6047 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6048 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006049 type = get_dict_string(d, (char_u *)"type", TRUE);
6050 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6051 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006052 if (text == NULL)
6053 text = vim_strsave((char_u *)"");
6054
6055 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006056 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006057 valid = FALSE;
6058
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006059 /* Mark entries with non-existing buffer number as not valid. Give the
6060 * error message only once. */
6061 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6062 {
6063 if (!did_bufnr_emsg)
6064 {
6065 did_bufnr_emsg = TRUE;
6066 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6067 }
6068 valid = FALSE;
6069 bufnum = 0;
6070 }
6071
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02006072 /* If the 'valid' field is present it overrules the detected value. */
6073 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6074 valid = (int)get_dict_number(d, (char_u *)"valid");
6075
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006076 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006077 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006078 NULL, /* dir */
6079 filename,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006080 module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006081 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006082 text,
6083 lnum,
6084 col,
6085 vcol, /* vis_col */
6086 pattern, /* search pattern */
6087 nr,
6088 type == NULL ? NUL : *type,
6089 valid);
6090
6091 vim_free(filename);
Bram Moolenaard76ce852018-05-01 15:02:04 +02006092 vim_free(module);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006093 vim_free(pattern);
6094 vim_free(text);
6095 vim_free(type);
6096
6097 if (status == FAIL)
6098 {
6099 retval = FAIL;
6100 break;
6101 }
6102 }
6103
Bram Moolenaara3921f42017-06-04 15:30:34 +02006104 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02006105 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02006106 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006107 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02006108 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006109 if (action != 'a')
6110 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006111 qi->qf_lists[qf_idx].qf_ptr =
6112 qi->qf_lists[qf_idx].qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006113 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara3921f42017-06-04 15:30:34 +02006114 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006115 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006116
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006117 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02006118 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006119
6120 return retval;
6121}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006122
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006123/*
6124 * Get the quickfix list index from 'nr' or 'id'
6125 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006126 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006127qf_setprop_get_qfidx(
6128 qf_info_T *qi,
6129 dict_T *what,
6130 int action,
6131 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006132{
6133 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006134 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006135
Bram Moolenaard823fa92016-08-12 16:29:27 +02006136 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6137 {
6138 /* Use the specified quickfix/location list */
6139 if (di->di_tv.v_type == VAR_NUMBER)
6140 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006141 /* for zero use the current list */
6142 if (di->di_tv.vval.v_number != 0)
6143 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006144
Bram Moolenaar55b69262017-08-13 13:42:01 +02006145 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6146 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006147 /*
6148 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02006149 * non-available list and add the new list at the end of the
6150 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006151 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006152 *newlist = TRUE;
6153 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006154 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006155 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006156 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006157 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006158 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02006159 }
6160 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006161 && di->di_tv.vval.v_string != NULL
6162 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006163 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006164 if (qi->qf_listcount > 0)
6165 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006166 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006167 qf_idx = 0;
6168 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006169 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006170 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006171 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006172 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006173 }
6174
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006175 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006176 {
6177 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006178 if (di->di_tv.v_type != VAR_NUMBER)
6179 return INVALID_QFIDX;
6180
6181 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006182 }
6183
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006184 return qf_idx;
6185}
6186
6187/*
6188 * Set the quickfix list title.
6189 */
6190 static int
6191qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6192{
6193 if (di->di_tv.v_type != VAR_STRING)
6194 return FAIL;
6195
6196 vim_free(qi->qf_lists[qf_idx].qf_title);
6197 qi->qf_lists[qf_idx].qf_title =
6198 get_dict_string(what, (char_u *)"title", TRUE);
6199 if (qf_idx == qi->qf_curlist)
6200 qf_update_win_titlevar(qi);
6201
6202 return OK;
6203}
6204
6205/*
6206 * Set quickfix list items/entries.
6207 */
6208 static int
6209qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6210{
6211 int retval = FAIL;
6212 char_u *title_save;
6213
6214 if (di->di_tv.v_type != VAR_LIST)
6215 return FAIL;
6216
6217 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6218 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6219 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006220 vim_free(title_save);
6221
6222 return retval;
6223}
6224
6225/*
6226 * Set quickfix list items/entries from a list of lines.
6227 */
6228 static int
6229qf_setprop_items_from_lines(
6230 qf_info_T *qi,
6231 int qf_idx,
6232 dict_T *what,
6233 dictitem_T *di,
6234 int action)
6235{
6236 char_u *errorformat = p_efm;
6237 dictitem_T *efm_di;
6238 int retval = FAIL;
6239
6240 /* Use the user supplied errorformat settings (if present) */
6241 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6242 {
6243 if (efm_di->di_tv.v_type != VAR_STRING ||
6244 efm_di->di_tv.vval.v_string == NULL)
6245 return FAIL;
6246 errorformat = efm_di->di_tv.vval.v_string;
6247 }
6248
6249 /* Only a List value is supported */
6250 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6251 return FAIL;
6252
6253 if (action == 'r')
6254 qf_free_items(qi, qf_idx);
6255 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6256 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6257 retval = OK;
6258
6259 return retval;
6260}
6261
6262/*
6263 * Set quickfix list context.
6264 */
6265 static int
6266qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di)
6267{
6268 typval_T *ctx;
6269
6270 free_tv(qi->qf_lists[qf_idx].qf_ctx);
6271 ctx = alloc_tv();
6272 if (ctx != NULL)
6273 copy_tv(&di->di_tv, ctx);
6274 qi->qf_lists[qf_idx].qf_ctx = ctx;
6275
6276 return OK;
6277}
6278
6279/*
6280 * Set quickfix/location list properties (title, items, context).
6281 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006282 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006283 */
6284 static int
6285qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6286{
6287 dictitem_T *di;
6288 int retval = FAIL;
6289 int qf_idx;
6290 int newlist = FALSE;
6291
6292 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
6293 newlist = TRUE;
6294
6295 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
6296 if (qf_idx == INVALID_QFIDX) /* List not found */
6297 return FAIL;
6298
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006299 if (newlist)
6300 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006301 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006302 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006303 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006304 }
6305
6306 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006307 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006308 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006309 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006310 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006311 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006312 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006313 retval = qf_setprop_context(qi, qf_idx, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006314
Bram Moolenaarb254af32017-12-18 19:48:58 +01006315 if (retval == OK)
6316 qf_list_changed(qi, qf_idx);
6317
Bram Moolenaard823fa92016-08-12 16:29:27 +02006318 return retval;
6319}
6320
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006321/*
6322 * Find the non-location list window with the specified location list.
6323 */
6324 static win_T *
6325find_win_with_ll(qf_info_T *qi)
6326{
6327 win_T *wp = NULL;
6328
6329 FOR_ALL_WINDOWS(wp)
6330 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6331 return wp;
6332
6333 return NULL;
6334}
6335
6336/*
6337 * Free the entire quickfix/location list stack.
6338 * If the quickfix/location list window is open, then clear it.
6339 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006340 static void
6341qf_free_stack(win_T *wp, qf_info_T *qi)
6342{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006343 win_T *qfwin = qf_find_win(qi);
6344 win_T *llwin = NULL;
6345 win_T *orig_wp = wp;
6346
6347 if (qfwin != NULL)
6348 {
6349 /* If the quickfix/location list window is open, then clear it */
6350 if (qi->qf_curlist < qi->qf_listcount)
6351 qf_free(qi, qi->qf_curlist);
6352 qf_update_buffer(qi, NULL);
6353 }
6354
6355 if (wp != NULL && IS_LL_WINDOW(wp))
6356 {
6357 /* If in the location list window, then use the non-location list
6358 * window with this location list (if present)
6359 */
6360 llwin = find_win_with_ll(qi);
6361 if (llwin != NULL)
6362 wp = llwin;
6363 }
6364
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006365 qf_free_all(wp);
6366 if (wp == NULL)
6367 {
6368 /* quickfix list */
6369 qi->qf_curlist = 0;
6370 qi->qf_listcount = 0;
6371 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006372 else if (IS_LL_WINDOW(orig_wp))
6373 {
6374 /* If the location list window is open, then create a new empty
6375 * location list */
6376 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006377
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006378 /* first free the list reference in the location list window */
6379 ll_free_all(&orig_wp->w_llist_ref);
6380
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006381 orig_wp->w_llist_ref = new_ll;
6382 if (llwin != NULL)
6383 {
6384 llwin->w_llist = new_ll;
6385 new_ll->qf_refcount++;
6386 }
6387 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006388}
6389
Bram Moolenaard823fa92016-08-12 16:29:27 +02006390/*
6391 * Populate the quickfix list with the items supplied in the list
6392 * of dictionaries. "title" will be copied to w:quickfix_title.
6393 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6394 */
6395 int
6396set_errorlist(
6397 win_T *wp,
6398 list_T *list,
6399 int action,
6400 char_u *title,
6401 dict_T *what)
6402{
6403 qf_info_T *qi = &ql_info;
6404 int retval = OK;
6405
6406 if (wp != NULL)
6407 {
6408 qi = ll_get_or_alloc_list(wp);
6409 if (qi == NULL)
6410 return FAIL;
6411 }
6412
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006413 if (action == 'f')
6414 {
6415 /* Free the entire quickfix or location list stack */
6416 qf_free_stack(wp, qi);
6417 }
6418 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006419 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006420 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006421 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006422 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006423 if (retval == OK)
6424 qf_list_changed(qi, qi->qf_curlist);
6425 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006426
6427 return retval;
6428}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006429
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006430/*
6431 * Mark the context as in use for all the lists in a quickfix stack.
6432 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006433 static int
6434mark_quickfix_ctx(qf_info_T *qi, int copyID)
6435{
6436 int i;
6437 int abort = FALSE;
6438 typval_T *ctx;
6439
6440 for (i = 0; i < LISTCOUNT && !abort; ++i)
6441 {
6442 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006443 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6444 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006445 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6446 }
6447
6448 return abort;
6449}
6450
6451/*
6452 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006453 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006454 */
6455 int
6456set_ref_in_quickfix(int copyID)
6457{
6458 int abort = FALSE;
6459 tabpage_T *tp;
6460 win_T *win;
6461
6462 abort = mark_quickfix_ctx(&ql_info, copyID);
6463 if (abort)
6464 return abort;
6465
6466 FOR_ALL_TAB_WINDOWS(tp, win)
6467 {
6468 if (win->w_llist != NULL)
6469 {
6470 abort = mark_quickfix_ctx(win->w_llist, copyID);
6471 if (abort)
6472 return abort;
6473 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006474 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6475 {
6476 /* In a location list window and none of the other windows is
6477 * referring to this location list. Mark the location list
6478 * context as still in use.
6479 */
6480 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6481 if (abort)
6482 return abort;
6483 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006484 }
6485
6486 return abort;
6487}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006488#endif
6489
Bram Moolenaar81695252004-12-29 20:58:21 +00006490/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006491 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006492 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006493 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006494 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006495 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006496 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006497 */
6498 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006499ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006500{
6501 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006502 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006503 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006504 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006505 int_u save_qfid;
6506 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006507
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006508 switch (eap->cmdidx)
6509 {
6510 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6511 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6512 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6513 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6514 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6515 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6516 default: break;
6517 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006518 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6519 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006520 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006521#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006522 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006523 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006524#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006525 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006526
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006527 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01006528 if (eap->cmdidx == CMD_lbuffer
6529 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006530 || eap->cmdidx == CMD_laddbuffer)
6531 {
6532 qi = ll_get_or_alloc_list(curwin);
6533 if (qi == NULL)
6534 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006535 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006536 }
6537
Bram Moolenaar86b68352004-12-27 21:59:20 +00006538 if (*eap->arg == NUL)
6539 buf = curbuf;
6540 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6541 buf = buflist_findnr(atoi((char *)eap->arg));
6542 if (buf == NULL)
6543 EMSG(_(e_invarg));
6544 else if (buf->b_ml.ml_mfp == NULL)
6545 EMSG(_("E681: Buffer is not loaded"));
6546 else
6547 {
6548 if (eap->addr_count == 0)
6549 {
6550 eap->line1 = 1;
6551 eap->line2 = buf->b_ml.ml_line_count;
6552 }
6553 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6554 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6555 EMSG(_(e_invrange));
6556 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006557 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006558 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006559
6560 if (buf->b_sfname)
6561 {
6562 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6563 (char *)qf_title, (char *)buf->b_sfname);
6564 qf_title = IObuff;
6565 }
6566
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006567 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006568 (eap->cmdidx != CMD_caddbuffer
6569 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006570 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006571 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006572 if (res >= 0)
6573 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006574
6575 // Remember the current quickfix list identifier, so that we can
6576 // check for autocommands changing the current quickfix list.
6577 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006578 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006579 {
6580 buf_T *curbuf_old = curbuf;
6581
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006582 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6583 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006584 if (curbuf != curbuf_old)
6585 // Autocommands changed buffer, don't jump now, "qi" may
6586 // be invalid.
6587 res = 0;
6588 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006589 // Jump to the first error for a new list and if autocmds didn't
6590 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006591 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006592 eap->cmdidx == CMD_lbuffer)
6593 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006594 // display the first error
6595 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar754b5602006-02-09 23:53:20 +00006596 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006597 }
6598}
6599
Bram Moolenaar1e015462005-09-25 22:16:38 +00006600#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006601/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006602 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6603 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006604 */
6605 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006606ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006607{
6608 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006609 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006610 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006611 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006612 int_u save_qfid;
6613 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006614
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006615 switch (eap->cmdidx)
6616 {
6617 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6618 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6619 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6620 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6621 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6622 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6623 default: break;
6624 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006625 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6626 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006627 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006628#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006629 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006630 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006631#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006632 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006633
Bram Moolenaar3c097222017-12-21 20:54:49 +01006634 if (eap->cmdidx == CMD_lexpr
6635 || eap->cmdidx == CMD_lgetexpr
6636 || eap->cmdidx == CMD_laddexpr)
6637 {
6638 qi = ll_get_or_alloc_list(curwin);
6639 if (qi == NULL)
6640 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006641 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006642 }
6643
Bram Moolenaar4770d092006-01-12 23:22:24 +00006644 /* Evaluate the expression. When the result is a string or a list we can
6645 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006646 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006647 if (tv != NULL)
6648 {
6649 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6650 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6651 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006652 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006653 (eap->cmdidx != CMD_caddexpr
6654 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006655 (linenr_T)0, (linenr_T)0,
6656 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006657 if (res >= 0)
6658 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006659
6660 // Remember the current quickfix list identifier, so that we can
6661 // check for autocommands changing the current quickfix list.
6662 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006663 if (au_name != NULL)
6664 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6665 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006666
6667 // Jump to the first error for a new list and if autocmds didn't
6668 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006669 if (res > 0 && (eap->cmdidx == CMD_cexpr
6670 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006671 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006672 // display the first error
6673 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006674 }
6675 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006676 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006677 free_tv(tv);
6678 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006679}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006680#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006681
6682/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006683 * Get the location list for ":lhelpgrep"
6684 */
6685 static qf_info_T *
6686hgr_get_ll(int *new_ll)
6687{
6688 win_T *wp;
6689 qf_info_T *qi;
6690
6691 /* If the current window is a help window, then use it */
6692 if (bt_help(curwin->w_buffer))
6693 wp = curwin;
6694 else
6695 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006696 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006697
6698 if (wp == NULL) /* Help window not found */
6699 qi = NULL;
6700 else
6701 qi = wp->w_llist;
6702
6703 if (qi == NULL)
6704 {
6705 /* Allocate a new location list for help text matches */
6706 if ((qi = ll_new_list()) == NULL)
6707 return NULL;
6708 *new_ll = TRUE;
6709 }
6710
6711 return qi;
6712}
6713
6714/*
6715 * Search for a pattern in a help file.
6716 */
6717 static void
6718hgr_search_file(
6719 qf_info_T *qi,
6720 char_u *fname,
6721#ifdef FEAT_MBYTE
6722 vimconv_T *p_vc,
6723#endif
6724 regmatch_T *p_regmatch)
6725{
6726 FILE *fd;
6727 long lnum;
6728
6729 fd = mch_fopen((char *)fname, "r");
6730 if (fd == NULL)
6731 return;
6732
6733 lnum = 1;
6734 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6735 {
6736 char_u *line = IObuff;
6737#ifdef FEAT_MBYTE
6738 /* Convert a line if 'encoding' is not utf-8 and
6739 * the line contains a non-ASCII character. */
6740 if (p_vc->vc_type != CONV_NONE
6741 && has_non_ascii(IObuff))
6742 {
6743 line = string_convert(p_vc, IObuff, NULL);
6744 if (line == NULL)
6745 line = IObuff;
6746 }
6747#endif
6748
6749 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6750 {
6751 int l = (int)STRLEN(line);
6752
6753 /* remove trailing CR, LF, spaces, etc. */
6754 while (l > 0 && line[l - 1] <= ' ')
6755 line[--l] = NUL;
6756
6757 if (qf_add_entry(qi,
6758 qi->qf_curlist,
6759 NULL, /* dir */
6760 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006761 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006762 0,
6763 line,
6764 lnum,
6765 (int)(p_regmatch->startp[0] - line)
6766 + 1, /* col */
6767 FALSE, /* vis_col */
6768 NULL, /* search pattern */
6769 0, /* nr */
6770 1, /* type */
6771 TRUE /* valid */
6772 ) == FAIL)
6773 {
6774 got_int = TRUE;
6775#ifdef FEAT_MBYTE
6776 if (line != IObuff)
6777 vim_free(line);
6778#endif
6779 break;
6780 }
6781 }
6782#ifdef FEAT_MBYTE
6783 if (line != IObuff)
6784 vim_free(line);
6785#endif
6786 ++lnum;
6787 line_breakcheck();
6788 }
6789 fclose(fd);
6790}
6791
6792/*
6793 * Search for a pattern in all the help files in the doc directory under
6794 * the given directory.
6795 */
6796 static void
6797hgr_search_files_in_dir(
6798 qf_info_T *qi,
6799 char_u *dirname,
6800 regmatch_T *p_regmatch
6801#ifdef FEAT_MBYTE
6802 , vimconv_T *p_vc
6803#endif
6804#ifdef FEAT_MULTI_LANG
6805 , char_u *lang
6806#endif
6807 )
6808{
6809 int fcount;
6810 char_u **fnames;
6811 int fi;
6812
6813 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6814 add_pathsep(dirname);
6815 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6816 if (gen_expand_wildcards(1, &dirname, &fcount,
6817 &fnames, EW_FILE|EW_SILENT) == OK
6818 && fcount > 0)
6819 {
6820 for (fi = 0; fi < fcount && !got_int; ++fi)
6821 {
6822#ifdef FEAT_MULTI_LANG
6823 /* Skip files for a different language. */
6824 if (lang != NULL
6825 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006826 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006827 && !(STRNICMP(lang, "en", 2) == 0
6828 && STRNICMP("txt", fnames[fi]
6829 + STRLEN(fnames[fi]) - 3, 3) == 0))
6830 continue;
6831#endif
6832
6833 hgr_search_file(qi, fnames[fi],
6834#ifdef FEAT_MBYTE
6835 p_vc,
6836#endif
6837 p_regmatch);
6838 }
6839 FreeWild(fcount, fnames);
6840 }
6841}
6842
6843/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006844 * Search for a pattern in all the help files in the 'runtimepath'
6845 * and add the matches to a quickfix list.
6846 * 'arg' is the language specifier. If supplied, then only matches in the
6847 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006848 */
6849 static void
6850hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *arg)
6851{
6852 char_u *p;
6853#ifdef FEAT_MULTI_LANG
6854 char_u *lang;
6855#endif
6856
6857#ifdef FEAT_MBYTE
6858 vimconv_T vc;
6859
6860 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6861 * differs. */
6862 vc.vc_type = CONV_NONE;
6863 if (!enc_utf8)
6864 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6865#endif
6866
6867#ifdef FEAT_MULTI_LANG
6868 /* Check for a specified language */
6869 lang = check_help_lang(arg);
6870#endif
6871
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006872 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006873 p = p_rtp;
6874 while (*p != NUL && !got_int)
6875 {
6876 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6877
6878 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6879#ifdef FEAT_MBYTE
6880 , &vc
6881#endif
6882#ifdef FEAT_MULTI_LANG
6883 , lang
6884#endif
6885 );
6886 }
6887
6888#ifdef FEAT_MBYTE
6889 if (vc.vc_type != CONV_NONE)
6890 convert_setup(&vc, NULL, NULL);
6891#endif
6892}
6893
6894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 * ":helpgrep {pattern}"
6896 */
6897 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006898ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899{
6900 regmatch_T regmatch;
6901 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006902 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006903 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006904 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
Bram Moolenaar73633f82012-01-20 13:39:07 +01006906 switch (eap->cmdidx)
6907 {
6908 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6909 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6910 default: break;
6911 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006912 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6913 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006914 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006915#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006916 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006917 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006918#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006919 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006920
6921 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6922 save_cpo = p_cpo;
6923 p_cpo = empty_option;
6924
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006925 if (eap->cmdidx == CMD_lhelpgrep)
6926 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006927 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006928 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006929 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006930 }
6931
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6933 regmatch.rm_ic = FALSE;
6934 if (regmatch.regprog != NULL)
6935 {
6936 /* create a new quickfix list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006937 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006939 hgr_search_in_rtp(qi, &regmatch, eap->arg);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006940
Bram Moolenaar473de612013-06-08 18:19:48 +02006941 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006943 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6944 qi->qf_lists[qi->qf_curlist].qf_ptr =
6945 qi->qf_lists[qi->qf_curlist].qf_start;
6946 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 }
6948
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006949 if (p_cpo == empty_option)
6950 p_cpo = save_cpo;
6951 else
6952 /* Darn, some plugin changed the value. */
6953 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
Bram Moolenaarb254af32017-12-18 19:48:58 +01006955 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006956 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957
Bram Moolenaar73633f82012-01-20 13:39:07 +01006958 if (au_name != NULL)
6959 {
6960 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6961 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02006962 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006963 /* autocommands made "qi" invalid */
6964 return;
6965 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006966
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006968 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006969 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006970 else
6971 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006972
6973 if (eap->cmdidx == CMD_lhelpgrep)
6974 {
6975 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00006976 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006977 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006978 {
6979 if (new_qi)
6980 ll_free_all(&qi);
6981 }
6982 else if (curwin->w_llist == NULL)
6983 curwin->w_llist = qi;
6984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985}
6986
6987#endif /* FEAT_QUICKFIX */