blob: 14fcfcff4363cb0c37eb4856d902af2025b1f64e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 qfline_T *qf_next; /* pointer to next error in the list */
31 qfline_T *qf_prev; /* pointer to previous error in the list */
32 linenr_T qf_lnum; /* line number where the error occurred */
33 int qf_fnum; /* file number for the line */
34 int qf_col; /* column where the error occurred */
35 int qf_nr; /* error number */
Bram Moolenaard76ce852018-05-01 15:02:04 +020036 char_u *qf_module; /* module name for this error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000037 char_u *qf_pattern; /* search pattern for the error */
38 char_u *qf_text; /* description of the error */
39 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
40 char_u qf_cleared; /* set to TRUE if line has been deleted */
41 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000043 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044};
45
46/*
47 * There is a stack of error lists.
48 */
49#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020050#define INVALID_QFIDX (-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020052/*
53 * Quickfix/Location list definition
54 * Contains a list of entries (qfline_T). qf_start points to the first entry
55 * and qf_last points to the last entry. qf_count contains the list size.
56 *
57 * Usually the list contains one or more entries. But an empty list can be
58 * created using setqflist()/setloclist() with a title and/or user context
59 * information and entries can be added later using setqflist()/setloclist().
60 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000062{
Bram Moolenaara539f4f2017-08-30 20:33:55 +020063 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020065 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000066 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020067 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000068 int qf_index; /* current index in the error list */
69 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020070 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020071 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020072 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020073
74 struct dir_stack_T *qf_dir_stack;
75 char_u *qf_directory;
76 struct dir_stack_T *qf_file_stack;
77 char_u *qf_currfile;
78 int qf_multiline;
79 int qf_multiignore;
80 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010081 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000082} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020084/*
85 * Quickfix/Location list stack definition
86 * Contains a list of quickfix/location lists (qf_list_T)
87 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000088struct qf_info_S
89{
90 /*
91 * Count of references to this list. Used only for location lists.
92 * When a location list window reference this list, qf_refcount
93 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
94 * reaches 0, the list is freed.
95 */
96 int qf_refcount;
97 int qf_listcount; /* current number of lists */
98 int qf_curlist; /* current error list */
99 qf_list_T qf_lists[LISTCOUNT];
100};
101
102static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200103static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
Bram Moolenaard76ce852018-05-01 15:02:04 +0200105#define FMT_PATTERNS 11 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
107/*
108 * Structure used to hold the info of one part of 'errorformat'
109 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000110typedef struct efm_S efm_T;
111struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112{
113 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000114 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
116 char_u prefix; /* prefix of this format line: */
117 /* 'D' enter directory */
118 /* 'X' leave directory */
119 /* 'A' start of multi-line message */
120 /* 'E' error message */
121 /* 'W' warning message */
122 /* 'I' informational message */
123 /* 'C' continuation line */
124 /* 'Z' end of multi-line message */
125 /* 'G' general, unspecific message */
126 /* 'P' push file (partial) message */
127 /* 'Q' pop/quit file (partial) message */
128 /* 'O' overread (partial) message */
129 char_u flags; /* additional flags given in prefix */
130 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000131 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000132 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133};
134
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100135static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
136
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaard76ce852018-05-01 15:02:04 +0200138static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void qf_free(qf_info_T *qi, int idx);
140static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200141static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200142static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200144static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static win_T *qf_find_win(qf_info_T *qi);
148static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200149static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200151static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static char_u *get_mef_name(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
154static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
155static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
156static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000158/* Quickfix window check helper macro */
159#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
160/* Location list window check helper macro */
161#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
162/*
163 * Return location list for window 'wp'
164 * For location list window, return the referenced location list
165 */
166#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200169 * Looking up a buffer can be slow if there are many. Remember the last one
170 * to make this a lot faster if there are multiple matches in the same file.
171 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200172static char_u *qf_last_bufname = NULL;
173static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200174
Bram Moolenaar3c097222017-12-21 20:54:49 +0100175static char *e_loc_list_changed =
176 N_("E926: Current location list was changed");
177
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200178/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200179 * Maximum number of bytes allowed per line while reading a errorfile.
180 */
181#define LINE_MAXLEN 4096
182
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200183static struct fmtpattern
184{
185 char_u convchar;
186 char *pattern;
187} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200188 {
189 {'f', ".\\+"}, /* only used when at end */
190 {'n', "\\d\\+"},
191 {'l', "\\d\\+"},
192 {'c', "\\d\\+"},
193 {'t', "."},
194 {'m', ".\\+"},
195 {'r', ".*"},
196 {'p', "[- .]*"},
197 {'v', "\\d\\+"},
198 {'s', ".\\+"},
199 {'o', ".\\+"}
200 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200201
202/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200203 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200204 * See fmt_pat definition above for the list of supported patterns. The
205 * pattern specifier is supplied in "efmpat". The converted pattern is stored
206 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200207 */
208 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200209efmpat_to_regpat(
210 char_u *efmpat,
211 char_u *regpat,
212 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200213 int idx,
214 int round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200215 char_u *errmsg)
216{
217 char_u *srcptr;
218
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200219 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200220 {
221 /* Each errorformat pattern can occur only once */
222 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200223 _("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200224 EMSG(errmsg);
225 return NULL;
226 }
227 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200228 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200229 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200230 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200231 {
232 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200233 _("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200234 EMSG(errmsg);
235 return NULL;
236 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200237 efminfo->addr[idx] = (char_u)++round;
238 *regpat++ = '\\';
239 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200240#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200241 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200242 {
243 /* Also match "c:" in the file name, even when
244 * checking for a colon next: "%f:".
245 * "\%(\a:\)\=" */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200246 STRCPY(regpat, "\\%(\\a:\\)\\=");
247 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200248 }
249#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200250 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200251 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200252 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200253 {
254 /* A file name may contain spaces, but this isn't
255 * in "\f". For "%f:%l:%m" there may be a ":" in
256 * the file name. Use ".\{-1,}x" instead (x is
257 * the next character), the requirement that :999:
258 * follows should work. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200259 STRCPY(regpat, ".\\{-1,}");
260 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200261 }
262 else
263 {
264 /* File name followed by '\\' or '%': include as
265 * many file name chars as possible. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200266 STRCPY(regpat, "\\f\\+");
267 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200268 }
269 }
270 else
271 {
272 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200273 while ((*regpat = *srcptr++) != NUL)
274 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200275 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200276 *regpat++ = '\\';
277 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200278
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200279 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200280}
281
282/*
283 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200284 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200285 */
286 static char_u *
287scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200288 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200289 char_u *efm,
290 int len,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200291 char_u *regpat,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200292 char_u *errmsg)
293{
294 char_u *efmp = *pefmp;
295
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200296 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200297 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200298 if ((*regpat++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200299 {
300 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200301 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200302 if (efmp < efm + len)
303 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200304 *regpat++ = *++efmp; /* could be ']' */
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200305 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200306 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200307 /* skip */;
308 if (efmp == efm + len)
309 {
310 EMSG(_("E374: Missing ] in format string"));
311 return NULL;
312 }
313 }
314 }
315 else if (efmp < efm + len) /* %*\D, %*\s etc. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200316 *regpat++ = *++efmp;
317 *regpat++ = '\\';
318 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200319 }
320 else
321 {
322 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
323 sprintf((char *)errmsg,
324 _("E375: Unsupported %%%c in format string"), *efmp);
325 EMSG(errmsg);
326 return NULL;
327 }
328
329 *pefmp = efmp;
330
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200331 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200332}
333
334/*
335 * Analyze/parse an errorformat prefix.
336 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200337 static char_u *
338efm_analyze_prefix(char_u *efmp, efm_T *efminfo, char_u *errmsg)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200339{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200340 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200341 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200342 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200343 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200344 else
345 {
346 sprintf((char *)errmsg,
347 _("E376: Invalid %%%c in format string prefix"), *efmp);
348 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200349 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200350 }
351
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200352 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200353}
354
355/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200356 * Converts a 'errorformat' string part in 'efm' to a regular expression
357 * pattern. The resulting regex pattern is returned in "regpat". Additional
358 * information about the 'erroformat' pattern is returned in "fmt_ptr".
359 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200360 */
361 static int
362efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200363 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200364 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200365 efm_T *fmt_ptr,
366 char_u *regpat,
367 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200368{
369 char_u *ptr;
370 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200371 int round;
372 int idx = 0;
373
374 /*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200375 * Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200376 */
377 ptr = regpat;
378 *ptr++ = '^';
379 round = 0;
380 for (efmp = efm; efmp < efm + len; ++efmp)
381 {
382 if (*efmp == '%')
383 {
384 ++efmp;
385 for (idx = 0; idx < FMT_PATTERNS; ++idx)
386 if (fmt_pat[idx].convchar == *efmp)
387 break;
388 if (idx < FMT_PATTERNS)
389 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200390 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200391 errmsg);
392 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200393 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200394 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200395 }
396 else if (*efmp == '*')
397 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200398 ++efmp;
399 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr, errmsg);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200400 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200401 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200402 }
403 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
404 *ptr++ = *efmp; /* regexp magic characters */
405 else if (*efmp == '#')
406 *ptr++ = '*';
407 else if (*efmp == '>')
408 fmt_ptr->conthere = TRUE;
409 else if (efmp == efm + 1) /* analyse prefix */
410 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200411 /*
412 * prefix is allowed only at the beginning of the errorformat
413 * option part
414 */
415 efmp = efm_analyze_prefix(efmp, fmt_ptr, errmsg);
416 if (efmp == NULL)
417 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200418 }
419 else
420 {
421 sprintf((char *)errmsg,
422 _("E377: Invalid %%%c in format string"), *efmp);
423 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200424 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200425 }
426 }
427 else /* copy normal character */
428 {
429 if (*efmp == '\\' && efmp + 1 < efm + len)
430 ++efmp;
431 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
432 *ptr++ = '\\'; /* escape regexp atoms */
433 if (*efmp)
434 *ptr++ = *efmp;
435 }
436 }
437 *ptr++ = '$';
438 *ptr = NUL;
439
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200440 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200441}
442
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200443/*
444 * Free the 'errorformat' information list
445 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200446 static void
447free_efm_list(efm_T **efm_first)
448{
449 efm_T *efm_ptr;
450
451 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
452 {
453 *efm_first = efm_ptr->next;
454 vim_regfree(efm_ptr->prog);
455 vim_free(efm_ptr);
456 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100457 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200458}
459
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200460/*
461 * Compute the size of the buffer used to convert a 'errorformat' pattern into
462 * a regular expression pattern.
463 */
464 static int
465efm_regpat_bufsz(char_u *efm)
466{
467 int sz;
468 int i;
469
470 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
471 for (i = FMT_PATTERNS; i > 0; )
472 sz += (int)STRLEN(fmt_pat[--i].pattern);
473#ifdef BACKSLASH_IN_FILENAME
474 sz += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
475#else
476 sz += 2; /* "%f" can become two chars longer */
477#endif
478
479 return sz;
480}
481
482/*
483 * Return the length of a 'errorformat' option part (separated by ",").
484 */
485 static int
486efm_option_part_len(char_u *efm)
487{
488 int len;
489
490 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
491 if (efm[len] == '\\' && efm[len + 1] != NUL)
492 ++len;
493
494 return len;
495}
496
497/*
498 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
499 * are parsed and converted to regular expressions. Returns information about
500 * the parsed 'errorformat' option.
501 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200502 static efm_T *
503parse_efm_option(char_u *efm)
504{
505 char_u *errmsg = NULL;
506 int errmsglen;
507 efm_T *fmt_ptr = NULL;
508 efm_T *fmt_first = NULL;
509 efm_T *fmt_last = NULL;
510 char_u *fmtstr = NULL;
511 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200512 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200513
514 errmsglen = CMDBUFFSIZE + 1;
515 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
516 if (errmsg == NULL)
517 goto parse_efm_end;
518
519 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200520 * Each part of the format string is copied and modified from errorformat
521 * to regex prog. Only a few % characters are allowed.
522 */
523
524 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200525 * Get some space to modify the format string into.
526 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200527 sz = efm_regpat_bufsz(efm);
528 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200529 goto parse_efm_error;
530
531 while (efm[0] != NUL)
532 {
533 /*
534 * Allocate a new eformat structure and put it at the end of the list
535 */
536 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
537 if (fmt_ptr == NULL)
538 goto parse_efm_error;
539 if (fmt_first == NULL) /* first one */
540 fmt_first = fmt_ptr;
541 else
542 fmt_last->next = fmt_ptr;
543 fmt_last = fmt_ptr;
544
545 /*
546 * Isolate one part in the 'errorformat' option
547 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200548 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200549
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200550 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200551 goto parse_efm_error;
552 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
553 goto parse_efm_error;
554 /*
555 * Advance to next part
556 */
557 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
558 }
559
560 if (fmt_first == NULL) /* nothing found */
561 EMSG(_("E378: 'errorformat' contains no pattern"));
562
563 goto parse_efm_end;
564
565parse_efm_error:
566 free_efm_list(&fmt_first);
567
568parse_efm_end:
569 vim_free(fmtstr);
570 vim_free(errmsg);
571
572 return fmt_first;
573}
574
Bram Moolenaare0d37972016-07-15 22:36:01 +0200575enum {
576 QF_FAIL = 0,
577 QF_OK = 1,
578 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200579 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200580 QF_IGNORE_LINE = 4,
581 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200582};
583
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200584/*
585 * State information used to parse lines and add entries to a quickfix/location
586 * list.
587 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200588typedef struct {
589 char_u *linebuf;
590 int linelen;
591 char_u *growbuf;
592 int growbufsiz;
593 FILE *fd;
594 typval_T *tv;
595 char_u *p_str;
596 listitem_T *p_li;
597 buf_T *buf;
598 linenr_T buflnum;
599 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100600 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200601} qfstate_T;
602
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200603/*
604 * Allocate more memory for the line buffer used for parsing lines.
605 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200606 static char_u *
607qf_grow_linebuf(qfstate_T *state, int newsz)
608{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200609 char_u *p;
610
Bram Moolenaare0d37972016-07-15 22:36:01 +0200611 /*
612 * If the line exceeds LINE_MAXLEN exclude the last
613 * byte since it's not a NL character.
614 */
615 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
616 if (state->growbuf == NULL)
617 {
618 state->growbuf = alloc(state->linelen + 1);
619 if (state->growbuf == NULL)
620 return NULL;
621 state->growbufsiz = state->linelen;
622 }
623 else if (state->linelen > state->growbufsiz)
624 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200625 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200626 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200627 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200628 state->growbufsiz = state->linelen;
629 }
630 return state->growbuf;
631}
632
633/*
634 * Get the next string (separated by newline) from state->p_str.
635 */
636 static int
637qf_get_next_str_line(qfstate_T *state)
638{
639 /* Get the next line from the supplied string */
640 char_u *p_str = state->p_str;
641 char_u *p;
642 int len;
643
644 if (*p_str == NUL) /* Reached the end of the string */
645 return QF_END_OF_INPUT;
646
647 p = vim_strchr(p_str, '\n');
648 if (p != NULL)
649 len = (int)(p - p_str) + 1;
650 else
651 len = (int)STRLEN(p_str);
652
653 if (len > IOSIZE - 2)
654 {
655 state->linebuf = qf_grow_linebuf(state, len);
656 if (state->linebuf == NULL)
657 return QF_NOMEM;
658 }
659 else
660 {
661 state->linebuf = IObuff;
662 state->linelen = len;
663 }
664 vim_strncpy(state->linebuf, p_str, state->linelen);
665
666 /*
667 * Increment using len in order to discard the rest of the
668 * line if it exceeds LINE_MAXLEN.
669 */
670 p_str += len;
671 state->p_str = p_str;
672
673 return QF_OK;
674}
675
676/*
677 * Get the next string from state->p_Li.
678 */
679 static int
680qf_get_next_list_line(qfstate_T *state)
681{
682 listitem_T *p_li = state->p_li;
683 int len;
684
685 while (p_li != NULL
686 && (p_li->li_tv.v_type != VAR_STRING
687 || p_li->li_tv.vval.v_string == NULL))
688 p_li = p_li->li_next; /* Skip non-string items */
689
690 if (p_li == NULL) /* End of the list */
691 {
692 state->p_li = NULL;
693 return QF_END_OF_INPUT;
694 }
695
696 len = (int)STRLEN(p_li->li_tv.vval.v_string);
697 if (len > IOSIZE - 2)
698 {
699 state->linebuf = qf_grow_linebuf(state, len);
700 if (state->linebuf == NULL)
701 return QF_NOMEM;
702 }
703 else
704 {
705 state->linebuf = IObuff;
706 state->linelen = len;
707 }
708
709 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
710
711 state->p_li = p_li->li_next; /* next item */
712 return QF_OK;
713}
714
715/*
716 * Get the next string from state->buf.
717 */
718 static int
719qf_get_next_buf_line(qfstate_T *state)
720{
721 char_u *p_buf = NULL;
722 int len;
723
724 /* Get the next line from the supplied buffer */
725 if (state->buflnum > state->lnumlast)
726 return QF_END_OF_INPUT;
727
728 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
729 state->buflnum += 1;
730
731 len = (int)STRLEN(p_buf);
732 if (len > IOSIZE - 2)
733 {
734 state->linebuf = qf_grow_linebuf(state, len);
735 if (state->linebuf == NULL)
736 return QF_NOMEM;
737 }
738 else
739 {
740 state->linebuf = IObuff;
741 state->linelen = len;
742 }
743 vim_strncpy(state->linebuf, p_buf, state->linelen);
744
745 return QF_OK;
746}
747
748/*
749 * Get the next string from file state->fd.
750 */
751 static int
752qf_get_next_file_line(qfstate_T *state)
753{
754 int discard;
755 int growbuflen;
756
757 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
758 return QF_END_OF_INPUT;
759
760 discard = FALSE;
761 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200762 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200763 {
764 /*
765 * The current line exceeds IObuff, continue reading using
766 * growbuf until EOL or LINE_MAXLEN bytes is read.
767 */
768 if (state->growbuf == NULL)
769 {
770 state->growbufsiz = 2 * (IOSIZE - 1);
771 state->growbuf = alloc(state->growbufsiz);
772 if (state->growbuf == NULL)
773 return QF_NOMEM;
774 }
775
776 /* Copy the read part of the line, excluding null-terminator */
777 memcpy(state->growbuf, IObuff, IOSIZE - 1);
778 growbuflen = state->linelen;
779
780 for (;;)
781 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200782 char_u *p;
783
Bram Moolenaare0d37972016-07-15 22:36:01 +0200784 if (fgets((char *)state->growbuf + growbuflen,
785 state->growbufsiz - growbuflen, state->fd) == NULL)
786 break;
787 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
788 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200789 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200790 break;
791 if (state->growbufsiz == LINE_MAXLEN)
792 {
793 discard = TRUE;
794 break;
795 }
796
797 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
798 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200799 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200800 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200801 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200802 }
803
804 while (discard)
805 {
806 /*
807 * The current line is longer than LINE_MAXLEN, continue
808 * reading but discard everything until EOL or EOF is
809 * reached.
810 */
811 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
812 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200813 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200814 break;
815 }
816
817 state->linebuf = state->growbuf;
818 state->linelen = growbuflen;
819 }
820 else
821 state->linebuf = IObuff;
822
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100823#ifdef FEAT_MBYTE
824 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200825 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
826 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100827 char_u *line;
828
829 line = string_convert(&state->vc, state->linebuf, &state->linelen);
830 if (line != NULL)
831 {
832 if (state->linelen < IOSIZE)
833 {
834 STRCPY(state->linebuf, line);
835 vim_free(line);
836 }
837 else
838 {
839 vim_free(state->growbuf);
840 state->linebuf = state->growbuf = line;
841 state->growbufsiz = state->linelen < LINE_MAXLEN
842 ? state->linelen : LINE_MAXLEN;
843 }
844 }
845 }
846#endif
847
Bram Moolenaare0d37972016-07-15 22:36:01 +0200848 return QF_OK;
849}
850
851/*
852 * Get the next string from a file/buffer/list/string.
853 */
854 static int
855qf_get_nextline(qfstate_T *state)
856{
857 int status = QF_FAIL;
858
859 if (state->fd == NULL)
860 {
861 if (state->tv != NULL)
862 {
863 if (state->tv->v_type == VAR_STRING)
864 /* Get the next line from the supplied string */
865 status = qf_get_next_str_line(state);
866 else if (state->tv->v_type == VAR_LIST)
867 /* Get the next line from the supplied list */
868 status = qf_get_next_list_line(state);
869 }
870 else
871 /* Get the next line from the supplied buffer */
872 status = qf_get_next_buf_line(state);
873 }
874 else
875 /* Get the next line from the supplied file */
876 status = qf_get_next_file_line(state);
877
878 if (status != QF_OK)
879 return status;
880
881 /* remove newline/CR from the line */
882 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200883 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200884 state->linebuf[state->linelen - 1] = NUL;
885#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200886 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
887 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200888#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200889 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200890
891#ifdef FEAT_MBYTE
892 remove_bom(state->linebuf);
893#endif
894
895 return QF_OK;
896}
897
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200898typedef struct {
899 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200900 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200901 char_u *errmsg;
902 int errmsglen;
903 long lnum;
904 int col;
905 char_u use_viscol;
906 char_u *pattern;
907 int enr;
908 int type;
909 int valid;
910} qffields_T;
911
912/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200913 * Parse the match for filename ('%f') pattern in regmatch.
914 * Return the matched value in "fields->namebuf".
915 */
916 static int
917qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
918{
919 int c;
920
921 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
922 return QF_FAIL;
923
924 /* Expand ~/file and $HOME/file to full path. */
925 c = *rmp->endp[midx];
926 *rmp->endp[midx] = NUL;
927 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
928 *rmp->endp[midx] = c;
929
930 /*
931 * For separate filename patterns (%O, %P and %Q), the specified file
932 * should exist.
933 */
934 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
935 && mch_getperm(fields->namebuf) == -1)
936 return QF_FAIL;
937
938 return QF_OK;
939}
940
941/*
942 * Parse the match for error number ('%n') pattern in regmatch.
943 * Return the matched value in "fields->enr".
944 */
945 static int
946qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
947{
948 if (rmp->startp[midx] == NULL)
949 return QF_FAIL;
950 fields->enr = (int)atol((char *)rmp->startp[midx]);
951 return QF_OK;
952}
953
954/*
955 * Parse the match for line number (%l') pattern in regmatch.
956 * Return the matched value in "fields->lnum".
957 */
958 static int
959qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
960{
961 if (rmp->startp[midx] == NULL)
962 return QF_FAIL;
963 fields->lnum = atol((char *)rmp->startp[midx]);
964 return QF_OK;
965}
966
967/*
968 * Parse the match for column number ('%c') pattern in regmatch.
969 * Return the matched value in "fields->col".
970 */
971 static int
972qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
973{
974 if (rmp->startp[midx] == NULL)
975 return QF_FAIL;
976 fields->col = (int)atol((char *)rmp->startp[midx]);
977 return QF_OK;
978}
979
980/*
981 * Parse the match for error type ('%t') pattern in regmatch.
982 * Return the matched value in "fields->type".
983 */
984 static int
985qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
986{
987 if (rmp->startp[midx] == NULL)
988 return QF_FAIL;
989 fields->type = *rmp->startp[midx];
990 return QF_OK;
991}
992
993/*
994 * Parse the match for '%+' format pattern. The whole matching line is included
995 * in the error string. Return the matched line in "fields->errmsg".
996 */
997 static int
998qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
999{
1000 char_u *p;
1001
1002 if (linelen >= fields->errmsglen)
1003 {
1004 /* linelen + null terminator */
1005 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1006 return QF_NOMEM;
1007 fields->errmsg = p;
1008 fields->errmsglen = linelen + 1;
1009 }
1010 vim_strncpy(fields->errmsg, linebuf, linelen);
1011 return QF_OK;
1012}
1013
1014/*
1015 * Parse the match for error message ('%m') pattern in regmatch.
1016 * Return the matched value in "fields->errmsg".
1017 */
1018 static int
1019qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1020{
1021 char_u *p;
1022 int len;
1023
1024 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1025 return QF_FAIL;
1026 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1027 if (len >= fields->errmsglen)
1028 {
1029 /* len + null terminator */
1030 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1031 return QF_NOMEM;
1032 fields->errmsg = p;
1033 fields->errmsglen = len + 1;
1034 }
1035 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1036 return QF_OK;
1037}
1038
1039/*
1040 * Parse the match for rest of a single-line file message ('%r') pattern.
1041 * Return the matched value in "tail".
1042 */
1043 static int
1044qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1045{
1046 if (rmp->startp[midx] == NULL)
1047 return QF_FAIL;
1048 *tail = rmp->startp[midx];
1049 return QF_OK;
1050}
1051
1052/*
1053 * Parse the match for the pointer line ('%p') pattern in regmatch.
1054 * Return the matched value in "fields->col".
1055 */
1056 static int
1057qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1058{
1059 char_u *match_ptr;
1060
1061 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1062 return QF_FAIL;
1063 fields->col = 0;
1064 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1065 ++match_ptr)
1066 {
1067 ++fields->col;
1068 if (*match_ptr == TAB)
1069 {
1070 fields->col += 7;
1071 fields->col -= fields->col % 8;
1072 }
1073 }
1074 ++fields->col;
1075 fields->use_viscol = TRUE;
1076 return QF_OK;
1077}
1078
1079/*
1080 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1081 * Return the matched value in "fields->col".
1082 */
1083 static int
1084qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1085{
1086 if (rmp->startp[midx] == NULL)
1087 return QF_FAIL;
1088 fields->col = (int)atol((char *)rmp->startp[midx]);
1089 fields->use_viscol = TRUE;
1090 return QF_OK;
1091}
1092
1093/*
1094 * Parse the match for the search text ('%s') pattern in regmatch.
1095 * Return the matched value in "fields->pattern".
1096 */
1097 static int
1098qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1099{
1100 int len;
1101
1102 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1103 return QF_FAIL;
1104 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1105 if (len > CMDBUFFSIZE - 5)
1106 len = CMDBUFFSIZE - 5;
1107 STRCPY(fields->pattern, "^\\V");
1108 STRNCAT(fields->pattern, rmp->startp[midx], len);
1109 fields->pattern[len + 3] = '\\';
1110 fields->pattern[len + 4] = '$';
1111 fields->pattern[len + 5] = NUL;
1112 return QF_OK;
1113}
1114
1115/*
1116 * Parse the match for the module ('%o') pattern in regmatch.
1117 * Return the matched value in "fields->module".
1118 */
1119 static int
1120qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1121{
1122 int len;
1123
1124 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1125 return QF_FAIL;
1126 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1127 if (len > CMDBUFFSIZE)
1128 len = CMDBUFFSIZE;
1129 STRNCAT(fields->module, rmp->startp[midx], len);
1130 return QF_OK;
1131}
1132
1133/*
1134 * 'errorformat' format pattern parser functions.
1135 * The '%f' and '%r' formats are parsed differently from other formats.
1136 * See qf_parse_match() for details.
1137 */
1138static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1139{
1140 NULL,
1141 qf_parse_fmt_n,
1142 qf_parse_fmt_l,
1143 qf_parse_fmt_c,
1144 qf_parse_fmt_t,
1145 qf_parse_fmt_m,
1146 NULL,
1147 qf_parse_fmt_p,
1148 qf_parse_fmt_v,
1149 qf_parse_fmt_s,
1150 qf_parse_fmt_o
1151};
1152
1153/*
1154 * Parse the error format pattern matches in "regmatch" and set the values in
1155 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1156 * match. Returns QF_OK if all the matches are successfully parsed. On
1157 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001158 */
1159 static int
1160qf_parse_match(
1161 char_u *linebuf,
1162 int linelen,
1163 efm_T *fmt_ptr,
1164 regmatch_T *regmatch,
1165 qffields_T *fields,
1166 int qf_multiline,
1167 int qf_multiscan,
1168 char_u **tail)
1169{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001170 int idx = fmt_ptr->prefix;
1171 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001172 int midx;
1173 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001174
1175 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1176 return QF_FAIL;
1177 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1178 fields->type = idx;
1179 else
1180 fields->type = 0;
1181 /*
1182 * Extract error message data from matched line.
1183 * We check for an actual submatch, because "\[" and "\]" in
1184 * the 'errorformat' may cause the wrong submatch to be used.
1185 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001186 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001187 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001188 status = QF_OK;
1189 midx = (int)fmt_ptr->addr[i];
1190 if (i == 0 && midx > 0) /* %f */
1191 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1192 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001193 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001194 if (fmt_ptr->flags == '+' && !qf_multiscan) /* %+ */
1195 status = qf_parse_fmt_plus(linebuf, linelen, fields);
1196 else if (midx > 0) /* %m */
1197 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001198 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001199 else if (i == 6 && midx > 0) /* %r */
1200 status = qf_parse_fmt_r(regmatch, midx, tail);
1201 else if (midx > 0) /* others */
1202 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001203
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001204 if (status != QF_OK)
1205 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001206 }
1207
1208 return QF_OK;
1209}
1210
1211/*
1212 * Parse an error line in 'linebuf' using a single error format string in
1213 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1214 * Returns QF_OK if the efm format matches completely and the fields are
1215 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1216 */
1217 static int
1218qf_parse_get_fields(
1219 char_u *linebuf,
1220 int linelen,
1221 efm_T *fmt_ptr,
1222 qffields_T *fields,
1223 int qf_multiline,
1224 int qf_multiscan,
1225 char_u **tail)
1226{
1227 regmatch_T regmatch;
1228 int status = QF_FAIL;
1229 int r;
1230
1231 if (qf_multiscan &&
1232 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1233 return QF_FAIL;
1234
1235 fields->namebuf[0] = NUL;
1236 fields->module[0] = NUL;
1237 fields->pattern[0] = NUL;
1238 if (!qf_multiscan)
1239 fields->errmsg[0] = NUL;
1240 fields->lnum = 0;
1241 fields->col = 0;
1242 fields->use_viscol = FALSE;
1243 fields->enr = -1;
1244 fields->type = 0;
1245 *tail = NULL;
1246
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001247 /* Always ignore case when looking for a matching error. */
1248 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001249 regmatch.regprog = fmt_ptr->prog;
1250 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1251 fmt_ptr->prog = regmatch.regprog;
1252 if (r)
1253 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1254 fields, qf_multiline, qf_multiscan, tail);
1255
1256 return status;
1257}
1258
1259/*
1260 * Parse directory error format prefixes (%D and %X).
1261 * Push and pop directories from the directory stack when scanning directory
1262 * names.
1263 */
1264 static int
1265qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1266{
1267 if (idx == 'D') /* enter directory */
1268 {
1269 if (*fields->namebuf == NUL)
1270 {
1271 EMSG(_("E379: Missing or empty directory name"));
1272 return QF_FAIL;
1273 }
1274 qfl->qf_directory =
1275 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1276 if (qfl->qf_directory == NULL)
1277 return QF_FAIL;
1278 }
1279 else if (idx == 'X') /* leave directory */
1280 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1281
1282 return QF_OK;
1283}
1284
1285/*
1286 * Parse global file name error format prefixes (%O, %P and %Q).
1287 */
1288 static int
1289qf_parse_file_pfx(
1290 int idx,
1291 qffields_T *fields,
1292 qf_list_T *qfl,
1293 char_u *tail)
1294{
1295 fields->valid = FALSE;
1296 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1297 {
1298 if (*fields->namebuf && idx == 'P')
1299 qfl->qf_currfile =
1300 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1301 else if (idx == 'Q')
1302 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1303 *fields->namebuf = NUL;
1304 if (tail && *tail)
1305 {
1306 STRMOVE(IObuff, skipwhite(tail));
1307 qfl->qf_multiscan = TRUE;
1308 return QF_MULTISCAN;
1309 }
1310 }
1311
1312 return QF_OK;
1313}
1314
1315/*
1316 * Parse a non-error line (a line which doesn't match any of the error
1317 * format in 'efm').
1318 */
1319 static int
1320qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1321{
1322 char_u *p;
1323
1324 fields->namebuf[0] = NUL; /* no match found, remove file name */
1325 fields->lnum = 0; /* don't jump to this line */
1326 fields->valid = FALSE;
1327 if (linelen >= fields->errmsglen)
1328 {
1329 /* linelen + null terminator */
1330 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1331 return QF_NOMEM;
1332 fields->errmsg = p;
1333 fields->errmsglen = linelen + 1;
1334 }
1335 /* copy whole line to error message */
1336 vim_strncpy(fields->errmsg, linebuf, linelen);
1337
1338 return QF_OK;
1339}
1340
1341/*
1342 * Parse multi-line error format prefixes (%C and %Z)
1343 */
1344 static int
1345qf_parse_multiline_pfx(
1346 qf_info_T *qi,
1347 int qf_idx,
1348 int idx,
1349 qf_list_T *qfl,
1350 qffields_T *fields)
1351{
1352 char_u *ptr;
1353 int len;
1354
1355 if (!qfl->qf_multiignore)
1356 {
1357 qfline_T *qfprev = qfl->qf_last;
1358
1359 if (qfprev == NULL)
1360 return QF_FAIL;
1361 if (*fields->errmsg && !qfl->qf_multiignore)
1362 {
1363 len = (int)STRLEN(qfprev->qf_text);
1364 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1365 == NULL)
1366 return QF_FAIL;
1367 STRCPY(ptr, qfprev->qf_text);
1368 vim_free(qfprev->qf_text);
1369 qfprev->qf_text = ptr;
1370 *(ptr += len) = '\n';
1371 STRCPY(++ptr, fields->errmsg);
1372 }
1373 if (qfprev->qf_nr == -1)
1374 qfprev->qf_nr = fields->enr;
1375 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1376 /* only printable chars allowed */
1377 qfprev->qf_type = fields->type;
1378
1379 if (!qfprev->qf_lnum)
1380 qfprev->qf_lnum = fields->lnum;
1381 if (!qfprev->qf_col)
1382 qfprev->qf_col = fields->col;
1383 qfprev->qf_viscol = fields->use_viscol;
1384 if (!qfprev->qf_fnum)
1385 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1386 qfl->qf_directory,
1387 *fields->namebuf || qfl->qf_directory != NULL
1388 ? fields->namebuf
1389 : qfl->qf_currfile != NULL && fields->valid
1390 ? qfl->qf_currfile : 0);
1391 }
1392 if (idx == 'Z')
1393 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1394 line_breakcheck();
1395
1396 return QF_IGNORE_LINE;
1397}
1398
1399/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001400 * Parse a line and get the quickfix fields.
1401 * Return the QF_ status.
1402 */
1403 static int
1404qf_parse_line(
1405 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001406 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001407 char_u *linebuf,
1408 int linelen,
1409 efm_T *fmt_first,
1410 qffields_T *fields)
1411{
1412 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001413 int idx = 0;
1414 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001415 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001416 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001417
Bram Moolenaare333e792018-04-08 13:27:39 +02001418restofline:
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001419 /* If there was no %> item start at the first pattern */
1420 if (fmt_start == NULL)
1421 fmt_ptr = fmt_first;
1422 else
1423 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001424 /* Otherwise start from the last used pattern */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001425 fmt_ptr = fmt_start;
1426 fmt_start = NULL;
1427 }
1428
1429 /*
1430 * Try to match each part of 'errorformat' until we find a complete
1431 * match or no match.
1432 */
1433 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001434 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1435 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001436 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001437 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1438 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1439 if (status == QF_NOMEM)
1440 return status;
1441 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001442 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001443 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001444 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001445
1446 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1447 {
1448 if (fmt_ptr != NULL)
1449 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001450 /* 'D' and 'X' directory specifiers */
1451 status = qf_parse_dir_pfx(idx, fields, qfl);
1452 if (status != QF_OK)
1453 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001454 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001455
1456 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1457 if (status != QF_OK)
1458 return status;
1459
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001460 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001461 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001462 }
1463 else if (fmt_ptr != NULL)
1464 {
1465 /* honor %> item */
1466 if (fmt_ptr->conthere)
1467 fmt_start = fmt_ptr;
1468
1469 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1470 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001471 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1472 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001473 }
1474 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1475 { /* continuation of multi-line msg */
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001476 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1477 if (status != QF_OK)
1478 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001479 }
1480 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001481 { /* global file names */
1482 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1483 if (status == QF_MULTISCAN)
1484 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001485 }
1486 if (fmt_ptr->flags == '-') /* generally exclude this line */
1487 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001488 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001489 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001490 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001491 return QF_IGNORE_LINE;
1492 }
1493 }
1494
1495 return QF_OK;
1496}
1497
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001498/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001499 * Returns TRUE if the specified quickfix/location list is empty.
1500 */
1501 static int
1502qf_list_empty(qf_info_T *qi, int qf_idx)
1503{
1504 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1505 return TRUE;
1506 return qi->qf_lists[qf_idx].qf_count <= 0;
1507}
1508
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001509/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001510 * Allocate the fields used for parsing lines and populating a quickfix list.
1511 */
1512 static int
1513qf_alloc_fields(qffields_T *pfields)
1514{
1515 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1516 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1517 pfields->errmsglen = CMDBUFFSIZE + 1;
1518 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1519 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1520 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1521 || pfields->pattern == NULL || pfields->module == NULL)
1522 return FAIL;
1523
1524 return OK;
1525}
1526
1527/*
1528 * Free the fields used for parsing lines and populating a quickfix list.
1529 */
1530 static void
1531qf_free_fields(qffields_T *pfields)
1532{
1533 vim_free(pfields->namebuf);
1534 vim_free(pfields->module);
1535 vim_free(pfields->errmsg);
1536 vim_free(pfields->pattern);
1537}
1538
1539/*
1540 * Setup the state information used for parsing lines and populating a
1541 * quickfix list.
1542 */
1543 static int
1544qf_setup_state(
1545 qfstate_T *pstate,
1546 char_u *enc,
1547 char_u *efile,
1548 typval_T *tv,
1549 buf_T *buf,
1550 linenr_T lnumfirst,
1551 linenr_T lnumlast)
1552{
1553#ifdef FEAT_MBYTE
1554 pstate->vc.vc_type = CONV_NONE;
1555 if (enc != NULL && *enc != NUL)
1556 convert_setup(&pstate->vc, enc, p_enc);
1557#endif
1558
1559 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1560 {
1561 EMSG2(_(e_openerrf), efile);
1562 return FAIL;
1563 }
1564
1565 if (tv != NULL)
1566 {
1567 if (tv->v_type == VAR_STRING)
1568 pstate->p_str = tv->vval.v_string;
1569 else if (tv->v_type == VAR_LIST)
1570 pstate->p_li = tv->vval.v_list->lv_first;
1571 pstate->tv = tv;
1572 }
1573 pstate->buf = buf;
1574 pstate->buflnum = lnumfirst;
1575 pstate->lnumlast = lnumlast;
1576
1577 return OK;
1578}
1579
1580/*
1581 * Cleanup the state information used for parsing lines and populating a
1582 * quickfix list.
1583 */
1584 static void
1585qf_cleanup_state(qfstate_T *pstate)
1586{
1587 if (pstate->fd != NULL)
1588 fclose(pstate->fd);
1589
1590 vim_free(pstate->growbuf);
1591#ifdef FEAT_MBYTE
1592 if (pstate->vc.vc_type != CONV_NONE)
1593 convert_setup(&pstate->vc, NULL, NULL);
1594#endif
1595}
1596
1597/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001598 * Read the errorfile "efile" into memory, line by line, building the error
1599 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001600 * Alternative: when "efile" is NULL read errors from buffer "buf".
1601 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001602 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001603 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1604 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001605 * Return -1 for error, number of errors for success.
1606 */
1607 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001608qf_init_ext(
1609 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001610 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001611 char_u *efile,
1612 buf_T *buf,
1613 typval_T *tv,
1614 char_u *errorformat,
1615 int newlist, /* TRUE: start a new error list */
1616 linenr_T lnumfirst, /* first line number to use */
1617 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001618 char_u *qf_title,
1619 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001620{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001621 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001622 qfstate_T state;
1623 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001624 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001625 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001626 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001628 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001630 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001632 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001633 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001634
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001635 vim_memset(&state, 0, sizeof(state));
1636 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001637 if ((qf_alloc_fields(&fields) == FAIL) ||
1638 (qf_setup_state(&state, enc, efile, tv, buf,
1639 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 goto qf_init_end;
1641
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001642 if (newlist || qf_idx == qi->qf_listcount)
1643 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001645 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001646 qf_idx = qi->qf_curlist;
1647 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001648 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001649 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001650 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001651 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001652 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001653 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001656 qfl = &qi->qf_lists[qf_idx];
1657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001659 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001660 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 else
1662 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001664 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001665 * If the errorformat didn't change between calls, then reuse the
1666 * previously parsed values.
1667 */
1668 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1669 {
1670 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001671 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001672 free_efm_list(&fmt_first);
1673
1674 /* parse the current 'efm' */
1675 fmt_first = parse_efm_option(efm);
1676 if (fmt_first != NULL)
1677 last_efm = vim_strsave(efm);
1678 }
1679
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
1683 /*
1684 * got_int is reset here, because it was probably set when killing the
1685 * ":make" command, but we still want to read the errorfile then.
1686 */
1687 got_int = FALSE;
1688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 /*
1690 * Read the lines in the error file one by one.
1691 * Try to recognize one of the error formats in each line.
1692 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001693 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001695 /* Get the next line from a file/buffer/list/string */
1696 status = qf_get_nextline(&state);
1697 if (status == QF_NOMEM) /* memory alloc failure */
1698 goto qf_init_end;
1699 if (status == QF_END_OF_INPUT) /* end of input */
1700 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001702 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1703 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001704 if (status == QF_FAIL)
1705 goto error2;
1706 if (status == QF_NOMEM)
1707 goto qf_init_end;
1708 if (status == QF_IGNORE_LINE)
1709 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001711 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001712 qf_idx,
1713 qfl->qf_directory,
1714 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001715 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001716 : ((qfl->qf_currfile != NULL && fields.valid)
1717 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001718 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001719 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001720 fields.errmsg,
1721 fields.lnum,
1722 fields.col,
1723 fields.use_viscol,
1724 fields.pattern,
1725 fields.enr,
1726 fields.type,
1727 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 goto error2;
1729 line_breakcheck();
1730 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001731 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001733 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001735 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001736 qfl->qf_ptr = qfl->qf_start;
1737 qfl->qf_index = 1;
1738 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
1740 else
1741 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001742 qfl->qf_nonevalid = FALSE;
1743 if (qfl->qf_ptr == NULL)
1744 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001746 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001747 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001748 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
1750 EMSG(_(e_readerrf));
1751error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001752 if (!adding)
1753 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001754 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001755 qf_free(qi, qi->qf_curlist);
1756 qi->qf_listcount--;
1757 if (qi->qf_curlist > 0)
1758 --qi->qf_curlist;
1759 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001760qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001761 if (qf_idx == qi->qf_curlist)
1762 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001763 qf_cleanup_state(&state);
1764 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
1766 return retval;
1767}
1768
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001769/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001770 * Read the errorfile "efile" into memory, line by line, building the error
1771 * list. Set the error list's title to qf_title.
1772 * Return -1 for error, number of errors for success.
1773 */
1774 int
1775qf_init(win_T *wp,
1776 char_u *efile,
1777 char_u *errorformat,
1778 int newlist, /* TRUE: start a new error list */
1779 char_u *qf_title,
1780 char_u *enc)
1781{
1782 qf_info_T *qi = &ql_info;
1783
1784 if (wp != NULL)
1785 {
1786 qi = ll_get_or_alloc_list(wp);
1787 if (qi == NULL)
1788 return FAIL;
1789 }
1790
1791 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1792 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1793}
1794
1795/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001796 * Set the title of the specified quickfix list. Frees the previous title.
1797 * Prepends ':' to the title.
1798 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001799 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001800qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001801{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001802 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001803
Bram Moolenaarfb604092014-07-23 15:55:00 +02001804 if (title != NULL)
1805 {
1806 char_u *p = alloc((int)STRLEN(title) + 2);
1807
Bram Moolenaara3921f42017-06-04 15:30:34 +02001808 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001809 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001810 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001811 }
1812}
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001815 * The title of a quickfix/location list is set, by default, to the command
1816 * that created the quickfix list with the ":" prefix.
1817 * Create a quickfix list title string by prepending ":" to a user command.
1818 * Returns a pointer to a static buffer with the title.
1819 */
1820 static char_u *
1821qf_cmdtitle(char_u *cmd)
1822{
1823 static char_u qftitle_str[IOSIZE];
1824
1825 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1826 return qftitle_str;
1827}
1828
1829/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001830 * Prepare for adding a new quickfix list. If the current list is in the
1831 * middle of the stack, then all the following lists are freed and then
1832 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 */
1834 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001835qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836{
1837 int i;
1838
1839 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001840 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 * the current entry. This makes it possible to browse in a tree-like
1842 * way with ":grep'.
1843 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001844 while (qi->qf_listcount > qi->qf_curlist + 1)
1845 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
1847 /*
1848 * When the stack is full, remove to oldest entry
1849 * Otherwise, add a new entry.
1850 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001851 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001853 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001855 qi->qf_lists[i - 1] = qi->qf_lists[i];
1856 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001859 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001860 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001861 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001862 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863}
1864
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001865/*
1866 * Free a location list
1867 */
1868 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001869ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001870{
1871 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001872 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001873
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001874 qi = *pqi;
1875 if (qi == NULL)
1876 return;
1877 *pqi = NULL; /* Remove reference to this list */
1878
1879 qi->qf_refcount--;
1880 if (qi->qf_refcount < 1)
1881 {
1882 /* No references to this location list */
1883 for (i = 0; i < qi->qf_listcount; ++i)
1884 qf_free(qi, i);
1885 vim_free(qi);
1886 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001887}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001888
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001889/*
1890 * Free all the quickfix/location lists in the stack.
1891 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001892 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001893qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001894{
1895 int i;
1896 qf_info_T *qi = &ql_info;
1897
1898 if (wp != NULL)
1899 {
1900 /* location list */
1901 ll_free_all(&wp->w_llist);
1902 ll_free_all(&wp->w_llist_ref);
1903 }
1904 else
1905 /* quickfix list */
1906 for (i = 0; i < qi->qf_listcount; ++i)
1907 qf_free(qi, i);
1908}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910/*
1911 * Add an entry to the end of the list of errors.
1912 * Returns OK or FAIL.
1913 */
1914 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001915qf_add_entry(
1916 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001917 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001918 char_u *dir, /* optional directory name */
1919 char_u *fname, /* file name or NULL */
Bram Moolenaard76ce852018-05-01 15:02:04 +02001920 char_u *module, /* module name or NULL */
Bram Moolenaar05540972016-01-30 20:31:25 +01001921 int bufnum, /* buffer number or zero */
1922 char_u *mesg, /* message */
1923 long lnum, /* line number */
1924 int col, /* column */
1925 int vis_col, /* using visual column */
1926 char_u *pattern, /* search pattern */
1927 int nr, /* error number */
1928 int type, /* type character */
1929 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001931 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001932 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001934 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001936 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001937 {
1938 buf_T *buf = buflist_findnr(bufnum);
1939
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001940 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001941 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001942 buf->b_has_qf_entry |=
1943 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001944 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001945 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001946 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1948 {
1949 vim_free(qfp);
1950 return FAIL;
1951 }
1952 qfp->qf_lnum = lnum;
1953 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001954 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001955 if (pattern == NULL || *pattern == NUL)
1956 qfp->qf_pattern = NULL;
1957 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1958 {
1959 vim_free(qfp->qf_text);
1960 vim_free(qfp);
1961 return FAIL;
1962 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001963 if (module == NULL || *module == NUL)
1964 qfp->qf_module = NULL;
1965 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
1966 {
1967 vim_free(qfp->qf_text);
1968 vim_free(qfp->qf_pattern);
1969 vim_free(qfp);
1970 return FAIL;
1971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 qfp->qf_nr = nr;
1973 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1974 type = 0;
1975 qfp->qf_type = type;
1976 qfp->qf_valid = valid;
1977
Bram Moolenaara3921f42017-06-04 15:30:34 +02001978 lastp = &qi->qf_lists[qf_idx].qf_last;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001979 if (qf_list_empty(qi, qf_idx)) /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001981 qi->qf_lists[qf_idx].qf_start = qfp;
1982 qi->qf_lists[qf_idx].qf_ptr = qfp;
1983 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001984 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 }
1986 else
1987 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001988 qfp->qf_prev = *lastp;
1989 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001991 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001993 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001994 ++qi->qf_lists[qf_idx].qf_count;
1995 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001996 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001998 qi->qf_lists[qf_idx].qf_index =
1999 qi->qf_lists[qf_idx].qf_count;
2000 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
2002
2003 return OK;
2004}
2005
2006/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002007 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002008 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002009 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002010ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002011{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002012 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002013
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002014 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
2015 if (qi != NULL)
2016 {
2017 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
2018 qi->qf_refcount++;
2019 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002020
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002021 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002022}
2023
2024/*
2025 * Return the location list for window 'wp'.
2026 * If not present, allocate a location list
2027 */
2028 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002029ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002030{
2031 if (IS_LL_WINDOW(wp))
2032 /* For a location list window, use the referenced location list */
2033 return wp->w_llist_ref;
2034
2035 /*
2036 * For a non-location list window, w_llist_ref should not point to a
2037 * location list.
2038 */
2039 ll_free_all(&wp->w_llist_ref);
2040
2041 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002042 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002043 return wp->w_llist;
2044}
2045
2046/*
2047 * Copy the location list from window "from" to window "to".
2048 */
2049 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002050copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002051{
2052 qf_info_T *qi;
2053 int idx;
2054 int i;
2055
2056 /*
2057 * When copying from a location list window, copy the referenced
2058 * location list. For other windows, copy the location list for
2059 * that window.
2060 */
2061 if (IS_LL_WINDOW(from))
2062 qi = from->w_llist_ref;
2063 else
2064 qi = from->w_llist;
2065
2066 if (qi == NULL) /* no location list to copy */
2067 return;
2068
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002069 /* allocate a new location list */
2070 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002071 return;
2072
2073 to->w_llist->qf_listcount = qi->qf_listcount;
2074
2075 /* Copy the location lists one at a time */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002076 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002077 {
2078 qf_list_T *from_qfl;
2079 qf_list_T *to_qfl;
2080
2081 to->w_llist->qf_curlist = idx;
2082
2083 from_qfl = &qi->qf_lists[idx];
2084 to_qfl = &to->w_llist->qf_lists[idx];
2085
2086 /* Some of the fields are populated by qf_add_entry() */
2087 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2088 to_qfl->qf_count = 0;
2089 to_qfl->qf_index = 0;
2090 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002091 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002092 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002093 if (from_qfl->qf_title != NULL)
2094 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2095 else
2096 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002097 if (from_qfl->qf_ctx != NULL)
2098 {
2099 to_qfl->qf_ctx = alloc_tv();
2100 if (to_qfl->qf_ctx != NULL)
2101 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2102 }
2103 else
2104 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002105
2106 if (from_qfl->qf_count)
2107 {
2108 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002109 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002110
2111 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002112 for (i = 0, from_qfp = from_qfl->qf_start;
2113 i < from_qfl->qf_count && from_qfp != NULL;
2114 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002115 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002116 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02002117 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002118 NULL,
2119 NULL,
Bram Moolenaard76ce852018-05-01 15:02:04 +02002120 from_qfp->qf_module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002121 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002122 from_qfp->qf_text,
2123 from_qfp->qf_lnum,
2124 from_qfp->qf_col,
2125 from_qfp->qf_viscol,
2126 from_qfp->qf_pattern,
2127 from_qfp->qf_nr,
2128 0,
2129 from_qfp->qf_valid) == FAIL)
2130 {
2131 qf_free_all(to);
2132 return;
2133 }
2134 /*
2135 * qf_add_entry() will not set the qf_num field, as the
2136 * directory and file names are not supplied. So the qf_fnum
2137 * field is copied here.
2138 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002139 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002140 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
2141 prevp->qf_type = from_qfp->qf_type; /* error type */
2142 if (from_qfl->qf_ptr == from_qfp)
2143 to_qfl->qf_ptr = prevp; /* current location */
2144 }
2145 }
2146
2147 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
2148
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002149 /* Assign a new ID for the location list */
2150 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01002151 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002152
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002153 /* When no valid entries are present in the list, qf_ptr points to
2154 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02002155 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002156 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002157 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002158 to_qfl->qf_index = 1;
2159 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002160 }
2161
2162 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
2163}
2164
2165/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002166 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002167 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 */
2169 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002170qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
Bram Moolenaar82404332016-07-10 17:00:38 +02002172 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002173 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002174 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002175
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (fname == NULL || *fname == NUL) /* no file name */
2177 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
Bram Moolenaare60acc12011-05-10 16:41:25 +02002179#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002180 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002181#endif
2182#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002183 if (directory != NULL)
2184 slash_adjust(directory);
2185 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002186#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002187 if (directory != NULL && !vim_isAbsName(fname)
2188 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2189 {
2190 /*
2191 * Here we check if the file really exists.
2192 * This should normally be true, but if make works without
2193 * "leaving directory"-messages we might have missed a
2194 * directory change.
2195 */
2196 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002199 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002200 if (directory)
2201 ptr = concat_fnames(directory, fname, TRUE);
2202 else
2203 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002205 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02002206 bufname = ptr;
2207 }
2208 else
2209 bufname = fname;
2210
2211 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002212 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002213 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002214 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002215 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002217 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002218 {
2219 vim_free(qf_last_bufname);
2220 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2221 if (bufname == ptr)
2222 qf_last_bufname = bufname;
2223 else
2224 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002225 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002226 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002227 if (buf == NULL)
2228 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002229
Bram Moolenaarc1542742016-07-20 21:44:37 +02002230 buf->b_has_qf_entry =
2231 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002232 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233}
2234
2235/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002236 * Push dirbuf onto the directory stack and return pointer to actual dir or
2237 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 */
2239 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002240qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
2242 struct dir_stack_T *ds_new;
2243 struct dir_stack_T *ds_ptr;
2244
2245 /* allocate new stack element and hook it in */
2246 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2247 if (ds_new == NULL)
2248 return NULL;
2249
2250 ds_new->next = *stackptr;
2251 *stackptr = ds_new;
2252
2253 /* store directory on the stack */
2254 if (vim_isAbsName(dirbuf)
2255 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002256 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 (*stackptr)->dirname = vim_strsave(dirbuf);
2258 else
2259 {
2260 /* Okay we don't have an absolute path.
2261 * dirbuf must be a subdir of one of the directories on the stack.
2262 * Let's search...
2263 */
2264 ds_new = (*stackptr)->next;
2265 (*stackptr)->dirname = NULL;
2266 while (ds_new)
2267 {
2268 vim_free((*stackptr)->dirname);
2269 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2270 TRUE);
2271 if (mch_isdir((*stackptr)->dirname) == TRUE)
2272 break;
2273
2274 ds_new = ds_new->next;
2275 }
2276
2277 /* clean up all dirs we already left */
2278 while ((*stackptr)->next != ds_new)
2279 {
2280 ds_ptr = (*stackptr)->next;
2281 (*stackptr)->next = (*stackptr)->next->next;
2282 vim_free(ds_ptr->dirname);
2283 vim_free(ds_ptr);
2284 }
2285
2286 /* Nothing found -> it must be on top level */
2287 if (ds_new == NULL)
2288 {
2289 vim_free((*stackptr)->dirname);
2290 (*stackptr)->dirname = vim_strsave(dirbuf);
2291 }
2292 }
2293
2294 if ((*stackptr)->dirname != NULL)
2295 return (*stackptr)->dirname;
2296 else
2297 {
2298 ds_ptr = *stackptr;
2299 *stackptr = (*stackptr)->next;
2300 vim_free(ds_ptr);
2301 return NULL;
2302 }
2303}
2304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305/*
2306 * pop dirbuf from the directory stack and return previous directory or NULL if
2307 * stack is empty
2308 */
2309 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002310qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311{
2312 struct dir_stack_T *ds_ptr;
2313
2314 /* TODO: Should we check if dirbuf is the directory on top of the stack?
2315 * What to do if it isn't? */
2316
2317 /* pop top element and free it */
2318 if (*stackptr != NULL)
2319 {
2320 ds_ptr = *stackptr;
2321 *stackptr = (*stackptr)->next;
2322 vim_free(ds_ptr->dirname);
2323 vim_free(ds_ptr);
2324 }
2325
2326 /* return NEW top element as current dir or NULL if stack is empty*/
2327 return *stackptr ? (*stackptr)->dirname : NULL;
2328}
2329
2330/*
2331 * clean up directory stack
2332 */
2333 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002334qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
2336 struct dir_stack_T *ds_ptr;
2337
2338 while ((ds_ptr = *stackptr) != NULL)
2339 {
2340 *stackptr = (*stackptr)->next;
2341 vim_free(ds_ptr->dirname);
2342 vim_free(ds_ptr);
2343 }
2344}
2345
2346/*
2347 * Check in which directory of the directory stack the given file can be
2348 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002349 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 * Cleans up intermediate directory entries.
2351 *
2352 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002353 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 * ./
2355 * ./aa
2356 * ./aa/bb
2357 * ./bb
2358 * ./bb/x.c
2359 * and make says:
2360 * making all in aa
2361 * making all in bb
2362 * x.c:9: Error
2363 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2364 * qf_guess_filepath will return NULL.
2365 */
2366 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002367qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368{
2369 struct dir_stack_T *ds_ptr;
2370 struct dir_stack_T *ds_tmp;
2371 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002372 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
2374 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002375 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 return NULL;
2377
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002378 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 fullname = NULL;
2380 while (ds_ptr)
2381 {
2382 vim_free(fullname);
2383 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2384
2385 /* If concat_fnames failed, just go on. The worst thing that can happen
2386 * is that we delete the entire stack.
2387 */
2388 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2389 break;
2390
2391 ds_ptr = ds_ptr->next;
2392 }
2393
2394 vim_free(fullname);
2395
2396 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002397 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002399 ds_tmp = qfl->qf_dir_stack->next;
2400 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 vim_free(ds_tmp->dirname);
2402 vim_free(ds_tmp);
2403 }
2404
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002405 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406}
2407
2408/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002409 * Returns TRUE if a quickfix/location list with the given identifier exists.
2410 */
2411 static int
2412qflist_valid (win_T *wp, int_u qf_id)
2413{
2414 qf_info_T *qi = &ql_info;
2415 int i;
2416
2417 if (wp != NULL)
2418 {
2419 qi = GET_LOC_LIST(wp); /* Location list */
2420 if (qi == NULL)
2421 return FALSE;
2422 }
2423
2424 for (i = 0; i < qi->qf_listcount; ++i)
2425 if (qi->qf_lists[i].qf_id == qf_id)
2426 return TRUE;
2427
2428 return FALSE;
2429}
2430
2431/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002432 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002433 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002434 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002435 * Similar to location list.
2436 */
2437 static int
2438is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
2439{
2440 qf_list_T *qfl;
2441 qfline_T *qfp;
2442 int i;
2443
2444 qfl = &qi->qf_lists[qi->qf_curlist];
2445
2446 /* Search for the entry in the current list */
2447 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2448 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002449 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002450 break;
2451
2452 if (i == qfl->qf_count) /* Entry is not found */
2453 return FALSE;
2454
2455 return TRUE;
2456}
2457
2458/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002459 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002460 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002461 */
2462 static qfline_T *
2463get_next_valid_entry(
2464 qf_info_T *qi,
2465 qfline_T *qf_ptr,
2466 int *qf_index,
2467 int dir)
2468{
2469 int idx;
2470 int old_qf_fnum;
2471
2472 idx = *qf_index;
2473 old_qf_fnum = qf_ptr->qf_fnum;
2474
2475 do
2476 {
2477 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2478 || qf_ptr->qf_next == NULL)
2479 return NULL;
2480 ++idx;
2481 qf_ptr = qf_ptr->qf_next;
2482 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2483 && !qf_ptr->qf_valid)
2484 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2485
2486 *qf_index = idx;
2487 return qf_ptr;
2488}
2489
2490/*
2491 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002492 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002493 */
2494 static qfline_T *
2495get_prev_valid_entry(
2496 qf_info_T *qi,
2497 qfline_T *qf_ptr,
2498 int *qf_index,
2499 int dir)
2500{
2501 int idx;
2502 int old_qf_fnum;
2503
2504 idx = *qf_index;
2505 old_qf_fnum = qf_ptr->qf_fnum;
2506
2507 do
2508 {
2509 if (idx == 1 || qf_ptr->qf_prev == NULL)
2510 return NULL;
2511 --idx;
2512 qf_ptr = qf_ptr->qf_prev;
2513 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2514 && !qf_ptr->qf_valid)
2515 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2516
2517 *qf_index = idx;
2518 return qf_ptr;
2519}
2520
2521/*
2522 * Get the n'th (errornr) previous/next valid entry from the current entry in
2523 * the quickfix list.
2524 * dir == FORWARD or FORWARD_FILE: next valid entry
2525 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2526 */
2527 static qfline_T *
2528get_nth_valid_entry(
2529 qf_info_T *qi,
2530 int errornr,
2531 qfline_T *qf_ptr,
2532 int *qf_index,
2533 int dir)
2534{
2535 qfline_T *prev_qf_ptr;
2536 int prev_index;
2537 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2538 char_u *err = e_no_more_items;
2539
2540 while (errornr--)
2541 {
2542 prev_qf_ptr = qf_ptr;
2543 prev_index = *qf_index;
2544
2545 if (dir == FORWARD || dir == FORWARD_FILE)
2546 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2547 else
2548 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2549 if (qf_ptr == NULL)
2550 {
2551 qf_ptr = prev_qf_ptr;
2552 *qf_index = prev_index;
2553 if (err != NULL)
2554 {
2555 EMSG(_(err));
2556 return NULL;
2557 }
2558 break;
2559 }
2560
2561 err = NULL;
2562 }
2563
2564 return qf_ptr;
2565}
2566
2567/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002568 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002569 */
2570 static qfline_T *
2571get_nth_entry(
2572 qf_info_T *qi,
2573 int errornr,
2574 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002575 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002576{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002577 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002578
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002579 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002580 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2581 {
2582 --qf_idx;
2583 qf_ptr = qf_ptr->qf_prev;
2584 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002585 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002586 while (errornr > qf_idx &&
2587 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2588 qf_ptr->qf_next != NULL)
2589 {
2590 ++qf_idx;
2591 qf_ptr = qf_ptr->qf_next;
2592 }
2593
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002594 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002595 return qf_ptr;
2596}
2597
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002598/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002599 * Find a window displaying a Vim help file.
2600 */
2601 static win_T *
2602qf_find_help_win(void)
2603{
2604 win_T *wp;
2605
2606 FOR_ALL_WINDOWS(wp)
2607 if (bt_help(wp->w_buffer))
2608 return wp;
2609
2610 return NULL;
2611}
2612
2613/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002614 * Find a help window or open one.
2615 */
2616 static int
2617jump_to_help_window(qf_info_T *qi, int *opened_window)
2618{
2619 win_T *wp;
2620 int flags;
2621
2622 if (cmdmod.tab != 0)
2623 wp = NULL;
2624 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002625 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002626 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2627 win_enter(wp, TRUE);
2628 else
2629 {
2630 /*
2631 * Split off help window; put it at far top if no position
2632 * specified, the current window is vertically split and narrow.
2633 */
2634 flags = WSP_HELP;
2635 if (cmdmod.split == 0 && curwin->w_width != Columns
2636 && curwin->w_width < 80)
2637 flags |= WSP_TOP;
2638 if (qi != &ql_info)
2639 flags |= WSP_NEWLOC; /* don't copy the location list */
2640
2641 if (win_split(0, flags) == FAIL)
2642 return FAIL;
2643
2644 *opened_window = TRUE;
2645
2646 if (curwin->w_height < p_hh)
2647 win_setheight((int)p_hh);
2648
2649 if (qi != &ql_info) /* not a quickfix list */
2650 {
2651 /* The new window should use the supplied location list */
2652 curwin->w_llist = qi;
2653 qi->qf_refcount++;
2654 }
2655 }
2656
2657 if (!p_im)
2658 restart_edit = 0; /* don't want insert mode in help file */
2659
2660 return OK;
2661}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002662
2663/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002664 * Find a non-quickfix window using the given location list.
2665 * Returns NULL if a matching window is not found.
2666 */
2667 static win_T *
2668qf_find_win_with_loclist(qf_info_T *ll)
2669{
2670 win_T *wp;
2671
2672 FOR_ALL_WINDOWS(wp)
2673 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2674 return wp;
2675
2676 return NULL;
2677}
2678
2679/*
2680 * Find a window containing a normal buffer
2681 */
2682 static win_T *
2683qf_find_win_with_normal_buf(void)
2684{
2685 win_T *wp;
2686
2687 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002688 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002689 return wp;
2690
2691 return NULL;
2692}
2693
2694/*
2695 * Go to a window in any tabpage containing the specified file. Returns TRUE
2696 * if successfully jumped to the window. Otherwise returns FALSE.
2697 */
2698 static int
2699qf_goto_tabwin_with_file(int fnum)
2700{
2701 tabpage_T *tp;
2702 win_T *wp;
2703
2704 FOR_ALL_TAB_WINDOWS(tp, wp)
2705 if (wp->w_buffer->b_fnum == fnum)
2706 {
2707 goto_tabpage_win(tp, wp);
2708 return TRUE;
2709 }
2710
2711 return FALSE;
2712}
2713
2714/*
2715 * Create a new window to show a file above the quickfix window. Called when
2716 * only the quickfix window is present.
2717 */
2718 static int
2719qf_open_new_file_win(qf_info_T *ll_ref)
2720{
2721 int flags;
2722
2723 flags = WSP_ABOVE;
2724 if (ll_ref != NULL)
2725 flags |= WSP_NEWLOC;
2726 if (win_split(0, flags) == FAIL)
2727 return FAIL; /* not enough room for window */
2728 p_swb = empty_option; /* don't split again */
2729 swb_flags = 0;
2730 RESET_BINDING(curwin);
2731 if (ll_ref != NULL)
2732 {
2733 /* The new window should use the location list from the
2734 * location list window */
2735 curwin->w_llist = ll_ref;
2736 ll_ref->qf_refcount++;
2737 }
2738 return OK;
2739}
2740
2741/*
2742 * Go to a window that shows the right buffer. If the window is not found, go
2743 * to the window just above the location list window. This is used for opening
2744 * a file from a location window and not from a quickfix window. If some usable
2745 * window is previously found, then it is supplied in 'use_win'.
2746 */
2747 static void
2748qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2749{
2750 win_T *win = use_win;
2751
2752 if (win == NULL)
2753 {
2754 /* Find the window showing the selected file */
2755 FOR_ALL_WINDOWS(win)
2756 if (win->w_buffer->b_fnum == qf_fnum)
2757 break;
2758 if (win == NULL)
2759 {
2760 /* Find a previous usable window */
2761 win = curwin;
2762 do
2763 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002764 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002765 break;
2766 if (win->w_prev == NULL)
2767 win = lastwin; /* wrap around the top */
2768 else
2769 win = win->w_prev; /* go to previous window */
2770 } while (win != curwin);
2771 }
2772 }
2773 win_goto(win);
2774
2775 /* If the location list for the window is not set, then set it
2776 * to the location list from the location window */
2777 if (win->w_llist == NULL)
2778 {
2779 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002780 if (ll_ref != NULL)
2781 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002782 }
2783}
2784
2785/*
2786 * Go to a window that shows the specified file. If a window is not found, go
2787 * to the window just above the quickfix window. This is used for opening a
2788 * file from a quickfix window and not from a location window.
2789 */
2790 static void
2791qf_goto_win_with_qfl_file(int qf_fnum)
2792{
2793 win_T *win;
2794 win_T *altwin;
2795
2796 win = curwin;
2797 altwin = NULL;
2798 for (;;)
2799 {
2800 if (win->w_buffer->b_fnum == qf_fnum)
2801 break;
2802 if (win->w_prev == NULL)
2803 win = lastwin; /* wrap around the top */
2804 else
2805 win = win->w_prev; /* go to previous window */
2806
2807 if (IS_QF_WINDOW(win))
2808 {
2809 /* Didn't find it, go to the window before the quickfix
2810 * window. */
2811 if (altwin != NULL)
2812 win = altwin;
2813 else if (curwin->w_prev != NULL)
2814 win = curwin->w_prev;
2815 else
2816 win = curwin->w_next;
2817 break;
2818 }
2819
2820 /* Remember a usable window. */
Bram Moolenaar91335e52018-08-01 17:53:12 +02002821 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002822 altwin = win;
2823 }
2824
2825 win_goto(win);
2826}
2827
2828/*
2829 * Find a suitable window for opening a file (qf_fnum) from the
2830 * quickfix/location list and jump to it. If the file is already opened in a
2831 * window, jump to it. Otherwise open a new window to display the file. This is
2832 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002833 */
2834 static int
2835qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2836{
2837 win_T *usable_win_ptr = NULL;
2838 int usable_win;
2839 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002840 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002841
2842 usable_win = 0;
2843
2844 ll_ref = curwin->w_llist_ref;
2845 if (ll_ref != NULL)
2846 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002847 /* Find a non-quickfix window with this location list */
2848 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2849 if (usable_win_ptr != NULL)
2850 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002851 }
2852
2853 if (!usable_win)
2854 {
2855 /* Locate a window showing a normal buffer */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002856 win = qf_find_win_with_normal_buf();
2857 if (win != NULL)
2858 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002859 }
2860
2861 /*
2862 * If no usable window is found and 'switchbuf' contains "usetab"
2863 * then search in other tabs.
2864 */
2865 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002866 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002867
2868 /*
2869 * If there is only one window and it is the quickfix window, create a
2870 * new one above the quickfix window.
2871 */
2872 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2873 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002874 if (qf_open_new_file_win(ll_ref) != OK)
2875 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002876 *opened_window = TRUE; /* close it when fail */
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002877 }
2878 else
2879 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002880 if (curwin->w_llist_ref != NULL) /* In a location window */
2881 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
2882 else /* In a quickfix window */
2883 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002884 }
2885
2886 return OK;
2887}
2888
2889/*
2890 * Edit the selected file or help file.
2891 */
2892 static int
2893qf_jump_edit_buffer(
2894 qf_info_T *qi,
2895 qfline_T *qf_ptr,
2896 int forceit,
2897 win_T *oldwin,
2898 int *opened_window,
2899 int *abort)
2900{
2901 int retval = OK;
2902
2903 if (qf_ptr->qf_type == 1)
2904 {
2905 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2906 * set b_p_ro flag). */
2907 if (!can_abandon(curbuf, forceit))
2908 {
2909 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002910 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002911 }
2912 else
2913 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2914 ECMD_HIDE + ECMD_SET_HELP,
2915 oldwin == curwin ? curwin : NULL);
2916 }
2917 else
2918 {
2919 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002920 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002921
2922 retval = buflist_getfile(qf_ptr->qf_fnum,
2923 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002924
2925 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002926 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002927 /*
2928 * Location list. Check whether the associated window is still
2929 * present and the list is still valid.
2930 */
2931 if (!win_valid_any_tab(oldwin))
2932 {
2933 EMSG(_("E924: Current window was closed"));
2934 *abort = TRUE;
2935 *opened_window = FALSE;
2936 }
2937 else if (!qflist_valid(oldwin, save_qfid))
2938 {
2939 EMSG(_(e_loc_list_changed));
2940 *abort = TRUE;
2941 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002942 }
2943 else if (old_qf_curlist != qi->qf_curlist
2944 || !is_qf_entry_present(qi, qf_ptr))
2945 {
2946 if (qi == &ql_info)
2947 EMSG(_("E925: Current quickfix was changed"));
2948 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002949 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002950 *abort = TRUE;
2951 }
2952
2953 if (*abort)
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002954 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002955 }
2956
2957 return retval;
2958}
2959
2960/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002961 * Go to the error line in the current file using either line/column number or
2962 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002963 */
2964 static void
2965qf_jump_goto_line(
2966 linenr_T qf_lnum,
2967 int qf_col,
2968 char_u qf_viscol,
2969 char_u *qf_pattern)
2970{
2971 linenr_T i;
2972 char_u *line;
2973 colnr_T screen_col;
2974 colnr_T char_col;
2975
2976 if (qf_pattern == NULL)
2977 {
2978 /*
2979 * Go to line with error, unless qf_lnum is 0.
2980 */
2981 i = qf_lnum;
2982 if (i > 0)
2983 {
2984 if (i > curbuf->b_ml.ml_line_count)
2985 i = curbuf->b_ml.ml_line_count;
2986 curwin->w_cursor.lnum = i;
2987 }
2988 if (qf_col > 0)
2989 {
2990 curwin->w_cursor.col = qf_col - 1;
2991#ifdef FEAT_VIRTUALEDIT
2992 curwin->w_cursor.coladd = 0;
2993#endif
2994 if (qf_viscol == TRUE)
2995 {
2996 /*
2997 * Check each character from the beginning of the error
2998 * line up to the error column. For each tab character
2999 * found, reduce the error column value by the length of
3000 * a tab character.
3001 */
3002 line = ml_get_curline();
3003 screen_col = 0;
3004 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3005 {
3006 if (*line == NUL)
3007 break;
3008 if (*line++ == '\t')
3009 {
3010 curwin->w_cursor.col -= 7 - (screen_col % 8);
3011 screen_col += 8 - (screen_col % 8);
3012 }
3013 else
3014 ++screen_col;
3015 }
3016 }
3017 check_cursor();
3018 }
3019 else
3020 beginline(BL_WHITE | BL_FIX);
3021 }
3022 else
3023 {
3024 pos_T save_cursor;
3025
3026 /* Move the cursor to the first line in the buffer */
3027 save_cursor = curwin->w_cursor;
3028 curwin->w_cursor.lnum = 0;
3029 if (!do_search(NULL, '/', qf_pattern, (long)1,
3030 SEARCH_KEEP, NULL, NULL))
3031 curwin->w_cursor = save_cursor;
3032 }
3033}
3034
3035/*
3036 * Display quickfix list index and size message
3037 */
3038 static void
3039qf_jump_print_msg(
3040 qf_info_T *qi,
3041 int qf_index,
3042 qfline_T *qf_ptr,
3043 buf_T *old_curbuf,
3044 linenr_T old_lnum)
3045{
3046 linenr_T i;
3047 int len;
3048
3049 /* Update the screen before showing the message, unless the screen
3050 * scrolled up. */
3051 if (!msg_scrolled)
3052 update_topline_redraw();
3053 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3054 qi->qf_lists[qi->qf_curlist].qf_count,
3055 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3056 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
3057 /* Add the message, skipping leading whitespace and newlines. */
3058 len = (int)STRLEN(IObuff);
3059 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3060
3061 /* Output the message. Overwrite to avoid scrolling when the 'O'
3062 * flag is present in 'shortmess'; But when not jumping, print the
3063 * whole message. */
3064 i = msg_scroll;
3065 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3066 msg_scroll = TRUE;
3067 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3068 msg_scroll = FALSE;
3069 msg_attr_keep(IObuff, 0, TRUE);
3070 msg_scroll = i;
3071}
3072
3073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 * jump to a quickfix line
3075 * if dir == FORWARD go "errornr" valid entries forward
3076 * if dir == BACKWARD go "errornr" valid entries backward
3077 * if dir == FORWARD_FILE go "errornr" valid entries files backward
3078 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
3079 * else if "errornr" is zero, redisplay the same line
3080 * else go to entry "errornr"
3081 */
3082 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003083qf_jump(qf_info_T *qi,
3084 int dir,
3085 int errornr,
3086 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003088 qfline_T *qf_ptr;
3089 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 buf_T *old_curbuf;
3093 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003094 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003095 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003097 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099#ifdef FEAT_FOLDING
3100 int old_KeyTyped = KeyTyped; /* getting file may reset it */
3101#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003102 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003104 if (qi == NULL)
3105 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003106
3107 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003108 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
3110 EMSG(_(e_quickfix));
3111 return;
3112 }
3113
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003114 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003116 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02003118 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003120 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
3121 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003123 qf_ptr = old_qf_ptr;
3124 qf_index = old_qf_index;
3125 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 }
3127 }
3128 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003129 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003131 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
3132 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 /* No need to print the error message if it's visible in the error
3134 * window */
3135 print_message = FALSE;
3136
3137 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003138 * For ":helpgrep" find a help window or open one.
3139 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003140 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003141 if (jump_to_help_window(qi, &opened_window) == FAIL)
3142 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003143
3144 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 * If currently in the quickfix window, find another window to show the
3146 * file in.
3147 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003148 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 {
3150 /*
3151 * If there is no file specified, we don't know where to go.
3152 * But do advance, otherwise ":cn" gets stuck.
3153 */
3154 if (qf_ptr->qf_fnum == 0)
3155 goto theend;
3156
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003157 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
3158 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
3161 /*
3162 * If there is a file name,
3163 * read the wanted file if needed, and check autowrite etc.
3164 */
3165 old_curbuf = curbuf;
3166 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003167
3168 if (qf_ptr->qf_fnum != 0)
3169 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003170 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003171
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003172 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3173 &opened_window, &abort);
3174 if (abort)
3175 {
3176 qi = NULL;
3177 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003178 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003179 }
3180
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003181 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 {
3183 /* When not switched to another buffer, still need to set pc mark */
3184 if (curbuf == old_curbuf)
3185 setpcmark();
3186
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003187 if (qf_ptr != NULL)
3188 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col,
3189 qf_ptr->qf_viscol, qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190
3191#ifdef FEAT_FOLDING
3192 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
3193 foldOpenCursor();
3194#endif
3195 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003196 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 }
3198 else
3199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (opened_window)
3201 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01003202 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 {
3204 /*
3205 * Couldn't open file, so put index back where it was. This could
3206 * happen if the file was readonly and we changed something.
3207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 qf_ptr = old_qf_ptr;
3210 qf_index = old_qf_index;
3211 }
3212 }
3213theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003214 if (qi != NULL)
3215 {
3216 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
3217 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
3218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 if (p_swb != old_swb && opened_window)
3220 {
3221 /* Restore old 'switchbuf' value, but not when an autocommand or
3222 * modeline has changed the value. */
3223 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003226 swb_flags = old_swb_flags;
3227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 else
3229 free_string_option(old_swb);
3230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231}
3232
3233/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003234 * Highlight attributes used for displaying entries from the quickfix list.
3235 */
3236static int qfFileAttr;
3237static int qfSepAttr;
3238static int qfLineAttr;
3239
3240/*
3241 * Display information about a single entry from the quickfix/location list.
3242 * Used by ":clist/:llist" commands.
3243 */
3244 static void
3245qf_list_entry(qf_info_T *qi, qfline_T *qfp, int qf_idx)
3246{
3247 char_u *fname;
3248 buf_T *buf;
3249 int filter_entry;
3250
3251 fname = NULL;
3252 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3253 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3254 (char *)qfp->qf_module);
3255 else {
3256 if (qfp->qf_fnum != 0
3257 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3258 {
3259 fname = buf->b_fname;
3260 if (qfp->qf_type == 1) /* :helpgrep */
3261 fname = gettail(fname);
3262 }
3263 if (fname == NULL)
3264 sprintf((char *)IObuff, "%2d", qf_idx);
3265 else
3266 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3267 qf_idx, (char *)fname);
3268 }
3269
3270 // Support for filtering entries using :filter /pat/ clist
3271 // Match against the module name, file name, search pattern and
3272 // text of the entry.
3273 filter_entry = TRUE;
3274 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3275 filter_entry &= message_filtered(qfp->qf_module);
3276 if (filter_entry && fname != NULL)
3277 filter_entry &= message_filtered(fname);
3278 if (filter_entry && qfp->qf_pattern != NULL)
3279 filter_entry &= message_filtered(qfp->qf_pattern);
3280 if (filter_entry)
3281 filter_entry &= message_filtered(qfp->qf_text);
3282 if (filter_entry)
3283 return;
3284
3285 msg_putchar('\n');
3286 msg_outtrans_attr(IObuff, qf_idx == qi->qf_lists[qi->qf_curlist].qf_index
3287 ? HL_ATTR(HLF_QFL) : qfFileAttr);
3288
3289 if (qfp->qf_lnum != 0)
3290 msg_puts_attr((char_u *)":", qfSepAttr);
3291 if (qfp->qf_lnum == 0)
3292 IObuff[0] = NUL;
3293 else if (qfp->qf_col == 0)
3294 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3295 else
3296 sprintf((char *)IObuff, "%ld col %d",
3297 qfp->qf_lnum, qfp->qf_col);
3298 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3299 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3300 msg_puts_attr(IObuff, qfLineAttr);
3301 msg_puts_attr((char_u *)":", qfSepAttr);
3302 if (qfp->qf_pattern != NULL)
3303 {
3304 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3305 msg_puts(IObuff);
3306 msg_puts_attr((char_u *)":", qfSepAttr);
3307 }
3308 msg_puts((char_u *)" ");
3309
3310 /* Remove newlines and leading whitespace from the text. For an
3311 * unrecognized line keep the indent, the compiler may mark a word
3312 * with ^^^^. */
3313 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3314 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3315 IObuff, IOSIZE);
3316 msg_prt_line(IObuff, FALSE);
3317 out_flush(); /* show one line at a time */
3318}
3319
3320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003322 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 */
3324 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003325qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003327 qfline_T *qfp;
3328 int i;
3329 int idx1 = 1;
3330 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003331 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003332 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003333 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003335 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003337 if (eap->cmdidx == CMD_llist)
3338 {
3339 qi = GET_LOC_LIST(curwin);
3340 if (qi == NULL)
3341 {
3342 EMSG(_(e_loclist));
3343 return;
3344 }
3345 }
3346
3347 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003348 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
3350 EMSG(_(e_quickfix));
3351 return;
3352 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003353 if (*arg == '+')
3354 {
3355 ++arg;
3356 plus = TRUE;
3357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3359 {
3360 EMSG(_(e_trailing));
3361 return;
3362 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003363 if (plus)
3364 {
3365 i = qi->qf_lists[qi->qf_curlist].qf_index;
3366 idx2 = i + idx1;
3367 idx1 = i;
3368 }
3369 else
3370 {
3371 i = qi->qf_lists[qi->qf_curlist].qf_count;
3372 if (idx1 < 0)
3373 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3374 if (idx2 < 0)
3375 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377
Bram Moolenaara796d462018-05-01 14:30:36 +02003378 /* Shorten all the file names, so that it is easy to read */
3379 shorten_fnames(FALSE);
3380
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003381 /*
3382 * Get the attributes for the different quickfix highlight items. Note
3383 * that this depends on syntax items defined in the qf.vim syntax file
3384 */
3385 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3386 if (qfFileAttr == 0)
3387 qfFileAttr = HL_ATTR(HLF_D);
3388 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3389 if (qfSepAttr == 0)
3390 qfSepAttr = HL_ATTR(HLF_D);
3391 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3392 if (qfLineAttr == 0)
3393 qfLineAttr = HL_ATTR(HLF_N);
3394
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003395 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003397 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3398 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 {
3400 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3401 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003402 if (got_int)
3403 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003404
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003405 qf_list_entry(qi, qfp, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003407
3408 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003409 if (qfp == NULL)
3410 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003411 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 ui_breakcheck();
3413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414}
3415
3416/*
3417 * Remove newlines and leading whitespace from an error message.
3418 * Put the result in "buf[bufsize]".
3419 */
3420 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003421qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
3423 int i;
3424 char_u *p = text;
3425
3426 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3427 {
3428 if (*p == '\n')
3429 {
3430 buf[i] = ' ';
3431 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003432 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 break;
3434 }
3435 else
3436 buf[i] = *p++;
3437 }
3438 buf[i] = NUL;
3439}
3440
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003441/*
3442 * Display information (list number, list size and the title) about a
3443 * quickfix/location list.
3444 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003445 static void
3446qf_msg(qf_info_T *qi, int which, char *lead)
3447{
3448 char *title = (char *)qi->qf_lists[which].qf_title;
3449 int count = qi->qf_lists[which].qf_count;
3450 char_u buf[IOSIZE];
3451
3452 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3453 lead,
3454 which + 1,
3455 qi->qf_listcount,
3456 count);
3457
3458 if (title != NULL)
3459 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003460 size_t len = STRLEN(buf);
3461
3462 if (len < 34)
3463 {
3464 vim_memset(buf + len, ' ', 34 - len);
3465 buf[34] = NUL;
3466 }
3467 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003468 }
3469 trunc_string(buf, buf, Columns - 1, IOSIZE);
3470 msg(buf);
3471}
3472
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473/*
3474 * ":colder [count]": Up in the quickfix stack.
3475 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003476 * ":lolder [count]": Up in the location list stack.
3477 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 */
3479 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003480qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003482 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 int count;
3484
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003485 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
3486 {
3487 qi = GET_LOC_LIST(curwin);
3488 if (qi == NULL)
3489 {
3490 EMSG(_(e_loclist));
3491 return;
3492 }
3493 }
3494
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 if (eap->addr_count != 0)
3496 count = eap->line2;
3497 else
3498 count = 1;
3499 while (count--)
3500 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003501 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003503 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
3505 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003506 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003508 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510 else
3511 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003512 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 {
3514 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003515 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003517 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003520 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003521 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522}
3523
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003524/*
3525 * Display the information about all the quickfix/location lists in the stack
3526 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003527 void
3528qf_history(exarg_T *eap)
3529{
3530 qf_info_T *qi = &ql_info;
3531 int i;
3532
3533 if (eap->cmdidx == CMD_lhistory)
3534 qi = GET_LOC_LIST(curwin);
3535 if (qi == NULL || (qi->qf_listcount == 0
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003536 && qf_list_empty(qi, qi->qf_curlist)))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003537 MSG(_("No entries"));
3538 else
3539 for (i = 0; i < qi->qf_listcount; ++i)
3540 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3541}
3542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003544 * Free all the entries in the error list "idx". Note that other information
3545 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 */
3547 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003548qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003550 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003551 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003552 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003553 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003555 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003557 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003558 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003559 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003560 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003561 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003562 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003563 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003564 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003565 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003566 if (stop)
3567 /* Somehow qf_count may have an incorrect value, set it to 1
3568 * to avoid crashing when it's wrong.
3569 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003570 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003571 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003572 qfl->qf_start = qfpnext;
3573 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003575
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003576 qfl->qf_index = 0;
3577 qfl->qf_start = NULL;
3578 qfl->qf_last = NULL;
3579 qfl->qf_ptr = NULL;
3580 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003581
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003582 qf_clean_dir_stack(&qfl->qf_dir_stack);
3583 qfl->qf_directory = NULL;
3584 qf_clean_dir_stack(&qfl->qf_file_stack);
3585 qfl->qf_currfile = NULL;
3586 qfl->qf_multiline = FALSE;
3587 qfl->qf_multiignore = FALSE;
3588 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589}
3590
3591/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003592 * Free error list "idx". Frees all the entries in the quickfix list,
3593 * associated context information and the title.
3594 */
3595 static void
3596qf_free(qf_info_T *qi, int idx)
3597{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003598 qf_list_T *qfl = &qi->qf_lists[idx];
3599
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003600 qf_free_items(qi, idx);
3601
Bram Moolenaard23a8232018-02-10 18:45:26 +01003602 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003603 free_tv(qfl->qf_ctx);
3604 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003605 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003606 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003607}
3608
3609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 * qf_mark_adjust: adjust marks
3611 */
3612 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003613qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003614 win_T *wp,
3615 linenr_T line1,
3616 linenr_T line2,
3617 long amount,
3618 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003620 int i;
3621 qfline_T *qfp;
3622 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003623 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003624 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003625 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
Bram Moolenaarc1542742016-07-20 21:44:37 +02003627 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003628 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003629 if (wp != NULL)
3630 {
3631 if (wp->w_llist == NULL)
3632 return;
3633 qi = wp->w_llist;
3634 }
3635
3636 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003637 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003638 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003639 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3640 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 if (qfp->qf_fnum == curbuf->b_fnum)
3642 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003643 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3645 {
3646 if (amount == MAXLNUM)
3647 qfp->qf_cleared = TRUE;
3648 else
3649 qfp->qf_lnum += amount;
3650 }
3651 else if (amount_after && qfp->qf_lnum > line2)
3652 qfp->qf_lnum += amount_after;
3653 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003654
3655 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003656 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657}
3658
3659/*
3660 * Make a nice message out of the error character and the error number:
3661 * char number message
3662 * e or E 0 " error"
3663 * w or W 0 " warning"
3664 * i or I 0 " info"
3665 * 0 0 ""
3666 * other 0 " c"
3667 * e or E n " error n"
3668 * w or W n " warning n"
3669 * i or I n " info n"
3670 * 0 n " error n"
3671 * other n " c n"
3672 * 1 x "" :helpgrep
3673 */
3674 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003675qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
3677 static char_u buf[20];
3678 static char_u cc[3];
3679 char_u *p;
3680
3681 if (c == 'W' || c == 'w')
3682 p = (char_u *)" warning";
3683 else if (c == 'I' || c == 'i')
3684 p = (char_u *)" info";
3685 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3686 p = (char_u *)" error";
3687 else if (c == 0 || c == 1)
3688 p = (char_u *)"";
3689 else
3690 {
3691 cc[0] = ' ';
3692 cc[1] = c;
3693 cc[2] = NUL;
3694 p = cc;
3695 }
3696
3697 if (nr <= 0)
3698 return p;
3699
3700 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3701 return buf;
3702}
3703
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003705 * When "split" is FALSE: Open the entry/result under the cursor.
3706 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3707 */
3708 void
3709qf_view_result(int split)
3710{
3711 qf_info_T *qi = &ql_info;
3712
3713 if (!bt_quickfix(curbuf))
3714 return;
3715
3716 if (IS_LL_WINDOW(curwin))
3717 qi = GET_LOC_LIST(curwin);
3718
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003719 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003720 {
3721 EMSG(_(e_quickfix));
3722 return;
3723 }
3724
3725 if (split)
3726 {
3727 char_u cmd[32];
3728
3729 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3730 (long)curwin->w_cursor.lnum,
3731 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3732 if (do_cmdline_cmd(cmd) == OK)
3733 do_cmdline_cmd((char_u *) "clearjumps");
3734 return;
3735 }
3736
3737 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3738}
3739
3740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 * ":cwindow": open the quickfix window if we have errors to display,
3742 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003743 * ":lwindow": open the location list window if we have locations to display,
3744 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 */
3746 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003747ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003749 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 win_T *win;
3751
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003752 if (eap->cmdidx == CMD_lwindow)
3753 {
3754 qi = GET_LOC_LIST(curwin);
3755 if (qi == NULL)
3756 return;
3757 }
3758
3759 /* Look for an existing quickfix window. */
3760 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761
3762 /*
3763 * If a quickfix window is open but we have no errors to display,
3764 * close the window. If a quickfix window is not open, then open
3765 * it if we have errors; otherwise, leave it closed.
3766 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003767 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003768 || qf_list_empty(qi, qi->qf_curlist)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003769 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 {
3771 if (win != NULL)
3772 ex_cclose(eap);
3773 }
3774 else if (win == NULL)
3775 ex_copen(eap);
3776}
3777
3778/*
3779 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003780 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003783ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003785 win_T *win = NULL;
3786 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003788 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
3789 {
3790 qi = GET_LOC_LIST(curwin);
3791 if (qi == NULL)
3792 return;
3793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003795 /* Find existing quickfix window and close it. */
3796 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 if (win != NULL)
3798 win_close(win, FALSE);
3799}
3800
3801/*
3802 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003803 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 */
3805 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003806ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003808 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003811 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003812 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003813 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003815 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
3816 {
3817 qi = GET_LOC_LIST(curwin);
3818 if (qi == NULL)
3819 {
3820 EMSG(_(e_loclist));
3821 return;
3822 }
3823 }
3824
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (eap->addr_count != 0)
3826 height = eap->line2;
3827 else
3828 height = QF_WINHEIGHT;
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831#ifdef FEAT_GUI
3832 need_mouse_correct = TRUE;
3833#endif
3834
3835 /*
3836 * Find existing quickfix window, or open a new one.
3837 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003838 win = qf_find_win(qi);
3839
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003840 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003841 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003843 if (eap->addr_count != 0)
3844 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003845 if (cmdmod.split & WSP_VERT)
3846 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003847 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003848 win_setwidth(height);
3849 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003850 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003851 win_setheight(height);
3852 }
3853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 else
3855 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003856 int flags = 0;
3857
Bram Moolenaar9c102382006-05-03 21:26:49 +00003858 qf_buf = qf_find_buf(qi);
3859
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 /* The current window becomes the previous window afterwards. */
3861 win = curwin;
3862
Bram Moolenaar77642c02012-11-20 17:55:10 +01003863 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3864 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003865 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003866 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003867 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003868 /* Default is to open the window below the current window */
3869 if (cmdmod.split == 0)
3870 flags = WSP_BELOW;
3871 flags |= WSP_NEWLOC;
3872 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003874 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003876 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003878 /*
3879 * For the location list window, create a reference to the
3880 * location list from the window 'win'.
3881 */
3882 curwin->w_llist_ref = win->w_llist;
3883 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003885
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003886 if (oldwin != curwin)
3887 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003888 if (qf_buf != NULL)
3889 /* Use the existing quickfix buffer */
3890 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003891 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003892 else
3893 {
3894 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003895 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003896 /* switch off 'swapfile' */
3897 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3898 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003899 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003900 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003901 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003902#ifdef FEAT_DIFF
3903 curwin->w_p_diff = FALSE;
3904#endif
3905#ifdef FEAT_FOLDING
3906 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3907 OPT_LOCAL);
3908#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003911 /* Only set the height when still in the same tab page and there is no
3912 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003913 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 win_setheight(height);
3915 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3916 if (win_valid(win))
3917 prevwin = win;
3918 }
3919
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003920 qf_set_title_var(qi);
3921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 /*
3923 * Fill the buffer with the quickfix list.
3924 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003925 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003927 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 curwin->w_cursor.col = 0;
3929 check_cursor();
3930 update_topline(); /* scroll to show the line */
3931}
3932
3933/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003934 * Move the cursor in the quickfix window to "lnum".
3935 */
3936 static void
3937qf_win_goto(win_T *win, linenr_T lnum)
3938{
3939 win_T *old_curwin = curwin;
3940
3941 curwin = win;
3942 curbuf = win->w_buffer;
3943 curwin->w_cursor.lnum = lnum;
3944 curwin->w_cursor.col = 0;
3945#ifdef FEAT_VIRTUALEDIT
3946 curwin->w_cursor.coladd = 0;
3947#endif
3948 curwin->w_curswant = 0;
3949 update_topline(); /* scroll to show the line */
3950 redraw_later(VALID);
3951 curwin->w_redr_status = TRUE; /* update ruler */
3952 curwin = old_curwin;
3953 curbuf = curwin->w_buffer;
3954}
3955
3956/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003957 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003958 */
3959 void
3960ex_cbottom(exarg_T *eap UNUSED)
3961{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003962 qf_info_T *qi = &ql_info;
3963 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003964
Bram Moolenaar537ef082016-07-09 17:56:19 +02003965 if (eap->cmdidx == CMD_lbottom)
3966 {
3967 qi = GET_LOC_LIST(curwin);
3968 if (qi == NULL)
3969 {
3970 EMSG(_(e_loclist));
3971 return;
3972 }
3973 }
3974
3975 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003976 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3977 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3978}
3979
3980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 * Return the number of the current entry (line number in the quickfix
3982 * window).
3983 */
3984 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003985qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003987 qf_info_T *qi = &ql_info;
3988
3989 if (IS_LL_WINDOW(wp))
3990 /* In the location list window, use the referenced location list */
3991 qi = wp->w_llist_ref;
3992
3993 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994}
3995
3996/*
3997 * Update the cursor position in the quickfix window to the current error.
3998 * Return TRUE if there is a quickfix window.
3999 */
4000 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004001qf_win_pos_update(
4002 qf_info_T *qi,
4003 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
4005 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004006 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
4008 /*
4009 * Put the cursor on the current error in the quickfix window, so that
4010 * it's viewable.
4011 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004012 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 if (win != NULL
4014 && qf_index <= win->w_buffer->b_ml.ml_line_count
4015 && old_qf_index != qf_index)
4016 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 if (qf_index > old_qf_index)
4018 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004019 win->w_redraw_top = old_qf_index;
4020 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
4022 else
4023 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004024 win->w_redraw_top = qf_index;
4025 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004027 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029 return win != NULL;
4030}
4031
4032/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004033 * Check whether the given window is displaying the specified quickfix/location
4034 * list buffer
4035 */
4036 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004037is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004038{
4039 /*
4040 * A window displaying the quickfix buffer will have the w_llist_ref field
4041 * set to NULL.
4042 * A window displaying a location list buffer will have the w_llist_ref
4043 * pointing to the location list.
4044 */
4045 if (bt_quickfix(win->w_buffer))
4046 if ((qi == &ql_info && win->w_llist_ref == NULL)
4047 || (qi != &ql_info && win->w_llist_ref == qi))
4048 return TRUE;
4049
4050 return FALSE;
4051}
4052
4053/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004054 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004055 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004056 */
4057 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004058qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004059{
4060 win_T *win;
4061
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004062 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004063 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004064 return win;
4065 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004066}
4067
4068/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004069 * Find a quickfix buffer.
4070 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 */
4072 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004073qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004075 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004076 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
Bram Moolenaar9c102382006-05-03 21:26:49 +00004078 FOR_ALL_TAB_WINDOWS(tp, win)
4079 if (is_qf_win(win, qi))
4080 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004081
Bram Moolenaar9c102382006-05-03 21:26:49 +00004082 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083}
4084
4085/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004086 * Update the w:quickfix_title variable in the quickfix/location list window
4087 */
4088 static void
4089qf_update_win_titlevar(qf_info_T *qi)
4090{
4091 win_T *win;
4092 win_T *curwin_save;
4093
4094 if ((win = qf_find_win(qi)) != NULL)
4095 {
4096 curwin_save = curwin;
4097 curwin = win;
4098 qf_set_title_var(qi);
4099 curwin = curwin_save;
4100 }
4101}
4102
4103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 * Find the quickfix buffer. If it exists, update the contents.
4105 */
4106 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004107qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108{
4109 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004110 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
4113 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004114 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 if (buf != NULL)
4116 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004117 linenr_T old_line_count = buf->b_ml.ml_line_count;
4118
4119 if (old_last == NULL)
4120 /* set curwin/curbuf to buf and save a few things */
4121 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
Bram Moolenaard823fa92016-08-12 16:29:27 +02004123 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004124
Bram Moolenaar864293a2016-06-02 13:40:04 +02004125 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004126 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004127
Bram Moolenaar864293a2016-06-02 13:40:04 +02004128 if (old_last == NULL)
4129 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004130 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004131
4132 /* restore curwin/curbuf and a few other things */
4133 aucmd_restbuf(&aco);
4134 }
4135
4136 /* Only redraw when added lines are visible. This avoids flickering
4137 * when the added lines are not visible. */
4138 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4139 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141}
4142
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004143/*
4144 * Set "w:quickfix_title" if "qi" has a title.
4145 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004146 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004147qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004148{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004149 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
4150 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004151 qi->qf_lists[qi->qf_curlist].qf_title);
4152}
4153
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004155 * Add an error line to the quickfix buffer.
4156 */
4157 static int
4158qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4159{
4160 int len;
4161 buf_T *errbuf;
4162
4163 if (qfp->qf_module != NULL)
4164 {
4165 STRCPY(IObuff, qfp->qf_module);
4166 len = (int)STRLEN(IObuff);
4167 }
4168 else if (qfp->qf_fnum != 0
4169 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4170 && errbuf->b_fname != NULL)
4171 {
4172 if (qfp->qf_type == 1) /* :helpgrep */
4173 STRCPY(IObuff, gettail(errbuf->b_fname));
4174 else
4175 {
4176 /* shorten the file name if not done already */
4177 if (errbuf->b_sfname == NULL
4178 || mch_isFullName(errbuf->b_sfname))
4179 {
4180 if (*dirname == NUL)
4181 mch_dirname(dirname, MAXPATHL);
4182 shorten_buf_fname(errbuf, dirname, FALSE);
4183 }
4184 STRCPY(IObuff, errbuf->b_fname);
4185 }
4186 len = (int)STRLEN(IObuff);
4187 }
4188 else
4189 len = 0;
4190 IObuff[len++] = '|';
4191
4192 if (qfp->qf_lnum > 0)
4193 {
4194 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4195 len += (int)STRLEN(IObuff + len);
4196
4197 if (qfp->qf_col > 0)
4198 {
4199 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4200 len += (int)STRLEN(IObuff + len);
4201 }
4202
4203 sprintf((char *)IObuff + len, "%s",
4204 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4205 len += (int)STRLEN(IObuff + len);
4206 }
4207 else if (qfp->qf_pattern != NULL)
4208 {
4209 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4210 len += (int)STRLEN(IObuff + len);
4211 }
4212 IObuff[len++] = '|';
4213 IObuff[len++] = ' ';
4214
4215 /* Remove newlines and leading whitespace from the text.
4216 * For an unrecognized line keep the indent, the compiler may
4217 * mark a word with ^^^^. */
4218 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4219 IObuff + len, IOSIZE - len);
4220
4221 if (ml_append_buf(buf, lnum, IObuff,
4222 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4223 return FAIL;
4224
4225 return OK;
4226}
4227
4228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 * Fill current buffer with quickfix errors, replacing any previous contents.
4230 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004231 * If "old_last" is not NULL append the items after this one.
4232 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4233 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 */
4235 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004236qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004238 linenr_T lnum;
4239 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004240 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
Bram Moolenaar864293a2016-06-02 13:40:04 +02004242 if (old_last == NULL)
4243 {
4244 if (buf != curbuf)
4245 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004246 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004247 return;
4248 }
4249
4250 /* delete all existing lines */
4251 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4252 (void)ml_delete((linenr_T)1, FALSE);
4253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
4255 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004256 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 {
Bram Moolenaara796d462018-05-01 14:30:36 +02004258 char_u dirname[MAXPATHL];
4259
4260 *dirname = NUL;
4261
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004263 if (old_last == NULL)
4264 {
4265 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4266 lnum = 0;
4267 }
4268 else
4269 {
4270 qfp = old_last->qf_next;
4271 lnum = buf->b_ml.ml_line_count;
4272 }
4273 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004275 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004277
Bram Moolenaar864293a2016-06-02 13:40:04 +02004278 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004280 if (qfp == NULL)
4281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004283
4284 if (old_last == NULL)
4285 /* Delete the empty line which is now at the end */
4286 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288
4289 /* correct cursor position */
4290 check_lnums(TRUE);
4291
Bram Moolenaar864293a2016-06-02 13:40:04 +02004292 if (old_last == NULL)
4293 {
4294 /* Set the 'filetype' to "qf" each time after filling the buffer.
4295 * This resembles reading a file into a buffer, it's more logical when
4296 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02004297 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004298 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4299 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300
Bram Moolenaar864293a2016-06-02 13:40:04 +02004301 keep_filetype = TRUE; /* don't detect 'filetype' */
4302 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004304 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004306 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004307 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004308
Bram Moolenaar864293a2016-06-02 13:40:04 +02004309 /* make sure it will be redrawn */
4310 redraw_curbuf_later(NOT_VALID);
4311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
4313 /* Restore KeyTyped, setting 'filetype' may reset it. */
4314 KeyTyped = old_KeyTyped;
4315}
4316
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004317/*
4318 * For every change made to the quickfix list, update the changed tick.
4319 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004320 static void
4321qf_list_changed(qf_info_T *qi, int qf_idx)
4322{
4323 qi->qf_lists[qf_idx].qf_changedtick++;
4324}
4325
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004327 * Return the quickfix/location list number with the given identifier.
4328 * Returns -1 if list is not found.
4329 */
4330 static int
4331qf_id2nr(qf_info_T *qi, int_u qfid)
4332{
4333 int qf_idx;
4334
4335 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4336 if (qi->qf_lists[qf_idx].qf_id == qfid)
4337 return qf_idx;
4338 return INVALID_QFIDX;
4339}
4340
4341/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004342 * Jump to the first entry if there is one.
4343 */
4344 static void
4345qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4346{
4347 // If autocommands changed the current list, then restore it
4348 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4349 qi->qf_curlist = qf_id2nr(qi, save_qfid);
4350
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004351 // Autocommands might have cleared the list, check for it
4352 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004353 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))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004876 // display the first error
4877 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004878}
4879
4880/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004881 * Return the vimgrep autocmd name.
4882 */
4883 static char_u *
4884vgr_get_auname(cmdidx_T cmdidx)
4885{
4886 switch (cmdidx)
4887 {
4888 case CMD_vimgrep: return (char_u *)"vimgrep";
4889 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4890 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4891 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4892 case CMD_grep: return (char_u *)"grep";
4893 case CMD_lgrep: return (char_u *)"lgrep";
4894 case CMD_grepadd: return (char_u *)"grepadd";
4895 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4896 default: return NULL;
4897 }
4898}
4899
4900/*
4901 * Initialize the regmatch used by vimgrep for pattern "s".
4902 */
4903 static void
4904vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4905{
4906 /* Get the search pattern: either white-separated or enclosed in // */
4907 regmatch->regprog = NULL;
4908
4909 if (s == NULL || *s == NUL)
4910 {
4911 /* Pattern is empty, use last search pattern. */
4912 if (last_search_pat() == NULL)
4913 {
4914 EMSG(_(e_noprevre));
4915 return;
4916 }
4917 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4918 }
4919 else
4920 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4921
4922 regmatch->rmm_ic = p_ic;
4923 regmatch->rmm_maxcol = 0;
4924}
4925
4926/*
4927 * Display a file name when vimgrep is running.
4928 */
4929 static void
4930vgr_display_fname(char_u *fname)
4931{
4932 char_u *p;
4933
4934 msg_start();
4935 p = msg_strtrunc(fname, TRUE);
4936 if (p == NULL)
4937 msg_outtrans(fname);
4938 else
4939 {
4940 msg_outtrans(p);
4941 vim_free(p);
4942 }
4943 msg_clr_eos();
4944 msg_didout = FALSE; /* overwrite this message */
4945 msg_nowait = TRUE; /* don't wait for this message */
4946 msg_col = 0;
4947 out_flush();
4948}
4949
4950/*
4951 * Load a dummy buffer to search for a pattern using vimgrep.
4952 */
4953 static buf_T *
4954vgr_load_dummy_buf(
4955 char_u *fname,
4956 char_u *dirname_start,
4957 char_u *dirname_now)
4958{
4959 int save_mls;
4960#if defined(FEAT_SYN_HL)
4961 char_u *save_ei = NULL;
4962#endif
4963 buf_T *buf;
4964
4965#if defined(FEAT_SYN_HL)
4966 /* Don't do Filetype autocommands to avoid loading syntax and
4967 * indent scripts, a great speed improvement. */
4968 save_ei = au_event_disable(",Filetype");
4969#endif
4970 /* Don't use modelines here, it's useless. */
4971 save_mls = p_mls;
4972 p_mls = 0;
4973
4974 /* Load file into a buffer, so that 'fileencoding' is detected,
4975 * autocommands applied, etc. */
4976 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
4977
4978 p_mls = save_mls;
4979#if defined(FEAT_SYN_HL)
4980 au_event_restore(save_ei);
4981#endif
4982
4983 return buf;
4984}
4985
4986/*
4987 * Check whether a quickfix/location list valid. Autocmds may remove or change
4988 * a quickfix list when vimgrep is running. If the list is not found, create a
4989 * new list.
4990 */
4991 static int
4992vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004993 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004994 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004995 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004996 char_u *title)
4997{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004998 /* Verify that the quickfix/location list was not freed by an autocmd */
4999 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005000 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005001 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005002 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005003 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005004 EMSG(_(e_loc_list_changed));
5005 return FALSE;
5006 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005007 else
5008 {
5009 /* Quickfix list is not found, create a new one. */
5010 qf_new_list(qi, title);
5011 return TRUE;
5012 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005013 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005014
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005015 if (qi->qf_lists[qi->qf_curlist].qf_id != qfid)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005016 /* Autocommands changed the quickfix list. Find the one we were
5017 * using and restore it. */
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005018 qi->qf_curlist = qf_id2nr(qi, qfid);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005019
5020 return TRUE;
5021}
5022
5023/*
5024 * Search for a pattern in all the lines in a buffer and add the matching lines
5025 * to a quickfix list.
5026 */
5027 static int
5028vgr_match_buflines(
5029 qf_info_T *qi,
5030 char_u *fname,
5031 buf_T *buf,
5032 regmmatch_T *regmatch,
5033 long tomatch,
5034 int duplicate_name,
5035 int flags)
5036{
5037 int found_match = FALSE;
5038 long lnum;
5039 colnr_T col;
5040
5041 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5042 {
5043 col = 0;
5044 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5045 col, NULL, NULL) > 0)
5046 {
5047 /* Pass the buffer number so that it gets used even for a
5048 * dummy buffer, unless duplicate_name is set, then the
5049 * buffer will be wiped out below. */
5050 if (qf_add_entry(qi,
5051 qi->qf_curlist,
5052 NULL, /* dir */
5053 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005054 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005055 duplicate_name ? 0 : buf->b_fnum,
5056 ml_get_buf(buf,
5057 regmatch->startpos[0].lnum + lnum, FALSE),
5058 regmatch->startpos[0].lnum + lnum,
5059 regmatch->startpos[0].col + 1,
5060 FALSE, /* vis_col */
5061 NULL, /* search pattern */
5062 0, /* nr */
5063 0, /* type */
5064 TRUE /* valid */
5065 ) == FAIL)
5066 {
5067 got_int = TRUE;
5068 break;
5069 }
5070 found_match = TRUE;
5071 if (--tomatch == 0)
5072 break;
5073 if ((flags & VGR_GLOBAL) == 0
5074 || regmatch->endpos[0].lnum > 0)
5075 break;
5076 col = regmatch->endpos[0].col
5077 + (col == regmatch->endpos[0].col);
5078 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5079 break;
5080 }
5081 line_breakcheck();
5082 if (got_int)
5083 break;
5084 }
5085
5086 return found_match;
5087}
5088
5089/*
5090 * Jump to the first match and update the directory.
5091 */
5092 static void
5093vgr_jump_to_match(
5094 qf_info_T *qi,
5095 int forceit,
5096 int *redraw_for_dummy,
5097 buf_T *first_match_buf,
5098 char_u *target_dir)
5099{
5100 buf_T *buf;
5101
5102 buf = curbuf;
5103 qf_jump(qi, 0, 0, forceit);
5104 if (buf != curbuf)
5105 /* If we jumped to another buffer redrawing will already be
5106 * taken care of. */
5107 *redraw_for_dummy = FALSE;
5108
5109 /* Jump to the directory used after loading the buffer. */
5110 if (curbuf == first_match_buf && target_dir != NULL)
5111 {
5112 exarg_T ea;
5113
5114 ea.arg = target_dir;
5115 ea.cmdidx = CMD_lcd;
5116 ex_cd(&ea);
5117 }
5118}
5119
5120/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005121 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005122 * ":vimgrepadd {pattern} file(s)"
5123 * ":lvimgrep {pattern} file(s)"
5124 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005125 */
5126 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005127ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005128{
Bram Moolenaar81695252004-12-29 20:58:21 +00005129 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005130 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005131 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005132 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005133 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005134 char_u *s;
5135 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005136 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005137 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005138 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005139 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005140 buf_T *buf;
5141 int duplicate_name = FALSE;
5142 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005143 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005144 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005145 buf_T *first_match_buf = NULL;
5146 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005147 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005148 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005149 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005150 char_u *dirname_start = NULL;
5151 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005152 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005153 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005154
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005155 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005156 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5157 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005158 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005159#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005160 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005161 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005162#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005163 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005164
Bram Moolenaar754b5602006-02-09 23:53:20 +00005165 if (eap->cmdidx == CMD_lgrep
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005166 || eap->cmdidx == CMD_lvimgrep
5167 || eap->cmdidx == CMD_lgrepadd
5168 || eap->cmdidx == CMD_lvimgrepadd)
Bram Moolenaara6557602006-02-04 22:43:20 +00005169 {
5170 qi = ll_get_or_alloc_list(curwin);
5171 if (qi == NULL)
5172 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005173 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005174 }
5175
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005176 if (eap->addr_count > 0)
5177 tomatch = eap->line2;
5178 else
5179 tomatch = MAXLNUM;
5180
Bram Moolenaar81695252004-12-29 20:58:21 +00005181 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005182 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005183 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005184 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005185 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005186 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005187 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005188 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005189 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005190
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005191 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005192 if (regmatch.regprog == NULL)
5193 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005194
5195 p = skipwhite(p);
5196 if (*p == NUL)
5197 {
5198 EMSG(_("E683: File name missing or invalid pattern"));
5199 goto theend;
5200 }
5201
Bram Moolenaar55b69262017-08-13 13:42:01 +02005202 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005203 && eap->cmdidx != CMD_vimgrepadd
5204 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005205 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005206 /* make place for a new list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005207 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005208
5209 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005210 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005211 goto theend;
5212 if (fcount == 0)
5213 {
5214 EMSG(_(e_nomatch));
5215 goto theend;
5216 }
5217
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005218 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5219 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005220 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005221 {
5222 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005223 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005224 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005225
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005226 /* Remember the current directory, because a BufRead autocommand that does
5227 * ":lcd %:p:h" changes the meaning of short path names. */
5228 mch_dirname(dirname_start, MAXPATHL);
5229
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005230 /* Remember the current quickfix list identifier, so that we can check for
5231 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005232 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005233
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005234 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005235 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005236 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005237 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005238 if (time(NULL) > seconds)
5239 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005240 /* Display the file name every second or so, show the user we are
5241 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005242 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005243 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005244 }
5245
Bram Moolenaar81695252004-12-29 20:58:21 +00005246 buf = buflist_findname_exp(fnames[fi]);
5247 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5248 {
5249 /* Remember that a buffer with this name already exists. */
5250 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005251 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005252 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005253
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005254 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005255 }
5256 else
5257 /* Use existing, loaded buffer. */
5258 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005259
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005260 /* Check whether the quickfix list is still valid. When loading a
5261 * buffer above, autocommands might have changed the quickfix list. */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005262 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005263 {
5264 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005265 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005266 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005267 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005268
Bram Moolenaar81695252004-12-29 20:58:21 +00005269 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005270 {
5271 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005272 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005273 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005274 else
5275 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005276 /* Try for a match in all lines of the buffer.
5277 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005278 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5279 tomatch, duplicate_name, flags);
5280
Bram Moolenaar81695252004-12-29 20:58:21 +00005281 if (using_dummy)
5282 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005283 if (found_match && first_match_buf == NULL)
5284 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005285 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005286 {
Bram Moolenaar81695252004-12-29 20:58:21 +00005287 /* Never keep a dummy buffer if there is another buffer
5288 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005289 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005290 buf = NULL;
5291 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005292 else if (!cmdmod.hide
5293 || buf->b_p_bh[0] == 'u' /* "unload" */
5294 || buf->b_p_bh[0] == 'w' /* "wipe" */
5295 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00005296 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005297 /* When no match was found we don't need to remember the
5298 * buffer, wipe it out. If there was a match and it
5299 * wasn't the first one or we won't jump there: only
5300 * unload the buffer.
5301 * Ignore 'hidden' here, because it may lead to having too
5302 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00005303 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005304 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005305 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005306 buf = NULL;
5307 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005308 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005309 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005310 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02005311 /* Keeping the buffer, remove the dummy flag. */
5312 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005313 buf = NULL;
5314 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005315 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005316
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005317 if (buf != NULL)
5318 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02005319 /* Keeping the buffer, remove the dummy flag. */
5320 buf->b_flags &= ~BF_DUMMY;
5321
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005322 /* If the buffer is still loaded we need to use the
5323 * directory we jumped to below. */
5324 if (buf == first_match_buf
5325 && target_dir == NULL
5326 && STRCMP(dirname_start, dirname_now) != 0)
5327 target_dir = vim_strsave(dirname_now);
5328
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005329 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005330 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00005331 * need to be done (again). But not the window-local
5332 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005333 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005334#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005335 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5336 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005337#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005338 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005339 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005340 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005341 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005342 }
5343 }
5344
5345 FreeWild(fcount, fnames);
5346
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005347 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5348 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
5349 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005350 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005351
Bram Moolenaar864293a2016-06-02 13:40:04 +02005352 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005353
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005354 if (au_name != NULL)
5355 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5356 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01005357 /*
5358 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5359 * is still valid.
5360 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005361 if (!qflist_valid(wp, save_qfid))
5362 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005363
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005364 // If autocommands changed the current list, then restore it
5365 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
5366 qi->qf_curlist = qf_id2nr(qi, save_qfid);
5367
Bram Moolenaar86b68352004-12-27 21:59:20 +00005368 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005369 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005370 {
5371 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005372 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5373 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005374 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005375 else
5376 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005377
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005378 /* If we loaded a dummy buffer into the current window, the autocommands
5379 * may have messed up things, need to redraw and recompute folds. */
5380 if (redraw_for_dummy)
5381 {
5382#ifdef FEAT_FOLDING
5383 foldUpdateAll(curwin);
5384#else
5385 redraw_later(NOT_VALID);
5386#endif
5387 }
5388
Bram Moolenaar86b68352004-12-27 21:59:20 +00005389theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005390 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005391 vim_free(dirname_now);
5392 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005393 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005394 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005395}
5396
5397/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005398 * Restore current working directory to "dirname_start" if they differ, taking
5399 * into account whether it is set locally or globally.
5400 */
5401 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005402restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005403{
5404 char_u *dirname_now = alloc(MAXPATHL);
5405
5406 if (NULL != dirname_now)
5407 {
5408 mch_dirname(dirname_now, MAXPATHL);
5409 if (STRCMP(dirname_start, dirname_now) != 0)
5410 {
5411 /* If the directory has changed, change it back by building up an
5412 * appropriate ex command and executing it. */
5413 exarg_T ea;
5414
5415 ea.arg = dirname_start;
5416 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5417 ex_cd(&ea);
5418 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005419 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005420 }
5421}
5422
5423/*
5424 * Load file "fname" into a dummy buffer and return the buffer pointer,
5425 * placing the directory resulting from the buffer load into the
5426 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5427 * prior to calling this function. Restores directory to "dirname_start" prior
5428 * to returning, if autocmds or the 'autochdir' option have changed it.
5429 *
5430 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5431 * or wipe_dummy_buffer() later!
5432 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005433 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005434 */
5435 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005436load_dummy_buffer(
5437 char_u *fname,
5438 char_u *dirname_start, /* in: old directory */
5439 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005440{
5441 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005442 bufref_T newbufref;
5443 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005444 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005445 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005446 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005447
5448 /* Allocate a buffer without putting it in the buffer list. */
5449 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5450 if (newbuf == NULL)
5451 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005452 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005453
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005454 /* Init the options. */
5455 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5456
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005457 /* need to open the memfile before putting the buffer in a window */
5458 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005459 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005460 /* Make sure this buffer isn't wiped out by autocommands. */
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005461 ++newbuf->b_locked;
5462
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005463 /* set curwin/curbuf to buf and save a few things */
5464 aucmd_prepbuf(&aco, newbuf);
5465
5466 /* Need to set the filename for autocommands. */
5467 (void)setfname(curbuf, fname, NULL, FALSE);
5468
Bram Moolenaar81695252004-12-29 20:58:21 +00005469 /* Create swap file now to avoid the ATTENTION message. */
5470 check_need_swap(TRUE);
5471
5472 /* Remove the "dummy" flag, otherwise autocommands may not
5473 * work. */
5474 curbuf->b_flags &= ~BF_DUMMY;
5475
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005476 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005477 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005478 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005479 NULL, READ_NEW | READ_DUMMY);
5480 --newbuf->b_locked;
5481 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005482 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005483 && !(curbuf->b_flags & BF_NEW))
5484 {
5485 failed = FALSE;
5486 if (curbuf != newbuf)
5487 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005488 /* Bloody autocommands changed the buffer! Can happen when
5489 * using netrw and editing a remote file. Use the current
5490 * buffer instead, delete the dummy one after restoring the
5491 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005492 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005493 newbuf = curbuf;
5494 }
5495 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005496
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005497 /* restore curwin/curbuf and a few other things */
5498 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005499 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5500 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005501
5502 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5503 * skip it. */
5504 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005505 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005506
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005507 /*
5508 * When autocommands/'autochdir' option changed directory: go back.
5509 * Let the caller know what the resulting dir was first, in case it is
5510 * important.
5511 */
5512 mch_dirname(resulting_dir, MAXPATHL);
5513 restore_start_dir(dirname_start);
5514
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005515 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005516 return NULL;
5517 if (failed)
5518 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005519 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005520 return NULL;
5521 }
5522 return newbuf;
5523}
5524
5525/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005526 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5527 * directory to "dirname_start" prior to returning, if autocmds or the
5528 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005529 */
5530 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005531wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005532{
5533 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005534 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005535#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005536 cleanup_T cs;
5537
5538 /* Reset the error/interrupt/exception state here so that aborting()
5539 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5540 * work when got_int is set. */
5541 enter_cleanup(&cs);
5542#endif
5543
Bram Moolenaar81695252004-12-29 20:58:21 +00005544 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005545
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005546#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005547 /* Restore the error/interrupt/exception state if not discarded by a
5548 * new aborting error, interrupt, or uncaught exception. */
5549 leave_cleanup(&cs);
5550#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005551 /* When autocommands/'autochdir' option changed directory: go back. */
5552 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005553 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005554}
5555
5556/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005557 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5558 * directory to "dirname_start" prior to returning, if autocmds or the
5559 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005560 */
5561 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005562unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005563{
5564 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005565 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005566 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005567
5568 /* When autocommands/'autochdir' option changed directory: go back. */
5569 restore_start_dir(dirname_start);
5570 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005571}
5572
Bram Moolenaar05159a02005-02-26 23:04:13 +00005573#if defined(FEAT_EVAL) || defined(PROTO)
5574/*
5575 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005576 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005577 */
5578 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005579get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005580{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005581 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005582 dict_T *dict;
5583 char_u buf[2];
5584 qfline_T *qfp;
5585 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005586 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005587
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005588 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005589 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005590 qi = &ql_info;
5591 if (wp != NULL)
5592 {
5593 qi = GET_LOC_LIST(wp);
5594 if (qi == NULL)
5595 return FAIL;
5596 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005597 }
5598
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005599 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005600 qf_idx = qi->qf_curlist;
5601
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005602 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005603 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005604
Bram Moolenaard823fa92016-08-12 16:29:27 +02005605 qfp = qi->qf_lists[qf_idx].qf_start;
5606 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005607 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005608 /* Handle entries with a non-existing buffer number. */
5609 bufnum = qfp->qf_fnum;
5610 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5611 bufnum = 0;
5612
Bram Moolenaar05159a02005-02-26 23:04:13 +00005613 if ((dict = dict_alloc()) == NULL)
5614 return FAIL;
5615 if (list_append_dict(list, dict) == FAIL)
5616 return FAIL;
5617
5618 buf[0] = qfp->qf_type;
5619 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005620 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5621 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5622 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5623 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5624 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5625 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5626 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5627 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5628 || dict_add_string(dict, "type", buf) == FAIL
5629 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005630 return FAIL;
5631
5632 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005633 if (qfp == NULL)
5634 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005635 }
5636 return OK;
5637}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005638
5639/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005640 * Flags used by getqflist()/getloclist() to determine which fields to return.
5641 */
5642enum {
5643 QF_GETLIST_NONE = 0x0,
5644 QF_GETLIST_TITLE = 0x1,
5645 QF_GETLIST_ITEMS = 0x2,
5646 QF_GETLIST_NR = 0x4,
5647 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005648 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005649 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005650 QF_GETLIST_IDX = 0x40,
5651 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005652 QF_GETLIST_TICK = 0x100,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005653 QF_GETLIST_ALL = 0x1FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005654};
5655
5656/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005657 * Parse text from 'di' and return the quickfix list items.
5658 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005659 */
5660 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005661qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005662{
5663 int status = FAIL;
5664 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005665 char_u *errorformat = p_efm;
5666 dictitem_T *efm_di;
5667 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005668
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005669 /* Only a List value is supported */
5670 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005671 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005672 /* If errorformat is supplied then use it, otherwise use the 'efm'
5673 * option setting
5674 */
5675 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5676 {
5677 if (efm_di->di_tv.v_type != VAR_STRING ||
5678 efm_di->di_tv.vval.v_string == NULL)
5679 return FAIL;
5680 errorformat = efm_di->di_tv.vval.v_string;
5681 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005682
Bram Moolenaar36538222017-09-02 19:51:44 +02005683 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005684 if (l == NULL)
5685 return FAIL;
5686
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005687 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
5688 if (qi != NULL)
5689 {
5690 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
5691 qi->qf_refcount++;
5692
Bram Moolenaar36538222017-09-02 19:51:44 +02005693 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005694 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5695 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005696 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005697 qf_free(qi, 0);
5698 }
5699 free(qi);
5700 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005701 dict_add_list(retdict, "items", l);
5702 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005703 }
5704
5705 return status;
5706}
5707
5708/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005709 * Return the quickfix/location list window identifier in the current tabpage.
5710 */
5711 static int
5712qf_winid(qf_info_T *qi)
5713{
5714 win_T *win;
5715
5716 /* The quickfix window can be opened even if the quickfix list is not set
5717 * using ":copen". This is not true for location lists. */
5718 if (qi == NULL)
5719 return 0;
5720 win = qf_find_win(qi);
5721 if (win != NULL)
5722 return win->w_id;
5723 return 0;
5724}
5725
5726/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005727 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005728 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005729 static int
5730qf_getprop_keys2flags(dict_T *what)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005731{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005732 int flags = QF_GETLIST_NONE;
5733
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005734 if (dict_find(what, (char_u *)"all", -1) != NULL)
5735 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005736
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005737 if (dict_find(what, (char_u *)"title", -1) != NULL)
5738 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005739
Bram Moolenaara6d48492017-12-12 22:45:31 +01005740 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5741 flags |= QF_GETLIST_NR;
5742
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005743 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5744 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005745
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005746 if (dict_find(what, (char_u *)"context", -1) != NULL)
5747 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005748
Bram Moolenaara6d48492017-12-12 22:45:31 +01005749 if (dict_find(what, (char_u *)"id", -1) != NULL)
5750 flags |= QF_GETLIST_ID;
5751
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005752 if (dict_find(what, (char_u *)"items", -1) != NULL)
5753 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005754
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005755 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5756 flags |= QF_GETLIST_IDX;
5757
5758 if (dict_find(what, (char_u *)"size", -1) != NULL)
5759 flags |= QF_GETLIST_SIZE;
5760
Bram Moolenaarb254af32017-12-18 19:48:58 +01005761 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5762 flags |= QF_GETLIST_TICK;
5763
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005764 return flags;
5765}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005766
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005767/*
5768 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5769 * If 'nr' and 'id' are not present in 'what' then return the current
5770 * quickfix list index.
5771 * If 'nr' is zero then return the current quickfix list index.
5772 * If 'nr' is '$' then return the last quickfix list index.
5773 * If 'id' is present then return the index of the quickfix list with that id.
5774 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5775 * Return -1, if quickfix list is not present or if the stack is empty.
5776 */
5777 static int
5778qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5779{
5780 int qf_idx;
5781 dictitem_T *di;
5782
5783 qf_idx = qi->qf_curlist; /* default is the current list */
5784 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5785 {
5786 /* Use the specified quickfix/location list */
5787 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005788 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005789 /* for zero use the current list */
5790 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005791 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005792 qf_idx = di->di_tv.vval.v_number - 1;
5793 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005794 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005795 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005796 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005797 else if (di->di_tv.v_type == VAR_STRING
5798 && di->di_tv.vval.v_string != NULL
5799 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5800 /* Get the last quickfix list number */
5801 qf_idx = qi->qf_listcount - 1;
5802 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005803 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005804 }
5805
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005806 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5807 {
5808 /* Look for a list with the specified id */
5809 if (di->di_tv.v_type == VAR_NUMBER)
5810 {
5811 /*
5812 * For zero, use the current list or the list specified by 'nr'
5813 */
5814 if (di->di_tv.vval.v_number != 0)
5815 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5816 }
5817 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005818 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005819 }
5820
5821 return qf_idx;
5822}
5823
5824/*
5825 * Return default values for quickfix list properties in retdict.
5826 */
5827 static int
5828qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5829{
5830 int status = OK;
5831
5832 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005833 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005834 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5835 {
5836 list_T *l = list_alloc();
5837 if (l != NULL)
5838 status = dict_add_list(retdict, "items", l);
5839 else
5840 status = FAIL;
5841 }
5842 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005843 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005844 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005845 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005846 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005847 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005848 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005849 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005850 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005851 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005852 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005853 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005854 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005855 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005856
5857 return status;
5858}
5859
5860/*
5861 * Return the quickfix list title as 'title' in retdict
5862 */
5863 static int
5864qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5865{
Bram Moolenaare0be1672018-07-08 16:50:37 +02005866 return dict_add_string(retdict, "title", qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005867}
5868
5869/*
5870 * Return the quickfix list items/entries as 'items' in retdict
5871 */
5872 static int
5873qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5874{
5875 int status = OK;
5876 list_T *l = list_alloc();
5877 if (l != NULL)
5878 {
5879 (void)get_errorlist(qi, NULL, qf_idx, l);
5880 dict_add_list(retdict, "items", l);
5881 }
5882 else
5883 status = FAIL;
5884
5885 return status;
5886}
5887
5888/*
5889 * Return the quickfix list context (if any) as 'context' in retdict.
5890 */
5891 static int
5892qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5893{
5894 int status;
5895 dictitem_T *di;
5896
5897 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5898 {
5899 di = dictitem_alloc((char_u *)"context");
5900 if (di != NULL)
5901 {
5902 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5903 status = dict_add(retdict, di);
5904 if (status == FAIL)
5905 dictitem_free(di);
5906 }
5907 else
5908 status = FAIL;
5909 }
5910 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02005911 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005912
5913 return status;
5914}
5915
5916/*
5917 * Return the quickfix list index as 'idx' in retdict
5918 */
5919 static int
5920qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5921{
5922 int idx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005923 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005924 /* For empty lists, qf_index is set to 1 */
5925 idx = 0;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005926 return dict_add_number(retdict, "idx", idx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005927}
5928
5929/*
5930 * Return quickfix/location list details (title) as a
5931 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5932 * then current list is used. Otherwise the specified list is used.
5933 */
5934 int
5935qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5936{
5937 qf_info_T *qi = &ql_info;
5938 int status = OK;
5939 int qf_idx;
5940 dictitem_T *di;
5941 int flags = QF_GETLIST_NONE;
5942
5943 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5944 return qf_get_list_from_lines(what, di, retdict);
5945
5946 if (wp != NULL)
5947 qi = GET_LOC_LIST(wp);
5948
5949 flags = qf_getprop_keys2flags(what);
5950
5951 if (qi != NULL && qi->qf_listcount != 0)
5952 qf_idx = qf_getprop_qfidx(qi, what);
5953
Bram Moolenaara6d48492017-12-12 22:45:31 +01005954 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005955 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005956 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005957
Bram Moolenaard823fa92016-08-12 16:29:27 +02005958 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005959 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005960 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005961 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005962 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005963 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005964 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005965 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005966 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005967 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005968 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005969 status = dict_add_number(retdict, "id", qi->qf_lists[qf_idx].qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005970 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005971 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005972 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005973 status = dict_add_number(retdict, "size",
5974 qi->qf_lists[qf_idx].qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005975 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005976 status = dict_add_number(retdict, "changedtick",
5977 qi->qf_lists[qf_idx].qf_changedtick);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005978
Bram Moolenaard823fa92016-08-12 16:29:27 +02005979 return status;
5980}
5981
5982/*
5983 * Add list of entries to quickfix/location list. Each list entry is
5984 * a dictionary with item information.
5985 */
5986 static int
5987qf_add_entries(
5988 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02005989 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005990 list_T *list,
5991 char_u *title,
5992 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005993{
5994 listitem_T *li;
5995 dict_T *d;
Bram Moolenaard76ce852018-05-01 15:02:04 +02005996 char_u *filename, *module, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005997 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005998 long lnum;
5999 int col, nr;
6000 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006001 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006002 int valid, status;
6003 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006004 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006005
Bram Moolenaara3921f42017-06-04 15:30:34 +02006006 if (action == ' ' || qf_idx == qi->qf_listcount)
6007 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006008 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006009 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006010 qf_idx = qi->qf_curlist;
6011 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006012 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006013 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02006014 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006015 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006016 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006017 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006018 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006019 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006020
6021 for (li = list->lv_first; li != NULL; li = li->li_next)
6022 {
6023 if (li->li_tv.v_type != VAR_DICT)
6024 continue; /* Skip non-dict items */
6025
6026 d = li->li_tv.vval.v_dict;
6027 if (d == NULL)
6028 continue;
6029
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006030 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaard76ce852018-05-01 15:02:04 +02006031 module = get_dict_string(d, (char_u *)"module", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006032 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6033 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6034 col = (int)get_dict_number(d, (char_u *)"col");
6035 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6036 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006037 type = get_dict_string(d, (char_u *)"type", TRUE);
6038 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6039 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006040 if (text == NULL)
6041 text = vim_strsave((char_u *)"");
6042
6043 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006044 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006045 valid = FALSE;
6046
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006047 /* Mark entries with non-existing buffer number as not valid. Give the
6048 * error message only once. */
6049 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6050 {
6051 if (!did_bufnr_emsg)
6052 {
6053 did_bufnr_emsg = TRUE;
6054 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6055 }
6056 valid = FALSE;
6057 bufnum = 0;
6058 }
6059
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02006060 /* If the 'valid' field is present it overrules the detected value. */
6061 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6062 valid = (int)get_dict_number(d, (char_u *)"valid");
6063
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006064 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006065 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006066 NULL, /* dir */
6067 filename,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006068 module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006069 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006070 text,
6071 lnum,
6072 col,
6073 vcol, /* vis_col */
6074 pattern, /* search pattern */
6075 nr,
6076 type == NULL ? NUL : *type,
6077 valid);
6078
6079 vim_free(filename);
Bram Moolenaard76ce852018-05-01 15:02:04 +02006080 vim_free(module);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006081 vim_free(pattern);
6082 vim_free(text);
6083 vim_free(type);
6084
6085 if (status == FAIL)
6086 {
6087 retval = FAIL;
6088 break;
6089 }
6090 }
6091
Bram Moolenaara3921f42017-06-04 15:30:34 +02006092 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02006093 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02006094 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006095 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02006096 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006097 if (action != 'a')
6098 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006099 qi->qf_lists[qf_idx].qf_ptr =
6100 qi->qf_lists[qf_idx].qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006101 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara3921f42017-06-04 15:30:34 +02006102 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006103 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006104
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006105 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02006106 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006107
6108 return retval;
6109}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006110
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006111/*
6112 * Get the quickfix list index from 'nr' or 'id'
6113 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006114 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006115qf_setprop_get_qfidx(
6116 qf_info_T *qi,
6117 dict_T *what,
6118 int action,
6119 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006120{
6121 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006122 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006123
Bram Moolenaard823fa92016-08-12 16:29:27 +02006124 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6125 {
6126 /* Use the specified quickfix/location list */
6127 if (di->di_tv.v_type == VAR_NUMBER)
6128 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006129 /* for zero use the current list */
6130 if (di->di_tv.vval.v_number != 0)
6131 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006132
Bram Moolenaar55b69262017-08-13 13:42:01 +02006133 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6134 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006135 /*
6136 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02006137 * non-available list and add the new list at the end of the
6138 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006139 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006140 *newlist = TRUE;
6141 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006142 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006143 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006144 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006145 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006146 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02006147 }
6148 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006149 && di->di_tv.vval.v_string != NULL
6150 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006151 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006152 if (qi->qf_listcount > 0)
6153 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006154 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006155 qf_idx = 0;
6156 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006157 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006158 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006159 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006160 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006161 }
6162
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006163 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006164 {
6165 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006166 if (di->di_tv.v_type != VAR_NUMBER)
6167 return INVALID_QFIDX;
6168
6169 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006170 }
6171
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006172 return qf_idx;
6173}
6174
6175/*
6176 * Set the quickfix list title.
6177 */
6178 static int
6179qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6180{
6181 if (di->di_tv.v_type != VAR_STRING)
6182 return FAIL;
6183
6184 vim_free(qi->qf_lists[qf_idx].qf_title);
6185 qi->qf_lists[qf_idx].qf_title =
6186 get_dict_string(what, (char_u *)"title", TRUE);
6187 if (qf_idx == qi->qf_curlist)
6188 qf_update_win_titlevar(qi);
6189
6190 return OK;
6191}
6192
6193/*
6194 * Set quickfix list items/entries.
6195 */
6196 static int
6197qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6198{
6199 int retval = FAIL;
6200 char_u *title_save;
6201
6202 if (di->di_tv.v_type != VAR_LIST)
6203 return FAIL;
6204
6205 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6206 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6207 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006208 vim_free(title_save);
6209
6210 return retval;
6211}
6212
6213/*
6214 * Set quickfix list items/entries from a list of lines.
6215 */
6216 static int
6217qf_setprop_items_from_lines(
6218 qf_info_T *qi,
6219 int qf_idx,
6220 dict_T *what,
6221 dictitem_T *di,
6222 int action)
6223{
6224 char_u *errorformat = p_efm;
6225 dictitem_T *efm_di;
6226 int retval = FAIL;
6227
6228 /* Use the user supplied errorformat settings (if present) */
6229 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6230 {
6231 if (efm_di->di_tv.v_type != VAR_STRING ||
6232 efm_di->di_tv.vval.v_string == NULL)
6233 return FAIL;
6234 errorformat = efm_di->di_tv.vval.v_string;
6235 }
6236
6237 /* Only a List value is supported */
6238 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6239 return FAIL;
6240
6241 if (action == 'r')
6242 qf_free_items(qi, qf_idx);
6243 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6244 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6245 retval = OK;
6246
6247 return retval;
6248}
6249
6250/*
6251 * Set quickfix list context.
6252 */
6253 static int
6254qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di)
6255{
6256 typval_T *ctx;
6257
6258 free_tv(qi->qf_lists[qf_idx].qf_ctx);
6259 ctx = alloc_tv();
6260 if (ctx != NULL)
6261 copy_tv(&di->di_tv, ctx);
6262 qi->qf_lists[qf_idx].qf_ctx = ctx;
6263
6264 return OK;
6265}
6266
6267/*
6268 * Set quickfix/location list properties (title, items, context).
6269 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006270 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006271 */
6272 static int
6273qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6274{
6275 dictitem_T *di;
6276 int retval = FAIL;
6277 int qf_idx;
6278 int newlist = FALSE;
6279
6280 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
6281 newlist = TRUE;
6282
6283 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
6284 if (qf_idx == INVALID_QFIDX) /* List not found */
6285 return FAIL;
6286
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006287 if (newlist)
6288 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006289 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006290 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006291 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006292 }
6293
6294 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006295 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006296 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006297 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006298 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006299 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006300 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006301 retval = qf_setprop_context(qi, qf_idx, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006302
Bram Moolenaarb254af32017-12-18 19:48:58 +01006303 if (retval == OK)
6304 qf_list_changed(qi, qf_idx);
6305
Bram Moolenaard823fa92016-08-12 16:29:27 +02006306 return retval;
6307}
6308
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006309/*
6310 * Find the non-location list window with the specified location list.
6311 */
6312 static win_T *
6313find_win_with_ll(qf_info_T *qi)
6314{
6315 win_T *wp = NULL;
6316
6317 FOR_ALL_WINDOWS(wp)
6318 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6319 return wp;
6320
6321 return NULL;
6322}
6323
6324/*
6325 * Free the entire quickfix/location list stack.
6326 * If the quickfix/location list window is open, then clear it.
6327 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006328 static void
6329qf_free_stack(win_T *wp, qf_info_T *qi)
6330{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006331 win_T *qfwin = qf_find_win(qi);
6332 win_T *llwin = NULL;
6333 win_T *orig_wp = wp;
6334
6335 if (qfwin != NULL)
6336 {
6337 /* If the quickfix/location list window is open, then clear it */
6338 if (qi->qf_curlist < qi->qf_listcount)
6339 qf_free(qi, qi->qf_curlist);
6340 qf_update_buffer(qi, NULL);
6341 }
6342
6343 if (wp != NULL && IS_LL_WINDOW(wp))
6344 {
6345 /* If in the location list window, then use the non-location list
6346 * window with this location list (if present)
6347 */
6348 llwin = find_win_with_ll(qi);
6349 if (llwin != NULL)
6350 wp = llwin;
6351 }
6352
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006353 qf_free_all(wp);
6354 if (wp == NULL)
6355 {
6356 /* quickfix list */
6357 qi->qf_curlist = 0;
6358 qi->qf_listcount = 0;
6359 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006360 else if (IS_LL_WINDOW(orig_wp))
6361 {
6362 /* If the location list window is open, then create a new empty
6363 * location list */
6364 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006365
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006366 /* first free the list reference in the location list window */
6367 ll_free_all(&orig_wp->w_llist_ref);
6368
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006369 orig_wp->w_llist_ref = new_ll;
6370 if (llwin != NULL)
6371 {
6372 llwin->w_llist = new_ll;
6373 new_ll->qf_refcount++;
6374 }
6375 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006376}
6377
Bram Moolenaard823fa92016-08-12 16:29:27 +02006378/*
6379 * Populate the quickfix list with the items supplied in the list
6380 * of dictionaries. "title" will be copied to w:quickfix_title.
6381 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6382 */
6383 int
6384set_errorlist(
6385 win_T *wp,
6386 list_T *list,
6387 int action,
6388 char_u *title,
6389 dict_T *what)
6390{
6391 qf_info_T *qi = &ql_info;
6392 int retval = OK;
6393
6394 if (wp != NULL)
6395 {
6396 qi = ll_get_or_alloc_list(wp);
6397 if (qi == NULL)
6398 return FAIL;
6399 }
6400
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006401 if (action == 'f')
6402 {
6403 /* Free the entire quickfix or location list stack */
6404 qf_free_stack(wp, qi);
6405 }
6406 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006407 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006408 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006409 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006410 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006411 if (retval == OK)
6412 qf_list_changed(qi, qi->qf_curlist);
6413 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006414
6415 return retval;
6416}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006417
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006418/*
6419 * Mark the context as in use for all the lists in a quickfix stack.
6420 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006421 static int
6422mark_quickfix_ctx(qf_info_T *qi, int copyID)
6423{
6424 int i;
6425 int abort = FALSE;
6426 typval_T *ctx;
6427
6428 for (i = 0; i < LISTCOUNT && !abort; ++i)
6429 {
6430 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006431 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6432 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006433 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6434 }
6435
6436 return abort;
6437}
6438
6439/*
6440 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006441 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006442 */
6443 int
6444set_ref_in_quickfix(int copyID)
6445{
6446 int abort = FALSE;
6447 tabpage_T *tp;
6448 win_T *win;
6449
6450 abort = mark_quickfix_ctx(&ql_info, copyID);
6451 if (abort)
6452 return abort;
6453
6454 FOR_ALL_TAB_WINDOWS(tp, win)
6455 {
6456 if (win->w_llist != NULL)
6457 {
6458 abort = mark_quickfix_ctx(win->w_llist, copyID);
6459 if (abort)
6460 return abort;
6461 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006462 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6463 {
6464 /* In a location list window and none of the other windows is
6465 * referring to this location list. Mark the location list
6466 * context as still in use.
6467 */
6468 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6469 if (abort)
6470 return abort;
6471 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006472 }
6473
6474 return abort;
6475}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006476#endif
6477
Bram Moolenaar81695252004-12-29 20:58:21 +00006478/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006479 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006480 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006481 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006482 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006483 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006484 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006485 */
6486 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006487ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006488{
6489 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006490 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006491 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006492 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006493 int_u save_qfid;
6494 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006495
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006496 switch (eap->cmdidx)
6497 {
6498 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6499 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6500 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6501 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6502 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6503 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6504 default: break;
6505 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006506 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6507 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006508 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006509#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006510 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006511 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006512#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006513 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006514
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006515 /* Must come after autocommands. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01006516 if (eap->cmdidx == CMD_lbuffer
6517 || eap->cmdidx == CMD_lgetbuffer
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006518 || eap->cmdidx == CMD_laddbuffer)
6519 {
6520 qi = ll_get_or_alloc_list(curwin);
6521 if (qi == NULL)
6522 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006523 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006524 }
6525
Bram Moolenaar86b68352004-12-27 21:59:20 +00006526 if (*eap->arg == NUL)
6527 buf = curbuf;
6528 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6529 buf = buflist_findnr(atoi((char *)eap->arg));
6530 if (buf == NULL)
6531 EMSG(_(e_invarg));
6532 else if (buf->b_ml.ml_mfp == NULL)
6533 EMSG(_("E681: Buffer is not loaded"));
6534 else
6535 {
6536 if (eap->addr_count == 0)
6537 {
6538 eap->line1 = 1;
6539 eap->line2 = buf->b_ml.ml_line_count;
6540 }
6541 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6542 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6543 EMSG(_(e_invrange));
6544 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006545 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006546 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006547
6548 if (buf->b_sfname)
6549 {
6550 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6551 (char *)qf_title, (char *)buf->b_sfname);
6552 qf_title = IObuff;
6553 }
6554
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006555 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006556 (eap->cmdidx != CMD_caddbuffer
6557 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006558 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006559 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006560 if (res >= 0)
6561 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006562
6563 // Remember the current quickfix list identifier, so that we can
6564 // check for autocommands changing the current quickfix list.
6565 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006566 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006567 {
6568 buf_T *curbuf_old = curbuf;
6569
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006570 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6571 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006572 if (curbuf != curbuf_old)
6573 // Autocommands changed buffer, don't jump now, "qi" may
6574 // be invalid.
6575 res = 0;
6576 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006577 // Jump to the first error for a new list and if autocmds didn't
6578 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006579 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006580 eap->cmdidx == CMD_lbuffer)
6581 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006582 // display the first error
6583 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar754b5602006-02-09 23:53:20 +00006584 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006585 }
6586}
6587
Bram Moolenaar1e015462005-09-25 22:16:38 +00006588#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006589/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006590 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6591 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006592 */
6593 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006594ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006595{
6596 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006597 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006598 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006599 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006600 int_u save_qfid;
6601 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006602
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006603 switch (eap->cmdidx)
6604 {
6605 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6606 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6607 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6608 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6609 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6610 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6611 default: break;
6612 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006613 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6614 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006615 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006616#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006617 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006618 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006619#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006620 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006621
Bram Moolenaar3c097222017-12-21 20:54:49 +01006622 if (eap->cmdidx == CMD_lexpr
6623 || eap->cmdidx == CMD_lgetexpr
6624 || eap->cmdidx == CMD_laddexpr)
6625 {
6626 qi = ll_get_or_alloc_list(curwin);
6627 if (qi == NULL)
6628 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006629 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006630 }
6631
Bram Moolenaar4770d092006-01-12 23:22:24 +00006632 /* Evaluate the expression. When the result is a string or a list we can
6633 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006634 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006635 if (tv != NULL)
6636 {
6637 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6638 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6639 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006640 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006641 (eap->cmdidx != CMD_caddexpr
6642 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006643 (linenr_T)0, (linenr_T)0,
6644 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006645 if (res >= 0)
6646 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006647
6648 // Remember the current quickfix list identifier, so that we can
6649 // check for autocommands changing the current quickfix list.
6650 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006651 if (au_name != NULL)
6652 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6653 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006654
6655 // Jump to the first error for a new list and if autocmds didn't
6656 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006657 if (res > 0 && (eap->cmdidx == CMD_cexpr
6658 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006659 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006660 // display the first error
6661 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006662 }
6663 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006664 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006665 free_tv(tv);
6666 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006667}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006668#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006669
6670/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006671 * Get the location list for ":lhelpgrep"
6672 */
6673 static qf_info_T *
6674hgr_get_ll(int *new_ll)
6675{
6676 win_T *wp;
6677 qf_info_T *qi;
6678
6679 /* If the current window is a help window, then use it */
6680 if (bt_help(curwin->w_buffer))
6681 wp = curwin;
6682 else
6683 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006684 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006685
6686 if (wp == NULL) /* Help window not found */
6687 qi = NULL;
6688 else
6689 qi = wp->w_llist;
6690
6691 if (qi == NULL)
6692 {
6693 /* Allocate a new location list for help text matches */
6694 if ((qi = ll_new_list()) == NULL)
6695 return NULL;
6696 *new_ll = TRUE;
6697 }
6698
6699 return qi;
6700}
6701
6702/*
6703 * Search for a pattern in a help file.
6704 */
6705 static void
6706hgr_search_file(
6707 qf_info_T *qi,
6708 char_u *fname,
6709#ifdef FEAT_MBYTE
6710 vimconv_T *p_vc,
6711#endif
6712 regmatch_T *p_regmatch)
6713{
6714 FILE *fd;
6715 long lnum;
6716
6717 fd = mch_fopen((char *)fname, "r");
6718 if (fd == NULL)
6719 return;
6720
6721 lnum = 1;
6722 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6723 {
6724 char_u *line = IObuff;
6725#ifdef FEAT_MBYTE
6726 /* Convert a line if 'encoding' is not utf-8 and
6727 * the line contains a non-ASCII character. */
6728 if (p_vc->vc_type != CONV_NONE
6729 && has_non_ascii(IObuff))
6730 {
6731 line = string_convert(p_vc, IObuff, NULL);
6732 if (line == NULL)
6733 line = IObuff;
6734 }
6735#endif
6736
6737 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6738 {
6739 int l = (int)STRLEN(line);
6740
6741 /* remove trailing CR, LF, spaces, etc. */
6742 while (l > 0 && line[l - 1] <= ' ')
6743 line[--l] = NUL;
6744
6745 if (qf_add_entry(qi,
6746 qi->qf_curlist,
6747 NULL, /* dir */
6748 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006749 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006750 0,
6751 line,
6752 lnum,
6753 (int)(p_regmatch->startp[0] - line)
6754 + 1, /* col */
6755 FALSE, /* vis_col */
6756 NULL, /* search pattern */
6757 0, /* nr */
6758 1, /* type */
6759 TRUE /* valid */
6760 ) == FAIL)
6761 {
6762 got_int = TRUE;
6763#ifdef FEAT_MBYTE
6764 if (line != IObuff)
6765 vim_free(line);
6766#endif
6767 break;
6768 }
6769 }
6770#ifdef FEAT_MBYTE
6771 if (line != IObuff)
6772 vim_free(line);
6773#endif
6774 ++lnum;
6775 line_breakcheck();
6776 }
6777 fclose(fd);
6778}
6779
6780/*
6781 * Search for a pattern in all the help files in the doc directory under
6782 * the given directory.
6783 */
6784 static void
6785hgr_search_files_in_dir(
6786 qf_info_T *qi,
6787 char_u *dirname,
6788 regmatch_T *p_regmatch
6789#ifdef FEAT_MBYTE
6790 , vimconv_T *p_vc
6791#endif
6792#ifdef FEAT_MULTI_LANG
6793 , char_u *lang
6794#endif
6795 )
6796{
6797 int fcount;
6798 char_u **fnames;
6799 int fi;
6800
6801 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6802 add_pathsep(dirname);
6803 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6804 if (gen_expand_wildcards(1, &dirname, &fcount,
6805 &fnames, EW_FILE|EW_SILENT) == OK
6806 && fcount > 0)
6807 {
6808 for (fi = 0; fi < fcount && !got_int; ++fi)
6809 {
6810#ifdef FEAT_MULTI_LANG
6811 /* Skip files for a different language. */
6812 if (lang != NULL
6813 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006814 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006815 && !(STRNICMP(lang, "en", 2) == 0
6816 && STRNICMP("txt", fnames[fi]
6817 + STRLEN(fnames[fi]) - 3, 3) == 0))
6818 continue;
6819#endif
6820
6821 hgr_search_file(qi, fnames[fi],
6822#ifdef FEAT_MBYTE
6823 p_vc,
6824#endif
6825 p_regmatch);
6826 }
6827 FreeWild(fcount, fnames);
6828 }
6829}
6830
6831/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006832 * Search for a pattern in all the help files in the 'runtimepath'
6833 * and add the matches to a quickfix list.
6834 * 'arg' is the language specifier. If supplied, then only matches in the
6835 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006836 */
6837 static void
6838hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *arg)
6839{
6840 char_u *p;
6841#ifdef FEAT_MULTI_LANG
6842 char_u *lang;
6843#endif
6844
6845#ifdef FEAT_MBYTE
6846 vimconv_T vc;
6847
6848 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6849 * differs. */
6850 vc.vc_type = CONV_NONE;
6851 if (!enc_utf8)
6852 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6853#endif
6854
6855#ifdef FEAT_MULTI_LANG
6856 /* Check for a specified language */
6857 lang = check_help_lang(arg);
6858#endif
6859
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006860 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006861 p = p_rtp;
6862 while (*p != NUL && !got_int)
6863 {
6864 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6865
6866 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6867#ifdef FEAT_MBYTE
6868 , &vc
6869#endif
6870#ifdef FEAT_MULTI_LANG
6871 , lang
6872#endif
6873 );
6874 }
6875
6876#ifdef FEAT_MBYTE
6877 if (vc.vc_type != CONV_NONE)
6878 convert_setup(&vc, NULL, NULL);
6879#endif
6880}
6881
6882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 * ":helpgrep {pattern}"
6884 */
6885 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006886ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887{
6888 regmatch_T regmatch;
6889 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006890 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006891 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006892 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893
Bram Moolenaar73633f82012-01-20 13:39:07 +01006894 switch (eap->cmdidx)
6895 {
6896 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6897 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6898 default: break;
6899 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006900 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6901 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006902 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006903#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006904 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006905 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006906#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006907 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006908
6909 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6910 save_cpo = p_cpo;
6911 p_cpo = empty_option;
6912
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006913 if (eap->cmdidx == CMD_lhelpgrep)
6914 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006915 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006916 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006917 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006918 }
6919
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6921 regmatch.rm_ic = FALSE;
6922 if (regmatch.regprog != NULL)
6923 {
6924 /* create a new quickfix list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006925 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006927 hgr_search_in_rtp(qi, &regmatch, eap->arg);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006928
Bram Moolenaar473de612013-06-08 18:19:48 +02006929 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006931 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6932 qi->qf_lists[qi->qf_curlist].qf_ptr =
6933 qi->qf_lists[qi->qf_curlist].qf_start;
6934 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 }
6936
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006937 if (p_cpo == empty_option)
6938 p_cpo = save_cpo;
6939 else
6940 /* Darn, some plugin changed the value. */
6941 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942
Bram Moolenaarb254af32017-12-18 19:48:58 +01006943 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006944 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
Bram Moolenaar73633f82012-01-20 13:39:07 +01006946 if (au_name != NULL)
6947 {
6948 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6949 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02006950 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006951 /* autocommands made "qi" invalid */
6952 return;
6953 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006954
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006956 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006957 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006958 else
6959 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006960
6961 if (eap->cmdidx == CMD_lhelpgrep)
6962 {
6963 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00006964 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006965 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006966 {
6967 if (new_qi)
6968 ll_free_all(&qi);
6969 }
6970 else if (curwin->w_llist == NULL)
6971 curwin->w_llist = qi;
6972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973}
6974
6975#endif /* FEAT_QUICKFIX */