blob: d4aa5090da38cfc7c05169a0cf371f77f8ec393f [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
1509
1510/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001511 * Allocate the fields used for parsing lines and populating a quickfix list.
1512 */
1513 static int
1514qf_alloc_fields(qffields_T *pfields)
1515{
1516 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1517 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1518 pfields->errmsglen = CMDBUFFSIZE + 1;
1519 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1520 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1521 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1522 || pfields->pattern == NULL || pfields->module == NULL)
1523 return FAIL;
1524
1525 return OK;
1526}
1527
1528/*
1529 * Free the fields used for parsing lines and populating a quickfix list.
1530 */
1531 static void
1532qf_free_fields(qffields_T *pfields)
1533{
1534 vim_free(pfields->namebuf);
1535 vim_free(pfields->module);
1536 vim_free(pfields->errmsg);
1537 vim_free(pfields->pattern);
1538}
1539
1540/*
1541 * Setup the state information used for parsing lines and populating a
1542 * quickfix list.
1543 */
1544 static int
1545qf_setup_state(
1546 qfstate_T *pstate,
1547 char_u *enc,
1548 char_u *efile,
1549 typval_T *tv,
1550 buf_T *buf,
1551 linenr_T lnumfirst,
1552 linenr_T lnumlast)
1553{
1554#ifdef FEAT_MBYTE
1555 pstate->vc.vc_type = CONV_NONE;
1556 if (enc != NULL && *enc != NUL)
1557 convert_setup(&pstate->vc, enc, p_enc);
1558#endif
1559
1560 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1561 {
1562 EMSG2(_(e_openerrf), efile);
1563 return FAIL;
1564 }
1565
1566 if (tv != NULL)
1567 {
1568 if (tv->v_type == VAR_STRING)
1569 pstate->p_str = tv->vval.v_string;
1570 else if (tv->v_type == VAR_LIST)
1571 pstate->p_li = tv->vval.v_list->lv_first;
1572 pstate->tv = tv;
1573 }
1574 pstate->buf = buf;
1575 pstate->buflnum = lnumfirst;
1576 pstate->lnumlast = lnumlast;
1577
1578 return OK;
1579}
1580
1581/*
1582 * Cleanup the state information used for parsing lines and populating a
1583 * quickfix list.
1584 */
1585 static void
1586qf_cleanup_state(qfstate_T *pstate)
1587{
1588 if (pstate->fd != NULL)
1589 fclose(pstate->fd);
1590
1591 vim_free(pstate->growbuf);
1592#ifdef FEAT_MBYTE
1593 if (pstate->vc.vc_type != CONV_NONE)
1594 convert_setup(&pstate->vc, NULL, NULL);
1595#endif
1596}
1597
1598/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001599 * Read the errorfile "efile" into memory, line by line, building the error
1600 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001601 * Alternative: when "efile" is NULL read errors from buffer "buf".
1602 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001603 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001604 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1605 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001606 * Return -1 for error, number of errors for success.
1607 */
1608 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001609qf_init_ext(
1610 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001611 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001612 char_u *efile,
1613 buf_T *buf,
1614 typval_T *tv,
1615 char_u *errorformat,
1616 int newlist, /* TRUE: start a new error list */
1617 linenr_T lnumfirst, /* first line number to use */
1618 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001619 char_u *qf_title,
1620 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001621{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001622 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001623 qfstate_T state;
1624 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001625 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001626 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001627 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001629 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001631 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001633 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001634 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001635
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001636 vim_memset(&state, 0, sizeof(state));
1637 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001638 if ((qf_alloc_fields(&fields) == FAIL) ||
1639 (qf_setup_state(&state, enc, efile, tv, buf,
1640 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 goto qf_init_end;
1642
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001643 if (newlist || qf_idx == qi->qf_listcount)
1644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001646 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001647 qf_idx = qi->qf_curlist;
1648 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001649 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001650 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001651 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001652 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001653 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001654 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001657 qfl = &qi->qf_lists[qf_idx];
1658
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001660 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001661 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 else
1663 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001665 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001666 * If the errorformat didn't change between calls, then reuse the
1667 * previously parsed values.
1668 */
1669 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1670 {
1671 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001672 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001673 free_efm_list(&fmt_first);
1674
1675 /* parse the current 'efm' */
1676 fmt_first = parse_efm_option(efm);
1677 if (fmt_first != NULL)
1678 last_efm = vim_strsave(efm);
1679 }
1680
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
1684 /*
1685 * got_int is reset here, because it was probably set when killing the
1686 * ":make" command, but we still want to read the errorfile then.
1687 */
1688 got_int = FALSE;
1689
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 /*
1691 * Read the lines in the error file one by one.
1692 * Try to recognize one of the error formats in each line.
1693 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001694 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001696 /* Get the next line from a file/buffer/list/string */
1697 status = qf_get_nextline(&state);
1698 if (status == QF_NOMEM) /* memory alloc failure */
1699 goto qf_init_end;
1700 if (status == QF_END_OF_INPUT) /* end of input */
1701 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001703 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1704 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001705 if (status == QF_FAIL)
1706 goto error2;
1707 if (status == QF_NOMEM)
1708 goto qf_init_end;
1709 if (status == QF_IGNORE_LINE)
1710 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001712 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001713 qf_idx,
1714 qfl->qf_directory,
1715 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001716 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001717 : ((qfl->qf_currfile != NULL && fields.valid)
1718 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001719 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001720 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001721 fields.errmsg,
1722 fields.lnum,
1723 fields.col,
1724 fields.use_viscol,
1725 fields.pattern,
1726 fields.enr,
1727 fields.type,
1728 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 goto error2;
1730 line_breakcheck();
1731 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001732 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001734 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001736 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001737 qfl->qf_ptr = qfl->qf_start;
1738 qfl->qf_index = 1;
1739 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 }
1741 else
1742 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001743 qfl->qf_nonevalid = FALSE;
1744 if (qfl->qf_ptr == NULL)
1745 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001747 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001748 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001749 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 }
1751 EMSG(_(e_readerrf));
1752error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001753 if (!adding)
1754 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001755 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001756 qf_free(qi, qi->qf_curlist);
1757 qi->qf_listcount--;
1758 if (qi->qf_curlist > 0)
1759 --qi->qf_curlist;
1760 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001761qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001762 if (qf_idx == qi->qf_curlist)
1763 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001764 qf_cleanup_state(&state);
1765 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766
1767 return retval;
1768}
1769
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001770/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001771 * Read the errorfile "efile" into memory, line by line, building the error
1772 * list. Set the error list's title to qf_title.
1773 * Return -1 for error, number of errors for success.
1774 */
1775 int
1776qf_init(win_T *wp,
1777 char_u *efile,
1778 char_u *errorformat,
1779 int newlist, /* TRUE: start a new error list */
1780 char_u *qf_title,
1781 char_u *enc)
1782{
1783 qf_info_T *qi = &ql_info;
1784
1785 if (wp != NULL)
1786 {
1787 qi = ll_get_or_alloc_list(wp);
1788 if (qi == NULL)
1789 return FAIL;
1790 }
1791
1792 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1793 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1794}
1795
1796/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001797 * Set the title of the specified quickfix list. Frees the previous title.
1798 * Prepends ':' to the title.
1799 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001800 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001801qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001802{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001803 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001804
Bram Moolenaarfb604092014-07-23 15:55:00 +02001805 if (title != NULL)
1806 {
1807 char_u *p = alloc((int)STRLEN(title) + 2);
1808
Bram Moolenaara3921f42017-06-04 15:30:34 +02001809 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001810 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001811 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001812 }
1813}
1814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001816 * The title of a quickfix/location list is set, by default, to the command
1817 * that created the quickfix list with the ":" prefix.
1818 * Create a quickfix list title string by prepending ":" to a user command.
1819 * Returns a pointer to a static buffer with the title.
1820 */
1821 static char_u *
1822qf_cmdtitle(char_u *cmd)
1823{
1824 static char_u qftitle_str[IOSIZE];
1825
1826 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1827 return qftitle_str;
1828}
1829
1830/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001831 * Prepare for adding a new quickfix list. If the current list is in the
1832 * middle of the stack, then all the following lists are freed and then
1833 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 */
1835 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001836qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
1838 int i;
1839
1840 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001841 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 * the current entry. This makes it possible to browse in a tree-like
1843 * way with ":grep'.
1844 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001845 while (qi->qf_listcount > qi->qf_curlist + 1)
1846 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
1848 /*
1849 * When the stack is full, remove to oldest entry
1850 * Otherwise, add a new entry.
1851 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001852 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001854 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001856 qi->qf_lists[i - 1] = qi->qf_lists[i];
1857 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 }
1859 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001860 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001861 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001862 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001863 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864}
1865
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001866/*
1867 * Free a location list
1868 */
1869 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001870ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001871{
1872 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001873 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001874
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001875 qi = *pqi;
1876 if (qi == NULL)
1877 return;
1878 *pqi = NULL; /* Remove reference to this list */
1879
1880 qi->qf_refcount--;
1881 if (qi->qf_refcount < 1)
1882 {
1883 /* No references to this location list */
1884 for (i = 0; i < qi->qf_listcount; ++i)
1885 qf_free(qi, i);
1886 vim_free(qi);
1887 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001888}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001889
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001890/*
1891 * Free all the quickfix/location lists in the stack.
1892 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001893 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001894qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001895{
1896 int i;
1897 qf_info_T *qi = &ql_info;
1898
1899 if (wp != NULL)
1900 {
1901 /* location list */
1902 ll_free_all(&wp->w_llist);
1903 ll_free_all(&wp->w_llist_ref);
1904 }
1905 else
1906 /* quickfix list */
1907 for (i = 0; i < qi->qf_listcount; ++i)
1908 qf_free(qi, i);
1909}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911/*
1912 * Add an entry to the end of the list of errors.
1913 * Returns OK or FAIL.
1914 */
1915 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001916qf_add_entry(
1917 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001918 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001919 char_u *dir, /* optional directory name */
1920 char_u *fname, /* file name or NULL */
Bram Moolenaard76ce852018-05-01 15:02:04 +02001921 char_u *module, /* module name or NULL */
Bram Moolenaar05540972016-01-30 20:31:25 +01001922 int bufnum, /* buffer number or zero */
1923 char_u *mesg, /* message */
1924 long lnum, /* line number */
1925 int col, /* column */
1926 int vis_col, /* using visual column */
1927 char_u *pattern, /* search pattern */
1928 int nr, /* error number */
1929 int type, /* type character */
1930 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001932 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001933 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001935 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001937 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001938 {
1939 buf_T *buf = buflist_findnr(bufnum);
1940
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001941 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001942 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001943 buf->b_has_qf_entry |=
1944 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001945 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001946 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001947 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1949 {
1950 vim_free(qfp);
1951 return FAIL;
1952 }
1953 qfp->qf_lnum = lnum;
1954 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001955 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001956 if (pattern == NULL || *pattern == NUL)
1957 qfp->qf_pattern = NULL;
1958 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1959 {
1960 vim_free(qfp->qf_text);
1961 vim_free(qfp);
1962 return FAIL;
1963 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001964 if (module == NULL || *module == NUL)
1965 qfp->qf_module = NULL;
1966 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
1967 {
1968 vim_free(qfp->qf_text);
1969 vim_free(qfp->qf_pattern);
1970 vim_free(qfp);
1971 return FAIL;
1972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 qfp->qf_nr = nr;
1974 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1975 type = 0;
1976 qfp->qf_type = type;
1977 qfp->qf_valid = valid;
1978
Bram Moolenaara3921f42017-06-04 15:30:34 +02001979 lastp = &qi->qf_lists[qf_idx].qf_last;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001980 if (qf_list_empty(qi, qf_idx)) /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001982 qi->qf_lists[qf_idx].qf_start = qfp;
1983 qi->qf_lists[qf_idx].qf_ptr = qfp;
1984 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001985 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
1987 else
1988 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001989 qfp->qf_prev = *lastp;
1990 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001992 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001994 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001995 ++qi->qf_lists[qf_idx].qf_count;
1996 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001997 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001999 qi->qf_lists[qf_idx].qf_index =
2000 qi->qf_lists[qf_idx].qf_count;
2001 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 }
2003
2004 return OK;
2005}
2006
2007/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002008 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002009 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002010 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002011ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002012{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002013 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002014
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002015 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
2016 if (qi != NULL)
2017 {
2018 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
2019 qi->qf_refcount++;
2020 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002021
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002022 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002023}
2024
2025/*
2026 * Return the location list for window 'wp'.
2027 * If not present, allocate a location list
2028 */
2029 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002030ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002031{
2032 if (IS_LL_WINDOW(wp))
2033 /* For a location list window, use the referenced location list */
2034 return wp->w_llist_ref;
2035
2036 /*
2037 * For a non-location list window, w_llist_ref should not point to a
2038 * location list.
2039 */
2040 ll_free_all(&wp->w_llist_ref);
2041
2042 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002043 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002044 return wp->w_llist;
2045}
2046
2047/*
2048 * Copy the location list from window "from" to window "to".
2049 */
2050 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002051copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002052{
2053 qf_info_T *qi;
2054 int idx;
2055 int i;
2056
2057 /*
2058 * When copying from a location list window, copy the referenced
2059 * location list. For other windows, copy the location list for
2060 * that window.
2061 */
2062 if (IS_LL_WINDOW(from))
2063 qi = from->w_llist_ref;
2064 else
2065 qi = from->w_llist;
2066
2067 if (qi == NULL) /* no location list to copy */
2068 return;
2069
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002070 /* allocate a new location list */
2071 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002072 return;
2073
2074 to->w_llist->qf_listcount = qi->qf_listcount;
2075
2076 /* Copy the location lists one at a time */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002077 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002078 {
2079 qf_list_T *from_qfl;
2080 qf_list_T *to_qfl;
2081
2082 to->w_llist->qf_curlist = idx;
2083
2084 from_qfl = &qi->qf_lists[idx];
2085 to_qfl = &to->w_llist->qf_lists[idx];
2086
2087 /* Some of the fields are populated by qf_add_entry() */
2088 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2089 to_qfl->qf_count = 0;
2090 to_qfl->qf_index = 0;
2091 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002092 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002093 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002094 if (from_qfl->qf_title != NULL)
2095 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2096 else
2097 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002098 if (from_qfl->qf_ctx != NULL)
2099 {
2100 to_qfl->qf_ctx = alloc_tv();
2101 if (to_qfl->qf_ctx != NULL)
2102 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2103 }
2104 else
2105 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002106
2107 if (from_qfl->qf_count)
2108 {
2109 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002110 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002111
2112 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002113 for (i = 0, from_qfp = from_qfl->qf_start;
2114 i < from_qfl->qf_count && from_qfp != NULL;
2115 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002116 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002117 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02002118 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002119 NULL,
2120 NULL,
Bram Moolenaard76ce852018-05-01 15:02:04 +02002121 from_qfp->qf_module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002122 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002123 from_qfp->qf_text,
2124 from_qfp->qf_lnum,
2125 from_qfp->qf_col,
2126 from_qfp->qf_viscol,
2127 from_qfp->qf_pattern,
2128 from_qfp->qf_nr,
2129 0,
2130 from_qfp->qf_valid) == FAIL)
2131 {
2132 qf_free_all(to);
2133 return;
2134 }
2135 /*
2136 * qf_add_entry() will not set the qf_num field, as the
2137 * directory and file names are not supplied. So the qf_fnum
2138 * field is copied here.
2139 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002140 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002141 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
2142 prevp->qf_type = from_qfp->qf_type; /* error type */
2143 if (from_qfl->qf_ptr == from_qfp)
2144 to_qfl->qf_ptr = prevp; /* current location */
2145 }
2146 }
2147
2148 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
2149
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002150 /* Assign a new ID for the location list */
2151 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01002152 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002153
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002154 /* When no valid entries are present in the list, qf_ptr points to
2155 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02002156 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002157 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002158 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002159 to_qfl->qf_index = 1;
2160 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002161 }
2162
2163 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
2164}
2165
2166/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002167 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002168 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 */
2170 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002171qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
Bram Moolenaar82404332016-07-10 17:00:38 +02002173 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002174 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002175 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002176
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 if (fname == NULL || *fname == NUL) /* no file name */
2178 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
Bram Moolenaare60acc12011-05-10 16:41:25 +02002180#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002181 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002182#endif
2183#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002184 if (directory != NULL)
2185 slash_adjust(directory);
2186 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002187#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002188 if (directory != NULL && !vim_isAbsName(fname)
2189 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2190 {
2191 /*
2192 * Here we check if the file really exists.
2193 * This should normally be true, but if make works without
2194 * "leaving directory"-messages we might have missed a
2195 * directory change.
2196 */
2197 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002200 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002201 if (directory)
2202 ptr = concat_fnames(directory, fname, TRUE);
2203 else
2204 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002206 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02002207 bufname = ptr;
2208 }
2209 else
2210 bufname = fname;
2211
2212 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002213 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002214 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002215 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002216 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002218 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002219 {
2220 vim_free(qf_last_bufname);
2221 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2222 if (bufname == ptr)
2223 qf_last_bufname = bufname;
2224 else
2225 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002226 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002227 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002228 if (buf == NULL)
2229 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002230
Bram Moolenaarc1542742016-07-20 21:44:37 +02002231 buf->b_has_qf_entry =
2232 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002233 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234}
2235
2236/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002237 * Push dirbuf onto the directory stack and return pointer to actual dir or
2238 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 */
2240 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002241qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242{
2243 struct dir_stack_T *ds_new;
2244 struct dir_stack_T *ds_ptr;
2245
2246 /* allocate new stack element and hook it in */
2247 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2248 if (ds_new == NULL)
2249 return NULL;
2250
2251 ds_new->next = *stackptr;
2252 *stackptr = ds_new;
2253
2254 /* store directory on the stack */
2255 if (vim_isAbsName(dirbuf)
2256 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002257 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 (*stackptr)->dirname = vim_strsave(dirbuf);
2259 else
2260 {
2261 /* Okay we don't have an absolute path.
2262 * dirbuf must be a subdir of one of the directories on the stack.
2263 * Let's search...
2264 */
2265 ds_new = (*stackptr)->next;
2266 (*stackptr)->dirname = NULL;
2267 while (ds_new)
2268 {
2269 vim_free((*stackptr)->dirname);
2270 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2271 TRUE);
2272 if (mch_isdir((*stackptr)->dirname) == TRUE)
2273 break;
2274
2275 ds_new = ds_new->next;
2276 }
2277
2278 /* clean up all dirs we already left */
2279 while ((*stackptr)->next != ds_new)
2280 {
2281 ds_ptr = (*stackptr)->next;
2282 (*stackptr)->next = (*stackptr)->next->next;
2283 vim_free(ds_ptr->dirname);
2284 vim_free(ds_ptr);
2285 }
2286
2287 /* Nothing found -> it must be on top level */
2288 if (ds_new == NULL)
2289 {
2290 vim_free((*stackptr)->dirname);
2291 (*stackptr)->dirname = vim_strsave(dirbuf);
2292 }
2293 }
2294
2295 if ((*stackptr)->dirname != NULL)
2296 return (*stackptr)->dirname;
2297 else
2298 {
2299 ds_ptr = *stackptr;
2300 *stackptr = (*stackptr)->next;
2301 vim_free(ds_ptr);
2302 return NULL;
2303 }
2304}
2305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306/*
2307 * pop dirbuf from the directory stack and return previous directory or NULL if
2308 * stack is empty
2309 */
2310 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002311qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312{
2313 struct dir_stack_T *ds_ptr;
2314
2315 /* TODO: Should we check if dirbuf is the directory on top of the stack?
2316 * What to do if it isn't? */
2317
2318 /* pop top element and free it */
2319 if (*stackptr != NULL)
2320 {
2321 ds_ptr = *stackptr;
2322 *stackptr = (*stackptr)->next;
2323 vim_free(ds_ptr->dirname);
2324 vim_free(ds_ptr);
2325 }
2326
2327 /* return NEW top element as current dir or NULL if stack is empty*/
2328 return *stackptr ? (*stackptr)->dirname : NULL;
2329}
2330
2331/*
2332 * clean up directory stack
2333 */
2334 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002335qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336{
2337 struct dir_stack_T *ds_ptr;
2338
2339 while ((ds_ptr = *stackptr) != NULL)
2340 {
2341 *stackptr = (*stackptr)->next;
2342 vim_free(ds_ptr->dirname);
2343 vim_free(ds_ptr);
2344 }
2345}
2346
2347/*
2348 * Check in which directory of the directory stack the given file can be
2349 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002350 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 * Cleans up intermediate directory entries.
2352 *
2353 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002354 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 * ./
2356 * ./aa
2357 * ./aa/bb
2358 * ./bb
2359 * ./bb/x.c
2360 * and make says:
2361 * making all in aa
2362 * making all in bb
2363 * x.c:9: Error
2364 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2365 * qf_guess_filepath will return NULL.
2366 */
2367 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002368qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370 struct dir_stack_T *ds_ptr;
2371 struct dir_stack_T *ds_tmp;
2372 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002373 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374
2375 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002376 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 return NULL;
2378
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002379 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 fullname = NULL;
2381 while (ds_ptr)
2382 {
2383 vim_free(fullname);
2384 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2385
2386 /* If concat_fnames failed, just go on. The worst thing that can happen
2387 * is that we delete the entire stack.
2388 */
2389 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2390 break;
2391
2392 ds_ptr = ds_ptr->next;
2393 }
2394
2395 vim_free(fullname);
2396
2397 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002398 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002400 ds_tmp = qfl->qf_dir_stack->next;
2401 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 vim_free(ds_tmp->dirname);
2403 vim_free(ds_tmp);
2404 }
2405
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002406 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407}
2408
2409/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002410 * Returns TRUE if a quickfix/location list with the given identifier exists.
2411 */
2412 static int
2413qflist_valid (win_T *wp, int_u qf_id)
2414{
2415 qf_info_T *qi = &ql_info;
2416 int i;
2417
2418 if (wp != NULL)
2419 {
2420 qi = GET_LOC_LIST(wp); /* Location list */
2421 if (qi == NULL)
2422 return FALSE;
2423 }
2424
2425 for (i = 0; i < qi->qf_listcount; ++i)
2426 if (qi->qf_lists[i].qf_id == qf_id)
2427 return TRUE;
2428
2429 return FALSE;
2430}
2431
2432/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002433 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002434 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002435 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002436 * Similar to location list.
2437 */
2438 static int
2439is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
2440{
2441 qf_list_T *qfl;
2442 qfline_T *qfp;
2443 int i;
2444
2445 qfl = &qi->qf_lists[qi->qf_curlist];
2446
2447 /* Search for the entry in the current list */
2448 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2449 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002450 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002451 break;
2452
2453 if (i == qfl->qf_count) /* Entry is not found */
2454 return FALSE;
2455
2456 return TRUE;
2457}
2458
2459/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002460 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002461 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002462 */
2463 static qfline_T *
2464get_next_valid_entry(
2465 qf_info_T *qi,
2466 qfline_T *qf_ptr,
2467 int *qf_index,
2468 int dir)
2469{
2470 int idx;
2471 int old_qf_fnum;
2472
2473 idx = *qf_index;
2474 old_qf_fnum = qf_ptr->qf_fnum;
2475
2476 do
2477 {
2478 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2479 || qf_ptr->qf_next == NULL)
2480 return NULL;
2481 ++idx;
2482 qf_ptr = qf_ptr->qf_next;
2483 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2484 && !qf_ptr->qf_valid)
2485 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2486
2487 *qf_index = idx;
2488 return qf_ptr;
2489}
2490
2491/*
2492 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002493 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002494 */
2495 static qfline_T *
2496get_prev_valid_entry(
2497 qf_info_T *qi,
2498 qfline_T *qf_ptr,
2499 int *qf_index,
2500 int dir)
2501{
2502 int idx;
2503 int old_qf_fnum;
2504
2505 idx = *qf_index;
2506 old_qf_fnum = qf_ptr->qf_fnum;
2507
2508 do
2509 {
2510 if (idx == 1 || qf_ptr->qf_prev == NULL)
2511 return NULL;
2512 --idx;
2513 qf_ptr = qf_ptr->qf_prev;
2514 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2515 && !qf_ptr->qf_valid)
2516 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2517
2518 *qf_index = idx;
2519 return qf_ptr;
2520}
2521
2522/*
2523 * Get the n'th (errornr) previous/next valid entry from the current entry in
2524 * the quickfix list.
2525 * dir == FORWARD or FORWARD_FILE: next valid entry
2526 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2527 */
2528 static qfline_T *
2529get_nth_valid_entry(
2530 qf_info_T *qi,
2531 int errornr,
2532 qfline_T *qf_ptr,
2533 int *qf_index,
2534 int dir)
2535{
2536 qfline_T *prev_qf_ptr;
2537 int prev_index;
2538 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2539 char_u *err = e_no_more_items;
2540
2541 while (errornr--)
2542 {
2543 prev_qf_ptr = qf_ptr;
2544 prev_index = *qf_index;
2545
2546 if (dir == FORWARD || dir == FORWARD_FILE)
2547 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2548 else
2549 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2550 if (qf_ptr == NULL)
2551 {
2552 qf_ptr = prev_qf_ptr;
2553 *qf_index = prev_index;
2554 if (err != NULL)
2555 {
2556 EMSG(_(err));
2557 return NULL;
2558 }
2559 break;
2560 }
2561
2562 err = NULL;
2563 }
2564
2565 return qf_ptr;
2566}
2567
2568/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002569 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002570 */
2571 static qfline_T *
2572get_nth_entry(
2573 qf_info_T *qi,
2574 int errornr,
2575 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002576 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002577{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002578 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002579
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002580 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002581 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2582 {
2583 --qf_idx;
2584 qf_ptr = qf_ptr->qf_prev;
2585 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002586 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002587 while (errornr > qf_idx &&
2588 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2589 qf_ptr->qf_next != NULL)
2590 {
2591 ++qf_idx;
2592 qf_ptr = qf_ptr->qf_next;
2593 }
2594
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002595 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002596 return qf_ptr;
2597}
2598
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002599/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002600 * Find a window displaying a Vim help file.
2601 */
2602 static win_T *
2603qf_find_help_win(void)
2604{
2605 win_T *wp;
2606
2607 FOR_ALL_WINDOWS(wp)
2608 if (bt_help(wp->w_buffer))
2609 return wp;
2610
2611 return NULL;
2612}
2613
2614/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002615 * Find a help window or open one.
2616 */
2617 static int
2618jump_to_help_window(qf_info_T *qi, int *opened_window)
2619{
2620 win_T *wp;
2621 int flags;
2622
2623 if (cmdmod.tab != 0)
2624 wp = NULL;
2625 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002626 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002627 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2628 win_enter(wp, TRUE);
2629 else
2630 {
2631 /*
2632 * Split off help window; put it at far top if no position
2633 * specified, the current window is vertically split and narrow.
2634 */
2635 flags = WSP_HELP;
2636 if (cmdmod.split == 0 && curwin->w_width != Columns
2637 && curwin->w_width < 80)
2638 flags |= WSP_TOP;
2639 if (qi != &ql_info)
2640 flags |= WSP_NEWLOC; /* don't copy the location list */
2641
2642 if (win_split(0, flags) == FAIL)
2643 return FAIL;
2644
2645 *opened_window = TRUE;
2646
2647 if (curwin->w_height < p_hh)
2648 win_setheight((int)p_hh);
2649
2650 if (qi != &ql_info) /* not a quickfix list */
2651 {
2652 /* The new window should use the supplied location list */
2653 curwin->w_llist = qi;
2654 qi->qf_refcount++;
2655 }
2656 }
2657
2658 if (!p_im)
2659 restart_edit = 0; /* don't want insert mode in help file */
2660
2661 return OK;
2662}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002663
2664/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002665 * Find a non-quickfix window using the given location list.
2666 * Returns NULL if a matching window is not found.
2667 */
2668 static win_T *
2669qf_find_win_with_loclist(qf_info_T *ll)
2670{
2671 win_T *wp;
2672
2673 FOR_ALL_WINDOWS(wp)
2674 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2675 return wp;
2676
2677 return NULL;
2678}
2679
2680/*
2681 * Find a window containing a normal buffer
2682 */
2683 static win_T *
2684qf_find_win_with_normal_buf(void)
2685{
2686 win_T *wp;
2687
2688 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002689 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002690 return wp;
2691
2692 return NULL;
2693}
2694
2695/*
2696 * Go to a window in any tabpage containing the specified file. Returns TRUE
2697 * if successfully jumped to the window. Otherwise returns FALSE.
2698 */
2699 static int
2700qf_goto_tabwin_with_file(int fnum)
2701{
2702 tabpage_T *tp;
2703 win_T *wp;
2704
2705 FOR_ALL_TAB_WINDOWS(tp, wp)
2706 if (wp->w_buffer->b_fnum == fnum)
2707 {
2708 goto_tabpage_win(tp, wp);
2709 return TRUE;
2710 }
2711
2712 return FALSE;
2713}
2714
2715/*
2716 * Create a new window to show a file above the quickfix window. Called when
2717 * only the quickfix window is present.
2718 */
2719 static int
2720qf_open_new_file_win(qf_info_T *ll_ref)
2721{
2722 int flags;
2723
2724 flags = WSP_ABOVE;
2725 if (ll_ref != NULL)
2726 flags |= WSP_NEWLOC;
2727 if (win_split(0, flags) == FAIL)
2728 return FAIL; /* not enough room for window */
2729 p_swb = empty_option; /* don't split again */
2730 swb_flags = 0;
2731 RESET_BINDING(curwin);
2732 if (ll_ref != NULL)
2733 {
2734 /* The new window should use the location list from the
2735 * location list window */
2736 curwin->w_llist = ll_ref;
2737 ll_ref->qf_refcount++;
2738 }
2739 return OK;
2740}
2741
2742/*
2743 * Go to a window that shows the right buffer. If the window is not found, go
2744 * to the window just above the location list window. This is used for opening
2745 * a file from a location window and not from a quickfix window. If some usable
2746 * window is previously found, then it is supplied in 'use_win'.
2747 */
2748 static void
2749qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2750{
2751 win_T *win = use_win;
2752
2753 if (win == NULL)
2754 {
2755 /* Find the window showing the selected file */
2756 FOR_ALL_WINDOWS(win)
2757 if (win->w_buffer->b_fnum == qf_fnum)
2758 break;
2759 if (win == NULL)
2760 {
2761 /* Find a previous usable window */
2762 win = curwin;
2763 do
2764 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002765 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002766 break;
2767 if (win->w_prev == NULL)
2768 win = lastwin; /* wrap around the top */
2769 else
2770 win = win->w_prev; /* go to previous window */
2771 } while (win != curwin);
2772 }
2773 }
2774 win_goto(win);
2775
2776 /* If the location list for the window is not set, then set it
2777 * to the location list from the location window */
2778 if (win->w_llist == NULL)
2779 {
2780 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002781 if (ll_ref != NULL)
2782 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002783 }
2784}
2785
2786/*
2787 * Go to a window that shows the specified file. If a window is not found, go
2788 * to the window just above the quickfix window. This is used for opening a
2789 * file from a quickfix window and not from a location window.
2790 */
2791 static void
2792qf_goto_win_with_qfl_file(int qf_fnum)
2793{
2794 win_T *win;
2795 win_T *altwin;
2796
2797 win = curwin;
2798 altwin = NULL;
2799 for (;;)
2800 {
2801 if (win->w_buffer->b_fnum == qf_fnum)
2802 break;
2803 if (win->w_prev == NULL)
2804 win = lastwin; /* wrap around the top */
2805 else
2806 win = win->w_prev; /* go to previous window */
2807
2808 if (IS_QF_WINDOW(win))
2809 {
2810 /* Didn't find it, go to the window before the quickfix
2811 * window. */
2812 if (altwin != NULL)
2813 win = altwin;
2814 else if (curwin->w_prev != NULL)
2815 win = curwin->w_prev;
2816 else
2817 win = curwin->w_next;
2818 break;
2819 }
2820
2821 /* Remember a usable window. */
Bram Moolenaar91335e52018-08-01 17:53:12 +02002822 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002823 altwin = win;
2824 }
2825
2826 win_goto(win);
2827}
2828
2829/*
2830 * Find a suitable window for opening a file (qf_fnum) from the
2831 * quickfix/location list and jump to it. If the file is already opened in a
2832 * window, jump to it. Otherwise open a new window to display the file. This is
2833 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002834 */
2835 static int
2836qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2837{
2838 win_T *usable_win_ptr = NULL;
2839 int usable_win;
2840 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002841 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002842
2843 usable_win = 0;
2844
2845 ll_ref = curwin->w_llist_ref;
2846 if (ll_ref != NULL)
2847 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002848 /* Find a non-quickfix window with this location list */
2849 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2850 if (usable_win_ptr != NULL)
2851 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002852 }
2853
2854 if (!usable_win)
2855 {
2856 /* Locate a window showing a normal buffer */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002857 win = qf_find_win_with_normal_buf();
2858 if (win != NULL)
2859 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002860 }
2861
2862 /*
2863 * If no usable window is found and 'switchbuf' contains "usetab"
2864 * then search in other tabs.
2865 */
2866 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002867 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002868
2869 /*
2870 * If there is only one window and it is the quickfix window, create a
2871 * new one above the quickfix window.
2872 */
2873 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2874 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002875 if (qf_open_new_file_win(ll_ref) != OK)
2876 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002877 *opened_window = TRUE; /* close it when fail */
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002878 }
2879 else
2880 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002881 if (curwin->w_llist_ref != NULL) /* In a location window */
2882 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
2883 else /* In a quickfix window */
2884 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002885 }
2886
2887 return OK;
2888}
2889
2890/*
2891 * Edit the selected file or help file.
2892 */
2893 static int
2894qf_jump_edit_buffer(
2895 qf_info_T *qi,
2896 qfline_T *qf_ptr,
2897 int forceit,
2898 win_T *oldwin,
2899 int *opened_window,
2900 int *abort)
2901{
2902 int retval = OK;
2903
2904 if (qf_ptr->qf_type == 1)
2905 {
2906 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2907 * set b_p_ro flag). */
2908 if (!can_abandon(curbuf, forceit))
2909 {
2910 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002911 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002912 }
2913 else
2914 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2915 ECMD_HIDE + ECMD_SET_HELP,
2916 oldwin == curwin ? curwin : NULL);
2917 }
2918 else
2919 {
2920 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002921 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002922
2923 retval = buflist_getfile(qf_ptr->qf_fnum,
2924 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002925
2926 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002927 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002928 /*
2929 * Location list. Check whether the associated window is still
2930 * present and the list is still valid.
2931 */
2932 if (!win_valid_any_tab(oldwin))
2933 {
2934 EMSG(_("E924: Current window was closed"));
2935 *abort = TRUE;
2936 *opened_window = FALSE;
2937 }
2938 else if (!qflist_valid(oldwin, save_qfid))
2939 {
2940 EMSG(_(e_loc_list_changed));
2941 *abort = TRUE;
2942 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002943 }
2944 else if (old_qf_curlist != qi->qf_curlist
2945 || !is_qf_entry_present(qi, qf_ptr))
2946 {
2947 if (qi == &ql_info)
2948 EMSG(_("E925: Current quickfix was changed"));
2949 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002950 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002951 *abort = TRUE;
2952 }
2953
2954 if (*abort)
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002955 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002956 }
2957
2958 return retval;
2959}
2960
2961/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002962 * Go to the error line in the current file using either line/column number or
2963 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002964 */
2965 static void
2966qf_jump_goto_line(
2967 linenr_T qf_lnum,
2968 int qf_col,
2969 char_u qf_viscol,
2970 char_u *qf_pattern)
2971{
2972 linenr_T i;
2973 char_u *line;
2974 colnr_T screen_col;
2975 colnr_T char_col;
2976
2977 if (qf_pattern == NULL)
2978 {
2979 /*
2980 * Go to line with error, unless qf_lnum is 0.
2981 */
2982 i = qf_lnum;
2983 if (i > 0)
2984 {
2985 if (i > curbuf->b_ml.ml_line_count)
2986 i = curbuf->b_ml.ml_line_count;
2987 curwin->w_cursor.lnum = i;
2988 }
2989 if (qf_col > 0)
2990 {
2991 curwin->w_cursor.col = qf_col - 1;
2992#ifdef FEAT_VIRTUALEDIT
2993 curwin->w_cursor.coladd = 0;
2994#endif
2995 if (qf_viscol == TRUE)
2996 {
2997 /*
2998 * Check each character from the beginning of the error
2999 * line up to the error column. For each tab character
3000 * found, reduce the error column value by the length of
3001 * a tab character.
3002 */
3003 line = ml_get_curline();
3004 screen_col = 0;
3005 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3006 {
3007 if (*line == NUL)
3008 break;
3009 if (*line++ == '\t')
3010 {
3011 curwin->w_cursor.col -= 7 - (screen_col % 8);
3012 screen_col += 8 - (screen_col % 8);
3013 }
3014 else
3015 ++screen_col;
3016 }
3017 }
3018 check_cursor();
3019 }
3020 else
3021 beginline(BL_WHITE | BL_FIX);
3022 }
3023 else
3024 {
3025 pos_T save_cursor;
3026
3027 /* Move the cursor to the first line in the buffer */
3028 save_cursor = curwin->w_cursor;
3029 curwin->w_cursor.lnum = 0;
3030 if (!do_search(NULL, '/', qf_pattern, (long)1,
3031 SEARCH_KEEP, NULL, NULL))
3032 curwin->w_cursor = save_cursor;
3033 }
3034}
3035
3036/*
3037 * Display quickfix list index and size message
3038 */
3039 static void
3040qf_jump_print_msg(
3041 qf_info_T *qi,
3042 int qf_index,
3043 qfline_T *qf_ptr,
3044 buf_T *old_curbuf,
3045 linenr_T old_lnum)
3046{
3047 linenr_T i;
3048 int len;
3049
3050 /* Update the screen before showing the message, unless the screen
3051 * scrolled up. */
3052 if (!msg_scrolled)
3053 update_topline_redraw();
3054 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3055 qi->qf_lists[qi->qf_curlist].qf_count,
3056 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3057 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
3058 /* Add the message, skipping leading whitespace and newlines. */
3059 len = (int)STRLEN(IObuff);
3060 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3061
3062 /* Output the message. Overwrite to avoid scrolling when the 'O'
3063 * flag is present in 'shortmess'; But when not jumping, print the
3064 * whole message. */
3065 i = msg_scroll;
3066 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3067 msg_scroll = TRUE;
3068 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3069 msg_scroll = FALSE;
3070 msg_attr_keep(IObuff, 0, TRUE);
3071 msg_scroll = i;
3072}
3073
3074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 * jump to a quickfix line
3076 * if dir == FORWARD go "errornr" valid entries forward
3077 * if dir == BACKWARD go "errornr" valid entries backward
3078 * if dir == FORWARD_FILE go "errornr" valid entries files backward
3079 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
3080 * else if "errornr" is zero, redisplay the same line
3081 * else go to entry "errornr"
3082 */
3083 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003084qf_jump(qf_info_T *qi,
3085 int dir,
3086 int errornr,
3087 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003089 qfline_T *qf_ptr;
3090 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 buf_T *old_curbuf;
3094 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003095 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003096 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003098 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100#ifdef FEAT_FOLDING
3101 int old_KeyTyped = KeyTyped; /* getting file may reset it */
3102#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003103 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003105 if (qi == NULL)
3106 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003107
3108 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003109 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 {
3111 EMSG(_(e_quickfix));
3112 return;
3113 }
3114
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003115 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003117 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02003119 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003121 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
3122 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003124 qf_ptr = old_qf_ptr;
3125 qf_index = old_qf_index;
3126 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 }
3128 }
3129 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003130 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003132 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
3133 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 /* No need to print the error message if it's visible in the error
3135 * window */
3136 print_message = FALSE;
3137
3138 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003139 * For ":helpgrep" find a help window or open one.
3140 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003141 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003142 if (jump_to_help_window(qi, &opened_window) == FAIL)
3143 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003144
3145 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 * If currently in the quickfix window, find another window to show the
3147 * file in.
3148 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003149 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 {
3151 /*
3152 * If there is no file specified, we don't know where to go.
3153 * But do advance, otherwise ":cn" gets stuck.
3154 */
3155 if (qf_ptr->qf_fnum == 0)
3156 goto theend;
3157
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003158 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
3159 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
3162 /*
3163 * If there is a file name,
3164 * read the wanted file if needed, and check autowrite etc.
3165 */
3166 old_curbuf = curbuf;
3167 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003168
3169 if (qf_ptr->qf_fnum != 0)
3170 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003171 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003172
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003173 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3174 &opened_window, &abort);
3175 if (abort)
3176 {
3177 qi = NULL;
3178 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003179 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003180 }
3181
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003182 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 {
3184 /* When not switched to another buffer, still need to set pc mark */
3185 if (curbuf == old_curbuf)
3186 setpcmark();
3187
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003188 if (qf_ptr != NULL)
3189 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col,
3190 qf_ptr->qf_viscol, qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
3192#ifdef FEAT_FOLDING
3193 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
3194 foldOpenCursor();
3195#endif
3196 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003197 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
3199 else
3200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 if (opened_window)
3202 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01003203 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
3205 /*
3206 * Couldn't open file, so put index back where it was. This could
3207 * happen if the file was readonly and we changed something.
3208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 qf_ptr = old_qf_ptr;
3211 qf_index = old_qf_index;
3212 }
3213 }
3214theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003215 if (qi != NULL)
3216 {
3217 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
3218 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
3219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 if (p_swb != old_swb && opened_window)
3221 {
3222 /* Restore old 'switchbuf' value, but not when an autocommand or
3223 * modeline has changed the value. */
3224 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003227 swb_flags = old_swb_flags;
3228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 else
3230 free_string_option(old_swb);
3231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232}
3233
3234/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003235 * Highlight attributes used for displaying entries from the quickfix list.
3236 */
3237static int qfFileAttr;
3238static int qfSepAttr;
3239static int qfLineAttr;
3240
3241/*
3242 * Display information about a single entry from the quickfix/location list.
3243 * Used by ":clist/:llist" commands.
3244 */
3245 static void
3246qf_list_entry(qf_info_T *qi, qfline_T *qfp, int qf_idx)
3247{
3248 char_u *fname;
3249 buf_T *buf;
3250 int filter_entry;
3251
3252 fname = NULL;
3253 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3254 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3255 (char *)qfp->qf_module);
3256 else {
3257 if (qfp->qf_fnum != 0
3258 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3259 {
3260 fname = buf->b_fname;
3261 if (qfp->qf_type == 1) /* :helpgrep */
3262 fname = gettail(fname);
3263 }
3264 if (fname == NULL)
3265 sprintf((char *)IObuff, "%2d", qf_idx);
3266 else
3267 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3268 qf_idx, (char *)fname);
3269 }
3270
3271 // Support for filtering entries using :filter /pat/ clist
3272 // Match against the module name, file name, search pattern and
3273 // text of the entry.
3274 filter_entry = TRUE;
3275 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3276 filter_entry &= message_filtered(qfp->qf_module);
3277 if (filter_entry && fname != NULL)
3278 filter_entry &= message_filtered(fname);
3279 if (filter_entry && qfp->qf_pattern != NULL)
3280 filter_entry &= message_filtered(qfp->qf_pattern);
3281 if (filter_entry)
3282 filter_entry &= message_filtered(qfp->qf_text);
3283 if (filter_entry)
3284 return;
3285
3286 msg_putchar('\n');
3287 msg_outtrans_attr(IObuff, qf_idx == qi->qf_lists[qi->qf_curlist].qf_index
3288 ? HL_ATTR(HLF_QFL) : qfFileAttr);
3289
3290 if (qfp->qf_lnum != 0)
3291 msg_puts_attr((char_u *)":", qfSepAttr);
3292 if (qfp->qf_lnum == 0)
3293 IObuff[0] = NUL;
3294 else if (qfp->qf_col == 0)
3295 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3296 else
3297 sprintf((char *)IObuff, "%ld col %d",
3298 qfp->qf_lnum, qfp->qf_col);
3299 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3300 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3301 msg_puts_attr(IObuff, qfLineAttr);
3302 msg_puts_attr((char_u *)":", qfSepAttr);
3303 if (qfp->qf_pattern != NULL)
3304 {
3305 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3306 msg_puts(IObuff);
3307 msg_puts_attr((char_u *)":", qfSepAttr);
3308 }
3309 msg_puts((char_u *)" ");
3310
3311 /* Remove newlines and leading whitespace from the text. For an
3312 * unrecognized line keep the indent, the compiler may mark a word
3313 * with ^^^^. */
3314 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3315 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3316 IObuff, IOSIZE);
3317 msg_prt_line(IObuff, FALSE);
3318 out_flush(); /* show one line at a time */
3319}
3320
3321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003323 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 */
3325 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003326qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003328 qfline_T *qfp;
3329 int i;
3330 int idx1 = 1;
3331 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003332 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003333 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003334 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003336 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003338 if (eap->cmdidx == CMD_llist)
3339 {
3340 qi = GET_LOC_LIST(curwin);
3341 if (qi == NULL)
3342 {
3343 EMSG(_(e_loclist));
3344 return;
3345 }
3346 }
3347
3348 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003349 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 {
3351 EMSG(_(e_quickfix));
3352 return;
3353 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003354 if (*arg == '+')
3355 {
3356 ++arg;
3357 plus = TRUE;
3358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3360 {
3361 EMSG(_(e_trailing));
3362 return;
3363 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003364 if (plus)
3365 {
3366 i = qi->qf_lists[qi->qf_curlist].qf_index;
3367 idx2 = i + idx1;
3368 idx1 = i;
3369 }
3370 else
3371 {
3372 i = qi->qf_lists[qi->qf_curlist].qf_count;
3373 if (idx1 < 0)
3374 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3375 if (idx2 < 0)
3376 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378
Bram Moolenaara796d462018-05-01 14:30:36 +02003379 /* Shorten all the file names, so that it is easy to read */
3380 shorten_fnames(FALSE);
3381
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003382 /*
3383 * Get the attributes for the different quickfix highlight items. Note
3384 * that this depends on syntax items defined in the qf.vim syntax file
3385 */
3386 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3387 if (qfFileAttr == 0)
3388 qfFileAttr = HL_ATTR(HLF_D);
3389 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3390 if (qfSepAttr == 0)
3391 qfSepAttr = HL_ATTR(HLF_D);
3392 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3393 if (qfLineAttr == 0)
3394 qfLineAttr = HL_ATTR(HLF_N);
3395
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003396 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003398 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3399 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
3401 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3402 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003403 if (got_int)
3404 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003405
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003406 qf_list_entry(qi, qfp, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003408
3409 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003410 if (qfp == NULL)
3411 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003412 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 ui_breakcheck();
3414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415}
3416
3417/*
3418 * Remove newlines and leading whitespace from an error message.
3419 * Put the result in "buf[bufsize]".
3420 */
3421 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003422qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
3424 int i;
3425 char_u *p = text;
3426
3427 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3428 {
3429 if (*p == '\n')
3430 {
3431 buf[i] = ' ';
3432 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003433 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 break;
3435 }
3436 else
3437 buf[i] = *p++;
3438 }
3439 buf[i] = NUL;
3440}
3441
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003442/*
3443 * Display information (list number, list size and the title) about a
3444 * quickfix/location list.
3445 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003446 static void
3447qf_msg(qf_info_T *qi, int which, char *lead)
3448{
3449 char *title = (char *)qi->qf_lists[which].qf_title;
3450 int count = qi->qf_lists[which].qf_count;
3451 char_u buf[IOSIZE];
3452
3453 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3454 lead,
3455 which + 1,
3456 qi->qf_listcount,
3457 count);
3458
3459 if (title != NULL)
3460 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003461 size_t len = STRLEN(buf);
3462
3463 if (len < 34)
3464 {
3465 vim_memset(buf + len, ' ', 34 - len);
3466 buf[34] = NUL;
3467 }
3468 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003469 }
3470 trunc_string(buf, buf, Columns - 1, IOSIZE);
3471 msg(buf);
3472}
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474/*
3475 * ":colder [count]": Up in the quickfix stack.
3476 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003477 * ":lolder [count]": Up in the location list stack.
3478 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 */
3480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003481qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003483 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 int count;
3485
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003486 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
3487 {
3488 qi = GET_LOC_LIST(curwin);
3489 if (qi == NULL)
3490 {
3491 EMSG(_(e_loclist));
3492 return;
3493 }
3494 }
3495
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (eap->addr_count != 0)
3497 count = eap->line2;
3498 else
3499 count = 1;
3500 while (count--)
3501 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003502 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003504 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003507 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003509 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511 else
3512 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003513 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 {
3515 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003516 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003518 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
3520 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003521 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003522 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523}
3524
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003525/*
3526 * Display the information about all the quickfix/location lists in the stack
3527 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003528 void
3529qf_history(exarg_T *eap)
3530{
3531 qf_info_T *qi = &ql_info;
3532 int i;
3533
3534 if (eap->cmdidx == CMD_lhistory)
3535 qi = GET_LOC_LIST(curwin);
3536 if (qi == NULL || (qi->qf_listcount == 0
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003537 && qf_list_empty(qi, qi->qf_curlist)))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003538 MSG(_("No entries"));
3539 else
3540 for (i = 0; i < qi->qf_listcount; ++i)
3541 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3542}
3543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003545 * Free all the entries in the error list "idx". Note that other information
3546 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 */
3548 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003549qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003551 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003552 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003553 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003554 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003556 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003558 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003559 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003560 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003561 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003562 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003563 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003564 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003565 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003566 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003567 if (stop)
3568 /* Somehow qf_count may have an incorrect value, set it to 1
3569 * to avoid crashing when it's wrong.
3570 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003571 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003572 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003573 qfl->qf_start = qfpnext;
3574 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003576
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003577 qfl->qf_index = 0;
3578 qfl->qf_start = NULL;
3579 qfl->qf_last = NULL;
3580 qfl->qf_ptr = NULL;
3581 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003582
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003583 qf_clean_dir_stack(&qfl->qf_dir_stack);
3584 qfl->qf_directory = NULL;
3585 qf_clean_dir_stack(&qfl->qf_file_stack);
3586 qfl->qf_currfile = NULL;
3587 qfl->qf_multiline = FALSE;
3588 qfl->qf_multiignore = FALSE;
3589 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590}
3591
3592/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003593 * Free error list "idx". Frees all the entries in the quickfix list,
3594 * associated context information and the title.
3595 */
3596 static void
3597qf_free(qf_info_T *qi, int idx)
3598{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003599 qf_list_T *qfl = &qi->qf_lists[idx];
3600
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003601 qf_free_items(qi, idx);
3602
Bram Moolenaard23a8232018-02-10 18:45:26 +01003603 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003604 free_tv(qfl->qf_ctx);
3605 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003606 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003607 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003608}
3609
3610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 * qf_mark_adjust: adjust marks
3612 */
3613 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003614qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003615 win_T *wp,
3616 linenr_T line1,
3617 linenr_T line2,
3618 long amount,
3619 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003621 int i;
3622 qfline_T *qfp;
3623 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003624 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003625 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003626 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627
Bram Moolenaarc1542742016-07-20 21:44:37 +02003628 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003629 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003630 if (wp != NULL)
3631 {
3632 if (wp->w_llist == NULL)
3633 return;
3634 qi = wp->w_llist;
3635 }
3636
3637 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003638 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003639 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003640 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3641 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (qfp->qf_fnum == curbuf->b_fnum)
3643 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003644 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3646 {
3647 if (amount == MAXLNUM)
3648 qfp->qf_cleared = TRUE;
3649 else
3650 qfp->qf_lnum += amount;
3651 }
3652 else if (amount_after && qfp->qf_lnum > line2)
3653 qfp->qf_lnum += amount_after;
3654 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003655
3656 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003657 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658}
3659
3660/*
3661 * Make a nice message out of the error character and the error number:
3662 * char number message
3663 * e or E 0 " error"
3664 * w or W 0 " warning"
3665 * i or I 0 " info"
3666 * 0 0 ""
3667 * other 0 " c"
3668 * e or E n " error n"
3669 * w or W n " warning n"
3670 * i or I n " info n"
3671 * 0 n " error n"
3672 * other n " c n"
3673 * 1 x "" :helpgrep
3674 */
3675 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003676qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677{
3678 static char_u buf[20];
3679 static char_u cc[3];
3680 char_u *p;
3681
3682 if (c == 'W' || c == 'w')
3683 p = (char_u *)" warning";
3684 else if (c == 'I' || c == 'i')
3685 p = (char_u *)" info";
3686 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3687 p = (char_u *)" error";
3688 else if (c == 0 || c == 1)
3689 p = (char_u *)"";
3690 else
3691 {
3692 cc[0] = ' ';
3693 cc[1] = c;
3694 cc[2] = NUL;
3695 p = cc;
3696 }
3697
3698 if (nr <= 0)
3699 return p;
3700
3701 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3702 return buf;
3703}
3704
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003706 * When "split" is FALSE: Open the entry/result under the cursor.
3707 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3708 */
3709 void
3710qf_view_result(int split)
3711{
3712 qf_info_T *qi = &ql_info;
3713
3714 if (!bt_quickfix(curbuf))
3715 return;
3716
3717 if (IS_LL_WINDOW(curwin))
3718 qi = GET_LOC_LIST(curwin);
3719
3720 if (qi == NULL || qi->qf_lists[qi->qf_curlist].qf_count == 0)
3721 {
3722 EMSG(_(e_quickfix));
3723 return;
3724 }
3725
3726 if (split)
3727 {
3728 char_u cmd[32];
3729
3730 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3731 (long)curwin->w_cursor.lnum,
3732 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3733 if (do_cmdline_cmd(cmd) == OK)
3734 do_cmdline_cmd((char_u *) "clearjumps");
3735 return;
3736 }
3737
3738 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3739}
3740
3741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 * ":cwindow": open the quickfix window if we have errors to display,
3743 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003744 * ":lwindow": open the location list window if we have locations to display,
3745 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 */
3747 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003748ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003750 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 win_T *win;
3752
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003753 if (eap->cmdidx == CMD_lwindow)
3754 {
3755 qi = GET_LOC_LIST(curwin);
3756 if (qi == NULL)
3757 return;
3758 }
3759
3760 /* Look for an existing quickfix window. */
3761 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
3763 /*
3764 * If a quickfix window is open but we have no errors to display,
3765 * close the window. If a quickfix window is not open, then open
3766 * it if we have errors; otherwise, leave it closed.
3767 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003768 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003769 || qf_list_empty(qi, qi->qf_curlist)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003770 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 {
3772 if (win != NULL)
3773 ex_cclose(eap);
3774 }
3775 else if (win == NULL)
3776 ex_copen(eap);
3777}
3778
3779/*
3780 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003781 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003784ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003786 win_T *win = NULL;
3787 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003789 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3790 {
3791 qi = GET_LOC_LIST(curwin);
3792 if (qi == NULL)
3793 return;
3794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003796 /* Find existing quickfix window and close it. */
3797 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (win != NULL)
3799 win_close(win, FALSE);
3800}
3801
3802/*
3803 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003804 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 */
3806 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003807ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003809 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003812 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003813 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003814 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003816 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3817 {
3818 qi = GET_LOC_LIST(curwin);
3819 if (qi == NULL)
3820 {
3821 EMSG(_(e_loclist));
3822 return;
3823 }
3824 }
3825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 if (eap->addr_count != 0)
3827 height = eap->line2;
3828 else
3829 height = QF_WINHEIGHT;
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832#ifdef FEAT_GUI
3833 need_mouse_correct = TRUE;
3834#endif
3835
3836 /*
3837 * Find existing quickfix window, or open a new one.
3838 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003839 win = qf_find_win(qi);
3840
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003841 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003844 if (eap->addr_count != 0)
3845 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003846 if (cmdmod.split & WSP_VERT)
3847 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003848 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003849 win_setwidth(height);
3850 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003851 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003852 win_setheight(height);
3853 }
3854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 else
3856 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003857 int flags = 0;
3858
Bram Moolenaar9c102382006-05-03 21:26:49 +00003859 qf_buf = qf_find_buf(qi);
3860
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 /* The current window becomes the previous window afterwards. */
3862 win = curwin;
3863
Bram Moolenaar77642c02012-11-20 17:55:10 +01003864 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3865 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003866 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003867 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003868 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003869 /* Default is to open the window below the current window */
3870 if (cmdmod.split == 0)
3871 flags = WSP_BELOW;
3872 flags |= WSP_NEWLOC;
3873 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003875 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003877 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003879 /*
3880 * For the location list window, create a reference to the
3881 * location list from the window 'win'.
3882 */
3883 curwin->w_llist_ref = win->w_llist;
3884 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003886
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003887 if (oldwin != curwin)
3888 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003889 if (qf_buf != NULL)
3890 /* Use the existing quickfix buffer */
3891 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003892 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003893 else
3894 {
3895 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003896 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003897 /* switch off 'swapfile' */
3898 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3899 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003900 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003901 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003902 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003903#ifdef FEAT_DIFF
3904 curwin->w_p_diff = FALSE;
3905#endif
3906#ifdef FEAT_FOLDING
3907 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3908 OPT_LOCAL);
3909#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003912 /* Only set the height when still in the same tab page and there is no
3913 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003914 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 win_setheight(height);
3916 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3917 if (win_valid(win))
3918 prevwin = win;
3919 }
3920
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003921 qf_set_title_var(qi);
3922
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 /*
3924 * Fill the buffer with the quickfix list.
3925 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003926 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003928 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 curwin->w_cursor.col = 0;
3930 check_cursor();
3931 update_topline(); /* scroll to show the line */
3932}
3933
3934/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003935 * Move the cursor in the quickfix window to "lnum".
3936 */
3937 static void
3938qf_win_goto(win_T *win, linenr_T lnum)
3939{
3940 win_T *old_curwin = curwin;
3941
3942 curwin = win;
3943 curbuf = win->w_buffer;
3944 curwin->w_cursor.lnum = lnum;
3945 curwin->w_cursor.col = 0;
3946#ifdef FEAT_VIRTUALEDIT
3947 curwin->w_cursor.coladd = 0;
3948#endif
3949 curwin->w_curswant = 0;
3950 update_topline(); /* scroll to show the line */
3951 redraw_later(VALID);
3952 curwin->w_redr_status = TRUE; /* update ruler */
3953 curwin = old_curwin;
3954 curbuf = curwin->w_buffer;
3955}
3956
3957/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003958 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003959 */
3960 void
3961ex_cbottom(exarg_T *eap UNUSED)
3962{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003963 qf_info_T *qi = &ql_info;
3964 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003965
Bram Moolenaar537ef082016-07-09 17:56:19 +02003966 if (eap->cmdidx == CMD_lbottom)
3967 {
3968 qi = GET_LOC_LIST(curwin);
3969 if (qi == NULL)
3970 {
3971 EMSG(_(e_loclist));
3972 return;
3973 }
3974 }
3975
3976 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003977 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3978 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3979}
3980
3981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 * Return the number of the current entry (line number in the quickfix
3983 * window).
3984 */
3985 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003986qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003988 qf_info_T *qi = &ql_info;
3989
3990 if (IS_LL_WINDOW(wp))
3991 /* In the location list window, use the referenced location list */
3992 qi = wp->w_llist_ref;
3993
3994 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995}
3996
3997/*
3998 * Update the cursor position in the quickfix window to the current error.
3999 * Return TRUE if there is a quickfix window.
4000 */
4001 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004002qf_win_pos_update(
4003 qf_info_T *qi,
4004 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
4006 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004007 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
4009 /*
4010 * Put the cursor on the current error in the quickfix window, so that
4011 * it's viewable.
4012 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004013 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 if (win != NULL
4015 && qf_index <= win->w_buffer->b_ml.ml_line_count
4016 && old_qf_index != qf_index)
4017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 if (qf_index > old_qf_index)
4019 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004020 win->w_redraw_top = old_qf_index;
4021 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 }
4023 else
4024 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004025 win->w_redraw_top = qf_index;
4026 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004028 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
4030 return win != NULL;
4031}
4032
4033/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004034 * Check whether the given window is displaying the specified quickfix/location
4035 * list buffer
4036 */
4037 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004038is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004039{
4040 /*
4041 * A window displaying the quickfix buffer will have the w_llist_ref field
4042 * set to NULL.
4043 * A window displaying a location list buffer will have the w_llist_ref
4044 * pointing to the location list.
4045 */
4046 if (bt_quickfix(win->w_buffer))
4047 if ((qi == &ql_info && win->w_llist_ref == NULL)
4048 || (qi != &ql_info && win->w_llist_ref == qi))
4049 return TRUE;
4050
4051 return FALSE;
4052}
4053
4054/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004055 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004056 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004057 */
4058 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004059qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004060{
4061 win_T *win;
4062
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004063 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004064 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004065 return win;
4066 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004067}
4068
4069/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004070 * Find a quickfix buffer.
4071 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 */
4073 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004074qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004076 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004077 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
Bram Moolenaar9c102382006-05-03 21:26:49 +00004079 FOR_ALL_TAB_WINDOWS(tp, win)
4080 if (is_qf_win(win, qi))
4081 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004082
Bram Moolenaar9c102382006-05-03 21:26:49 +00004083 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084}
4085
4086/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004087 * Update the w:quickfix_title variable in the quickfix/location list window
4088 */
4089 static void
4090qf_update_win_titlevar(qf_info_T *qi)
4091{
4092 win_T *win;
4093 win_T *curwin_save;
4094
4095 if ((win = qf_find_win(qi)) != NULL)
4096 {
4097 curwin_save = curwin;
4098 curwin = win;
4099 qf_set_title_var(qi);
4100 curwin = curwin_save;
4101 }
4102}
4103
4104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 * Find the quickfix buffer. If it exists, update the contents.
4106 */
4107 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004108qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109{
4110 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004111 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
4114 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004115 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 if (buf != NULL)
4117 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004118 linenr_T old_line_count = buf->b_ml.ml_line_count;
4119
4120 if (old_last == NULL)
4121 /* set curwin/curbuf to buf and save a few things */
4122 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
Bram Moolenaard823fa92016-08-12 16:29:27 +02004124 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004125
Bram Moolenaar864293a2016-06-02 13:40:04 +02004126 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004127 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004128
Bram Moolenaar864293a2016-06-02 13:40:04 +02004129 if (old_last == NULL)
4130 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004131 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004132
4133 /* restore curwin/curbuf and a few other things */
4134 aucmd_restbuf(&aco);
4135 }
4136
4137 /* Only redraw when added lines are visible. This avoids flickering
4138 * when the added lines are not visible. */
4139 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4140 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
4142}
4143
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004144/*
4145 * Set "w:quickfix_title" if "qi" has a title.
4146 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004147 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004148qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004149{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004150 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
4151 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004152 qi->qf_lists[qi->qf_curlist].qf_title);
4153}
4154
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004156 * Add an error line to the quickfix buffer.
4157 */
4158 static int
4159qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4160{
4161 int len;
4162 buf_T *errbuf;
4163
4164 if (qfp->qf_module != NULL)
4165 {
4166 STRCPY(IObuff, qfp->qf_module);
4167 len = (int)STRLEN(IObuff);
4168 }
4169 else if (qfp->qf_fnum != 0
4170 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4171 && errbuf->b_fname != NULL)
4172 {
4173 if (qfp->qf_type == 1) /* :helpgrep */
4174 STRCPY(IObuff, gettail(errbuf->b_fname));
4175 else
4176 {
4177 /* shorten the file name if not done already */
4178 if (errbuf->b_sfname == NULL
4179 || mch_isFullName(errbuf->b_sfname))
4180 {
4181 if (*dirname == NUL)
4182 mch_dirname(dirname, MAXPATHL);
4183 shorten_buf_fname(errbuf, dirname, FALSE);
4184 }
4185 STRCPY(IObuff, errbuf->b_fname);
4186 }
4187 len = (int)STRLEN(IObuff);
4188 }
4189 else
4190 len = 0;
4191 IObuff[len++] = '|';
4192
4193 if (qfp->qf_lnum > 0)
4194 {
4195 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4196 len += (int)STRLEN(IObuff + len);
4197
4198 if (qfp->qf_col > 0)
4199 {
4200 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4201 len += (int)STRLEN(IObuff + len);
4202 }
4203
4204 sprintf((char *)IObuff + len, "%s",
4205 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4206 len += (int)STRLEN(IObuff + len);
4207 }
4208 else if (qfp->qf_pattern != NULL)
4209 {
4210 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4211 len += (int)STRLEN(IObuff + len);
4212 }
4213 IObuff[len++] = '|';
4214 IObuff[len++] = ' ';
4215
4216 /* Remove newlines and leading whitespace from the text.
4217 * For an unrecognized line keep the indent, the compiler may
4218 * mark a word with ^^^^. */
4219 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4220 IObuff + len, IOSIZE - len);
4221
4222 if (ml_append_buf(buf, lnum, IObuff,
4223 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4224 return FAIL;
4225
4226 return OK;
4227}
4228
4229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 * Fill current buffer with quickfix errors, replacing any previous contents.
4231 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004232 * If "old_last" is not NULL append the items after this one.
4233 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4234 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 */
4236 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004237qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004239 linenr_T lnum;
4240 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004241 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
Bram Moolenaar864293a2016-06-02 13:40:04 +02004243 if (old_last == NULL)
4244 {
4245 if (buf != curbuf)
4246 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004247 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004248 return;
4249 }
4250
4251 /* delete all existing lines */
4252 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4253 (void)ml_delete((linenr_T)1, FALSE);
4254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
4256 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004257 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
Bram Moolenaara796d462018-05-01 14:30:36 +02004259 char_u dirname[MAXPATHL];
4260
4261 *dirname = NUL;
4262
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004264 if (old_last == NULL)
4265 {
4266 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4267 lnum = 0;
4268 }
4269 else
4270 {
4271 qfp = old_last->qf_next;
4272 lnum = buf->b_ml.ml_line_count;
4273 }
4274 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004276 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004278
Bram Moolenaar864293a2016-06-02 13:40:04 +02004279 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004281 if (qfp == NULL)
4282 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004284
4285 if (old_last == NULL)
4286 /* Delete the empty line which is now at the end */
4287 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
4289
4290 /* correct cursor position */
4291 check_lnums(TRUE);
4292
Bram Moolenaar864293a2016-06-02 13:40:04 +02004293 if (old_last == NULL)
4294 {
4295 /* Set the 'filetype' to "qf" each time after filling the buffer.
4296 * This resembles reading a file into a buffer, it's more logical when
4297 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02004298 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004299 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4300 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301
Bram Moolenaar864293a2016-06-02 13:40:04 +02004302 keep_filetype = TRUE; /* don't detect 'filetype' */
4303 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004305 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004307 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004308 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004309
Bram Moolenaar864293a2016-06-02 13:40:04 +02004310 /* make sure it will be redrawn */
4311 redraw_curbuf_later(NOT_VALID);
4312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
4314 /* Restore KeyTyped, setting 'filetype' may reset it. */
4315 KeyTyped = old_KeyTyped;
4316}
4317
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004318/*
4319 * For every change made to the quickfix list, update the changed tick.
4320 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004321 static void
4322qf_list_changed(qf_info_T *qi, int qf_idx)
4323{
4324 qi->qf_lists[qf_idx].qf_changedtick++;
4325}
4326
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004328 * Return the quickfix/location list number with the given identifier.
4329 * Returns -1 if list is not found.
4330 */
4331 static int
4332qf_id2nr(qf_info_T *qi, int_u qfid)
4333{
4334 int qf_idx;
4335
4336 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4337 if (qi->qf_lists[qf_idx].qf_id == qfid)
4338 return qf_idx;
4339 return INVALID_QFIDX;
4340}
4341
4342/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004343 * Jump to the first entry if there is one.
4344 */
4345 static void
4346qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4347{
4348 // If autocommands changed the current list, then restore it
4349 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4350 qi->qf_curlist = qf_id2nr(qi, save_qfid);
4351
4352 if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
4353 qf_jump(qi, 0, 0, forceit);
4354}
4355
4356/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004357 * Return TRUE when using ":vimgrep" for ":grep".
4358 */
4359 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004360grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004361{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004362 return ((cmdidx == CMD_grep
4363 || cmdidx == CMD_lgrep
4364 || cmdidx == CMD_grepadd
4365 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004366 && STRCMP("internal",
4367 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4368}
4369
4370/*
Bram Moolenaara6557602006-02-04 22:43:20 +00004371 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 */
4373 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004374ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375{
Bram Moolenaar7c626922005-02-07 22:01:03 +00004376 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004378 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00004380 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004381 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004382 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004383 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004384 int_u save_qfid;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004385
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004386 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
4387 if (grep_internal(eap->cmdidx))
4388 {
4389 ex_vimgrep(eap);
4390 return;
4391 }
4392
Bram Moolenaar7c626922005-02-07 22:01:03 +00004393 switch (eap->cmdidx)
4394 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00004395 case CMD_make: au_name = (char_u *)"make"; break;
4396 case CMD_lmake: au_name = (char_u *)"lmake"; break;
4397 case CMD_grep: au_name = (char_u *)"grep"; break;
4398 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4399 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4400 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004401 default: break;
4402 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004403 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4404 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004405 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004406#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004407 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004408 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004409#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004410 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004411#ifdef FEAT_MBYTE
4412 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
Bram Moolenaara6557602006-02-04 22:43:20 +00004415 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep
4416 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00004417 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004418
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00004420 fname = get_mef_name();
4421 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004423 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425 /*
4426 * If 'shellpipe' empty: don't redirect to 'errorfile'.
4427 */
4428 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
4429 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00004430 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 cmd = alloc(len);
4432 if (cmd == NULL)
4433 return;
4434 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
4435 (char *)p_shq);
4436 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004437 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 /*
4439 * Output a newline if there's something else than the :make command that
4440 * was typed (in which case the cursor is in column 0).
4441 */
4442 if (msg_col == 0)
4443 msg_didout = FALSE;
4444 msg_start();
4445 MSG_PUTS(":!");
4446 msg_outtrans(cmd); /* show what we are doing */
4447
4448 /* let the shell know if we are redirecting output or not */
4449 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4450
4451#ifdef AMIGA
4452 out_flush();
4453 /* read window status report and redraw before message */
4454 (void)char_avail();
4455#endif
4456
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004457 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00004458 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4459 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004460 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004461 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004462 if (wp != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004463 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004464 qi = GET_LOC_LIST(wp);
4465 if (qi == NULL)
4466 goto cleanup;
4467 }
4468 if (res >= 0)
4469 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004470
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004471 // Remember the current quickfix list identifier, so that we can
4472 // check for autocommands changing the current quickfix list.
4473 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4474 if (au_name != NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004475 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4476 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004477 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004478 // display the first error
4479 qf_jump_first(qi, save_qfid, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004481cleanup:
Bram Moolenaar7c626922005-02-07 22:01:03 +00004482 mch_remove(fname);
4483 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 vim_free(cmd);
4485}
4486
4487/*
4488 * Return the name for the errorfile, in allocated memory.
4489 * Find a new unique name when 'makeef' contains "##".
4490 * Returns NULL for error.
4491 */
4492 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004493get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494{
4495 char_u *p;
4496 char_u *name;
4497 static int start = -1;
4498 static int off = 0;
4499#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004500 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501#endif
4502
4503 if (*p_mef == NUL)
4504 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004505 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 if (name == NULL)
4507 EMSG(_(e_notmp));
4508 return name;
4509 }
4510
4511 for (p = p_mef; *p; ++p)
4512 if (p[0] == '#' && p[1] == '#')
4513 break;
4514
4515 if (*p == NUL)
4516 return vim_strsave(p_mef);
4517
4518 /* Keep trying until the name doesn't exist yet. */
4519 for (;;)
4520 {
4521 if (start == -1)
4522 start = mch_get_pid();
4523 else
4524 off += 19;
4525
4526 name = alloc((unsigned)STRLEN(p_mef) + 30);
4527 if (name == NULL)
4528 break;
4529 STRCPY(name, p_mef);
4530 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4531 STRCAT(name, p + 2);
4532 if (mch_getperm(name) < 0
4533#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02004534 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 && mch_lstat((char *)name, &sb) < 0
4536#endif
4537 )
4538 break;
4539 vim_free(name);
4540 }
4541 return name;
4542}
4543
4544/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004545 * Returns the number of valid entries in the current quickfix/location list.
4546 */
4547 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004548qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004549{
4550 qf_info_T *qi = &ql_info;
4551 qfline_T *qfp;
4552 int i, sz = 0;
4553 int prev_fnum = 0;
4554
4555 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4556 {
4557 /* Location list */
4558 qi = GET_LOC_LIST(curwin);
4559 if (qi == NULL)
4560 return 0;
4561 }
4562
4563 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004564 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004565 ++i, qfp = qfp->qf_next)
4566 {
4567 if (qfp->qf_valid)
4568 {
4569 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4570 sz++; /* Count all valid entries */
4571 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4572 {
4573 /* Count the number of files */
4574 sz++;
4575 prev_fnum = qfp->qf_fnum;
4576 }
4577 }
4578 }
4579
4580 return sz;
4581}
4582
4583/*
4584 * Returns the current index of the quickfix/location list.
4585 * Returns 0 if there is an error.
4586 */
4587 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004588qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004589{
4590 qf_info_T *qi = &ql_info;
4591
4592 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4593 {
4594 /* Location list */
4595 qi = GET_LOC_LIST(curwin);
4596 if (qi == NULL)
4597 return 0;
4598 }
4599
4600 return qi->qf_lists[qi->qf_curlist].qf_index;
4601}
4602
4603/*
4604 * Returns the current index in the quickfix/location list (counting only valid
4605 * entries). If no valid entries are in the list, then returns 1.
4606 */
4607 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004608qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004609{
4610 qf_info_T *qi = &ql_info;
4611 qf_list_T *qfl;
4612 qfline_T *qfp;
4613 int i, eidx = 0;
4614 int prev_fnum = 0;
4615
4616 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo)
4617 {
4618 /* Location list */
4619 qi = GET_LOC_LIST(curwin);
4620 if (qi == NULL)
4621 return 1;
4622 }
4623
4624 qfl = &qi->qf_lists[qi->qf_curlist];
4625 qfp = qfl->qf_start;
4626
4627 /* check if the list has valid errors */
4628 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4629 return 1;
4630
4631 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4632 {
4633 if (qfp->qf_valid)
4634 {
4635 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4636 {
4637 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4638 {
4639 /* Count the number of files */
4640 eidx++;
4641 prev_fnum = qfp->qf_fnum;
4642 }
4643 }
4644 else
4645 eidx++;
4646 }
4647 }
4648
4649 return eidx ? eidx : 1;
4650}
4651
4652/*
4653 * Get the 'n'th valid error entry in the quickfix or location list.
4654 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4655 * For :cdo and :ldo returns the 'n'th valid error entry.
4656 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4657 */
4658 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004659qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004660{
4661 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4662 qfline_T *qfp = qfl->qf_start;
4663 int i, eidx;
4664 int prev_fnum = 0;
4665
4666 /* check if the list has valid errors */
4667 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4668 return 1;
4669
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004670 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004671 i++, qfp = qfp->qf_next)
4672 {
4673 if (qfp->qf_valid)
4674 {
4675 if (fdo)
4676 {
4677 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4678 {
4679 /* Count the number of files */
4680 eidx++;
4681 prev_fnum = qfp->qf_fnum;
4682 }
4683 }
4684 else
4685 eidx++;
4686 }
4687
4688 if (eidx == n)
4689 break;
4690 }
4691
4692 if (i <= qfl->qf_count)
4693 return i;
4694 else
4695 return 1;
4696}
4697
4698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004700 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004701 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 */
4703 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004704ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004706 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004707 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004708
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004709 if (eap->cmdidx == CMD_ll
4710 || eap->cmdidx == CMD_lrewind
4711 || eap->cmdidx == CMD_lfirst
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004712 || eap->cmdidx == CMD_llast
4713 || eap->cmdidx == CMD_ldo
4714 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004715 {
4716 qi = GET_LOC_LIST(curwin);
4717 if (qi == NULL)
4718 {
4719 EMSG(_(e_loclist));
4720 return;
4721 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004722 }
4723
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004724 if (eap->addr_count > 0)
4725 errornr = (int)eap->line2;
4726 else
4727 {
4728 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll)
4729 errornr = 0;
4730 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind
4731 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst)
4732 errornr = 1;
4733 else
4734 errornr = 32767;
4735 }
4736
4737 /* For cdo and ldo commands, jump to the nth valid error.
4738 * For cfdo and lfdo commands, jump to the nth valid file entry.
4739 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004740 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4741 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004742 errornr = qf_get_nth_valid_entry(qi,
4743 eap->addr_count > 0 ? (int)eap->line1 : 1,
4744 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4745
4746 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747}
4748
4749/*
4750 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004751 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004752 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 */
4754 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004755ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004757 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004758 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004759
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004760 if (eap->cmdidx == CMD_lnext
4761 || eap->cmdidx == CMD_lNext
4762 || eap->cmdidx == CMD_lprevious
4763 || eap->cmdidx == CMD_lnfile
4764 || eap->cmdidx == CMD_lNfile
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004765 || eap->cmdidx == CMD_lpfile
4766 || eap->cmdidx == CMD_ldo
4767 || eap->cmdidx == CMD_lfdo)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004768 {
4769 qi = GET_LOC_LIST(curwin);
4770 if (qi == NULL)
4771 {
4772 EMSG(_(e_loclist));
4773 return;
4774 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004775 }
4776
Bram Moolenaar55b69262017-08-13 13:42:01 +02004777 if (eap->addr_count > 0
4778 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4779 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004780 errornr = (int)eap->line2;
4781 else
4782 errornr = 1;
4783
4784 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext
4785 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 ? FORWARD
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004787 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile
4788 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 ? FORWARD_FILE
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004790 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile
4791 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 ? BACKWARD_FILE
4793 : BACKWARD,
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004794 errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795}
4796
4797/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004798 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004799 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 */
4801 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004802ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004804 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004805 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004806 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004807 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004808 int_u save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004809 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004810
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004811 switch (eap->cmdidx)
4812 {
4813 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4814 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4815 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4816 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4817 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4818 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4819 default: break;
4820 }
4821 if (au_name != NULL)
4822 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004823#ifdef FEAT_MBYTE
4824 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4825#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004826#ifdef FEAT_BROWSE
4827 if (cmdmod.browse)
4828 {
4829 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004830 NULL, NULL,
4831 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004832 if (browse_file == NULL)
4833 return;
4834 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4835 vim_free(browse_file);
4836 }
4837 else
4838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004840 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004841
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004842 if (eap->cmdidx == CMD_lfile
4843 || eap->cmdidx == CMD_lgetfile
4844 || eap->cmdidx == CMD_laddfile)
4845 wp = curwin;
4846
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004847 /*
4848 * This function is used by the :cfile, :cgetfile and :caddfile
4849 * commands.
4850 * :cfile always creates a new quickfix list and jumps to the
4851 * first error.
4852 * :cgetfile creates a new quickfix list but doesn't jump to the
4853 * first error.
4854 * :caddfile adds to an existing quickfix list. If there is no
4855 * quickfix list then a new list is created.
4856 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004857 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004858 && eap->cmdidx != CMD_laddfile),
4859 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004860 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004861 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01004862 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004863 if (qi == NULL)
4864 return;
4865 }
4866 if (res >= 0)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004867 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004868 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004869 if (au_name != NULL)
4870 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004871
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004872 // Jump to the first error for a new list and if autocmds didn't
4873 // free the list.
4874 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
4875 && qflist_valid(wp, save_qfid))
4876 {
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004877 // display the first error
4878 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004879 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004880}
4881
4882/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004883 * Return the vimgrep autocmd name.
4884 */
4885 static char_u *
4886vgr_get_auname(cmdidx_T cmdidx)
4887{
4888 switch (cmdidx)
4889 {
4890 case CMD_vimgrep: return (char_u *)"vimgrep";
4891 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4892 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4893 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4894 case CMD_grep: return (char_u *)"grep";
4895 case CMD_lgrep: return (char_u *)"lgrep";
4896 case CMD_grepadd: return (char_u *)"grepadd";
4897 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4898 default: return NULL;
4899 }
4900}
4901
4902/*
4903 * Initialize the regmatch used by vimgrep for pattern "s".
4904 */
4905 static void
4906vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4907{
4908 /* Get the search pattern: either white-separated or enclosed in // */
4909 regmatch->regprog = NULL;
4910
4911 if (s == NULL || *s == NUL)
4912 {
4913 /* Pattern is empty, use last search pattern. */
4914 if (last_search_pat() == NULL)
4915 {
4916 EMSG(_(e_noprevre));
4917 return;
4918 }
4919 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4920 }
4921 else
4922 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4923
4924 regmatch->rmm_ic = p_ic;
4925 regmatch->rmm_maxcol = 0;
4926}
4927
4928/*
4929 * Display a file name when vimgrep is running.
4930 */
4931 static void
4932vgr_display_fname(char_u *fname)
4933{
4934 char_u *p;
4935
4936 msg_start();
4937 p = msg_strtrunc(fname, TRUE);
4938 if (p == NULL)
4939 msg_outtrans(fname);
4940 else
4941 {
4942 msg_outtrans(p);
4943 vim_free(p);
4944 }
4945 msg_clr_eos();
4946 msg_didout = FALSE; /* overwrite this message */
4947 msg_nowait = TRUE; /* don't wait for this message */
4948 msg_col = 0;
4949 out_flush();
4950}
4951
4952/*
4953 * Load a dummy buffer to search for a pattern using vimgrep.
4954 */
4955 static buf_T *
4956vgr_load_dummy_buf(
4957 char_u *fname,
4958 char_u *dirname_start,
4959 char_u *dirname_now)
4960{
4961 int save_mls;
4962#if defined(FEAT_SYN_HL)
4963 char_u *save_ei = NULL;
4964#endif
4965 buf_T *buf;
4966
4967#if defined(FEAT_SYN_HL)
4968 /* Don't do Filetype autocommands to avoid loading syntax and
4969 * indent scripts, a great speed improvement. */
4970 save_ei = au_event_disable(",Filetype");
4971#endif
4972 /* Don't use modelines here, it's useless. */
4973 save_mls = p_mls;
4974 p_mls = 0;
4975
4976 /* Load file into a buffer, so that 'fileencoding' is detected,
4977 * autocommands applied, etc. */
4978 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
4979
4980 p_mls = save_mls;
4981#if defined(FEAT_SYN_HL)
4982 au_event_restore(save_ei);
4983#endif
4984
4985 return buf;
4986}
4987
4988/*
4989 * Check whether a quickfix/location list valid. Autocmds may remove or change
4990 * a quickfix list when vimgrep is running. If the list is not found, create a
4991 * new list.
4992 */
4993 static int
4994vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004995 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004996 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004997 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004998 char_u *title)
4999{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005000 /* Verify that the quickfix/location list was not freed by an autocmd */
5001 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005002 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005003 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005004 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005005 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005006 EMSG(_(e_loc_list_changed));
5007 return FALSE;
5008 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005009 else
5010 {
5011 /* Quickfix list is not found, create a new one. */
5012 qf_new_list(qi, title);
5013 return TRUE;
5014 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005015 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005016
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005017 if (qi->qf_lists[qi->qf_curlist].qf_id != qfid)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005018 /* Autocommands changed the quickfix list. Find the one we were
5019 * using and restore it. */
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005020 qi->qf_curlist = qf_id2nr(qi, qfid);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005021
5022 return TRUE;
5023}
5024
5025/*
5026 * Search for a pattern in all the lines in a buffer and add the matching lines
5027 * to a quickfix list.
5028 */
5029 static int
5030vgr_match_buflines(
5031 qf_info_T *qi,
5032 char_u *fname,
5033 buf_T *buf,
5034 regmmatch_T *regmatch,
5035 long tomatch,
5036 int duplicate_name,
5037 int flags)
5038{
5039 int found_match = FALSE;
5040 long lnum;
5041 colnr_T col;
5042
5043 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5044 {
5045 col = 0;
5046 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5047 col, NULL, NULL) > 0)
5048 {
5049 /* Pass the buffer number so that it gets used even for a
5050 * dummy buffer, unless duplicate_name is set, then the
5051 * buffer will be wiped out below. */
5052 if (qf_add_entry(qi,
5053 qi->qf_curlist,
5054 NULL, /* dir */
5055 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005056 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005057 duplicate_name ? 0 : buf->b_fnum,
5058 ml_get_buf(buf,
5059 regmatch->startpos[0].lnum + lnum, FALSE),
5060 regmatch->startpos[0].lnum + lnum,
5061 regmatch->startpos[0].col + 1,
5062 FALSE, /* vis_col */
5063 NULL, /* search pattern */
5064 0, /* nr */
5065 0, /* type */
5066 TRUE /* valid */
5067 ) == FAIL)
5068 {
5069 got_int = TRUE;
5070 break;
5071 }
5072 found_match = TRUE;
5073 if (--tomatch == 0)
5074 break;
5075 if ((flags & VGR_GLOBAL) == 0
5076 || regmatch->endpos[0].lnum > 0)
5077 break;
5078 col = regmatch->endpos[0].col
5079 + (col == regmatch->endpos[0].col);
5080 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5081 break;
5082 }
5083 line_breakcheck();
5084 if (got_int)
5085 break;
5086 }
5087
5088 return found_match;
5089}
5090
5091/*
5092 * Jump to the first match and update the directory.
5093 */
5094 static void
5095vgr_jump_to_match(
5096 qf_info_T *qi,
5097 int forceit,
5098 int *redraw_for_dummy,
5099 buf_T *first_match_buf,
5100 char_u *target_dir)
5101{
5102 buf_T *buf;
5103
5104 buf = curbuf;
5105 qf_jump(qi, 0, 0, forceit);
5106 if (buf != curbuf)
5107 /* If we jumped to another buffer redrawing will already be
5108 * taken care of. */
5109 *redraw_for_dummy = FALSE;
5110
5111 /* Jump to the directory used after loading the buffer. */
5112 if (curbuf == first_match_buf && target_dir != NULL)
5113 {
5114 exarg_T ea;
5115
5116 ea.arg = target_dir;
5117 ea.cmdidx = CMD_lcd;
5118 ex_cd(&ea);
5119 }
5120}
5121
5122/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005123 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005124 * ":vimgrepadd {pattern} file(s)"
5125 * ":lvimgrep {pattern} file(s)"
5126 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005127 */
5128 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005129ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005130{
Bram Moolenaar81695252004-12-29 20:58:21 +00005131 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005132 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005133 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005134 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005135 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005136 char_u *s;
5137 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005138 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005139 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005140 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005141 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005142 buf_T *buf;
5143 int duplicate_name = FALSE;
5144 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005145 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005146 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005147 buf_T *first_match_buf = NULL;
5148 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005149 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005150 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005151 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005152 char_u *dirname_start = NULL;
5153 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005154 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005155 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005156
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005157 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005158 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5159 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005160 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005161#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005162 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005163 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005164#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005165 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005166
Bram Moolenaar754b5602006-02-09 23:53:20 +00005167 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005168 || eap->cmdidx == CMD_lvimgrep
5169 || eap->cmdidx == CMD_lgrepadd
5170 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00005171 {
5172 qi = ll_get_or_alloc_list(curwin);
5173 if (qi == NULL)
5174 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005175 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005176 }
5177
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005178 if (eap->addr_count > 0)
5179 tomatch = eap->line2;
5180 else
5181 tomatch = MAXLNUM;
5182
Bram Moolenaar81695252004-12-29 20:58:21 +00005183 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005184 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005185 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005186 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005187 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005188 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005189 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005190 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005191 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005192
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005193 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005194 if (regmatch.regprog == NULL)
5195 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005196
5197 p = skipwhite(p);
5198 if (*p == NUL)
5199 {
5200 EMSG(_("E683: File name missing or invalid pattern"));
5201 goto theend;
5202 }
5203
Bram Moolenaar55b69262017-08-13 13:42:01 +02005204 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005205 && eap->cmdidx != CMD_vimgrepadd
5206 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005207 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005208 /* make place for a new list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005209 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005210
5211 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005212 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005213 goto theend;
5214 if (fcount == 0)
5215 {
5216 EMSG(_(e_nomatch));
5217 goto theend;
5218 }
5219
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005220 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5221 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005222 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005223 {
5224 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005225 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005226 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005227
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005228 /* Remember the current directory, because a BufRead autocommand that does
5229 * ":lcd %:p:h" changes the meaning of short path names. */
5230 mch_dirname(dirname_start, MAXPATHL);
5231
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005232 /* Remember the current quickfix list identifier, so that we can check for
5233 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005234 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005235
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005236 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005237 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005238 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005239 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005240 if (time(NULL) > seconds)
5241 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005242 /* Display the file name every second or so, show the user we are
5243 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005244 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005245 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005246 }
5247
Bram Moolenaar81695252004-12-29 20:58:21 +00005248 buf = buflist_findname_exp(fnames[fi]);
5249 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5250 {
5251 /* Remember that a buffer with this name already exists. */
5252 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005253 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005254 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005255
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005256 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005257 }
5258 else
5259 /* Use existing, loaded buffer. */
5260 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005261
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005262 /* Check whether the quickfix list is still valid. When loading a
5263 * buffer above, autocommands might have changed the quickfix list. */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005264 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005265 {
5266 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005267 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005268 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005269 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005270
Bram Moolenaar81695252004-12-29 20:58:21 +00005271 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005272 {
5273 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005274 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005275 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005276 else
5277 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005278 /* Try for a match in all lines of the buffer.
5279 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005280 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5281 tomatch, duplicate_name, flags);
5282
Bram Moolenaar81695252004-12-29 20:58:21 +00005283 if (using_dummy)
5284 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005285 if (found_match && first_match_buf == NULL)
5286 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005287 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005288 {
Bram Moolenaar81695252004-12-29 20:58:21 +00005289 /* Never keep a dummy buffer if there is another buffer
5290 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005291 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005292 buf = NULL;
5293 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005294 else if (!cmdmod.hide
5295 || buf->b_p_bh[0] == 'u' /* "unload" */
5296 || buf->b_p_bh[0] == 'w' /* "wipe" */
5297 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00005298 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005299 /* When no match was found we don't need to remember the
5300 * buffer, wipe it out. If there was a match and it
5301 * wasn't the first one or we won't jump there: only
5302 * unload the buffer.
5303 * Ignore 'hidden' here, because it may lead to having too
5304 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00005305 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005306 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005307 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005308 buf = NULL;
5309 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005310 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005311 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005312 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02005313 /* Keeping the buffer, remove the dummy flag. */
5314 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005315 buf = NULL;
5316 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005317 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005318
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005319 if (buf != NULL)
5320 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02005321 /* Keeping the buffer, remove the dummy flag. */
5322 buf->b_flags &= ~BF_DUMMY;
5323
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005324 /* If the buffer is still loaded we need to use the
5325 * directory we jumped to below. */
5326 if (buf == first_match_buf
5327 && target_dir == NULL
5328 && STRCMP(dirname_start, dirname_now) != 0)
5329 target_dir = vim_strsave(dirname_now);
5330
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005331 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005332 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00005333 * need to be done (again). But not the window-local
5334 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005335 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005336#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005337 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5338 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005339#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005340 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005341 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005342 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005343 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005344 }
5345 }
5346
5347 FreeWild(fcount, fnames);
5348
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005349 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5350 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
5351 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005352 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005353
Bram Moolenaar864293a2016-06-02 13:40:04 +02005354 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005355
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005356 if (au_name != NULL)
5357 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5358 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01005359 /*
5360 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5361 * is still valid.
5362 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005363 if (!qflist_valid(wp, save_qfid))
5364 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005365
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005366 // If autocommands changed the current list, then restore it
5367 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
5368 qi->qf_curlist = qf_id2nr(qi, save_qfid);
5369
Bram Moolenaar86b68352004-12-27 21:59:20 +00005370 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005371 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005372 {
5373 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005374 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5375 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005376 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005377 else
5378 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005379
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005380 /* If we loaded a dummy buffer into the current window, the autocommands
5381 * may have messed up things, need to redraw and recompute folds. */
5382 if (redraw_for_dummy)
5383 {
5384#ifdef FEAT_FOLDING
5385 foldUpdateAll(curwin);
5386#else
5387 redraw_later(NOT_VALID);
5388#endif
5389 }
5390
Bram Moolenaar86b68352004-12-27 21:59:20 +00005391theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005392 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005393 vim_free(dirname_now);
5394 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005395 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005396 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005397}
5398
5399/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005400 * Restore current working directory to "dirname_start" if they differ, taking
5401 * into account whether it is set locally or globally.
5402 */
5403 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005404restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005405{
5406 char_u *dirname_now = alloc(MAXPATHL);
5407
5408 if (NULL != dirname_now)
5409 {
5410 mch_dirname(dirname_now, MAXPATHL);
5411 if (STRCMP(dirname_start, dirname_now) != 0)
5412 {
5413 /* If the directory has changed, change it back by building up an
5414 * appropriate ex command and executing it. */
5415 exarg_T ea;
5416
5417 ea.arg = dirname_start;
5418 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5419 ex_cd(&ea);
5420 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005421 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005422 }
5423}
5424
5425/*
5426 * Load file "fname" into a dummy buffer and return the buffer pointer,
5427 * placing the directory resulting from the buffer load into the
5428 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5429 * prior to calling this function. Restores directory to "dirname_start" prior
5430 * to returning, if autocmds or the 'autochdir' option have changed it.
5431 *
5432 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5433 * or wipe_dummy_buffer() later!
5434 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005435 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005436 */
5437 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005438load_dummy_buffer(
5439 char_u *fname,
5440 char_u *dirname_start, /* in: old directory */
5441 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005442{
5443 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005444 bufref_T newbufref;
5445 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005446 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005447 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005448 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005449
5450 /* Allocate a buffer without putting it in the buffer list. */
5451 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5452 if (newbuf == NULL)
5453 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005454 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005455
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005456 /* Init the options. */
5457 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5458
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005459 /* need to open the memfile before putting the buffer in a window */
5460 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005461 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005462 /* Make sure this buffer isn't wiped out by autocommands. */
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005463 ++newbuf->b_locked;
5464
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005465 /* set curwin/curbuf to buf and save a few things */
5466 aucmd_prepbuf(&aco, newbuf);
5467
5468 /* Need to set the filename for autocommands. */
5469 (void)setfname(curbuf, fname, NULL, FALSE);
5470
Bram Moolenaar81695252004-12-29 20:58:21 +00005471 /* Create swap file now to avoid the ATTENTION message. */
5472 check_need_swap(TRUE);
5473
5474 /* Remove the "dummy" flag, otherwise autocommands may not
5475 * work. */
5476 curbuf->b_flags &= ~BF_DUMMY;
5477
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005478 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005479 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005480 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005481 NULL, READ_NEW | READ_DUMMY);
5482 --newbuf->b_locked;
5483 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005484 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005485 && !(curbuf->b_flags & BF_NEW))
5486 {
5487 failed = FALSE;
5488 if (curbuf != newbuf)
5489 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005490 /* Bloody autocommands changed the buffer! Can happen when
5491 * using netrw and editing a remote file. Use the current
5492 * buffer instead, delete the dummy one after restoring the
5493 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005494 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005495 newbuf = curbuf;
5496 }
5497 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005498
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005499 /* restore curwin/curbuf and a few other things */
5500 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005501 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5502 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005503
5504 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5505 * skip it. */
5506 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005507 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005508
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005509 /*
5510 * When autocommands/'autochdir' option changed directory: go back.
5511 * Let the caller know what the resulting dir was first, in case it is
5512 * important.
5513 */
5514 mch_dirname(resulting_dir, MAXPATHL);
5515 restore_start_dir(dirname_start);
5516
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005517 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005518 return NULL;
5519 if (failed)
5520 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005521 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005522 return NULL;
5523 }
5524 return newbuf;
5525}
5526
5527/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005528 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5529 * directory to "dirname_start" prior to returning, if autocmds or the
5530 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005531 */
5532 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005533wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005534{
5535 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005536 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005537#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005538 cleanup_T cs;
5539
5540 /* Reset the error/interrupt/exception state here so that aborting()
5541 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5542 * work when got_int is set. */
5543 enter_cleanup(&cs);
5544#endif
5545
Bram Moolenaar81695252004-12-29 20:58:21 +00005546 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005547
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005548#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005549 /* Restore the error/interrupt/exception state if not discarded by a
5550 * new aborting error, interrupt, or uncaught exception. */
5551 leave_cleanup(&cs);
5552#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005553 /* When autocommands/'autochdir' option changed directory: go back. */
5554 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005555 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005556}
5557
5558/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005559 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5560 * directory to "dirname_start" prior to returning, if autocmds or the
5561 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005562 */
5563 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005564unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005565{
5566 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005567 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005568 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005569
5570 /* When autocommands/'autochdir' option changed directory: go back. */
5571 restore_start_dir(dirname_start);
5572 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005573}
5574
Bram Moolenaar05159a02005-02-26 23:04:13 +00005575#if defined(FEAT_EVAL) || defined(PROTO)
5576/*
5577 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005578 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005579 */
5580 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005581get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005582{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005583 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005584 dict_T *dict;
5585 char_u buf[2];
5586 qfline_T *qfp;
5587 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005588 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005589
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005590 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005591 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005592 qi = &ql_info;
5593 if (wp != NULL)
5594 {
5595 qi = GET_LOC_LIST(wp);
5596 if (qi == NULL)
5597 return FAIL;
5598 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005599 }
5600
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005601 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005602 qf_idx = qi->qf_curlist;
5603
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005604 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005605 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005606
Bram Moolenaard823fa92016-08-12 16:29:27 +02005607 qfp = qi->qf_lists[qf_idx].qf_start;
5608 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005609 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005610 /* Handle entries with a non-existing buffer number. */
5611 bufnum = qfp->qf_fnum;
5612 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5613 bufnum = 0;
5614
Bram Moolenaar05159a02005-02-26 23:04:13 +00005615 if ((dict = dict_alloc()) == NULL)
5616 return FAIL;
5617 if (list_append_dict(list, dict) == FAIL)
5618 return FAIL;
5619
5620 buf[0] = qfp->qf_type;
5621 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005622 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5623 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5624 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5625 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5626 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5627 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5628 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5629 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5630 || dict_add_string(dict, "type", buf) == FAIL
5631 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005632 return FAIL;
5633
5634 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005635 if (qfp == NULL)
5636 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005637 }
5638 return OK;
5639}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005640
5641/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005642 * Flags used by getqflist()/getloclist() to determine which fields to return.
5643 */
5644enum {
5645 QF_GETLIST_NONE = 0x0,
5646 QF_GETLIST_TITLE = 0x1,
5647 QF_GETLIST_ITEMS = 0x2,
5648 QF_GETLIST_NR = 0x4,
5649 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005650 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005651 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005652 QF_GETLIST_IDX = 0x40,
5653 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005654 QF_GETLIST_TICK = 0x100,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005655 QF_GETLIST_ALL = 0x1FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005656};
5657
5658/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005659 * Parse text from 'di' and return the quickfix list items.
5660 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005661 */
5662 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005663qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005664{
5665 int status = FAIL;
5666 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005667 char_u *errorformat = p_efm;
5668 dictitem_T *efm_di;
5669 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005670
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005671 /* Only a List value is supported */
5672 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005673 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005674 /* If errorformat is supplied then use it, otherwise use the 'efm'
5675 * option setting
5676 */
5677 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5678 {
5679 if (efm_di->di_tv.v_type != VAR_STRING ||
5680 efm_di->di_tv.vval.v_string == NULL)
5681 return FAIL;
5682 errorformat = efm_di->di_tv.vval.v_string;
5683 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005684
Bram Moolenaar36538222017-09-02 19:51:44 +02005685 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005686 if (l == NULL)
5687 return FAIL;
5688
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005689 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
5690 if (qi != NULL)
5691 {
5692 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
5693 qi->qf_refcount++;
5694
Bram Moolenaar36538222017-09-02 19:51:44 +02005695 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005696 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5697 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005698 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005699 qf_free(qi, 0);
5700 }
5701 free(qi);
5702 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005703 dict_add_list(retdict, "items", l);
5704 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005705 }
5706
5707 return status;
5708}
5709
5710/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005711 * Return the quickfix/location list window identifier in the current tabpage.
5712 */
5713 static int
5714qf_winid(qf_info_T *qi)
5715{
5716 win_T *win;
5717
5718 /* The quickfix window can be opened even if the quickfix list is not set
5719 * using ":copen". This is not true for location lists. */
5720 if (qi == NULL)
5721 return 0;
5722 win = qf_find_win(qi);
5723 if (win != NULL)
5724 return win->w_id;
5725 return 0;
5726}
5727
5728/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005729 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005730 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005731 static int
5732qf_getprop_keys2flags(dict_T *what)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005733{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005734 int flags = QF_GETLIST_NONE;
5735
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005736 if (dict_find(what, (char_u *)"all", -1) != NULL)
5737 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005738
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005739 if (dict_find(what, (char_u *)"title", -1) != NULL)
5740 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005741
Bram Moolenaara6d48492017-12-12 22:45:31 +01005742 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5743 flags |= QF_GETLIST_NR;
5744
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005745 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5746 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005747
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005748 if (dict_find(what, (char_u *)"context", -1) != NULL)
5749 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005750
Bram Moolenaara6d48492017-12-12 22:45:31 +01005751 if (dict_find(what, (char_u *)"id", -1) != NULL)
5752 flags |= QF_GETLIST_ID;
5753
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005754 if (dict_find(what, (char_u *)"items", -1) != NULL)
5755 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005756
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005757 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5758 flags |= QF_GETLIST_IDX;
5759
5760 if (dict_find(what, (char_u *)"size", -1) != NULL)
5761 flags |= QF_GETLIST_SIZE;
5762
Bram Moolenaarb254af32017-12-18 19:48:58 +01005763 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5764 flags |= QF_GETLIST_TICK;
5765
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005766 return flags;
5767}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005768
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005769/*
5770 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5771 * If 'nr' and 'id' are not present in 'what' then return the current
5772 * quickfix list index.
5773 * If 'nr' is zero then return the current quickfix list index.
5774 * If 'nr' is '$' then return the last quickfix list index.
5775 * If 'id' is present then return the index of the quickfix list with that id.
5776 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5777 * Return -1, if quickfix list is not present or if the stack is empty.
5778 */
5779 static int
5780qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5781{
5782 int qf_idx;
5783 dictitem_T *di;
5784
5785 qf_idx = qi->qf_curlist; /* default is the current list */
5786 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5787 {
5788 /* Use the specified quickfix/location list */
5789 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005790 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005791 /* for zero use the current list */
5792 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005793 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005794 qf_idx = di->di_tv.vval.v_number - 1;
5795 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005796 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005797 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005798 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005799 else if (di->di_tv.v_type == VAR_STRING
5800 && di->di_tv.vval.v_string != NULL
5801 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5802 /* Get the last quickfix list number */
5803 qf_idx = qi->qf_listcount - 1;
5804 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005805 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005806 }
5807
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005808 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5809 {
5810 /* Look for a list with the specified id */
5811 if (di->di_tv.v_type == VAR_NUMBER)
5812 {
5813 /*
5814 * For zero, use the current list or the list specified by 'nr'
5815 */
5816 if (di->di_tv.vval.v_number != 0)
5817 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5818 }
5819 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005820 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005821 }
5822
5823 return qf_idx;
5824}
5825
5826/*
5827 * Return default values for quickfix list properties in retdict.
5828 */
5829 static int
5830qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5831{
5832 int status = OK;
5833
5834 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005835 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005836 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5837 {
5838 list_T *l = list_alloc();
5839 if (l != NULL)
5840 status = dict_add_list(retdict, "items", l);
5841 else
5842 status = FAIL;
5843 }
5844 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005845 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005846 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005847 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005848 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005849 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005850 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005851 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005852 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005853 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005854 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005855 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005856 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005857 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005858
5859 return status;
5860}
5861
5862/*
5863 * Return the quickfix list title as 'title' in retdict
5864 */
5865 static int
5866qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5867{
Bram Moolenaare0be1672018-07-08 16:50:37 +02005868 return dict_add_string(retdict, "title", qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005869}
5870
5871/*
5872 * Return the quickfix list items/entries as 'items' in retdict
5873 */
5874 static int
5875qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5876{
5877 int status = OK;
5878 list_T *l = list_alloc();
5879 if (l != NULL)
5880 {
5881 (void)get_errorlist(qi, NULL, qf_idx, l);
5882 dict_add_list(retdict, "items", l);
5883 }
5884 else
5885 status = FAIL;
5886
5887 return status;
5888}
5889
5890/*
5891 * Return the quickfix list context (if any) as 'context' in retdict.
5892 */
5893 static int
5894qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5895{
5896 int status;
5897 dictitem_T *di;
5898
5899 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5900 {
5901 di = dictitem_alloc((char_u *)"context");
5902 if (di != NULL)
5903 {
5904 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5905 status = dict_add(retdict, di);
5906 if (status == FAIL)
5907 dictitem_free(di);
5908 }
5909 else
5910 status = FAIL;
5911 }
5912 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02005913 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005914
5915 return status;
5916}
5917
5918/*
5919 * Return the quickfix list index as 'idx' in retdict
5920 */
5921 static int
5922qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5923{
5924 int idx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005925 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005926 /* For empty lists, qf_index is set to 1 */
5927 idx = 0;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005928 return dict_add_number(retdict, "idx", idx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005929}
5930
5931/*
5932 * Return quickfix/location list details (title) as a
5933 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5934 * then current list is used. Otherwise the specified list is used.
5935 */
5936 int
5937qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5938{
5939 qf_info_T *qi = &ql_info;
5940 int status = OK;
5941 int qf_idx;
5942 dictitem_T *di;
5943 int flags = QF_GETLIST_NONE;
5944
5945 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5946 return qf_get_list_from_lines(what, di, retdict);
5947
5948 if (wp != NULL)
5949 qi = GET_LOC_LIST(wp);
5950
5951 flags = qf_getprop_keys2flags(what);
5952
5953 if (qi != NULL && qi->qf_listcount != 0)
5954 qf_idx = qf_getprop_qfidx(qi, what);
5955
Bram Moolenaara6d48492017-12-12 22:45:31 +01005956 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005957 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005958 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005959
Bram Moolenaard823fa92016-08-12 16:29:27 +02005960 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005961 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005962 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005963 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005964 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005965 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005966 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005967 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005968 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005969 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005970 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005971 status = dict_add_number(retdict, "id", qi->qf_lists[qf_idx].qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005972 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005973 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005974 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005975 status = dict_add_number(retdict, "size",
5976 qi->qf_lists[qf_idx].qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005977 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005978 status = dict_add_number(retdict, "changedtick",
5979 qi->qf_lists[qf_idx].qf_changedtick);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005980
Bram Moolenaard823fa92016-08-12 16:29:27 +02005981 return status;
5982}
5983
5984/*
5985 * Add list of entries to quickfix/location list. Each list entry is
5986 * a dictionary with item information.
5987 */
5988 static int
5989qf_add_entries(
5990 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005991 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005992 list_T *list,
5993 char_u *title,
5994 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005995{
5996 listitem_T *li;
5997 dict_T *d;
Bram Moolenaard76ce852018-05-01 15:02:04 +02005998 char_u *filename, *module, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005999 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006000 long lnum;
6001 int col, nr;
6002 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006003 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006004 int valid, status;
6005 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006006 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006007
Bram Moolenaara3921f42017-06-04 15:30:34 +02006008 if (action == ' ' || qf_idx == qi->qf_listcount)
6009 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006010 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006011 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006012 qf_idx = qi->qf_curlist;
6013 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006014 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006015 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02006016 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006017 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006018 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006019 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006020 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006021 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006022
6023 for (li = list->lv_first; li != NULL; li = li->li_next)
6024 {
6025 if (li->li_tv.v_type != VAR_DICT)
6026 continue; /* Skip non-dict items */
6027
6028 d = li->li_tv.vval.v_dict;
6029 if (d == NULL)
6030 continue;
6031
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006032 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaard76ce852018-05-01 15:02:04 +02006033 module = get_dict_string(d, (char_u *)"module", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006034 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6035 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6036 col = (int)get_dict_number(d, (char_u *)"col");
6037 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6038 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006039 type = get_dict_string(d, (char_u *)"type", TRUE);
6040 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6041 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006042 if (text == NULL)
6043 text = vim_strsave((char_u *)"");
6044
6045 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006046 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006047 valid = FALSE;
6048
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006049 /* Mark entries with non-existing buffer number as not valid. Give the
6050 * error message only once. */
6051 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6052 {
6053 if (!did_bufnr_emsg)
6054 {
6055 did_bufnr_emsg = TRUE;
6056 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6057 }
6058 valid = FALSE;
6059 bufnum = 0;
6060 }
6061
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02006062 /* If the 'valid' field is present it overrules the detected value. */
6063 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6064 valid = (int)get_dict_number(d, (char_u *)"valid");
6065
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006066 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006067 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006068 NULL, /* dir */
6069 filename,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006070 module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006071 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006072 text,
6073 lnum,
6074 col,
6075 vcol, /* vis_col */
6076 pattern, /* search pattern */
6077 nr,
6078 type == NULL ? NUL : *type,
6079 valid);
6080
6081 vim_free(filename);
Bram Moolenaard76ce852018-05-01 15:02:04 +02006082 vim_free(module);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006083 vim_free(pattern);
6084 vim_free(text);
6085 vim_free(type);
6086
6087 if (status == FAIL)
6088 {
6089 retval = FAIL;
6090 break;
6091 }
6092 }
6093
Bram Moolenaara3921f42017-06-04 15:30:34 +02006094 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02006095 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02006096 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006097 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02006098 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006099 if (action != 'a')
6100 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006101 qi->qf_lists[qf_idx].qf_ptr =
6102 qi->qf_lists[qf_idx].qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006103 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara3921f42017-06-04 15:30:34 +02006104 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006105 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006106
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006107 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02006108 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006109
6110 return retval;
6111}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006112
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006113/*
6114 * Get the quickfix list index from 'nr' or 'id'
6115 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006116 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006117qf_setprop_get_qfidx(
6118 qf_info_T *qi,
6119 dict_T *what,
6120 int action,
6121 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006122{
6123 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006124 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006125
Bram Moolenaard823fa92016-08-12 16:29:27 +02006126 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6127 {
6128 /* Use the specified quickfix/location list */
6129 if (di->di_tv.v_type == VAR_NUMBER)
6130 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006131 /* for zero use the current list */
6132 if (di->di_tv.vval.v_number != 0)
6133 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006134
Bram Moolenaar55b69262017-08-13 13:42:01 +02006135 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6136 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006137 /*
6138 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02006139 * non-available list and add the new list at the end of the
6140 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006141 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006142 *newlist = TRUE;
6143 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006144 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006145 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006146 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006147 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006148 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02006149 }
6150 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006151 && di->di_tv.vval.v_string != NULL
6152 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006153 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006154 if (qi->qf_listcount > 0)
6155 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006156 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006157 qf_idx = 0;
6158 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006159 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006160 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006161 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006162 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006163 }
6164
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006165 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006166 {
6167 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006168 if (di->di_tv.v_type != VAR_NUMBER)
6169 return INVALID_QFIDX;
6170
6171 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006172 }
6173
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006174 return qf_idx;
6175}
6176
6177/*
6178 * Set the quickfix list title.
6179 */
6180 static int
6181qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6182{
6183 if (di->di_tv.v_type != VAR_STRING)
6184 return FAIL;
6185
6186 vim_free(qi->qf_lists[qf_idx].qf_title);
6187 qi->qf_lists[qf_idx].qf_title =
6188 get_dict_string(what, (char_u *)"title", TRUE);
6189 if (qf_idx == qi->qf_curlist)
6190 qf_update_win_titlevar(qi);
6191
6192 return OK;
6193}
6194
6195/*
6196 * Set quickfix list items/entries.
6197 */
6198 static int
6199qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6200{
6201 int retval = FAIL;
6202 char_u *title_save;
6203
6204 if (di->di_tv.v_type != VAR_LIST)
6205 return FAIL;
6206
6207 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6208 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6209 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006210 vim_free(title_save);
6211
6212 return retval;
6213}
6214
6215/*
6216 * Set quickfix list items/entries from a list of lines.
6217 */
6218 static int
6219qf_setprop_items_from_lines(
6220 qf_info_T *qi,
6221 int qf_idx,
6222 dict_T *what,
6223 dictitem_T *di,
6224 int action)
6225{
6226 char_u *errorformat = p_efm;
6227 dictitem_T *efm_di;
6228 int retval = FAIL;
6229
6230 /* Use the user supplied errorformat settings (if present) */
6231 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6232 {
6233 if (efm_di->di_tv.v_type != VAR_STRING ||
6234 efm_di->di_tv.vval.v_string == NULL)
6235 return FAIL;
6236 errorformat = efm_di->di_tv.vval.v_string;
6237 }
6238
6239 /* Only a List value is supported */
6240 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6241 return FAIL;
6242
6243 if (action == 'r')
6244 qf_free_items(qi, qf_idx);
6245 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6246 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6247 retval = OK;
6248
6249 return retval;
6250}
6251
6252/*
6253 * Set quickfix list context.
6254 */
6255 static int
6256qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di)
6257{
6258 typval_T *ctx;
6259
6260 free_tv(qi->qf_lists[qf_idx].qf_ctx);
6261 ctx = alloc_tv();
6262 if (ctx != NULL)
6263 copy_tv(&di->di_tv, ctx);
6264 qi->qf_lists[qf_idx].qf_ctx = ctx;
6265
6266 return OK;
6267}
6268
6269/*
6270 * Set quickfix/location list properties (title, items, context).
6271 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006272 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006273 */
6274 static int
6275qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6276{
6277 dictitem_T *di;
6278 int retval = FAIL;
6279 int qf_idx;
6280 int newlist = FALSE;
6281
6282 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
6283 newlist = TRUE;
6284
6285 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
6286 if (qf_idx == INVALID_QFIDX) /* List not found */
6287 return FAIL;
6288
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006289 if (newlist)
6290 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006291 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006292 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006293 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006294 }
6295
6296 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006297 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006298 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006299 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006300 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006301 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006302 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006303 retval = qf_setprop_context(qi, qf_idx, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006304
Bram Moolenaarb254af32017-12-18 19:48:58 +01006305 if (retval == OK)
6306 qf_list_changed(qi, qf_idx);
6307
Bram Moolenaard823fa92016-08-12 16:29:27 +02006308 return retval;
6309}
6310
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006311/*
6312 * Find the non-location list window with the specified location list.
6313 */
6314 static win_T *
6315find_win_with_ll(qf_info_T *qi)
6316{
6317 win_T *wp = NULL;
6318
6319 FOR_ALL_WINDOWS(wp)
6320 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6321 return wp;
6322
6323 return NULL;
6324}
6325
6326/*
6327 * Free the entire quickfix/location list stack.
6328 * If the quickfix/location list window is open, then clear it.
6329 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006330 static void
6331qf_free_stack(win_T *wp, qf_info_T *qi)
6332{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006333 win_T *qfwin = qf_find_win(qi);
6334 win_T *llwin = NULL;
6335 win_T *orig_wp = wp;
6336
6337 if (qfwin != NULL)
6338 {
6339 /* If the quickfix/location list window is open, then clear it */
6340 if (qi->qf_curlist < qi->qf_listcount)
6341 qf_free(qi, qi->qf_curlist);
6342 qf_update_buffer(qi, NULL);
6343 }
6344
6345 if (wp != NULL && IS_LL_WINDOW(wp))
6346 {
6347 /* If in the location list window, then use the non-location list
6348 * window with this location list (if present)
6349 */
6350 llwin = find_win_with_ll(qi);
6351 if (llwin != NULL)
6352 wp = llwin;
6353 }
6354
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006355 qf_free_all(wp);
6356 if (wp == NULL)
6357 {
6358 /* quickfix list */
6359 qi->qf_curlist = 0;
6360 qi->qf_listcount = 0;
6361 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006362 else if (IS_LL_WINDOW(orig_wp))
6363 {
6364 /* If the location list window is open, then create a new empty
6365 * location list */
6366 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006367
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006368 /* first free the list reference in the location list window */
6369 ll_free_all(&orig_wp->w_llist_ref);
6370
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006371 orig_wp->w_llist_ref = new_ll;
6372 if (llwin != NULL)
6373 {
6374 llwin->w_llist = new_ll;
6375 new_ll->qf_refcount++;
6376 }
6377 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006378}
6379
Bram Moolenaard823fa92016-08-12 16:29:27 +02006380/*
6381 * Populate the quickfix list with the items supplied in the list
6382 * of dictionaries. "title" will be copied to w:quickfix_title.
6383 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6384 */
6385 int
6386set_errorlist(
6387 win_T *wp,
6388 list_T *list,
6389 int action,
6390 char_u *title,
6391 dict_T *what)
6392{
6393 qf_info_T *qi = &ql_info;
6394 int retval = OK;
6395
6396 if (wp != NULL)
6397 {
6398 qi = ll_get_or_alloc_list(wp);
6399 if (qi == NULL)
6400 return FAIL;
6401 }
6402
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006403 if (action == 'f')
6404 {
6405 /* Free the entire quickfix or location list stack */
6406 qf_free_stack(wp, qi);
6407 }
6408 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006409 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006410 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006411 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006412 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006413 if (retval == OK)
6414 qf_list_changed(qi, qi->qf_curlist);
6415 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006416
6417 return retval;
6418}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006419
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006420/*
6421 * Mark the context as in use for all the lists in a quickfix stack.
6422 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006423 static int
6424mark_quickfix_ctx(qf_info_T *qi, int copyID)
6425{
6426 int i;
6427 int abort = FALSE;
6428 typval_T *ctx;
6429
6430 for (i = 0; i < LISTCOUNT && !abort; ++i)
6431 {
6432 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006433 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6434 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006435 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6436 }
6437
6438 return abort;
6439}
6440
6441/*
6442 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006443 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006444 */
6445 int
6446set_ref_in_quickfix(int copyID)
6447{
6448 int abort = FALSE;
6449 tabpage_T *tp;
6450 win_T *win;
6451
6452 abort = mark_quickfix_ctx(&ql_info, copyID);
6453 if (abort)
6454 return abort;
6455
6456 FOR_ALL_TAB_WINDOWS(tp, win)
6457 {
6458 if (win->w_llist != NULL)
6459 {
6460 abort = mark_quickfix_ctx(win->w_llist, copyID);
6461 if (abort)
6462 return abort;
6463 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006464 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6465 {
6466 /* In a location list window and none of the other windows is
6467 * referring to this location list. Mark the location list
6468 * context as still in use.
6469 */
6470 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6471 if (abort)
6472 return abort;
6473 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006474 }
6475
6476 return abort;
6477}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006478#endif
6479
Bram Moolenaar81695252004-12-29 20:58:21 +00006480/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006481 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006482 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006483 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006484 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006485 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006486 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006487 */
6488 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006489ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006490{
6491 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006492 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006493 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006494 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006495 int_u save_qfid;
6496 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006497
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006498 switch (eap->cmdidx)
6499 {
6500 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6501 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6502 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6503 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6504 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6505 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6506 default: break;
6507 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006508 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6509 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006510 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006511#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006512 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006513 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006514#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006515 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006516
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006517 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01006518 if (eap->cmdidx == CMD_lbuffer
6519 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006520 || eap->cmdidx == CMD_laddbuffer)
6521 {
6522 qi = ll_get_or_alloc_list(curwin);
6523 if (qi == NULL)
6524 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006525 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006526 }
6527
Bram Moolenaar86b68352004-12-27 21:59:20 +00006528 if (*eap->arg == NUL)
6529 buf = curbuf;
6530 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6531 buf = buflist_findnr(atoi((char *)eap->arg));
6532 if (buf == NULL)
6533 EMSG(_(e_invarg));
6534 else if (buf->b_ml.ml_mfp == NULL)
6535 EMSG(_("E681: Buffer is not loaded"));
6536 else
6537 {
6538 if (eap->addr_count == 0)
6539 {
6540 eap->line1 = 1;
6541 eap->line2 = buf->b_ml.ml_line_count;
6542 }
6543 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6544 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6545 EMSG(_(e_invrange));
6546 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006547 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006548 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006549
6550 if (buf->b_sfname)
6551 {
6552 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6553 (char *)qf_title, (char *)buf->b_sfname);
6554 qf_title = IObuff;
6555 }
6556
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006557 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006558 (eap->cmdidx != CMD_caddbuffer
6559 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006560 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006561 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006562 if (res >= 0)
6563 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006564
6565 // Remember the current quickfix list identifier, so that we can
6566 // check for autocommands changing the current quickfix list.
6567 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006568 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006569 {
6570 buf_T *curbuf_old = curbuf;
6571
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006572 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6573 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006574 if (curbuf != curbuf_old)
6575 // Autocommands changed buffer, don't jump now, "qi" may
6576 // be invalid.
6577 res = 0;
6578 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006579 // Jump to the first error for a new list and if autocmds didn't
6580 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006581 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006582 eap->cmdidx == CMD_lbuffer)
6583 && qflist_valid(wp, save_qfid))
6584 {
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006585 // display the first error
6586 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006587 }
Bram Moolenaar754b5602006-02-09 23:53:20 +00006588 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006589 }
6590}
6591
Bram Moolenaar1e015462005-09-25 22:16:38 +00006592#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006593/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006594 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6595 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006596 */
6597 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006598ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006599{
6600 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006601 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006602 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006603 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006604 int_u save_qfid;
6605 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006606
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006607 switch (eap->cmdidx)
6608 {
6609 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6610 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6611 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6612 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6613 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6614 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6615 default: break;
6616 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006617 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6618 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006619 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006620#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006621 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006622 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006623#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006624 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006625
Bram Moolenaar3c097222017-12-21 20:54:49 +01006626 if (eap->cmdidx == CMD_lexpr
6627 || eap->cmdidx == CMD_lgetexpr
6628 || eap->cmdidx == CMD_laddexpr)
6629 {
6630 qi = ll_get_or_alloc_list(curwin);
6631 if (qi == NULL)
6632 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006633 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006634 }
6635
Bram Moolenaar4770d092006-01-12 23:22:24 +00006636 /* Evaluate the expression. When the result is a string or a list we can
6637 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006638 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006639 if (tv != NULL)
6640 {
6641 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6642 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6643 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006644 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006645 (eap->cmdidx != CMD_caddexpr
6646 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006647 (linenr_T)0, (linenr_T)0,
6648 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006649 if (res >= 0)
6650 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006651
6652 // Remember the current quickfix list identifier, so that we can
6653 // check for autocommands changing the current quickfix list.
6654 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006655 if (au_name != NULL)
6656 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6657 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006658
6659 // Jump to the first error for a new list and if autocmds didn't
6660 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006661 if (res > 0 && (eap->cmdidx == CMD_cexpr
6662 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006663 && qflist_valid(wp, save_qfid))
6664 {
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006665 // display the first error
6666 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006667 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006668 }
6669 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006670 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006671 free_tv(tv);
6672 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006673}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006674#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006675
6676/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006677 * Get the location list for ":lhelpgrep"
6678 */
6679 static qf_info_T *
6680hgr_get_ll(int *new_ll)
6681{
6682 win_T *wp;
6683 qf_info_T *qi;
6684
6685 /* If the current window is a help window, then use it */
6686 if (bt_help(curwin->w_buffer))
6687 wp = curwin;
6688 else
6689 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006690 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006691
6692 if (wp == NULL) /* Help window not found */
6693 qi = NULL;
6694 else
6695 qi = wp->w_llist;
6696
6697 if (qi == NULL)
6698 {
6699 /* Allocate a new location list for help text matches */
6700 if ((qi = ll_new_list()) == NULL)
6701 return NULL;
6702 *new_ll = TRUE;
6703 }
6704
6705 return qi;
6706}
6707
6708/*
6709 * Search for a pattern in a help file.
6710 */
6711 static void
6712hgr_search_file(
6713 qf_info_T *qi,
6714 char_u *fname,
6715#ifdef FEAT_MBYTE
6716 vimconv_T *p_vc,
6717#endif
6718 regmatch_T *p_regmatch)
6719{
6720 FILE *fd;
6721 long lnum;
6722
6723 fd = mch_fopen((char *)fname, "r");
6724 if (fd == NULL)
6725 return;
6726
6727 lnum = 1;
6728 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6729 {
6730 char_u *line = IObuff;
6731#ifdef FEAT_MBYTE
6732 /* Convert a line if 'encoding' is not utf-8 and
6733 * the line contains a non-ASCII character. */
6734 if (p_vc->vc_type != CONV_NONE
6735 && has_non_ascii(IObuff))
6736 {
6737 line = string_convert(p_vc, IObuff, NULL);
6738 if (line == NULL)
6739 line = IObuff;
6740 }
6741#endif
6742
6743 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6744 {
6745 int l = (int)STRLEN(line);
6746
6747 /* remove trailing CR, LF, spaces, etc. */
6748 while (l > 0 && line[l - 1] <= ' ')
6749 line[--l] = NUL;
6750
6751 if (qf_add_entry(qi,
6752 qi->qf_curlist,
6753 NULL, /* dir */
6754 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006755 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006756 0,
6757 line,
6758 lnum,
6759 (int)(p_regmatch->startp[0] - line)
6760 + 1, /* col */
6761 FALSE, /* vis_col */
6762 NULL, /* search pattern */
6763 0, /* nr */
6764 1, /* type */
6765 TRUE /* valid */
6766 ) == FAIL)
6767 {
6768 got_int = TRUE;
6769#ifdef FEAT_MBYTE
6770 if (line != IObuff)
6771 vim_free(line);
6772#endif
6773 break;
6774 }
6775 }
6776#ifdef FEAT_MBYTE
6777 if (line != IObuff)
6778 vim_free(line);
6779#endif
6780 ++lnum;
6781 line_breakcheck();
6782 }
6783 fclose(fd);
6784}
6785
6786/*
6787 * Search for a pattern in all the help files in the doc directory under
6788 * the given directory.
6789 */
6790 static void
6791hgr_search_files_in_dir(
6792 qf_info_T *qi,
6793 char_u *dirname,
6794 regmatch_T *p_regmatch
6795#ifdef FEAT_MBYTE
6796 , vimconv_T *p_vc
6797#endif
6798#ifdef FEAT_MULTI_LANG
6799 , char_u *lang
6800#endif
6801 )
6802{
6803 int fcount;
6804 char_u **fnames;
6805 int fi;
6806
6807 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6808 add_pathsep(dirname);
6809 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6810 if (gen_expand_wildcards(1, &dirname, &fcount,
6811 &fnames, EW_FILE|EW_SILENT) == OK
6812 && fcount > 0)
6813 {
6814 for (fi = 0; fi < fcount && !got_int; ++fi)
6815 {
6816#ifdef FEAT_MULTI_LANG
6817 /* Skip files for a different language. */
6818 if (lang != NULL
6819 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006820 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006821 && !(STRNICMP(lang, "en", 2) == 0
6822 && STRNICMP("txt", fnames[fi]
6823 + STRLEN(fnames[fi]) - 3, 3) == 0))
6824 continue;
6825#endif
6826
6827 hgr_search_file(qi, fnames[fi],
6828#ifdef FEAT_MBYTE
6829 p_vc,
6830#endif
6831 p_regmatch);
6832 }
6833 FreeWild(fcount, fnames);
6834 }
6835}
6836
6837/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006838 * Search for a pattern in all the help files in the 'runtimepath'
6839 * and add the matches to a quickfix list.
6840 * 'arg' is the language specifier. If supplied, then only matches in the
6841 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006842 */
6843 static void
6844hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *arg)
6845{
6846 char_u *p;
6847#ifdef FEAT_MULTI_LANG
6848 char_u *lang;
6849#endif
6850
6851#ifdef FEAT_MBYTE
6852 vimconv_T vc;
6853
6854 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6855 * differs. */
6856 vc.vc_type = CONV_NONE;
6857 if (!enc_utf8)
6858 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6859#endif
6860
6861#ifdef FEAT_MULTI_LANG
6862 /* Check for a specified language */
6863 lang = check_help_lang(arg);
6864#endif
6865
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006866 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006867 p = p_rtp;
6868 while (*p != NUL && !got_int)
6869 {
6870 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6871
6872 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6873#ifdef FEAT_MBYTE
6874 , &vc
6875#endif
6876#ifdef FEAT_MULTI_LANG
6877 , lang
6878#endif
6879 );
6880 }
6881
6882#ifdef FEAT_MBYTE
6883 if (vc.vc_type != CONV_NONE)
6884 convert_setup(&vc, NULL, NULL);
6885#endif
6886}
6887
6888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 * ":helpgrep {pattern}"
6890 */
6891 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006892ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
6894 regmatch_T regmatch;
6895 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006896 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006897 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006898 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899
Bram Moolenaar73633f82012-01-20 13:39:07 +01006900 switch (eap->cmdidx)
6901 {
6902 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6903 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6904 default: break;
6905 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006906 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6907 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006908 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006909#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006910 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006911 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006912#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006913 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006914
6915 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6916 save_cpo = p_cpo;
6917 p_cpo = empty_option;
6918
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006919 if (eap->cmdidx == CMD_lhelpgrep)
6920 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006921 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006922 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006923 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006924 }
6925
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6927 regmatch.rm_ic = FALSE;
6928 if (regmatch.regprog != NULL)
6929 {
6930 /* create a new quickfix list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006931 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006933 hgr_search_in_rtp(qi, &regmatch, eap->arg);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006934
Bram Moolenaar473de612013-06-08 18:19:48 +02006935 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006937 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6938 qi->qf_lists[qi->qf_curlist].qf_ptr =
6939 qi->qf_lists[qi->qf_curlist].qf_start;
6940 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 }
6942
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006943 if (p_cpo == empty_option)
6944 p_cpo = save_cpo;
6945 else
6946 /* Darn, some plugin changed the value. */
6947 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948
Bram Moolenaarb254af32017-12-18 19:48:58 +01006949 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006950 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
Bram Moolenaar73633f82012-01-20 13:39:07 +01006952 if (au_name != NULL)
6953 {
6954 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6955 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02006956 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006957 /* autocommands made "qi" invalid */
6958 return;
6959 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006960
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006962 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006963 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006964 else
6965 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006966
6967 if (eap->cmdidx == CMD_lhelpgrep)
6968 {
6969 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00006970 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006971 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006972 {
6973 if (new_qi)
6974 ll_free_all(&qi);
6975 }
6976 else if (curwin->w_llist == NULL)
6977 curwin->w_llist = qi;
6978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979}
6980
6981#endif /* FEAT_QUICKFIX */