blob: 78df7081f8a13879d1b6e338dbed474a998c0909 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar68b76a62005-03-25 21:53:48 +000030 qfline_T *qf_next; /* pointer to next error in the list */
31 qfline_T *qf_prev; /* pointer to previous error in the list */
32 linenr_T qf_lnum; /* line number where the error occurred */
33 int qf_fnum; /* file number for the line */
34 int qf_col; /* column where the error occurred */
35 int qf_nr; /* error number */
Bram Moolenaard76ce852018-05-01 15:02:04 +020036 char_u *qf_module; /* module name for this error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000037 char_u *qf_pattern; /* search pattern for the error */
38 char_u *qf_text; /* description of the error */
39 char_u qf_viscol; /* set to TRUE if qf_col is screen column */
40 char_u qf_cleared; /* set to TRUE if line has been deleted */
41 char_u qf_type; /* type of the error (mostly 'E'); 1 for
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 :helpgrep */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000043 char_u qf_valid; /* valid error message detected */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044};
45
46/*
47 * There is a stack of error lists.
48 */
49#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020050#define INVALID_QFIDX (-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020052/*
53 * Quickfix/Location list definition
54 * Contains a list of entries (qfline_T). qf_start points to the first entry
55 * and qf_last points to the last entry. qf_count contains the list size.
56 *
57 * Usually the list contains one or more entries. But an empty list can be
58 * created using setqflist()/setloclist() with a title and/or user context
59 * information and entries can be added later using setqflist()/setloclist().
60 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000061typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000062{
Bram Moolenaara539f4f2017-08-30 20:33:55 +020063 int_u qf_id; /* Unique identifier for this list */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000064 qfline_T *qf_start; /* pointer to the first error */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +020065 qfline_T *qf_last; /* pointer to the last error */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000066 qfline_T *qf_ptr; /* pointer to the current error */
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020067 int qf_count; /* number of errors (0 means empty list) */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000068 int qf_index; /* current index in the error list */
69 int qf_nonevalid; /* TRUE if not a single valid entry found */
Bram Moolenaar7fd73202010-07-25 16:58:46 +020070 char_u *qf_title; /* title derived from the command that created
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020071 * the error list or set by setqflist */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +020072 typval_T *qf_ctx; /* context set by setqflist/setloclist */
Bram Moolenaara7df8c72017-07-19 13:23:06 +020073
74 struct dir_stack_T *qf_dir_stack;
75 char_u *qf_directory;
76 struct dir_stack_T *qf_file_stack;
77 char_u *qf_currfile;
78 int qf_multiline;
79 int qf_multiignore;
80 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010081 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000082} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020084/*
85 * Quickfix/Location list stack definition
86 * Contains a list of quickfix/location lists (qf_list_T)
87 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000088struct qf_info_S
89{
90 /*
91 * Count of references to this list. Used only for location lists.
92 * When a location list window reference this list, qf_refcount
93 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
94 * reaches 0, the list is freed.
95 */
96 int qf_refcount;
97 int qf_listcount; /* current number of lists */
98 int qf_curlist; /* current error list */
99 qf_list_T qf_lists[LISTCOUNT];
100};
101
102static qf_info_T ql_info; /* global quickfix list */
Bram Moolenaara539f4f2017-08-30 20:33:55 +0200103static int_u last_qf_id = 0; /* Last used quickfix list id */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
Bram Moolenaard76ce852018-05-01 15:02:04 +0200105#define FMT_PATTERNS 11 /* maximum number of % recognized */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
107/*
108 * Structure used to hold the info of one part of 'errorformat'
109 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000110typedef struct efm_S efm_T;
111struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112{
113 regprog_T *prog; /* pre-formatted part of 'errorformat' */
Bram Moolenaar01265852006-03-20 21:50:15 +0000114 efm_T *next; /* pointer to next (NULL if last) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
116 char_u prefix; /* prefix of this format line: */
117 /* 'D' enter directory */
118 /* 'X' leave directory */
119 /* 'A' start of multi-line message */
120 /* 'E' error message */
121 /* 'W' warning message */
122 /* 'I' informational message */
123 /* 'C' continuation line */
124 /* 'Z' end of multi-line message */
125 /* 'G' general, unspecific message */
126 /* 'P' push file (partial) message */
127 /* 'Q' pop/quit file (partial) message */
128 /* 'O' overread (partial) message */
129 char_u flags; /* additional flags given in prefix */
130 /* '-' do not include this line */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000131 /* '+' include whole line in message */
Bram Moolenaar01265852006-03-20 21:50:15 +0000132 int conthere; /* %> used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133};
134
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100135static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */
136
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaard76ce852018-05-01 15:02:04 +0200138static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void qf_free(qf_info_T *qi, int idx);
140static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200141static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200142static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200144static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static win_T *qf_find_win(qf_info_T *qi);
148static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200149static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void qf_set_title_var(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200151static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static char_u *get_mef_name(void);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
154static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
155static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
156static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000158/* Quickfix window check helper macro */
159#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
160/* Location list window check helper macro */
161#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
162/*
163 * Return location list for window 'wp'
164 * For location list window, return the referenced location list
165 */
166#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200169 * Looking up a buffer can be slow if there are many. Remember the last one
170 * to make this a lot faster if there are multiple matches in the same file.
171 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200172static char_u *qf_last_bufname = NULL;
173static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200174
Bram Moolenaar3c097222017-12-21 20:54:49 +0100175static char *e_loc_list_changed =
176 N_("E926: Current location list was changed");
177
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200178/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200179 * Maximum number of bytes allowed per line while reading a errorfile.
180 */
181#define LINE_MAXLEN 4096
182
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200183static struct fmtpattern
184{
185 char_u convchar;
186 char *pattern;
187} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200188 {
189 {'f', ".\\+"}, /* only used when at end */
190 {'n', "\\d\\+"},
191 {'l', "\\d\\+"},
192 {'c', "\\d\\+"},
193 {'t', "."},
194 {'m', ".\\+"},
195 {'r', ".*"},
196 {'p', "[- .]*"},
197 {'v', "\\d\\+"},
198 {'s', ".\\+"},
199 {'o', ".\\+"}
200 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200201
202/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200203 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200204 * See fmt_pat definition above for the list of supported patterns. The
205 * pattern specifier is supplied in "efmpat". The converted pattern is stored
206 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200207 */
208 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200209efmpat_to_regpat(
210 char_u *efmpat,
211 char_u *regpat,
212 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200213 int idx,
214 int round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200215 char_u *errmsg)
216{
217 char_u *srcptr;
218
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200219 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200220 {
221 /* Each errorformat pattern can occur only once */
222 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200223 _("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200224 EMSG(errmsg);
225 return NULL;
226 }
227 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200228 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200229 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200230 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200231 {
232 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200233 _("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200234 EMSG(errmsg);
235 return NULL;
236 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200237 efminfo->addr[idx] = (char_u)++round;
238 *regpat++ = '\\';
239 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200240#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200241 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200242 {
243 /* Also match "c:" in the file name, even when
244 * checking for a colon next: "%f:".
245 * "\%(\a:\)\=" */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200246 STRCPY(regpat, "\\%(\\a:\\)\\=");
247 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200248 }
249#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200250 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200251 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200252 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200253 {
254 /* A file name may contain spaces, but this isn't
255 * in "\f". For "%f:%l:%m" there may be a ":" in
256 * the file name. Use ".\{-1,}x" instead (x is
257 * the next character), the requirement that :999:
258 * follows should work. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200259 STRCPY(regpat, ".\\{-1,}");
260 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200261 }
262 else
263 {
264 /* File name followed by '\\' or '%': include as
265 * many file name chars as possible. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200266 STRCPY(regpat, "\\f\\+");
267 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200268 }
269 }
270 else
271 {
272 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200273 while ((*regpat = *srcptr++) != NUL)
274 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200275 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200276 *regpat++ = '\\';
277 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200278
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200279 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200280}
281
282/*
283 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200284 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200285 */
286 static char_u *
287scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200288 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200289 char_u *efm,
290 int len,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200291 char_u *regpat,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200292 char_u *errmsg)
293{
294 char_u *efmp = *pefmp;
295
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200296 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200297 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200298 if ((*regpat++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200299 {
300 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200301 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200302 if (efmp < efm + len)
303 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200304 *regpat++ = *++efmp; /* could be ']' */
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200305 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200306 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200307 /* skip */;
308 if (efmp == efm + len)
309 {
310 EMSG(_("E374: Missing ] in format string"));
311 return NULL;
312 }
313 }
314 }
315 else if (efmp < efm + len) /* %*\D, %*\s etc. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200316 *regpat++ = *++efmp;
317 *regpat++ = '\\';
318 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200319 }
320 else
321 {
322 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
323 sprintf((char *)errmsg,
324 _("E375: Unsupported %%%c in format string"), *efmp);
325 EMSG(errmsg);
326 return NULL;
327 }
328
329 *pefmp = efmp;
330
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200331 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200332}
333
334/*
335 * Analyze/parse an errorformat prefix.
336 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200337 static char_u *
338efm_analyze_prefix(char_u *efmp, efm_T *efminfo, char_u *errmsg)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200339{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200340 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200341 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200342 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200343 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200344 else
345 {
346 sprintf((char *)errmsg,
347 _("E376: Invalid %%%c in format string prefix"), *efmp);
348 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200349 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200350 }
351
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200352 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200353}
354
355/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200356 * Converts a 'errorformat' string part in 'efm' to a regular expression
357 * pattern. The resulting regex pattern is returned in "regpat". Additional
358 * information about the 'erroformat' pattern is returned in "fmt_ptr".
359 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200360 */
361 static int
362efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200363 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200364 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200365 efm_T *fmt_ptr,
366 char_u *regpat,
367 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200368{
369 char_u *ptr;
370 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200371 int round;
372 int idx = 0;
373
374 /*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200375 * Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200376 */
377 ptr = regpat;
378 *ptr++ = '^';
379 round = 0;
380 for (efmp = efm; efmp < efm + len; ++efmp)
381 {
382 if (*efmp == '%')
383 {
384 ++efmp;
385 for (idx = 0; idx < FMT_PATTERNS; ++idx)
386 if (fmt_pat[idx].convchar == *efmp)
387 break;
388 if (idx < FMT_PATTERNS)
389 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200390 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200391 errmsg);
392 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200393 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200394 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200395 }
396 else if (*efmp == '*')
397 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200398 ++efmp;
399 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr, errmsg);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200400 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200401 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200402 }
403 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
404 *ptr++ = *efmp; /* regexp magic characters */
405 else if (*efmp == '#')
406 *ptr++ = '*';
407 else if (*efmp == '>')
408 fmt_ptr->conthere = TRUE;
409 else if (efmp == efm + 1) /* analyse prefix */
410 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200411 /*
412 * prefix is allowed only at the beginning of the errorformat
413 * option part
414 */
415 efmp = efm_analyze_prefix(efmp, fmt_ptr, errmsg);
416 if (efmp == NULL)
417 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200418 }
419 else
420 {
421 sprintf((char *)errmsg,
422 _("E377: Invalid %%%c in format string"), *efmp);
423 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200424 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200425 }
426 }
427 else /* copy normal character */
428 {
429 if (*efmp == '\\' && efmp + 1 < efm + len)
430 ++efmp;
431 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
432 *ptr++ = '\\'; /* escape regexp atoms */
433 if (*efmp)
434 *ptr++ = *efmp;
435 }
436 }
437 *ptr++ = '$';
438 *ptr = NUL;
439
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200440 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200441}
442
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200443/*
444 * Free the 'errorformat' information list
445 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200446 static void
447free_efm_list(efm_T **efm_first)
448{
449 efm_T *efm_ptr;
450
451 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
452 {
453 *efm_first = efm_ptr->next;
454 vim_regfree(efm_ptr->prog);
455 vim_free(efm_ptr);
456 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100457 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200458}
459
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200460/*
461 * Compute the size of the buffer used to convert a 'errorformat' pattern into
462 * a regular expression pattern.
463 */
464 static int
465efm_regpat_bufsz(char_u *efm)
466{
467 int sz;
468 int i;
469
470 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
471 for (i = FMT_PATTERNS; i > 0; )
472 sz += (int)STRLEN(fmt_pat[--i].pattern);
473#ifdef BACKSLASH_IN_FILENAME
474 sz += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */
475#else
476 sz += 2; /* "%f" can become two chars longer */
477#endif
478
479 return sz;
480}
481
482/*
483 * Return the length of a 'errorformat' option part (separated by ",").
484 */
485 static int
486efm_option_part_len(char_u *efm)
487{
488 int len;
489
490 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
491 if (efm[len] == '\\' && efm[len + 1] != NUL)
492 ++len;
493
494 return len;
495}
496
497/*
498 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
499 * are parsed and converted to regular expressions. Returns information about
500 * the parsed 'errorformat' option.
501 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200502 static efm_T *
503parse_efm_option(char_u *efm)
504{
505 char_u *errmsg = NULL;
506 int errmsglen;
507 efm_T *fmt_ptr = NULL;
508 efm_T *fmt_first = NULL;
509 efm_T *fmt_last = NULL;
510 char_u *fmtstr = NULL;
511 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200512 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200513
514 errmsglen = CMDBUFFSIZE + 1;
515 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
516 if (errmsg == NULL)
517 goto parse_efm_end;
518
519 /*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200520 * Each part of the format string is copied and modified from errorformat
521 * to regex prog. Only a few % characters are allowed.
522 */
523
524 /*
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200525 * Get some space to modify the format string into.
526 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200527 sz = efm_regpat_bufsz(efm);
528 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200529 goto parse_efm_error;
530
531 while (efm[0] != NUL)
532 {
533 /*
534 * Allocate a new eformat structure and put it at the end of the list
535 */
536 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
537 if (fmt_ptr == NULL)
538 goto parse_efm_error;
539 if (fmt_first == NULL) /* first one */
540 fmt_first = fmt_ptr;
541 else
542 fmt_last->next = fmt_ptr;
543 fmt_last = fmt_ptr;
544
545 /*
546 * Isolate one part in the 'errorformat' option
547 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200548 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200549
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200550 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200551 goto parse_efm_error;
552 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
553 goto parse_efm_error;
554 /*
555 * Advance to next part
556 */
557 efm = skip_to_option_part(efm + len); /* skip comma and spaces */
558 }
559
560 if (fmt_first == NULL) /* nothing found */
561 EMSG(_("E378: 'errorformat' contains no pattern"));
562
563 goto parse_efm_end;
564
565parse_efm_error:
566 free_efm_list(&fmt_first);
567
568parse_efm_end:
569 vim_free(fmtstr);
570 vim_free(errmsg);
571
572 return fmt_first;
573}
574
Bram Moolenaare0d37972016-07-15 22:36:01 +0200575enum {
576 QF_FAIL = 0,
577 QF_OK = 1,
578 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200579 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200580 QF_IGNORE_LINE = 4,
581 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200582};
583
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200584/*
585 * State information used to parse lines and add entries to a quickfix/location
586 * list.
587 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200588typedef struct {
589 char_u *linebuf;
590 int linelen;
591 char_u *growbuf;
592 int growbufsiz;
593 FILE *fd;
594 typval_T *tv;
595 char_u *p_str;
596 listitem_T *p_li;
597 buf_T *buf;
598 linenr_T buflnum;
599 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100600 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200601} qfstate_T;
602
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200603/*
604 * Allocate more memory for the line buffer used for parsing lines.
605 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200606 static char_u *
607qf_grow_linebuf(qfstate_T *state, int newsz)
608{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200609 char_u *p;
610
Bram Moolenaare0d37972016-07-15 22:36:01 +0200611 /*
612 * If the line exceeds LINE_MAXLEN exclude the last
613 * byte since it's not a NL character.
614 */
615 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
616 if (state->growbuf == NULL)
617 {
618 state->growbuf = alloc(state->linelen + 1);
619 if (state->growbuf == NULL)
620 return NULL;
621 state->growbufsiz = state->linelen;
622 }
623 else if (state->linelen > state->growbufsiz)
624 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200625 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200626 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200627 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200628 state->growbufsiz = state->linelen;
629 }
630 return state->growbuf;
631}
632
633/*
634 * Get the next string (separated by newline) from state->p_str.
635 */
636 static int
637qf_get_next_str_line(qfstate_T *state)
638{
639 /* Get the next line from the supplied string */
640 char_u *p_str = state->p_str;
641 char_u *p;
642 int len;
643
644 if (*p_str == NUL) /* Reached the end of the string */
645 return QF_END_OF_INPUT;
646
647 p = vim_strchr(p_str, '\n');
648 if (p != NULL)
649 len = (int)(p - p_str) + 1;
650 else
651 len = (int)STRLEN(p_str);
652
653 if (len > IOSIZE - 2)
654 {
655 state->linebuf = qf_grow_linebuf(state, len);
656 if (state->linebuf == NULL)
657 return QF_NOMEM;
658 }
659 else
660 {
661 state->linebuf = IObuff;
662 state->linelen = len;
663 }
664 vim_strncpy(state->linebuf, p_str, state->linelen);
665
666 /*
667 * Increment using len in order to discard the rest of the
668 * line if it exceeds LINE_MAXLEN.
669 */
670 p_str += len;
671 state->p_str = p_str;
672
673 return QF_OK;
674}
675
676/*
677 * Get the next string from state->p_Li.
678 */
679 static int
680qf_get_next_list_line(qfstate_T *state)
681{
682 listitem_T *p_li = state->p_li;
683 int len;
684
685 while (p_li != NULL
686 && (p_li->li_tv.v_type != VAR_STRING
687 || p_li->li_tv.vval.v_string == NULL))
688 p_li = p_li->li_next; /* Skip non-string items */
689
690 if (p_li == NULL) /* End of the list */
691 {
692 state->p_li = NULL;
693 return QF_END_OF_INPUT;
694 }
695
696 len = (int)STRLEN(p_li->li_tv.vval.v_string);
697 if (len > IOSIZE - 2)
698 {
699 state->linebuf = qf_grow_linebuf(state, len);
700 if (state->linebuf == NULL)
701 return QF_NOMEM;
702 }
703 else
704 {
705 state->linebuf = IObuff;
706 state->linelen = len;
707 }
708
709 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
710
711 state->p_li = p_li->li_next; /* next item */
712 return QF_OK;
713}
714
715/*
716 * Get the next string from state->buf.
717 */
718 static int
719qf_get_next_buf_line(qfstate_T *state)
720{
721 char_u *p_buf = NULL;
722 int len;
723
724 /* Get the next line from the supplied buffer */
725 if (state->buflnum > state->lnumlast)
726 return QF_END_OF_INPUT;
727
728 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
729 state->buflnum += 1;
730
731 len = (int)STRLEN(p_buf);
732 if (len > IOSIZE - 2)
733 {
734 state->linebuf = qf_grow_linebuf(state, len);
735 if (state->linebuf == NULL)
736 return QF_NOMEM;
737 }
738 else
739 {
740 state->linebuf = IObuff;
741 state->linelen = len;
742 }
743 vim_strncpy(state->linebuf, p_buf, state->linelen);
744
745 return QF_OK;
746}
747
748/*
749 * Get the next string from file state->fd.
750 */
751 static int
752qf_get_next_file_line(qfstate_T *state)
753{
754 int discard;
755 int growbuflen;
756
757 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
758 return QF_END_OF_INPUT;
759
760 discard = FALSE;
761 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200762 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200763 {
764 /*
765 * The current line exceeds IObuff, continue reading using
766 * growbuf until EOL or LINE_MAXLEN bytes is read.
767 */
768 if (state->growbuf == NULL)
769 {
770 state->growbufsiz = 2 * (IOSIZE - 1);
771 state->growbuf = alloc(state->growbufsiz);
772 if (state->growbuf == NULL)
773 return QF_NOMEM;
774 }
775
776 /* Copy the read part of the line, excluding null-terminator */
777 memcpy(state->growbuf, IObuff, IOSIZE - 1);
778 growbuflen = state->linelen;
779
780 for (;;)
781 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200782 char_u *p;
783
Bram Moolenaare0d37972016-07-15 22:36:01 +0200784 if (fgets((char *)state->growbuf + growbuflen,
785 state->growbufsiz - growbuflen, state->fd) == NULL)
786 break;
787 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
788 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200789 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200790 break;
791 if (state->growbufsiz == LINE_MAXLEN)
792 {
793 discard = TRUE;
794 break;
795 }
796
797 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
798 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200799 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200800 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200801 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200802 }
803
804 while (discard)
805 {
806 /*
807 * The current line is longer than LINE_MAXLEN, continue
808 * reading but discard everything until EOL or EOF is
809 * reached.
810 */
811 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
812 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200813 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200814 break;
815 }
816
817 state->linebuf = state->growbuf;
818 state->linelen = growbuflen;
819 }
820 else
821 state->linebuf = IObuff;
822
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100823#ifdef FEAT_MBYTE
824 /* Convert a line if it contains a non-ASCII character. */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200825 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
826 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100827 char_u *line;
828
829 line = string_convert(&state->vc, state->linebuf, &state->linelen);
830 if (line != NULL)
831 {
832 if (state->linelen < IOSIZE)
833 {
834 STRCPY(state->linebuf, line);
835 vim_free(line);
836 }
837 else
838 {
839 vim_free(state->growbuf);
840 state->linebuf = state->growbuf = line;
841 state->growbufsiz = state->linelen < LINE_MAXLEN
842 ? state->linelen : LINE_MAXLEN;
843 }
844 }
845 }
846#endif
847
Bram Moolenaare0d37972016-07-15 22:36:01 +0200848 return QF_OK;
849}
850
851/*
852 * Get the next string from a file/buffer/list/string.
853 */
854 static int
855qf_get_nextline(qfstate_T *state)
856{
857 int status = QF_FAIL;
858
859 if (state->fd == NULL)
860 {
861 if (state->tv != NULL)
862 {
863 if (state->tv->v_type == VAR_STRING)
864 /* Get the next line from the supplied string */
865 status = qf_get_next_str_line(state);
866 else if (state->tv->v_type == VAR_LIST)
867 /* Get the next line from the supplied list */
868 status = qf_get_next_list_line(state);
869 }
870 else
871 /* Get the next line from the supplied buffer */
872 status = qf_get_next_buf_line(state);
873 }
874 else
875 /* Get the next line from the supplied file */
876 status = qf_get_next_file_line(state);
877
878 if (status != QF_OK)
879 return status;
880
881 /* remove newline/CR from the line */
882 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200883 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200884 state->linebuf[state->linelen - 1] = NUL;
885#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200886 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
887 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200888#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200889 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200890
891#ifdef FEAT_MBYTE
892 remove_bom(state->linebuf);
893#endif
894
895 return QF_OK;
896}
897
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200898typedef struct {
899 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200900 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200901 char_u *errmsg;
902 int errmsglen;
903 long lnum;
904 int col;
905 char_u use_viscol;
906 char_u *pattern;
907 int enr;
908 int type;
909 int valid;
910} qffields_T;
911
912/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200913 * Parse the match for filename ('%f') pattern in regmatch.
914 * Return the matched value in "fields->namebuf".
915 */
916 static int
917qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
918{
919 int c;
920
921 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
922 return QF_FAIL;
923
924 /* Expand ~/file and $HOME/file to full path. */
925 c = *rmp->endp[midx];
926 *rmp->endp[midx] = NUL;
927 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
928 *rmp->endp[midx] = c;
929
930 /*
931 * For separate filename patterns (%O, %P and %Q), the specified file
932 * should exist.
933 */
934 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
935 && mch_getperm(fields->namebuf) == -1)
936 return QF_FAIL;
937
938 return QF_OK;
939}
940
941/*
942 * Parse the match for error number ('%n') pattern in regmatch.
943 * Return the matched value in "fields->enr".
944 */
945 static int
946qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
947{
948 if (rmp->startp[midx] == NULL)
949 return QF_FAIL;
950 fields->enr = (int)atol((char *)rmp->startp[midx]);
951 return QF_OK;
952}
953
954/*
955 * Parse the match for line number (%l') pattern in regmatch.
956 * Return the matched value in "fields->lnum".
957 */
958 static int
959qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
960{
961 if (rmp->startp[midx] == NULL)
962 return QF_FAIL;
963 fields->lnum = atol((char *)rmp->startp[midx]);
964 return QF_OK;
965}
966
967/*
968 * Parse the match for column number ('%c') pattern in regmatch.
969 * Return the matched value in "fields->col".
970 */
971 static int
972qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
973{
974 if (rmp->startp[midx] == NULL)
975 return QF_FAIL;
976 fields->col = (int)atol((char *)rmp->startp[midx]);
977 return QF_OK;
978}
979
980/*
981 * Parse the match for error type ('%t') pattern in regmatch.
982 * Return the matched value in "fields->type".
983 */
984 static int
985qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
986{
987 if (rmp->startp[midx] == NULL)
988 return QF_FAIL;
989 fields->type = *rmp->startp[midx];
990 return QF_OK;
991}
992
993/*
994 * Parse the match for '%+' format pattern. The whole matching line is included
995 * in the error string. Return the matched line in "fields->errmsg".
996 */
997 static int
998qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
999{
1000 char_u *p;
1001
1002 if (linelen >= fields->errmsglen)
1003 {
1004 /* linelen + null terminator */
1005 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1006 return QF_NOMEM;
1007 fields->errmsg = p;
1008 fields->errmsglen = linelen + 1;
1009 }
1010 vim_strncpy(fields->errmsg, linebuf, linelen);
1011 return QF_OK;
1012}
1013
1014/*
1015 * Parse the match for error message ('%m') pattern in regmatch.
1016 * Return the matched value in "fields->errmsg".
1017 */
1018 static int
1019qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1020{
1021 char_u *p;
1022 int len;
1023
1024 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1025 return QF_FAIL;
1026 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1027 if (len >= fields->errmsglen)
1028 {
1029 /* len + null terminator */
1030 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1031 return QF_NOMEM;
1032 fields->errmsg = p;
1033 fields->errmsglen = len + 1;
1034 }
1035 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1036 return QF_OK;
1037}
1038
1039/*
1040 * Parse the match for rest of a single-line file message ('%r') pattern.
1041 * Return the matched value in "tail".
1042 */
1043 static int
1044qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1045{
1046 if (rmp->startp[midx] == NULL)
1047 return QF_FAIL;
1048 *tail = rmp->startp[midx];
1049 return QF_OK;
1050}
1051
1052/*
1053 * Parse the match for the pointer line ('%p') pattern in regmatch.
1054 * Return the matched value in "fields->col".
1055 */
1056 static int
1057qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1058{
1059 char_u *match_ptr;
1060
1061 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1062 return QF_FAIL;
1063 fields->col = 0;
1064 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1065 ++match_ptr)
1066 {
1067 ++fields->col;
1068 if (*match_ptr == TAB)
1069 {
1070 fields->col += 7;
1071 fields->col -= fields->col % 8;
1072 }
1073 }
1074 ++fields->col;
1075 fields->use_viscol = TRUE;
1076 return QF_OK;
1077}
1078
1079/*
1080 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1081 * Return the matched value in "fields->col".
1082 */
1083 static int
1084qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1085{
1086 if (rmp->startp[midx] == NULL)
1087 return QF_FAIL;
1088 fields->col = (int)atol((char *)rmp->startp[midx]);
1089 fields->use_viscol = TRUE;
1090 return QF_OK;
1091}
1092
1093/*
1094 * Parse the match for the search text ('%s') pattern in regmatch.
1095 * Return the matched value in "fields->pattern".
1096 */
1097 static int
1098qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1099{
1100 int len;
1101
1102 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1103 return QF_FAIL;
1104 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1105 if (len > CMDBUFFSIZE - 5)
1106 len = CMDBUFFSIZE - 5;
1107 STRCPY(fields->pattern, "^\\V");
1108 STRNCAT(fields->pattern, rmp->startp[midx], len);
1109 fields->pattern[len + 3] = '\\';
1110 fields->pattern[len + 4] = '$';
1111 fields->pattern[len + 5] = NUL;
1112 return QF_OK;
1113}
1114
1115/*
1116 * Parse the match for the module ('%o') pattern in regmatch.
1117 * Return the matched value in "fields->module".
1118 */
1119 static int
1120qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1121{
1122 int len;
1123
1124 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1125 return QF_FAIL;
1126 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1127 if (len > CMDBUFFSIZE)
1128 len = CMDBUFFSIZE;
1129 STRNCAT(fields->module, rmp->startp[midx], len);
1130 return QF_OK;
1131}
1132
1133/*
1134 * 'errorformat' format pattern parser functions.
1135 * The '%f' and '%r' formats are parsed differently from other formats.
1136 * See qf_parse_match() for details.
1137 */
1138static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1139{
1140 NULL,
1141 qf_parse_fmt_n,
1142 qf_parse_fmt_l,
1143 qf_parse_fmt_c,
1144 qf_parse_fmt_t,
1145 qf_parse_fmt_m,
1146 NULL,
1147 qf_parse_fmt_p,
1148 qf_parse_fmt_v,
1149 qf_parse_fmt_s,
1150 qf_parse_fmt_o
1151};
1152
1153/*
1154 * Parse the error format pattern matches in "regmatch" and set the values in
1155 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1156 * match. Returns QF_OK if all the matches are successfully parsed. On
1157 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001158 */
1159 static int
1160qf_parse_match(
1161 char_u *linebuf,
1162 int linelen,
1163 efm_T *fmt_ptr,
1164 regmatch_T *regmatch,
1165 qffields_T *fields,
1166 int qf_multiline,
1167 int qf_multiscan,
1168 char_u **tail)
1169{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001170 int idx = fmt_ptr->prefix;
1171 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001172 int midx;
1173 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001174
1175 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1176 return QF_FAIL;
1177 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1178 fields->type = idx;
1179 else
1180 fields->type = 0;
1181 /*
1182 * Extract error message data from matched line.
1183 * We check for an actual submatch, because "\[" and "\]" in
1184 * the 'errorformat' may cause the wrong submatch to be used.
1185 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001186 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001187 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001188 status = QF_OK;
1189 midx = (int)fmt_ptr->addr[i];
1190 if (i == 0 && midx > 0) /* %f */
1191 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1192 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001193 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001194 if (fmt_ptr->flags == '+' && !qf_multiscan) /* %+ */
1195 status = qf_parse_fmt_plus(linebuf, linelen, fields);
1196 else if (midx > 0) /* %m */
1197 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001198 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001199 else if (i == 6 && midx > 0) /* %r */
1200 status = qf_parse_fmt_r(regmatch, midx, tail);
1201 else if (midx > 0) /* others */
1202 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001203
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001204 if (status != QF_OK)
1205 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001206 }
1207
1208 return QF_OK;
1209}
1210
1211/*
1212 * Parse an error line in 'linebuf' using a single error format string in
1213 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1214 * Returns QF_OK if the efm format matches completely and the fields are
1215 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1216 */
1217 static int
1218qf_parse_get_fields(
1219 char_u *linebuf,
1220 int linelen,
1221 efm_T *fmt_ptr,
1222 qffields_T *fields,
1223 int qf_multiline,
1224 int qf_multiscan,
1225 char_u **tail)
1226{
1227 regmatch_T regmatch;
1228 int status = QF_FAIL;
1229 int r;
1230
1231 if (qf_multiscan &&
1232 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1233 return QF_FAIL;
1234
1235 fields->namebuf[0] = NUL;
1236 fields->module[0] = NUL;
1237 fields->pattern[0] = NUL;
1238 if (!qf_multiscan)
1239 fields->errmsg[0] = NUL;
1240 fields->lnum = 0;
1241 fields->col = 0;
1242 fields->use_viscol = FALSE;
1243 fields->enr = -1;
1244 fields->type = 0;
1245 *tail = NULL;
1246
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001247 /* Always ignore case when looking for a matching error. */
1248 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001249 regmatch.regprog = fmt_ptr->prog;
1250 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1251 fmt_ptr->prog = regmatch.regprog;
1252 if (r)
1253 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1254 fields, qf_multiline, qf_multiscan, tail);
1255
1256 return status;
1257}
1258
1259/*
1260 * Parse directory error format prefixes (%D and %X).
1261 * Push and pop directories from the directory stack when scanning directory
1262 * names.
1263 */
1264 static int
1265qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1266{
1267 if (idx == 'D') /* enter directory */
1268 {
1269 if (*fields->namebuf == NUL)
1270 {
1271 EMSG(_("E379: Missing or empty directory name"));
1272 return QF_FAIL;
1273 }
1274 qfl->qf_directory =
1275 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1276 if (qfl->qf_directory == NULL)
1277 return QF_FAIL;
1278 }
1279 else if (idx == 'X') /* leave directory */
1280 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1281
1282 return QF_OK;
1283}
1284
1285/*
1286 * Parse global file name error format prefixes (%O, %P and %Q).
1287 */
1288 static int
1289qf_parse_file_pfx(
1290 int idx,
1291 qffields_T *fields,
1292 qf_list_T *qfl,
1293 char_u *tail)
1294{
1295 fields->valid = FALSE;
1296 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1297 {
1298 if (*fields->namebuf && idx == 'P')
1299 qfl->qf_currfile =
1300 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1301 else if (idx == 'Q')
1302 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1303 *fields->namebuf = NUL;
1304 if (tail && *tail)
1305 {
1306 STRMOVE(IObuff, skipwhite(tail));
1307 qfl->qf_multiscan = TRUE;
1308 return QF_MULTISCAN;
1309 }
1310 }
1311
1312 return QF_OK;
1313}
1314
1315/*
1316 * Parse a non-error line (a line which doesn't match any of the error
1317 * format in 'efm').
1318 */
1319 static int
1320qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1321{
1322 char_u *p;
1323
1324 fields->namebuf[0] = NUL; /* no match found, remove file name */
1325 fields->lnum = 0; /* don't jump to this line */
1326 fields->valid = FALSE;
1327 if (linelen >= fields->errmsglen)
1328 {
1329 /* linelen + null terminator */
1330 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1331 return QF_NOMEM;
1332 fields->errmsg = p;
1333 fields->errmsglen = linelen + 1;
1334 }
1335 /* copy whole line to error message */
1336 vim_strncpy(fields->errmsg, linebuf, linelen);
1337
1338 return QF_OK;
1339}
1340
1341/*
1342 * Parse multi-line error format prefixes (%C and %Z)
1343 */
1344 static int
1345qf_parse_multiline_pfx(
1346 qf_info_T *qi,
1347 int qf_idx,
1348 int idx,
1349 qf_list_T *qfl,
1350 qffields_T *fields)
1351{
1352 char_u *ptr;
1353 int len;
1354
1355 if (!qfl->qf_multiignore)
1356 {
1357 qfline_T *qfprev = qfl->qf_last;
1358
1359 if (qfprev == NULL)
1360 return QF_FAIL;
1361 if (*fields->errmsg && !qfl->qf_multiignore)
1362 {
1363 len = (int)STRLEN(qfprev->qf_text);
1364 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1365 == NULL)
1366 return QF_FAIL;
1367 STRCPY(ptr, qfprev->qf_text);
1368 vim_free(qfprev->qf_text);
1369 qfprev->qf_text = ptr;
1370 *(ptr += len) = '\n';
1371 STRCPY(++ptr, fields->errmsg);
1372 }
1373 if (qfprev->qf_nr == -1)
1374 qfprev->qf_nr = fields->enr;
1375 if (vim_isprintc(fields->type) && !qfprev->qf_type)
1376 /* only printable chars allowed */
1377 qfprev->qf_type = fields->type;
1378
1379 if (!qfprev->qf_lnum)
1380 qfprev->qf_lnum = fields->lnum;
1381 if (!qfprev->qf_col)
1382 qfprev->qf_col = fields->col;
1383 qfprev->qf_viscol = fields->use_viscol;
1384 if (!qfprev->qf_fnum)
1385 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1386 qfl->qf_directory,
1387 *fields->namebuf || qfl->qf_directory != NULL
1388 ? fields->namebuf
1389 : qfl->qf_currfile != NULL && fields->valid
1390 ? qfl->qf_currfile : 0);
1391 }
1392 if (idx == 'Z')
1393 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1394 line_breakcheck();
1395
1396 return QF_IGNORE_LINE;
1397}
1398
1399/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001400 * Parse a line and get the quickfix fields.
1401 * Return the QF_ status.
1402 */
1403 static int
1404qf_parse_line(
1405 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001406 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001407 char_u *linebuf,
1408 int linelen,
1409 efm_T *fmt_first,
1410 qffields_T *fields)
1411{
1412 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001413 int idx = 0;
1414 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001415 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001416 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001417
Bram Moolenaare333e792018-04-08 13:27:39 +02001418restofline:
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001419 /* If there was no %> item start at the first pattern */
1420 if (fmt_start == NULL)
1421 fmt_ptr = fmt_first;
1422 else
1423 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001424 /* Otherwise start from the last used pattern */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001425 fmt_ptr = fmt_start;
1426 fmt_start = NULL;
1427 }
1428
1429 /*
1430 * Try to match each part of 'errorformat' until we find a complete
1431 * match or no match.
1432 */
1433 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001434 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1435 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001436 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001437 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1438 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1439 if (status == QF_NOMEM)
1440 return status;
1441 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001442 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001443 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001444 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001445
1446 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1447 {
1448 if (fmt_ptr != NULL)
1449 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001450 /* 'D' and 'X' directory specifiers */
1451 status = qf_parse_dir_pfx(idx, fields, qfl);
1452 if (status != QF_OK)
1453 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001454 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001455
1456 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1457 if (status != QF_OK)
1458 return status;
1459
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001460 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001461 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001462 }
1463 else if (fmt_ptr != NULL)
1464 {
1465 /* honor %> item */
1466 if (fmt_ptr->conthere)
1467 fmt_start = fmt_ptr;
1468
1469 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1470 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001471 qfl->qf_multiline = TRUE; /* start of a multi-line message */
1472 qfl->qf_multiignore = FALSE;/* reset continuation */
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001473 }
1474 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
1475 { /* continuation of multi-line msg */
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001476 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1477 if (status != QF_OK)
1478 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001479 }
1480 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001481 { /* global file names */
1482 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1483 if (status == QF_MULTISCAN)
1484 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001485 }
1486 if (fmt_ptr->flags == '-') /* generally exclude this line */
1487 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001488 if (qfl->qf_multiline)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001489 /* also exclude continuation lines */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001490 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001491 return QF_IGNORE_LINE;
1492 }
1493 }
1494
1495 return QF_OK;
1496}
1497
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001498/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001499 * Returns TRUE if the specified quickfix/location list is empty.
1500 */
1501 static int
1502qf_list_empty(qf_info_T *qi, int qf_idx)
1503{
1504 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1505 return TRUE;
1506 return qi->qf_lists[qf_idx].qf_count <= 0;
1507}
1508
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001509/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001510 * Allocate the fields used for parsing lines and populating a quickfix list.
1511 */
1512 static int
1513qf_alloc_fields(qffields_T *pfields)
1514{
1515 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1516 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1517 pfields->errmsglen = CMDBUFFSIZE + 1;
1518 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1519 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1520 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1521 || pfields->pattern == NULL || pfields->module == NULL)
1522 return FAIL;
1523
1524 return OK;
1525}
1526
1527/*
1528 * Free the fields used for parsing lines and populating a quickfix list.
1529 */
1530 static void
1531qf_free_fields(qffields_T *pfields)
1532{
1533 vim_free(pfields->namebuf);
1534 vim_free(pfields->module);
1535 vim_free(pfields->errmsg);
1536 vim_free(pfields->pattern);
1537}
1538
1539/*
1540 * Setup the state information used for parsing lines and populating a
1541 * quickfix list.
1542 */
1543 static int
1544qf_setup_state(
1545 qfstate_T *pstate,
1546 char_u *enc,
1547 char_u *efile,
1548 typval_T *tv,
1549 buf_T *buf,
1550 linenr_T lnumfirst,
1551 linenr_T lnumlast)
1552{
1553#ifdef FEAT_MBYTE
1554 pstate->vc.vc_type = CONV_NONE;
1555 if (enc != NULL && *enc != NUL)
1556 convert_setup(&pstate->vc, enc, p_enc);
1557#endif
1558
1559 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1560 {
1561 EMSG2(_(e_openerrf), efile);
1562 return FAIL;
1563 }
1564
1565 if (tv != NULL)
1566 {
1567 if (tv->v_type == VAR_STRING)
1568 pstate->p_str = tv->vval.v_string;
1569 else if (tv->v_type == VAR_LIST)
1570 pstate->p_li = tv->vval.v_list->lv_first;
1571 pstate->tv = tv;
1572 }
1573 pstate->buf = buf;
1574 pstate->buflnum = lnumfirst;
1575 pstate->lnumlast = lnumlast;
1576
1577 return OK;
1578}
1579
1580/*
1581 * Cleanup the state information used for parsing lines and populating a
1582 * quickfix list.
1583 */
1584 static void
1585qf_cleanup_state(qfstate_T *pstate)
1586{
1587 if (pstate->fd != NULL)
1588 fclose(pstate->fd);
1589
1590 vim_free(pstate->growbuf);
1591#ifdef FEAT_MBYTE
1592 if (pstate->vc.vc_type != CONV_NONE)
1593 convert_setup(&pstate->vc, NULL, NULL);
1594#endif
1595}
1596
1597/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001598 * Read the errorfile "efile" into memory, line by line, building the error
1599 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001600 * Alternative: when "efile" is NULL read errors from buffer "buf".
1601 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001602 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001603 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1604 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001605 * Return -1 for error, number of errors for success.
1606 */
1607 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001608qf_init_ext(
1609 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001610 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001611 char_u *efile,
1612 buf_T *buf,
1613 typval_T *tv,
1614 char_u *errorformat,
1615 int newlist, /* TRUE: start a new error list */
1616 linenr_T lnumfirst, /* first line number to use */
1617 linenr_T lnumlast, /* last line number to use */
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001618 char_u *qf_title,
1619 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001620{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001621 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001622 qfstate_T state;
1623 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001624 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001625 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001626 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001628 static char_u *last_efm = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 int retval = -1; /* default: return error flag */
Bram Moolenaare0d37972016-07-15 22:36:01 +02001630 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001632 /* Do not used the cached buffer, it may have been wiped out. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001633 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001634
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001635 vim_memset(&state, 0, sizeof(state));
1636 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001637 if ((qf_alloc_fields(&fields) == FAIL) ||
1638 (qf_setup_state(&state, enc, efile, tv, buf,
1639 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 goto qf_init_end;
1641
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001642 if (newlist || qf_idx == qi->qf_listcount)
1643 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01001645 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001646 qf_idx = qi->qf_curlist;
1647 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001648 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001649 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001650 /* Adding to existing list, use last entry. */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001651 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001652 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001653 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001656 qfl = &qi->qf_lists[qf_idx];
1657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 /* Use the local value of 'errorformat' if it's set. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001659 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001660 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 else
1662 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001664 /*
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001665 * If the errorformat didn't change between calls, then reuse the
1666 * previously parsed values.
1667 */
1668 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1669 {
1670 /* free the previously parsed data */
Bram Moolenaard23a8232018-02-10 18:45:26 +01001671 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001672 free_efm_list(&fmt_first);
1673
1674 /* parse the current 'efm' */
1675 fmt_first = parse_efm_option(efm);
1676 if (fmt_first != NULL)
1677 last_efm = vim_strsave(efm);
1678 }
1679
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (fmt_first == NULL) /* nothing found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
1683 /*
1684 * got_int is reset here, because it was probably set when killing the
1685 * ":make" command, but we still want to read the errorfile then.
1686 */
1687 got_int = FALSE;
1688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 /*
1690 * Read the lines in the error file one by one.
1691 * Try to recognize one of the error formats in each line.
1692 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001693 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
Bram Moolenaare0d37972016-07-15 22:36:01 +02001695 /* Get the next line from a file/buffer/list/string */
1696 status = qf_get_nextline(&state);
1697 if (status == QF_NOMEM) /* memory alloc failure */
1698 goto qf_init_end;
1699 if (status == QF_END_OF_INPUT) /* end of input */
1700 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001702 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1703 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001704 if (status == QF_FAIL)
1705 goto error2;
1706 if (status == QF_NOMEM)
1707 goto qf_init_end;
1708 if (status == QF_IGNORE_LINE)
1709 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001711 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001712 qf_idx,
1713 qfl->qf_directory,
1714 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001715 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001716 : ((qfl->qf_currfile != NULL && fields.valid)
1717 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001718 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001719 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001720 fields.errmsg,
1721 fields.lnum,
1722 fields.col,
1723 fields.use_viscol,
1724 fields.pattern,
1725 fields.enr,
1726 fields.type,
1727 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 goto error2;
1729 line_breakcheck();
1730 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001731 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001733 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001735 /* no valid entry found */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001736 qfl->qf_ptr = qfl->qf_start;
1737 qfl->qf_index = 1;
1738 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
1740 else
1741 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001742 qfl->qf_nonevalid = FALSE;
1743 if (qfl->qf_ptr == NULL)
1744 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001746 /* return number of matches */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001747 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001748 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
1750 EMSG(_(e_readerrf));
1751error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001752 if (!adding)
1753 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001754 /* Error when creating a new list. Free the new list */
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001755 qf_free(qi, qi->qf_curlist);
1756 qi->qf_listcount--;
1757 if (qi->qf_curlist > 0)
1758 --qi->qf_curlist;
1759 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001760qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001761 if (qf_idx == qi->qf_curlist)
1762 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001763 qf_cleanup_state(&state);
1764 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
1766 return retval;
1767}
1768
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001769/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001770 * Read the errorfile "efile" into memory, line by line, building the error
1771 * list. Set the error list's title to qf_title.
1772 * Return -1 for error, number of errors for success.
1773 */
1774 int
1775qf_init(win_T *wp,
1776 char_u *efile,
1777 char_u *errorformat,
1778 int newlist, /* TRUE: start a new error list */
1779 char_u *qf_title,
1780 char_u *enc)
1781{
1782 qf_info_T *qi = &ql_info;
1783
1784 if (wp != NULL)
1785 {
1786 qi = ll_get_or_alloc_list(wp);
1787 if (qi == NULL)
1788 return FAIL;
1789 }
1790
1791 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1792 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1793}
1794
1795/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001796 * Set the title of the specified quickfix list. Frees the previous title.
1797 * Prepends ':' to the title.
1798 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001799 static void
Bram Moolenaara3921f42017-06-04 15:30:34 +02001800qf_store_title(qf_info_T *qi, int qf_idx, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001801{
Bram Moolenaard23a8232018-02-10 18:45:26 +01001802 VIM_CLEAR(qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001803
Bram Moolenaarfb604092014-07-23 15:55:00 +02001804 if (title != NULL)
1805 {
1806 char_u *p = alloc((int)STRLEN(title) + 2);
1807
Bram Moolenaara3921f42017-06-04 15:30:34 +02001808 qi->qf_lists[qf_idx].qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001809 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001810 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001811 }
1812}
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001815 * The title of a quickfix/location list is set, by default, to the command
1816 * that created the quickfix list with the ":" prefix.
1817 * Create a quickfix list title string by prepending ":" to a user command.
1818 * Returns a pointer to a static buffer with the title.
1819 */
1820 static char_u *
1821qf_cmdtitle(char_u *cmd)
1822{
1823 static char_u qftitle_str[IOSIZE];
1824
1825 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1826 return qftitle_str;
1827}
1828
1829/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001830 * Prepare for adding a new quickfix list. If the current list is in the
1831 * middle of the stack, then all the following lists are freed and then
1832 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 */
1834 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001835qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836{
1837 int i;
1838
1839 /*
Bram Moolenaarfb604092014-07-23 15:55:00 +02001840 * If the current entry is not the last entry, delete entries beyond
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 * the current entry. This makes it possible to browse in a tree-like
1842 * way with ":grep'.
1843 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001844 while (qi->qf_listcount > qi->qf_curlist + 1)
1845 qf_free(qi, --qi->qf_listcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
1847 /*
1848 * When the stack is full, remove to oldest entry
1849 * Otherwise, add a new entry.
1850 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001851 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001853 qf_free(qi, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001855 qi->qf_lists[i - 1] = qi->qf_lists[i];
1856 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001859 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaara0f299b2012-01-10 17:13:52 +01001860 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T)));
Bram Moolenaara3921f42017-06-04 15:30:34 +02001861 qf_store_title(qi, qi->qf_curlist, qf_title);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001862 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863}
1864
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001865/*
1866 * Free a location list
1867 */
1868 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001869ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001870{
1871 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001872 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001873
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001874 qi = *pqi;
1875 if (qi == NULL)
1876 return;
1877 *pqi = NULL; /* Remove reference to this list */
1878
1879 qi->qf_refcount--;
1880 if (qi->qf_refcount < 1)
1881 {
1882 /* No references to this location list */
1883 for (i = 0; i < qi->qf_listcount; ++i)
1884 qf_free(qi, i);
1885 vim_free(qi);
1886 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001887}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001888
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001889/*
1890 * Free all the quickfix/location lists in the stack.
1891 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001892 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001893qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001894{
1895 int i;
1896 qf_info_T *qi = &ql_info;
1897
1898 if (wp != NULL)
1899 {
1900 /* location list */
1901 ll_free_all(&wp->w_llist);
1902 ll_free_all(&wp->w_llist_ref);
1903 }
1904 else
1905 /* quickfix list */
1906 for (i = 0; i < qi->qf_listcount; ++i)
1907 qf_free(qi, i);
1908}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910/*
1911 * Add an entry to the end of the list of errors.
1912 * Returns OK or FAIL.
1913 */
1914 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001915qf_add_entry(
1916 qf_info_T *qi, /* quickfix list */
Bram Moolenaara3921f42017-06-04 15:30:34 +02001917 int qf_idx, /* list index */
Bram Moolenaar05540972016-01-30 20:31:25 +01001918 char_u *dir, /* optional directory name */
1919 char_u *fname, /* file name or NULL */
Bram Moolenaard76ce852018-05-01 15:02:04 +02001920 char_u *module, /* module name or NULL */
Bram Moolenaar05540972016-01-30 20:31:25 +01001921 int bufnum, /* buffer number or zero */
1922 char_u *mesg, /* message */
1923 long lnum, /* line number */
1924 int col, /* column */
1925 int vis_col, /* using visual column */
1926 char_u *pattern, /* search pattern */
1927 int nr, /* error number */
1928 int type, /* type character */
1929 int valid) /* valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001931 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001932 qfline_T **lastp; /* pointer to qf_last or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001934 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001936 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001937 {
1938 buf_T *buf = buflist_findnr(bufnum);
1939
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001940 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001941 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001942 buf->b_has_qf_entry |=
1943 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001944 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001945 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001946 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
1948 {
1949 vim_free(qfp);
1950 return FAIL;
1951 }
1952 qfp->qf_lnum = lnum;
1953 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001954 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001955 if (pattern == NULL || *pattern == NUL)
1956 qfp->qf_pattern = NULL;
1957 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
1958 {
1959 vim_free(qfp->qf_text);
1960 vim_free(qfp);
1961 return FAIL;
1962 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02001963 if (module == NULL || *module == NUL)
1964 qfp->qf_module = NULL;
1965 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
1966 {
1967 vim_free(qfp->qf_text);
1968 vim_free(qfp->qf_pattern);
1969 vim_free(qfp);
1970 return FAIL;
1971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 qfp->qf_nr = nr;
1973 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
1974 type = 0;
1975 qfp->qf_type = type;
1976 qfp->qf_valid = valid;
1977
Bram Moolenaara3921f42017-06-04 15:30:34 +02001978 lastp = &qi->qf_lists[qf_idx].qf_last;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001979 if (qf_list_empty(qi, qf_idx)) /* first element in the list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001981 qi->qf_lists[qf_idx].qf_start = qfp;
1982 qi->qf_lists[qf_idx].qf_ptr = qfp;
1983 qi->qf_lists[qf_idx].qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001984 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 }
1986 else
1987 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001988 qfp->qf_prev = *lastp;
1989 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001991 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001993 *lastp = qfp;
Bram Moolenaara3921f42017-06-04 15:30:34 +02001994 ++qi->qf_lists[qf_idx].qf_count;
1995 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001996 /* first valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02001998 qi->qf_lists[qf_idx].qf_index =
1999 qi->qf_lists[qf_idx].qf_count;
2000 qi->qf_lists[qf_idx].qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
2002
2003 return OK;
2004}
2005
2006/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002007 * Allocate a new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002008 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002009 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002010ll_new_list(void)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002011{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002012 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002013
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002014 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002015 if (qi != NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002016 qi->qf_refcount++;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002017 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002018}
2019
2020/*
2021 * Return the location list for window 'wp'.
2022 * If not present, allocate a location list
2023 */
2024 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002025ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002026{
2027 if (IS_LL_WINDOW(wp))
2028 /* For a location list window, use the referenced location list */
2029 return wp->w_llist_ref;
2030
2031 /*
2032 * For a non-location list window, w_llist_ref should not point to a
2033 * location list.
2034 */
2035 ll_free_all(&wp->w_llist_ref);
2036
2037 if (wp->w_llist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002038 wp->w_llist = ll_new_list(); /* new location list */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002039 return wp->w_llist;
2040}
2041
2042/*
2043 * Copy the location list from window "from" to window "to".
2044 */
2045 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002046copy_loclist(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002047{
2048 qf_info_T *qi;
2049 int idx;
2050 int i;
2051
2052 /*
2053 * When copying from a location list window, copy the referenced
2054 * location list. For other windows, copy the location list for
2055 * that window.
2056 */
2057 if (IS_LL_WINDOW(from))
2058 qi = from->w_llist_ref;
2059 else
2060 qi = from->w_llist;
2061
2062 if (qi == NULL) /* no location list to copy */
2063 return;
2064
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002065 /* allocate a new location list */
2066 if ((to->w_llist = ll_new_list()) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002067 return;
2068
2069 to->w_llist->qf_listcount = qi->qf_listcount;
2070
2071 /* Copy the location lists one at a time */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002072 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002073 {
2074 qf_list_T *from_qfl;
2075 qf_list_T *to_qfl;
2076
2077 to->w_llist->qf_curlist = idx;
2078
2079 from_qfl = &qi->qf_lists[idx];
2080 to_qfl = &to->w_llist->qf_lists[idx];
2081
2082 /* Some of the fields are populated by qf_add_entry() */
2083 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2084 to_qfl->qf_count = 0;
2085 to_qfl->qf_index = 0;
2086 to_qfl->qf_start = NULL;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002087 to_qfl->qf_last = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002088 to_qfl->qf_ptr = NULL;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02002089 if (from_qfl->qf_title != NULL)
2090 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2091 else
2092 to_qfl->qf_title = NULL;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002093 if (from_qfl->qf_ctx != NULL)
2094 {
2095 to_qfl->qf_ctx = alloc_tv();
2096 if (to_qfl->qf_ctx != NULL)
2097 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2098 }
2099 else
2100 to_qfl->qf_ctx = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002101
2102 if (from_qfl->qf_count)
2103 {
2104 qfline_T *from_qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002105 qfline_T *prevp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002106
2107 /* copy all the location entries in this list */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002108 for (i = 0, from_qfp = from_qfl->qf_start;
2109 i < from_qfl->qf_count && from_qfp != NULL;
2110 ++i, from_qfp = from_qfp->qf_next)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002111 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002112 if (qf_add_entry(to->w_llist,
Bram Moolenaara3921f42017-06-04 15:30:34 +02002113 to->w_llist->qf_curlist,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002114 NULL,
2115 NULL,
Bram Moolenaard76ce852018-05-01 15:02:04 +02002116 from_qfp->qf_module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002117 0,
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002118 from_qfp->qf_text,
2119 from_qfp->qf_lnum,
2120 from_qfp->qf_col,
2121 from_qfp->qf_viscol,
2122 from_qfp->qf_pattern,
2123 from_qfp->qf_nr,
2124 0,
2125 from_qfp->qf_valid) == FAIL)
2126 {
2127 qf_free_all(to);
2128 return;
2129 }
2130 /*
2131 * qf_add_entry() will not set the qf_num field, as the
2132 * directory and file names are not supplied. So the qf_fnum
2133 * field is copied here.
2134 */
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002135 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002136 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
2137 prevp->qf_type = from_qfp->qf_type; /* error type */
2138 if (from_qfl->qf_ptr == from_qfp)
2139 to_qfl->qf_ptr = prevp; /* current location */
2140 }
2141 }
2142
2143 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
2144
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002145 /* Assign a new ID for the location list */
2146 to_qfl->qf_id = ++last_qf_id;
Bram Moolenaarb254af32017-12-18 19:48:58 +01002147 to_qfl->qf_changedtick = 0L;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002148
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002149 /* When no valid entries are present in the list, qf_ptr points to
2150 * the first item in the list */
Bram Moolenaard236ac02011-05-05 17:14:14 +02002151 if (to_qfl->qf_nonevalid)
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002152 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002153 to_qfl->qf_ptr = to_qfl->qf_start;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002154 to_qfl->qf_index = 1;
2155 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002156 }
2157
2158 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
2159}
2160
2161/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002162 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002163 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 */
2165 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002166qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167{
Bram Moolenaar82404332016-07-10 17:00:38 +02002168 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002169 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002170 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002171
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 if (fname == NULL || *fname == NUL) /* no file name */
2173 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
Bram Moolenaare60acc12011-05-10 16:41:25 +02002175#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002176 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002177#endif
2178#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002179 if (directory != NULL)
2180 slash_adjust(directory);
2181 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002182#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002183 if (directory != NULL && !vim_isAbsName(fname)
2184 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2185 {
2186 /*
2187 * Here we check if the file really exists.
2188 * This should normally be true, but if make works without
2189 * "leaving directory"-messages we might have missed a
2190 * directory change.
2191 */
2192 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 vim_free(ptr);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002195 directory = qf_guess_filepath(qi, qf_idx, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002196 if (directory)
2197 ptr = concat_fnames(directory, fname, TRUE);
2198 else
2199 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002201 /* Use concatenated directory name and file name */
Bram Moolenaar82404332016-07-10 17:00:38 +02002202 bufname = ptr;
2203 }
2204 else
2205 bufname = fname;
2206
2207 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002208 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002209 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002210 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002211 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002213 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002214 {
2215 vim_free(qf_last_bufname);
2216 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2217 if (bufname == ptr)
2218 qf_last_bufname = bufname;
2219 else
2220 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002221 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002222 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002223 if (buf == NULL)
2224 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002225
Bram Moolenaarc1542742016-07-20 21:44:37 +02002226 buf->b_has_qf_entry =
2227 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002228 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229}
2230
2231/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002232 * Push dirbuf onto the directory stack and return pointer to actual dir or
2233 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 */
2235 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002236qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237{
2238 struct dir_stack_T *ds_new;
2239 struct dir_stack_T *ds_ptr;
2240
2241 /* allocate new stack element and hook it in */
2242 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2243 if (ds_new == NULL)
2244 return NULL;
2245
2246 ds_new->next = *stackptr;
2247 *stackptr = ds_new;
2248
2249 /* store directory on the stack */
2250 if (vim_isAbsName(dirbuf)
2251 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002252 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 (*stackptr)->dirname = vim_strsave(dirbuf);
2254 else
2255 {
2256 /* Okay we don't have an absolute path.
2257 * dirbuf must be a subdir of one of the directories on the stack.
2258 * Let's search...
2259 */
2260 ds_new = (*stackptr)->next;
2261 (*stackptr)->dirname = NULL;
2262 while (ds_new)
2263 {
2264 vim_free((*stackptr)->dirname);
2265 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2266 TRUE);
2267 if (mch_isdir((*stackptr)->dirname) == TRUE)
2268 break;
2269
2270 ds_new = ds_new->next;
2271 }
2272
2273 /* clean up all dirs we already left */
2274 while ((*stackptr)->next != ds_new)
2275 {
2276 ds_ptr = (*stackptr)->next;
2277 (*stackptr)->next = (*stackptr)->next->next;
2278 vim_free(ds_ptr->dirname);
2279 vim_free(ds_ptr);
2280 }
2281
2282 /* Nothing found -> it must be on top level */
2283 if (ds_new == NULL)
2284 {
2285 vim_free((*stackptr)->dirname);
2286 (*stackptr)->dirname = vim_strsave(dirbuf);
2287 }
2288 }
2289
2290 if ((*stackptr)->dirname != NULL)
2291 return (*stackptr)->dirname;
2292 else
2293 {
2294 ds_ptr = *stackptr;
2295 *stackptr = (*stackptr)->next;
2296 vim_free(ds_ptr);
2297 return NULL;
2298 }
2299}
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301/*
2302 * pop dirbuf from the directory stack and return previous directory or NULL if
2303 * stack is empty
2304 */
2305 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002306qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
2308 struct dir_stack_T *ds_ptr;
2309
2310 /* TODO: Should we check if dirbuf is the directory on top of the stack?
2311 * What to do if it isn't? */
2312
2313 /* pop top element and free it */
2314 if (*stackptr != NULL)
2315 {
2316 ds_ptr = *stackptr;
2317 *stackptr = (*stackptr)->next;
2318 vim_free(ds_ptr->dirname);
2319 vim_free(ds_ptr);
2320 }
2321
2322 /* return NEW top element as current dir or NULL if stack is empty*/
2323 return *stackptr ? (*stackptr)->dirname : NULL;
2324}
2325
2326/*
2327 * clean up directory stack
2328 */
2329 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002330qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331{
2332 struct dir_stack_T *ds_ptr;
2333
2334 while ((ds_ptr = *stackptr) != NULL)
2335 {
2336 *stackptr = (*stackptr)->next;
2337 vim_free(ds_ptr->dirname);
2338 vim_free(ds_ptr);
2339 }
2340}
2341
2342/*
2343 * Check in which directory of the directory stack the given file can be
2344 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002345 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 * Cleans up intermediate directory entries.
2347 *
2348 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002349 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 * ./
2351 * ./aa
2352 * ./aa/bb
2353 * ./bb
2354 * ./bb/x.c
2355 * and make says:
2356 * making all in aa
2357 * making all in bb
2358 * x.c:9: Error
2359 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2360 * qf_guess_filepath will return NULL.
2361 */
2362 static char_u *
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002363qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364{
2365 struct dir_stack_T *ds_ptr;
2366 struct dir_stack_T *ds_tmp;
2367 char_u *fullname;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002368 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 /* no dirs on the stack - there's nothing we can do */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002371 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 return NULL;
2373
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002374 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 fullname = NULL;
2376 while (ds_ptr)
2377 {
2378 vim_free(fullname);
2379 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2380
2381 /* If concat_fnames failed, just go on. The worst thing that can happen
2382 * is that we delete the entire stack.
2383 */
2384 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2385 break;
2386
2387 ds_ptr = ds_ptr->next;
2388 }
2389
2390 vim_free(fullname);
2391
2392 /* clean up all dirs we already left */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002393 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002395 ds_tmp = qfl->qf_dir_stack->next;
2396 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 vim_free(ds_tmp->dirname);
2398 vim_free(ds_tmp);
2399 }
2400
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002401 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402}
2403
2404/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002405 * Returns TRUE if a quickfix/location list with the given identifier exists.
2406 */
2407 static int
2408qflist_valid (win_T *wp, int_u qf_id)
2409{
2410 qf_info_T *qi = &ql_info;
2411 int i;
2412
2413 if (wp != NULL)
2414 {
2415 qi = GET_LOC_LIST(wp); /* Location list */
2416 if (qi == NULL)
2417 return FALSE;
2418 }
2419
2420 for (i = 0; i < qi->qf_listcount; ++i)
2421 if (qi->qf_lists[i].qf_id == qf_id)
2422 return TRUE;
2423
2424 return FALSE;
2425}
2426
2427/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002428 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002429 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002430 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002431 * Similar to location list.
2432 */
2433 static int
2434is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr)
2435{
2436 qf_list_T *qfl;
2437 qfline_T *qfp;
2438 int i;
2439
2440 qfl = &qi->qf_lists[qi->qf_curlist];
2441
2442 /* Search for the entry in the current list */
2443 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2444 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002445 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002446 break;
2447
2448 if (i == qfl->qf_count) /* Entry is not found */
2449 return FALSE;
2450
2451 return TRUE;
2452}
2453
2454/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002455 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002456 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002457 */
2458 static qfline_T *
2459get_next_valid_entry(
2460 qf_info_T *qi,
2461 qfline_T *qf_ptr,
2462 int *qf_index,
2463 int dir)
2464{
2465 int idx;
2466 int old_qf_fnum;
2467
2468 idx = *qf_index;
2469 old_qf_fnum = qf_ptr->qf_fnum;
2470
2471 do
2472 {
2473 if (idx == qi->qf_lists[qi->qf_curlist].qf_count
2474 || qf_ptr->qf_next == NULL)
2475 return NULL;
2476 ++idx;
2477 qf_ptr = qf_ptr->qf_next;
2478 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2479 && !qf_ptr->qf_valid)
2480 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2481
2482 *qf_index = idx;
2483 return qf_ptr;
2484}
2485
2486/*
2487 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002488 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002489 */
2490 static qfline_T *
2491get_prev_valid_entry(
2492 qf_info_T *qi,
2493 qfline_T *qf_ptr,
2494 int *qf_index,
2495 int dir)
2496{
2497 int idx;
2498 int old_qf_fnum;
2499
2500 idx = *qf_index;
2501 old_qf_fnum = qf_ptr->qf_fnum;
2502
2503 do
2504 {
2505 if (idx == 1 || qf_ptr->qf_prev == NULL)
2506 return NULL;
2507 --idx;
2508 qf_ptr = qf_ptr->qf_prev;
2509 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
2510 && !qf_ptr->qf_valid)
2511 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2512
2513 *qf_index = idx;
2514 return qf_ptr;
2515}
2516
2517/*
2518 * Get the n'th (errornr) previous/next valid entry from the current entry in
2519 * the quickfix list.
2520 * dir == FORWARD or FORWARD_FILE: next valid entry
2521 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2522 */
2523 static qfline_T *
2524get_nth_valid_entry(
2525 qf_info_T *qi,
2526 int errornr,
2527 qfline_T *qf_ptr,
2528 int *qf_index,
2529 int dir)
2530{
2531 qfline_T *prev_qf_ptr;
2532 int prev_index;
2533 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2534 char_u *err = e_no_more_items;
2535
2536 while (errornr--)
2537 {
2538 prev_qf_ptr = qf_ptr;
2539 prev_index = *qf_index;
2540
2541 if (dir == FORWARD || dir == FORWARD_FILE)
2542 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir);
2543 else
2544 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir);
2545 if (qf_ptr == NULL)
2546 {
2547 qf_ptr = prev_qf_ptr;
2548 *qf_index = prev_index;
2549 if (err != NULL)
2550 {
2551 EMSG(_(err));
2552 return NULL;
2553 }
2554 break;
2555 }
2556
2557 err = NULL;
2558 }
2559
2560 return qf_ptr;
2561}
2562
2563/*
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002564 * Get n'th (errornr) quickfix entry
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002565 */
2566 static qfline_T *
2567get_nth_entry(
2568 qf_info_T *qi,
2569 int errornr,
2570 qfline_T *qf_ptr,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002571 int *cur_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002572{
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002573 int qf_idx = *cur_qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002574
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002575 /* New error number is less than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002576 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2577 {
2578 --qf_idx;
2579 qf_ptr = qf_ptr->qf_prev;
2580 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002581 /* New error number is greater than the current error number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002582 while (errornr > qf_idx &&
2583 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count &&
2584 qf_ptr->qf_next != NULL)
2585 {
2586 ++qf_idx;
2587 qf_ptr = qf_ptr->qf_next;
2588 }
2589
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002590 *cur_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002591 return qf_ptr;
2592}
2593
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002594/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002595 * Find a window displaying a Vim help file.
2596 */
2597 static win_T *
2598qf_find_help_win(void)
2599{
2600 win_T *wp;
2601
2602 FOR_ALL_WINDOWS(wp)
2603 if (bt_help(wp->w_buffer))
2604 return wp;
2605
2606 return NULL;
2607}
2608
2609/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002610 * Find a help window or open one.
2611 */
2612 static int
2613jump_to_help_window(qf_info_T *qi, int *opened_window)
2614{
2615 win_T *wp;
2616 int flags;
2617
2618 if (cmdmod.tab != 0)
2619 wp = NULL;
2620 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002621 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002622 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2623 win_enter(wp, TRUE);
2624 else
2625 {
2626 /*
2627 * Split off help window; put it at far top if no position
2628 * specified, the current window is vertically split and narrow.
2629 */
2630 flags = WSP_HELP;
2631 if (cmdmod.split == 0 && curwin->w_width != Columns
2632 && curwin->w_width < 80)
2633 flags |= WSP_TOP;
2634 if (qi != &ql_info)
2635 flags |= WSP_NEWLOC; /* don't copy the location list */
2636
2637 if (win_split(0, flags) == FAIL)
2638 return FAIL;
2639
2640 *opened_window = TRUE;
2641
2642 if (curwin->w_height < p_hh)
2643 win_setheight((int)p_hh);
2644
2645 if (qi != &ql_info) /* not a quickfix list */
2646 {
2647 /* The new window should use the supplied location list */
2648 curwin->w_llist = qi;
2649 qi->qf_refcount++;
2650 }
2651 }
2652
2653 if (!p_im)
2654 restart_edit = 0; /* don't want insert mode in help file */
2655
2656 return OK;
2657}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002658
2659/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002660 * Find a non-quickfix window using the given location list.
2661 * Returns NULL if a matching window is not found.
2662 */
2663 static win_T *
2664qf_find_win_with_loclist(qf_info_T *ll)
2665{
2666 win_T *wp;
2667
2668 FOR_ALL_WINDOWS(wp)
2669 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2670 return wp;
2671
2672 return NULL;
2673}
2674
2675/*
2676 * Find a window containing a normal buffer
2677 */
2678 static win_T *
2679qf_find_win_with_normal_buf(void)
2680{
2681 win_T *wp;
2682
2683 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002684 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002685 return wp;
2686
2687 return NULL;
2688}
2689
2690/*
2691 * Go to a window in any tabpage containing the specified file. Returns TRUE
2692 * if successfully jumped to the window. Otherwise returns FALSE.
2693 */
2694 static int
2695qf_goto_tabwin_with_file(int fnum)
2696{
2697 tabpage_T *tp;
2698 win_T *wp;
2699
2700 FOR_ALL_TAB_WINDOWS(tp, wp)
2701 if (wp->w_buffer->b_fnum == fnum)
2702 {
2703 goto_tabpage_win(tp, wp);
2704 return TRUE;
2705 }
2706
2707 return FALSE;
2708}
2709
2710/*
2711 * Create a new window to show a file above the quickfix window. Called when
2712 * only the quickfix window is present.
2713 */
2714 static int
2715qf_open_new_file_win(qf_info_T *ll_ref)
2716{
2717 int flags;
2718
2719 flags = WSP_ABOVE;
2720 if (ll_ref != NULL)
2721 flags |= WSP_NEWLOC;
2722 if (win_split(0, flags) == FAIL)
2723 return FAIL; /* not enough room for window */
2724 p_swb = empty_option; /* don't split again */
2725 swb_flags = 0;
2726 RESET_BINDING(curwin);
2727 if (ll_ref != NULL)
2728 {
2729 /* The new window should use the location list from the
2730 * location list window */
2731 curwin->w_llist = ll_ref;
2732 ll_ref->qf_refcount++;
2733 }
2734 return OK;
2735}
2736
2737/*
2738 * Go to a window that shows the right buffer. If the window is not found, go
2739 * to the window just above the location list window. This is used for opening
2740 * a file from a location window and not from a quickfix window. If some usable
2741 * window is previously found, then it is supplied in 'use_win'.
2742 */
2743 static void
2744qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2745{
2746 win_T *win = use_win;
2747
2748 if (win == NULL)
2749 {
2750 /* Find the window showing the selected file */
2751 FOR_ALL_WINDOWS(win)
2752 if (win->w_buffer->b_fnum == qf_fnum)
2753 break;
2754 if (win == NULL)
2755 {
2756 /* Find a previous usable window */
2757 win = curwin;
2758 do
2759 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002760 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002761 break;
2762 if (win->w_prev == NULL)
2763 win = lastwin; /* wrap around the top */
2764 else
2765 win = win->w_prev; /* go to previous window */
2766 } while (win != curwin);
2767 }
2768 }
2769 win_goto(win);
2770
2771 /* If the location list for the window is not set, then set it
2772 * to the location list from the location window */
2773 if (win->w_llist == NULL)
2774 {
2775 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002776 if (ll_ref != NULL)
2777 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002778 }
2779}
2780
2781/*
2782 * Go to a window that shows the specified file. If a window is not found, go
2783 * to the window just above the quickfix window. This is used for opening a
2784 * file from a quickfix window and not from a location window.
2785 */
2786 static void
2787qf_goto_win_with_qfl_file(int qf_fnum)
2788{
2789 win_T *win;
2790 win_T *altwin;
2791
2792 win = curwin;
2793 altwin = NULL;
2794 for (;;)
2795 {
2796 if (win->w_buffer->b_fnum == qf_fnum)
2797 break;
2798 if (win->w_prev == NULL)
2799 win = lastwin; /* wrap around the top */
2800 else
2801 win = win->w_prev; /* go to previous window */
2802
2803 if (IS_QF_WINDOW(win))
2804 {
2805 /* Didn't find it, go to the window before the quickfix
2806 * window. */
2807 if (altwin != NULL)
2808 win = altwin;
2809 else if (curwin->w_prev != NULL)
2810 win = curwin->w_prev;
2811 else
2812 win = curwin->w_next;
2813 break;
2814 }
2815
2816 /* Remember a usable window. */
Bram Moolenaar91335e52018-08-01 17:53:12 +02002817 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002818 altwin = win;
2819 }
2820
2821 win_goto(win);
2822}
2823
2824/*
2825 * Find a suitable window for opening a file (qf_fnum) from the
2826 * quickfix/location list and jump to it. If the file is already opened in a
2827 * window, jump to it. Otherwise open a new window to display the file. This is
2828 * called from either a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002829 */
2830 static int
2831qf_jump_to_usable_window(int qf_fnum, int *opened_window)
2832{
2833 win_T *usable_win_ptr = NULL;
2834 int usable_win;
2835 qf_info_T *ll_ref;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002836 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002837
2838 usable_win = 0;
2839
2840 ll_ref = curwin->w_llist_ref;
2841 if (ll_ref != NULL)
2842 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002843 /* Find a non-quickfix window with this location list */
2844 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2845 if (usable_win_ptr != NULL)
2846 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002847 }
2848
2849 if (!usable_win)
2850 {
2851 /* Locate a window showing a normal buffer */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002852 win = qf_find_win_with_normal_buf();
2853 if (win != NULL)
2854 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002855 }
2856
2857 /*
2858 * If no usable window is found and 'switchbuf' contains "usetab"
2859 * then search in other tabs.
2860 */
2861 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002862 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002863
2864 /*
2865 * If there is only one window and it is the quickfix window, create a
2866 * new one above the quickfix window.
2867 */
2868 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win)
2869 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002870 if (qf_open_new_file_win(ll_ref) != OK)
2871 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002872 *opened_window = TRUE; /* close it when fail */
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002873 }
2874 else
2875 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002876 if (curwin->w_llist_ref != NULL) /* In a location window */
2877 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
2878 else /* In a quickfix window */
2879 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002880 }
2881
2882 return OK;
2883}
2884
2885/*
2886 * Edit the selected file or help file.
2887 */
2888 static int
2889qf_jump_edit_buffer(
2890 qf_info_T *qi,
2891 qfline_T *qf_ptr,
2892 int forceit,
2893 win_T *oldwin,
2894 int *opened_window,
2895 int *abort)
2896{
2897 int retval = OK;
2898
2899 if (qf_ptr->qf_type == 1)
2900 {
2901 /* Open help file (do_ecmd() will set b_help flag, readfile() will
2902 * set b_p_ro flag). */
2903 if (!can_abandon(curbuf, forceit))
2904 {
2905 no_write_message();
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002906 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002907 }
2908 else
2909 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
2910 ECMD_HIDE + ECMD_SET_HELP,
2911 oldwin == curwin ? curwin : NULL);
2912 }
2913 else
2914 {
2915 int old_qf_curlist = qi->qf_curlist;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002916 int save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002917
2918 retval = buflist_getfile(qf_ptr->qf_fnum,
2919 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01002920
2921 if (qi != &ql_info)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002922 {
Bram Moolenaar3c097222017-12-21 20:54:49 +01002923 /*
2924 * Location list. Check whether the associated window is still
2925 * present and the list is still valid.
2926 */
2927 if (!win_valid_any_tab(oldwin))
2928 {
2929 EMSG(_("E924: Current window was closed"));
2930 *abort = TRUE;
2931 *opened_window = FALSE;
2932 }
2933 else if (!qflist_valid(oldwin, save_qfid))
2934 {
2935 EMSG(_(e_loc_list_changed));
2936 *abort = TRUE;
2937 }
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002938 }
2939 else if (old_qf_curlist != qi->qf_curlist
2940 || !is_qf_entry_present(qi, qf_ptr))
2941 {
2942 if (qi == &ql_info)
2943 EMSG(_("E925: Current quickfix was changed"));
2944 else
Bram Moolenaar3c097222017-12-21 20:54:49 +01002945 EMSG(_(e_loc_list_changed));
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002946 *abort = TRUE;
2947 }
2948
2949 if (*abort)
Bram Moolenaar29ce4092018-04-28 21:56:44 +02002950 retval = FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002951 }
2952
2953 return retval;
2954}
2955
2956/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002957 * Go to the error line in the current file using either line/column number or
2958 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002959 */
2960 static void
2961qf_jump_goto_line(
2962 linenr_T qf_lnum,
2963 int qf_col,
2964 char_u qf_viscol,
2965 char_u *qf_pattern)
2966{
2967 linenr_T i;
2968 char_u *line;
2969 colnr_T screen_col;
2970 colnr_T char_col;
2971
2972 if (qf_pattern == NULL)
2973 {
2974 /*
2975 * Go to line with error, unless qf_lnum is 0.
2976 */
2977 i = qf_lnum;
2978 if (i > 0)
2979 {
2980 if (i > curbuf->b_ml.ml_line_count)
2981 i = curbuf->b_ml.ml_line_count;
2982 curwin->w_cursor.lnum = i;
2983 }
2984 if (qf_col > 0)
2985 {
2986 curwin->w_cursor.col = qf_col - 1;
2987#ifdef FEAT_VIRTUALEDIT
2988 curwin->w_cursor.coladd = 0;
2989#endif
2990 if (qf_viscol == TRUE)
2991 {
2992 /*
2993 * Check each character from the beginning of the error
2994 * line up to the error column. For each tab character
2995 * found, reduce the error column value by the length of
2996 * a tab character.
2997 */
2998 line = ml_get_curline();
2999 screen_col = 0;
3000 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3001 {
3002 if (*line == NUL)
3003 break;
3004 if (*line++ == '\t')
3005 {
3006 curwin->w_cursor.col -= 7 - (screen_col % 8);
3007 screen_col += 8 - (screen_col % 8);
3008 }
3009 else
3010 ++screen_col;
3011 }
3012 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003013 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003014 check_cursor();
3015 }
3016 else
3017 beginline(BL_WHITE | BL_FIX);
3018 }
3019 else
3020 {
3021 pos_T save_cursor;
3022
3023 /* Move the cursor to the first line in the buffer */
3024 save_cursor = curwin->w_cursor;
3025 curwin->w_cursor.lnum = 0;
3026 if (!do_search(NULL, '/', qf_pattern, (long)1,
3027 SEARCH_KEEP, NULL, NULL))
3028 curwin->w_cursor = save_cursor;
3029 }
3030}
3031
3032/*
3033 * Display quickfix list index and size message
3034 */
3035 static void
3036qf_jump_print_msg(
3037 qf_info_T *qi,
3038 int qf_index,
3039 qfline_T *qf_ptr,
3040 buf_T *old_curbuf,
3041 linenr_T old_lnum)
3042{
3043 linenr_T i;
3044 int len;
3045
3046 /* Update the screen before showing the message, unless the screen
3047 * scrolled up. */
3048 if (!msg_scrolled)
3049 update_topline_redraw();
3050 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3051 qi->qf_lists[qi->qf_curlist].qf_count,
3052 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3053 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
3054 /* Add the message, skipping leading whitespace and newlines. */
3055 len = (int)STRLEN(IObuff);
3056 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3057
3058 /* Output the message. Overwrite to avoid scrolling when the 'O'
3059 * flag is present in 'shortmess'; But when not jumping, print the
3060 * whole message. */
3061 i = msg_scroll;
3062 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3063 msg_scroll = TRUE;
3064 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3065 msg_scroll = FALSE;
3066 msg_attr_keep(IObuff, 0, TRUE);
3067 msg_scroll = i;
3068}
3069
3070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 * jump to a quickfix line
3072 * if dir == FORWARD go "errornr" valid entries forward
3073 * if dir == BACKWARD go "errornr" valid entries backward
3074 * if dir == FORWARD_FILE go "errornr" valid entries files backward
3075 * if dir == BACKWARD_FILE go "errornr" valid entries files backward
3076 * else if "errornr" is zero, redisplay the same line
3077 * else go to entry "errornr"
3078 */
3079 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003080qf_jump(qf_info_T *qi,
3081 int dir,
3082 int errornr,
3083 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003085 qfline_T *qf_ptr;
3086 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 int old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 buf_T *old_curbuf;
3090 linenr_T old_lnum;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003091 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003092 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003094 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096#ifdef FEAT_FOLDING
3097 int old_KeyTyped = KeyTyped; /* getting file may reset it */
3098#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003099 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003101 if (qi == NULL)
3102 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003103
3104 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003105 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 {
3107 EMSG(_(e_quickfix));
3108 return;
3109 }
3110
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003111 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 old_qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003113 qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 old_qf_index = qf_index;
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02003115 if (dir != 0) /* next/prev valid entry */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003117 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir);
3118 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003120 qf_ptr = old_qf_ptr;
3121 qf_index = old_qf_index;
3122 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 }
3124 }
3125 else if (errornr != 0) /* go to specified number */
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003126 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003128 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
3129 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 /* No need to print the error message if it's visible in the error
3131 * window */
3132 print_message = FALSE;
3133
3134 /*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003135 * For ":helpgrep" find a help window or open one.
3136 */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003137 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003138 if (jump_to_help_window(qi, &opened_window) == FAIL)
3139 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003140
3141 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * If currently in the quickfix window, find another window to show the
3143 * file in.
3144 */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00003145 if (bt_quickfix(curbuf) && !opened_window)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 {
3147 /*
3148 * If there is no file specified, we don't know where to go.
3149 * But do advance, otherwise ":cn" gets stuck.
3150 */
3151 if (qf_ptr->qf_fnum == 0)
3152 goto theend;
3153
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003154 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL)
3155 goto failed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
3158 /*
3159 * If there is a file name,
3160 * read the wanted file if needed, and check autowrite etc.
3161 */
3162 old_curbuf = curbuf;
3163 old_lnum = curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003164
3165 if (qf_ptr->qf_fnum != 0)
3166 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003167 int abort = FALSE;
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003168
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003169 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3170 &opened_window, &abort);
3171 if (abort)
3172 {
3173 qi = NULL;
3174 qf_ptr = NULL;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003175 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003176 }
3177
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003178 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
3180 /* When not switched to another buffer, still need to set pc mark */
3181 if (curbuf == old_curbuf)
3182 setpcmark();
3183
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003184 if (qf_ptr != NULL)
3185 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col,
3186 qf_ptr->qf_viscol, qf_ptr->qf_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188#ifdef FEAT_FOLDING
3189 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
3190 foldOpenCursor();
3191#endif
3192 if (print_message)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003193 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 }
3195 else
3196 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 if (opened_window)
3198 win_close(curwin, TRUE); /* Close opened window */
Bram Moolenaar0899d692016-03-19 13:35:03 +01003199 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 {
3201 /*
3202 * Couldn't open file, so put index back where it was. This could
3203 * happen if the file was readonly and we changed something.
3204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 qf_ptr = old_qf_ptr;
3207 qf_index = old_qf_index;
3208 }
3209 }
3210theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003211 if (qi != NULL)
3212 {
3213 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
3214 qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
3215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 if (p_swb != old_swb && opened_window)
3217 {
3218 /* Restore old 'switchbuf' value, but not when an autocommand or
3219 * modeline has changed the value. */
3220 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003223 swb_flags = old_swb_flags;
3224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 else
3226 free_string_option(old_swb);
3227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228}
3229
3230/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003231 * Highlight attributes used for displaying entries from the quickfix list.
3232 */
3233static int qfFileAttr;
3234static int qfSepAttr;
3235static int qfLineAttr;
3236
3237/*
3238 * Display information about a single entry from the quickfix/location list.
3239 * Used by ":clist/:llist" commands.
3240 */
3241 static void
3242qf_list_entry(qf_info_T *qi, qfline_T *qfp, int qf_idx)
3243{
3244 char_u *fname;
3245 buf_T *buf;
3246 int filter_entry;
3247
3248 fname = NULL;
3249 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3250 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3251 (char *)qfp->qf_module);
3252 else {
3253 if (qfp->qf_fnum != 0
3254 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3255 {
3256 fname = buf->b_fname;
3257 if (qfp->qf_type == 1) /* :helpgrep */
3258 fname = gettail(fname);
3259 }
3260 if (fname == NULL)
3261 sprintf((char *)IObuff, "%2d", qf_idx);
3262 else
3263 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3264 qf_idx, (char *)fname);
3265 }
3266
3267 // Support for filtering entries using :filter /pat/ clist
3268 // Match against the module name, file name, search pattern and
3269 // text of the entry.
3270 filter_entry = TRUE;
3271 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3272 filter_entry &= message_filtered(qfp->qf_module);
3273 if (filter_entry && fname != NULL)
3274 filter_entry &= message_filtered(fname);
3275 if (filter_entry && qfp->qf_pattern != NULL)
3276 filter_entry &= message_filtered(qfp->qf_pattern);
3277 if (filter_entry)
3278 filter_entry &= message_filtered(qfp->qf_text);
3279 if (filter_entry)
3280 return;
3281
3282 msg_putchar('\n');
3283 msg_outtrans_attr(IObuff, qf_idx == qi->qf_lists[qi->qf_curlist].qf_index
3284 ? HL_ATTR(HLF_QFL) : qfFileAttr);
3285
3286 if (qfp->qf_lnum != 0)
3287 msg_puts_attr((char_u *)":", qfSepAttr);
3288 if (qfp->qf_lnum == 0)
3289 IObuff[0] = NUL;
3290 else if (qfp->qf_col == 0)
3291 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3292 else
3293 sprintf((char *)IObuff, "%ld col %d",
3294 qfp->qf_lnum, qfp->qf_col);
3295 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3296 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3297 msg_puts_attr(IObuff, qfLineAttr);
3298 msg_puts_attr((char_u *)":", qfSepAttr);
3299 if (qfp->qf_pattern != NULL)
3300 {
3301 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3302 msg_puts(IObuff);
3303 msg_puts_attr((char_u *)":", qfSepAttr);
3304 }
3305 msg_puts((char_u *)" ");
3306
3307 /* Remove newlines and leading whitespace from the text. For an
3308 * unrecognized line keep the indent, the compiler may mark a word
3309 * with ^^^^. */
3310 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3311 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3312 IObuff, IOSIZE);
3313 msg_prt_line(IObuff, FALSE);
3314 out_flush(); /* show one line at a time */
3315}
3316
3317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003319 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 */
3321 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003322qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003324 qfline_T *qfp;
3325 int i;
3326 int idx1 = 1;
3327 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003328 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003329 int plus = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003330 int all = eap->forceit; /* if not :cl!, only show
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 recognised errors */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003332 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaar39665952018-08-15 20:59:48 +02003334 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003335 {
3336 qi = GET_LOC_LIST(curwin);
3337 if (qi == NULL)
3338 {
3339 EMSG(_(e_loclist));
3340 return;
3341 }
3342 }
3343
3344 if (qi->qf_curlist >= qi->qf_listcount
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003345 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
3347 EMSG(_(e_quickfix));
3348 return;
3349 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003350 if (*arg == '+')
3351 {
3352 ++arg;
3353 plus = TRUE;
3354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3356 {
3357 EMSG(_(e_trailing));
3358 return;
3359 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003360 if (plus)
3361 {
3362 i = qi->qf_lists[qi->qf_curlist].qf_index;
3363 idx2 = i + idx1;
3364 idx1 = i;
3365 }
3366 else
3367 {
3368 i = qi->qf_lists[qi->qf_curlist].qf_count;
3369 if (idx1 < 0)
3370 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3371 if (idx2 < 0)
3372 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
Bram Moolenaara796d462018-05-01 14:30:36 +02003375 /* Shorten all the file names, so that it is easy to read */
3376 shorten_fnames(FALSE);
3377
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003378 /*
3379 * Get the attributes for the different quickfix highlight items. Note
3380 * that this depends on syntax items defined in the qf.vim syntax file
3381 */
3382 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3383 if (qfFileAttr == 0)
3384 qfFileAttr = HL_ATTR(HLF_D);
3385 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3386 if (qfSepAttr == 0)
3387 qfSepAttr = HL_ATTR(HLF_D);
3388 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3389 if (qfLineAttr == 0)
3390 qfLineAttr = HL_ATTR(HLF_N);
3391
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003392 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 all = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003394 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
3395 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
3397 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3398 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003399 if (got_int)
3400 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003401
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003402 qf_list_entry(qi, qfp, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003404
3405 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003406 if (qfp == NULL)
3407 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003408 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 ui_breakcheck();
3410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411}
3412
3413/*
3414 * Remove newlines and leading whitespace from an error message.
3415 * Put the result in "buf[bufsize]".
3416 */
3417 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003418qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
3420 int i;
3421 char_u *p = text;
3422
3423 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3424 {
3425 if (*p == '\n')
3426 {
3427 buf[i] = ' ';
3428 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003429 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 break;
3431 }
3432 else
3433 buf[i] = *p++;
3434 }
3435 buf[i] = NUL;
3436}
3437
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003438/*
3439 * Display information (list number, list size and the title) about a
3440 * quickfix/location list.
3441 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003442 static void
3443qf_msg(qf_info_T *qi, int which, char *lead)
3444{
3445 char *title = (char *)qi->qf_lists[which].qf_title;
3446 int count = qi->qf_lists[which].qf_count;
3447 char_u buf[IOSIZE];
3448
3449 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3450 lead,
3451 which + 1,
3452 qi->qf_listcount,
3453 count);
3454
3455 if (title != NULL)
3456 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003457 size_t len = STRLEN(buf);
3458
3459 if (len < 34)
3460 {
3461 vim_memset(buf + len, ' ', 34 - len);
3462 buf[34] = NUL;
3463 }
3464 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003465 }
3466 trunc_string(buf, buf, Columns - 1, IOSIZE);
3467 msg(buf);
3468}
3469
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470/*
3471 * ":colder [count]": Up in the quickfix stack.
3472 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003473 * ":lolder [count]": Up in the location list stack.
3474 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 */
3476 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003477qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003479 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 int count;
3481
Bram Moolenaar39665952018-08-15 20:59:48 +02003482 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003483 {
3484 qi = GET_LOC_LIST(curwin);
3485 if (qi == NULL)
3486 {
3487 EMSG(_(e_loclist));
3488 return;
3489 }
3490 }
3491
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 if (eap->addr_count != 0)
3493 count = eap->line2;
3494 else
3495 count = 1;
3496 while (count--)
3497 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003498 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003500 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
3502 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003503 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003505 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
3507 else
3508 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003509 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 {
3511 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003512 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003514 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 }
3516 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003517 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003518 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519}
3520
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003521/*
3522 * Display the information about all the quickfix/location lists in the stack
3523 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003524 void
3525qf_history(exarg_T *eap)
3526{
3527 qf_info_T *qi = &ql_info;
3528 int i;
3529
Bram Moolenaar39665952018-08-15 20:59:48 +02003530 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003531 qi = GET_LOC_LIST(curwin);
3532 if (qi == NULL || (qi->qf_listcount == 0
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003533 && qf_list_empty(qi, qi->qf_curlist)))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003534 MSG(_("No entries"));
3535 else
3536 for (i = 0; i < qi->qf_listcount; ++i)
3537 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3538}
3539
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003541 * Free all the entries in the error list "idx". Note that other information
3542 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 */
3544 static void
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003545qf_free_items(qf_info_T *qi, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003547 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003548 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003549 int stop = FALSE;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003550 qf_list_T *qfl = &qi->qf_lists[idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003552 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003554 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003555 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003556 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003557 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003558 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003559 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003560 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003561 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003562 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003563 if (stop)
3564 /* Somehow qf_count may have an incorrect value, set it to 1
3565 * to avoid crashing when it's wrong.
3566 * TODO: Avoid qf_count being incorrect. */
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003567 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003568 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003569 qfl->qf_start = qfpnext;
3570 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003572
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003573 qfl->qf_index = 0;
3574 qfl->qf_start = NULL;
3575 qfl->qf_last = NULL;
3576 qfl->qf_ptr = NULL;
3577 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003578
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003579 qf_clean_dir_stack(&qfl->qf_dir_stack);
3580 qfl->qf_directory = NULL;
3581 qf_clean_dir_stack(&qfl->qf_file_stack);
3582 qfl->qf_currfile = NULL;
3583 qfl->qf_multiline = FALSE;
3584 qfl->qf_multiignore = FALSE;
3585 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586}
3587
3588/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003589 * Free error list "idx". Frees all the entries in the quickfix list,
3590 * associated context information and the title.
3591 */
3592 static void
3593qf_free(qf_info_T *qi, int idx)
3594{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003595 qf_list_T *qfl = &qi->qf_lists[idx];
3596
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003597 qf_free_items(qi, idx);
3598
Bram Moolenaard23a8232018-02-10 18:45:26 +01003599 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003600 free_tv(qfl->qf_ctx);
3601 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003602 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003603 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003604}
3605
3606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 * qf_mark_adjust: adjust marks
3608 */
3609 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003610qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003611 win_T *wp,
3612 linenr_T line1,
3613 linenr_T line2,
3614 long amount,
3615 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003617 int i;
3618 qfline_T *qfp;
3619 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003620 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003621 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003622 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
Bram Moolenaarc1542742016-07-20 21:44:37 +02003624 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003625 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003626 if (wp != NULL)
3627 {
3628 if (wp->w_llist == NULL)
3629 return;
3630 qi = wp->w_llist;
3631 }
3632
3633 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003634 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003635 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003636 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3637 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 if (qfp->qf_fnum == curbuf->b_fnum)
3639 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003640 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3642 {
3643 if (amount == MAXLNUM)
3644 qfp->qf_cleared = TRUE;
3645 else
3646 qfp->qf_lnum += amount;
3647 }
3648 else if (amount_after && qfp->qf_lnum > line2)
3649 qfp->qf_lnum += amount_after;
3650 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003651
3652 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003653 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654}
3655
3656/*
3657 * Make a nice message out of the error character and the error number:
3658 * char number message
3659 * e or E 0 " error"
3660 * w or W 0 " warning"
3661 * i or I 0 " info"
3662 * 0 0 ""
3663 * other 0 " c"
3664 * e or E n " error n"
3665 * w or W n " warning n"
3666 * i or I n " info n"
3667 * 0 n " error n"
3668 * other n " c n"
3669 * 1 x "" :helpgrep
3670 */
3671 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003672qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
3674 static char_u buf[20];
3675 static char_u cc[3];
3676 char_u *p;
3677
3678 if (c == 'W' || c == 'w')
3679 p = (char_u *)" warning";
3680 else if (c == 'I' || c == 'i')
3681 p = (char_u *)" info";
3682 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3683 p = (char_u *)" error";
3684 else if (c == 0 || c == 1)
3685 p = (char_u *)"";
3686 else
3687 {
3688 cc[0] = ' ';
3689 cc[1] = c;
3690 cc[2] = NUL;
3691 p = cc;
3692 }
3693
3694 if (nr <= 0)
3695 return p;
3696
3697 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3698 return buf;
3699}
3700
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003702 * When "split" is FALSE: Open the entry/result under the cursor.
3703 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3704 */
3705 void
3706qf_view_result(int split)
3707{
3708 qf_info_T *qi = &ql_info;
3709
3710 if (!bt_quickfix(curbuf))
3711 return;
3712
3713 if (IS_LL_WINDOW(curwin))
3714 qi = GET_LOC_LIST(curwin);
3715
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003716 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003717 {
3718 EMSG(_(e_quickfix));
3719 return;
3720 }
3721
3722 if (split)
3723 {
3724 char_u cmd[32];
3725
3726 vim_snprintf((char *)cmd, sizeof(cmd), "split +%ld%s",
3727 (long)curwin->w_cursor.lnum,
3728 IS_LL_WINDOW(curwin) ? "ll" : "cc");
3729 if (do_cmdline_cmd(cmd) == OK)
3730 do_cmdline_cmd((char_u *) "clearjumps");
3731 return;
3732 }
3733
3734 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3735}
3736
3737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 * ":cwindow": open the quickfix window if we have errors to display,
3739 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003740 * ":lwindow": open the location list window if we have locations to display,
3741 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 */
3743 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003744ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003746 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 win_T *win;
3748
Bram Moolenaar39665952018-08-15 20:59:48 +02003749 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003750 {
3751 qi = GET_LOC_LIST(curwin);
3752 if (qi == NULL)
3753 return;
3754 }
3755
3756 /* Look for an existing quickfix window. */
3757 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758
3759 /*
3760 * If a quickfix window is open but we have no errors to display,
3761 * close the window. If a quickfix window is not open, then open
3762 * it if we have errors; otherwise, leave it closed.
3763 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003764 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003765 || qf_list_empty(qi, qi->qf_curlist)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003766 || qi->qf_curlist >= qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
3768 if (win != NULL)
3769 ex_cclose(eap);
3770 }
3771 else if (win == NULL)
3772 ex_copen(eap);
3773}
3774
3775/*
3776 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003777 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003780ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003782 win_T *win = NULL;
3783 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaar39665952018-08-15 20:59:48 +02003785 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003786 {
3787 qi = GET_LOC_LIST(curwin);
3788 if (qi == NULL)
3789 return;
3790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003792 /* Find existing quickfix window and close it. */
3793 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 if (win != NULL)
3795 win_close(win, FALSE);
3796}
3797
3798/*
3799 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003800 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 */
3802 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003803ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003805 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 int height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 win_T *win;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003808 tabpage_T *prevtab = curtab;
Bram Moolenaar9c102382006-05-03 21:26:49 +00003809 buf_T *qf_buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003810 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811
Bram Moolenaar39665952018-08-15 20:59:48 +02003812 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003813 {
3814 qi = GET_LOC_LIST(curwin);
3815 if (qi == NULL)
3816 {
3817 EMSG(_(e_loclist));
3818 return;
3819 }
3820 }
3821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 if (eap->addr_count != 0)
3823 height = eap->line2;
3824 else
3825 height = QF_WINHEIGHT;
3826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828#ifdef FEAT_GUI
3829 need_mouse_correct = TRUE;
3830#endif
3831
3832 /*
3833 * Find existing quickfix window, or open a new one.
3834 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003835 win = qf_find_win(qi);
3836
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003837 if (win != NULL && cmdmod.tab == 0)
Bram Moolenaar15886412014-03-27 17:02:27 +01003838 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 win_goto(win);
Bram Moolenaar15886412014-03-27 17:02:27 +01003840 if (eap->addr_count != 0)
3841 {
Bram Moolenaar15886412014-03-27 17:02:27 +01003842 if (cmdmod.split & WSP_VERT)
3843 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003844 if (height != win->w_width)
Bram Moolenaar15886412014-03-27 17:02:27 +01003845 win_setwidth(height);
3846 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003847 else if (height != win->w_height)
Bram Moolenaar15886412014-03-27 17:02:27 +01003848 win_setheight(height);
3849 }
3850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 else
3852 {
Bram Moolenaarde046542017-12-26 13:53:11 +01003853 int flags = 0;
3854
Bram Moolenaar9c102382006-05-03 21:26:49 +00003855 qf_buf = qf_find_buf(qi);
3856
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 /* The current window becomes the previous window afterwards. */
3858 win = curwin;
3859
Bram Moolenaar77642c02012-11-20 17:55:10 +01003860 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
3861 && cmdmod.split == 0)
Bram Moolenaarde046542017-12-26 13:53:11 +01003862 /* Create the new quickfix window at the very bottom, except when
Bram Moolenaar77642c02012-11-20 17:55:10 +01003863 * :belowright or :aboveleft is used. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003864 win_goto(lastwin);
Bram Moolenaarde046542017-12-26 13:53:11 +01003865 /* Default is to open the window below the current window */
3866 if (cmdmod.split == 0)
3867 flags = WSP_BELOW;
3868 flags |= WSP_NEWLOC;
3869 if (win_split(height, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 return; /* not enough room for window */
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003871 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003873 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003875 /*
3876 * For the location list window, create a reference to the
3877 * location list from the window 'win'.
3878 */
3879 curwin->w_llist_ref = win->w_llist;
3880 win->w_llist->qf_refcount++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003882
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003883 if (oldwin != curwin)
3884 oldwin = NULL; /* don't store info when in another window */
Bram Moolenaar9c102382006-05-03 21:26:49 +00003885 if (qf_buf != NULL)
3886 /* Use the existing quickfix buffer */
3887 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003888 ECMD_HIDE + ECMD_OLDBUF, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003889 else
3890 {
3891 /* Create a new quickfix buffer */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003892 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003893 /* switch off 'swapfile' */
3894 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3895 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
Bram Moolenaar838bb712006-03-11 21:24:08 +00003896 OPT_LOCAL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00003897 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01003898 RESET_BINDING(curwin);
Bram Moolenaar04c0f8a2009-04-29 09:52:12 +00003899#ifdef FEAT_DIFF
3900 curwin->w_p_diff = FALSE;
3901#endif
3902#ifdef FEAT_FOLDING
3903 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3904 OPT_LOCAL);
3905#endif
Bram Moolenaar9c102382006-05-03 21:26:49 +00003906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003908 /* Only set the height when still in the same tab page and there is no
3909 * window to the side. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003910 if (curtab == prevtab && curwin->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 win_setheight(height);
3912 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
3913 if (win_valid(win))
3914 prevwin = win;
3915 }
3916
Bram Moolenaar81278ef2015-05-04 12:34:22 +02003917 qf_set_title_var(qi);
3918
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 /*
3920 * Fill the buffer with the quickfix list.
3921 */
Bram Moolenaar864293a2016-06-02 13:40:04 +02003922 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003924 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 curwin->w_cursor.col = 0;
3926 check_cursor();
3927 update_topline(); /* scroll to show the line */
3928}
3929
3930/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02003931 * Move the cursor in the quickfix window to "lnum".
3932 */
3933 static void
3934qf_win_goto(win_T *win, linenr_T lnum)
3935{
3936 win_T *old_curwin = curwin;
3937
3938 curwin = win;
3939 curbuf = win->w_buffer;
3940 curwin->w_cursor.lnum = lnum;
3941 curwin->w_cursor.col = 0;
3942#ifdef FEAT_VIRTUALEDIT
3943 curwin->w_cursor.coladd = 0;
3944#endif
3945 curwin->w_curswant = 0;
3946 update_topline(); /* scroll to show the line */
3947 redraw_later(VALID);
3948 curwin->w_redr_status = TRUE; /* update ruler */
3949 curwin = old_curwin;
3950 curbuf = curwin->w_buffer;
3951}
3952
3953/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02003954 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02003955 */
3956 void
Bram Moolenaar39665952018-08-15 20:59:48 +02003957ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02003958{
Bram Moolenaar537ef082016-07-09 17:56:19 +02003959 qf_info_T *qi = &ql_info;
3960 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02003961
Bram Moolenaar39665952018-08-15 20:59:48 +02003962 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02003963 {
3964 qi = GET_LOC_LIST(curwin);
3965 if (qi == NULL)
3966 {
3967 EMSG(_(e_loclist));
3968 return;
3969 }
3970 }
3971
3972 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02003973 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
3974 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
3975}
3976
3977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 * Return the number of the current entry (line number in the quickfix
3979 * window).
3980 */
3981 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01003982qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003984 qf_info_T *qi = &ql_info;
3985
3986 if (IS_LL_WINDOW(wp))
3987 /* In the location list window, use the referenced location list */
3988 qi = wp->w_llist_ref;
3989
3990 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991}
3992
3993/*
3994 * Update the cursor position in the quickfix window to the current error.
3995 * Return TRUE if there is a quickfix window.
3996 */
3997 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003998qf_win_pos_update(
3999 qf_info_T *qi,
4000 int old_qf_index) /* previous qf_index or zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001{
4002 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004003 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
4005 /*
4006 * Put the cursor on the current error in the quickfix window, so that
4007 * it's viewable.
4008 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004009 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (win != NULL
4011 && qf_index <= win->w_buffer->b_ml.ml_line_count
4012 && old_qf_index != qf_index)
4013 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 if (qf_index > old_qf_index)
4015 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004016 win->w_redraw_top = old_qf_index;
4017 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019 else
4020 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004021 win->w_redraw_top = qf_index;
4022 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004024 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
4026 return win != NULL;
4027}
4028
4029/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004030 * Check whether the given window is displaying the specified quickfix/location
4031 * list buffer
4032 */
4033 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004034is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004035{
4036 /*
4037 * A window displaying the quickfix buffer will have the w_llist_ref field
4038 * set to NULL.
4039 * A window displaying a location list buffer will have the w_llist_ref
4040 * pointing to the location list.
4041 */
4042 if (bt_quickfix(win->w_buffer))
4043 if ((qi == &ql_info && win->w_llist_ref == NULL)
4044 || (qi != &ql_info && win->w_llist_ref == qi))
4045 return TRUE;
4046
4047 return FALSE;
4048}
4049
4050/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004051 * Find a window displaying the quickfix/location list 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004052 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004053 */
4054 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004055qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004056{
4057 win_T *win;
4058
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004059 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004060 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004061 return win;
4062 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004063}
4064
4065/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004066 * Find a quickfix buffer.
4067 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 */
4069 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004070qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004072 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004073 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
Bram Moolenaar9c102382006-05-03 21:26:49 +00004075 FOR_ALL_TAB_WINDOWS(tp, win)
4076 if (is_qf_win(win, qi))
4077 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004078
Bram Moolenaar9c102382006-05-03 21:26:49 +00004079 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080}
4081
4082/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004083 * Update the w:quickfix_title variable in the quickfix/location list window
4084 */
4085 static void
4086qf_update_win_titlevar(qf_info_T *qi)
4087{
4088 win_T *win;
4089 win_T *curwin_save;
4090
4091 if ((win = qf_find_win(qi)) != NULL)
4092 {
4093 curwin_save = curwin;
4094 curwin = win;
4095 qf_set_title_var(qi);
4096 curwin = curwin_save;
4097 }
4098}
4099
4100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 * Find the quickfix buffer. If it exists, update the contents.
4102 */
4103 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004104qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105{
4106 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004107 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109
4110 /* Check if a buffer for the quickfix list exists. Update it. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004111 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 if (buf != NULL)
4113 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004114 linenr_T old_line_count = buf->b_ml.ml_line_count;
4115
4116 if (old_last == NULL)
4117 /* set curwin/curbuf to buf and save a few things */
4118 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
Bram Moolenaard823fa92016-08-12 16:29:27 +02004120 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004121
Bram Moolenaar864293a2016-06-02 13:40:04 +02004122 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004123 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004124
Bram Moolenaar864293a2016-06-02 13:40:04 +02004125 if (old_last == NULL)
4126 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004127 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004128
4129 /* restore curwin/curbuf and a few other things */
4130 aucmd_restbuf(&aco);
4131 }
4132
4133 /* Only redraw when added lines are visible. This avoids flickering
4134 * when the added lines are not visible. */
4135 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4136 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
4138}
4139
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004140/*
4141 * Set "w:quickfix_title" if "qi" has a title.
4142 */
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004143 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01004144qf_set_title_var(qf_info_T *qi)
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004145{
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004146 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL)
4147 set_internal_string_var((char_u *)"w:quickfix_title",
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004148 qi->qf_lists[qi->qf_curlist].qf_title);
4149}
4150
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004152 * Add an error line to the quickfix buffer.
4153 */
4154 static int
4155qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4156{
4157 int len;
4158 buf_T *errbuf;
4159
4160 if (qfp->qf_module != NULL)
4161 {
4162 STRCPY(IObuff, qfp->qf_module);
4163 len = (int)STRLEN(IObuff);
4164 }
4165 else if (qfp->qf_fnum != 0
4166 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4167 && errbuf->b_fname != NULL)
4168 {
4169 if (qfp->qf_type == 1) /* :helpgrep */
4170 STRCPY(IObuff, gettail(errbuf->b_fname));
4171 else
4172 {
4173 /* shorten the file name if not done already */
4174 if (errbuf->b_sfname == NULL
4175 || mch_isFullName(errbuf->b_sfname))
4176 {
4177 if (*dirname == NUL)
4178 mch_dirname(dirname, MAXPATHL);
4179 shorten_buf_fname(errbuf, dirname, FALSE);
4180 }
4181 STRCPY(IObuff, errbuf->b_fname);
4182 }
4183 len = (int)STRLEN(IObuff);
4184 }
4185 else
4186 len = 0;
4187 IObuff[len++] = '|';
4188
4189 if (qfp->qf_lnum > 0)
4190 {
4191 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4192 len += (int)STRLEN(IObuff + len);
4193
4194 if (qfp->qf_col > 0)
4195 {
4196 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4197 len += (int)STRLEN(IObuff + len);
4198 }
4199
4200 sprintf((char *)IObuff + len, "%s",
4201 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4202 len += (int)STRLEN(IObuff + len);
4203 }
4204 else if (qfp->qf_pattern != NULL)
4205 {
4206 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4207 len += (int)STRLEN(IObuff + len);
4208 }
4209 IObuff[len++] = '|';
4210 IObuff[len++] = ' ';
4211
4212 /* Remove newlines and leading whitespace from the text.
4213 * For an unrecognized line keep the indent, the compiler may
4214 * mark a word with ^^^^. */
4215 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4216 IObuff + len, IOSIZE - len);
4217
4218 if (ml_append_buf(buf, lnum, IObuff,
4219 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4220 return FAIL;
4221
4222 return OK;
4223}
4224
4225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 * Fill current buffer with quickfix errors, replacing any previous contents.
4227 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004228 * If "old_last" is not NULL append the items after this one.
4229 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4230 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 */
4232 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004233qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004235 linenr_T lnum;
4236 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004237 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Bram Moolenaar864293a2016-06-02 13:40:04 +02004239 if (old_last == NULL)
4240 {
4241 if (buf != curbuf)
4242 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004243 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004244 return;
4245 }
4246
4247 /* delete all existing lines */
4248 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4249 (void)ml_delete((linenr_T)1, FALSE);
4250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 /* Check if there is anything to display */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004253 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
Bram Moolenaara796d462018-05-01 14:30:36 +02004255 char_u dirname[MAXPATHL];
4256
4257 *dirname = NUL;
4258
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 /* Add one line for each error */
Bram Moolenaar864293a2016-06-02 13:40:04 +02004260 if (old_last == NULL)
4261 {
4262 qfp = qi->qf_lists[qi->qf_curlist].qf_start;
4263 lnum = 0;
4264 }
4265 else
4266 {
4267 qfp = old_last->qf_next;
4268 lnum = buf->b_ml.ml_line_count;
4269 }
4270 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004272 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004274
Bram Moolenaar864293a2016-06-02 13:40:04 +02004275 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004277 if (qfp == NULL)
4278 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004280
4281 if (old_last == NULL)
4282 /* Delete the empty line which is now at the end */
4283 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285
4286 /* correct cursor position */
4287 check_lnums(TRUE);
4288
Bram Moolenaar864293a2016-06-02 13:40:04 +02004289 if (old_last == NULL)
4290 {
4291 /* Set the 'filetype' to "qf" each time after filling the buffer.
4292 * This resembles reading a file into a buffer, it's more logical when
4293 * using autocommands. */
Bram Moolenaar18141832017-06-25 21:17:25 +02004294 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004295 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4296 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297
Bram Moolenaar864293a2016-06-02 13:40:04 +02004298 keep_filetype = TRUE; /* don't detect 'filetype' */
4299 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004301 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004303 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004304 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004305
Bram Moolenaar864293a2016-06-02 13:40:04 +02004306 /* make sure it will be redrawn */
4307 redraw_curbuf_later(NOT_VALID);
4308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 /* Restore KeyTyped, setting 'filetype' may reset it. */
4311 KeyTyped = old_KeyTyped;
4312}
4313
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004314/*
4315 * For every change made to the quickfix list, update the changed tick.
4316 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004317 static void
4318qf_list_changed(qf_info_T *qi, int qf_idx)
4319{
4320 qi->qf_lists[qf_idx].qf_changedtick++;
4321}
4322
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004324 * Return the quickfix/location list number with the given identifier.
4325 * Returns -1 if list is not found.
4326 */
4327 static int
4328qf_id2nr(qf_info_T *qi, int_u qfid)
4329{
4330 int qf_idx;
4331
4332 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4333 if (qi->qf_lists[qf_idx].qf_id == qfid)
4334 return qf_idx;
4335 return INVALID_QFIDX;
4336}
4337
4338/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004339 * If the current list is not "save_qfid" and we can find the list with that ID
4340 * then make it the current list.
4341 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004342 * Returns OK if successfully restored the list. Returns FAIL if the list with
4343 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004344 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004345 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004346qf_restore_list(qf_info_T *qi, int_u save_qfid)
4347{
4348 int curlist;
4349
4350 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4351 {
4352 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004353 if (curlist < 0)
4354 // list is not present
4355 return FAIL;
4356 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004357 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004358 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004359}
4360
4361/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004362 * Jump to the first entry if there is one.
4363 */
4364 static void
4365qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4366{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004367 if (qf_restore_list(qi, save_qfid) == FAIL)
4368 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004369
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004370 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004371 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004372 qf_jump(qi, 0, 0, forceit);
4373}
4374
4375/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004376 * Return TRUE when using ":vimgrep" for ":grep".
4377 */
4378 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004379grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004380{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004381 return ((cmdidx == CMD_grep
4382 || cmdidx == CMD_lgrep
4383 || cmdidx == CMD_grepadd
4384 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004385 && STRCMP("internal",
4386 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4387}
4388
4389/*
Bram Moolenaara6557602006-02-04 22:43:20 +00004390 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 */
4392 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004393ex_make(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394{
Bram Moolenaar7c626922005-02-07 22:01:03 +00004395 char_u *fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 char_u *cmd;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004397 char_u *enc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 unsigned len;
Bram Moolenaara6557602006-02-04 22:43:20 +00004399 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004400 qf_info_T *qi = &ql_info;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004401 int res;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004402 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004403 int_u save_qfid;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004404
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004405 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */
4406 if (grep_internal(eap->cmdidx))
4407 {
4408 ex_vimgrep(eap);
4409 return;
4410 }
4411
Bram Moolenaar7c626922005-02-07 22:01:03 +00004412 switch (eap->cmdidx)
4413 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00004414 case CMD_make: au_name = (char_u *)"make"; break;
4415 case CMD_lmake: au_name = (char_u *)"lmake"; break;
4416 case CMD_grep: au_name = (char_u *)"grep"; break;
4417 case CMD_lgrep: au_name = (char_u *)"lgrep"; break;
4418 case CMD_grepadd: au_name = (char_u *)"grepadd"; break;
4419 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004420 default: break;
4421 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01004422 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4423 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00004424 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004425#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01004426 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00004427 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004428#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004429 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004430#ifdef FEAT_MBYTE
4431 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
Bram Moolenaar39665952018-08-15 20:59:48 +02004434 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00004435 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00004436
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 autowrite_all();
Bram Moolenaar7c626922005-02-07 22:01:03 +00004438 fname = get_mef_name();
4439 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00004441 mch_remove(fname); /* in case it's not unique */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442
4443 /*
4444 * If 'shellpipe' empty: don't redirect to 'errorfile'.
4445 */
4446 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1;
4447 if (*p_sp != NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00004448 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 cmd = alloc(len);
4450 if (cmd == NULL)
4451 return;
4452 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg,
4453 (char *)p_shq);
4454 if (*p_sp != NUL)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004455 append_redir(cmd, len, p_sp, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 /*
4457 * Output a newline if there's something else than the :make command that
4458 * was typed (in which case the cursor is in column 0).
4459 */
4460 if (msg_col == 0)
4461 msg_didout = FALSE;
4462 msg_start();
4463 MSG_PUTS(":!");
4464 msg_outtrans(cmd); /* show what we are doing */
4465
4466 /* let the shell know if we are redirecting output or not */
4467 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4468
4469#ifdef AMIGA
4470 out_flush();
4471 /* read window status report and redraw before message */
4472 (void)char_avail();
4473#endif
4474
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004475 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
Bram Moolenaara6557602006-02-04 22:43:20 +00004476 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4477 (eap->cmdidx != CMD_grepadd
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004478 && eap->cmdidx != CMD_lgrepadd),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004479 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004480 if (wp != NULL)
Bram Moolenaarefa8e802011-05-19 17:42:59 +02004481 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004482 qi = GET_LOC_LIST(wp);
4483 if (qi == NULL)
4484 goto cleanup;
4485 }
4486 if (res >= 0)
4487 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004488
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004489 // Remember the current quickfix list identifier, so that we can
4490 // check for autocommands changing the current quickfix list.
4491 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4492 if (au_name != NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004493 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4494 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004495 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004496 // display the first error
4497 qf_jump_first(qi, save_qfid, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004499cleanup:
Bram Moolenaar7c626922005-02-07 22:01:03 +00004500 mch_remove(fname);
4501 vim_free(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 vim_free(cmd);
4503}
4504
4505/*
4506 * Return the name for the errorfile, in allocated memory.
4507 * Find a new unique name when 'makeef' contains "##".
4508 * Returns NULL for error.
4509 */
4510 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004511get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512{
4513 char_u *p;
4514 char_u *name;
4515 static int start = -1;
4516 static int off = 0;
4517#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004518 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519#endif
4520
4521 if (*p_mef == NUL)
4522 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004523 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 if (name == NULL)
4525 EMSG(_(e_notmp));
4526 return name;
4527 }
4528
4529 for (p = p_mef; *p; ++p)
4530 if (p[0] == '#' && p[1] == '#')
4531 break;
4532
4533 if (*p == NUL)
4534 return vim_strsave(p_mef);
4535
4536 /* Keep trying until the name doesn't exist yet. */
4537 for (;;)
4538 {
4539 if (start == -1)
4540 start = mch_get_pid();
4541 else
4542 off += 19;
4543
4544 name = alloc((unsigned)STRLEN(p_mef) + 30);
4545 if (name == NULL)
4546 break;
4547 STRCPY(name, p_mef);
4548 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4549 STRCAT(name, p + 2);
4550 if (mch_getperm(name) < 0
4551#ifdef HAVE_LSTAT
Bram Moolenaar9af41842016-09-25 21:45:05 +02004552 /* Don't accept a symbolic link, it's a security risk. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 && mch_lstat((char *)name, &sb) < 0
4554#endif
4555 )
4556 break;
4557 vim_free(name);
4558 }
4559 return name;
4560}
4561
4562/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004563 * Returns the number of valid entries in the current quickfix/location list.
4564 */
4565 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004566qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004567{
4568 qf_info_T *qi = &ql_info;
4569 qfline_T *qfp;
4570 int i, sz = 0;
4571 int prev_fnum = 0;
4572
Bram Moolenaar39665952018-08-15 20:59:48 +02004573 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004574 {
4575 /* Location list */
4576 qi = GET_LOC_LIST(curwin);
4577 if (qi == NULL)
4578 return 0;
4579 }
4580
4581 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004582 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004583 ++i, qfp = qfp->qf_next)
4584 {
4585 if (qfp->qf_valid)
4586 {
4587 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
4588 sz++; /* Count all valid entries */
4589 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4590 {
4591 /* Count the number of files */
4592 sz++;
4593 prev_fnum = qfp->qf_fnum;
4594 }
4595 }
4596 }
4597
4598 return sz;
4599}
4600
4601/*
4602 * Returns the current index of the quickfix/location list.
4603 * Returns 0 if there is an error.
4604 */
4605 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004606qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004607{
4608 qf_info_T *qi = &ql_info;
4609
Bram Moolenaar39665952018-08-15 20:59:48 +02004610 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004611 {
4612 /* Location list */
4613 qi = GET_LOC_LIST(curwin);
4614 if (qi == NULL)
4615 return 0;
4616 }
4617
4618 return qi->qf_lists[qi->qf_curlist].qf_index;
4619}
4620
4621/*
4622 * Returns the current index in the quickfix/location list (counting only valid
4623 * entries). If no valid entries are in the list, then returns 1.
4624 */
4625 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004626qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004627{
4628 qf_info_T *qi = &ql_info;
4629 qf_list_T *qfl;
4630 qfline_T *qfp;
4631 int i, eidx = 0;
4632 int prev_fnum = 0;
4633
Bram Moolenaar39665952018-08-15 20:59:48 +02004634 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004635 {
4636 /* Location list */
4637 qi = GET_LOC_LIST(curwin);
4638 if (qi == NULL)
4639 return 1;
4640 }
4641
4642 qfl = &qi->qf_lists[qi->qf_curlist];
4643 qfp = qfl->qf_start;
4644
4645 /* check if the list has valid errors */
4646 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4647 return 1;
4648
4649 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4650 {
4651 if (qfp->qf_valid)
4652 {
4653 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4654 {
4655 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4656 {
4657 /* Count the number of files */
4658 eidx++;
4659 prev_fnum = qfp->qf_fnum;
4660 }
4661 }
4662 else
4663 eidx++;
4664 }
4665 }
4666
4667 return eidx ? eidx : 1;
4668}
4669
4670/*
4671 * Get the 'n'th valid error entry in the quickfix or location list.
4672 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4673 * For :cdo and :ldo returns the 'n'th valid error entry.
4674 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4675 */
4676 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004677qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004678{
4679 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4680 qfline_T *qfp = qfl->qf_start;
4681 int i, eidx;
4682 int prev_fnum = 0;
4683
4684 /* check if the list has valid errors */
4685 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4686 return 1;
4687
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004688 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004689 i++, qfp = qfp->qf_next)
4690 {
4691 if (qfp->qf_valid)
4692 {
4693 if (fdo)
4694 {
4695 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4696 {
4697 /* Count the number of files */
4698 eidx++;
4699 prev_fnum = qfp->qf_fnum;
4700 }
4701 }
4702 else
4703 eidx++;
4704 }
4705
4706 if (eidx == n)
4707 break;
4708 }
4709
4710 if (i <= qfl->qf_count)
4711 return i;
4712 else
4713 return 1;
4714}
4715
4716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004718 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004719 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 */
4721 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004722ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004724 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004725 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004726
Bram Moolenaar39665952018-08-15 20:59:48 +02004727 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004728 {
4729 qi = GET_LOC_LIST(curwin);
4730 if (qi == NULL)
4731 {
4732 EMSG(_(e_loclist));
4733 return;
4734 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004735 }
4736
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004737 if (eap->addr_count > 0)
4738 errornr = (int)eap->line2;
4739 else
4740 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004741 switch (eap->cmdidx)
4742 {
4743 case CMD_cc: case CMD_ll:
4744 errornr = 0;
4745 break;
4746 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4747 case CMD_lfirst:
4748 errornr = 1;
4749 break;
4750 default:
4751 errornr = 32767;
4752 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004753 }
4754
4755 /* For cdo and ldo commands, jump to the nth valid error.
4756 * For cfdo and lfdo commands, jump to the nth valid file entry.
4757 */
Bram Moolenaar55b69262017-08-13 13:42:01 +02004758 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4759 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004760 errornr = qf_get_nth_valid_entry(qi,
4761 eap->addr_count > 0 ? (int)eap->line1 : 1,
4762 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4763
4764 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765}
4766
4767/*
4768 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004769 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004770 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 */
4772 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004773ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004775 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004776 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004777 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004778
Bram Moolenaar39665952018-08-15 20:59:48 +02004779 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004780 {
4781 qi = GET_LOC_LIST(curwin);
4782 if (qi == NULL)
4783 {
4784 EMSG(_(e_loclist));
4785 return;
4786 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004787 }
4788
Bram Moolenaar55b69262017-08-13 13:42:01 +02004789 if (eap->addr_count > 0
4790 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4791 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004792 errornr = (int)eap->line2;
4793 else
4794 errornr = 1;
4795
Bram Moolenaar39665952018-08-15 20:59:48 +02004796 // Depending on the command jump to either next or previous entry/file.
4797 switch (eap->cmdidx)
4798 {
4799 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4800 dir = FORWARD;
4801 break;
4802 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4803 case CMD_lNext:
4804 dir = BACKWARD;
4805 break;
4806 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4807 dir = FORWARD_FILE;
4808 break;
4809 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4810 dir = BACKWARD_FILE;
4811 break;
4812 default:
4813 dir = FORWARD;
4814 break;
4815 }
4816
4817 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818}
4819
4820/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004821 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004822 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 */
4824 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004825ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004827 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004828 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004829 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004830 char_u *au_name = NULL;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004831 int_u save_qfid = 0; /* init for gcc */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004832 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004833
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004834 switch (eap->cmdidx)
4835 {
4836 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4837 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4838 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4839 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4840 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4841 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4842 default: break;
4843 }
4844 if (au_name != NULL)
4845 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004846#ifdef FEAT_MBYTE
4847 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4848#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02004849#ifdef FEAT_BROWSE
4850 if (cmdmod.browse)
4851 {
4852 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02004853 NULL, NULL,
4854 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02004855 if (browse_file == NULL)
4856 return;
4857 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
4858 vim_free(browse_file);
4859 }
4860 else
4861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004863 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004864
Bram Moolenaar39665952018-08-15 20:59:48 +02004865 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004866 wp = curwin;
4867
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004868 /*
4869 * This function is used by the :cfile, :cgetfile and :caddfile
4870 * commands.
4871 * :cfile always creates a new quickfix list and jumps to the
4872 * first error.
4873 * :cgetfile creates a new quickfix list but doesn't jump to the
4874 * first error.
4875 * :caddfile adds to an existing quickfix list. If there is no
4876 * quickfix list then a new list is created.
4877 */
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004878 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004879 && eap->cmdidx != CMD_laddfile),
4880 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01004881 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004882 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01004883 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004884 if (qi == NULL)
4885 return;
4886 }
4887 if (res >= 0)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004888 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004889 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004890 if (au_name != NULL)
4891 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01004892
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004893 // Jump to the first error for a new list and if autocmds didn't
4894 // free the list.
4895 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
4896 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004897 // display the first error
4898 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004899}
4900
4901/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01004902 * Return the vimgrep autocmd name.
4903 */
4904 static char_u *
4905vgr_get_auname(cmdidx_T cmdidx)
4906{
4907 switch (cmdidx)
4908 {
4909 case CMD_vimgrep: return (char_u *)"vimgrep";
4910 case CMD_lvimgrep: return (char_u *)"lvimgrep";
4911 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
4912 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
4913 case CMD_grep: return (char_u *)"grep";
4914 case CMD_lgrep: return (char_u *)"lgrep";
4915 case CMD_grepadd: return (char_u *)"grepadd";
4916 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4917 default: return NULL;
4918 }
4919}
4920
4921/*
4922 * Initialize the regmatch used by vimgrep for pattern "s".
4923 */
4924 static void
4925vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
4926{
4927 /* Get the search pattern: either white-separated or enclosed in // */
4928 regmatch->regprog = NULL;
4929
4930 if (s == NULL || *s == NUL)
4931 {
4932 /* Pattern is empty, use last search pattern. */
4933 if (last_search_pat() == NULL)
4934 {
4935 EMSG(_(e_noprevre));
4936 return;
4937 }
4938 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
4939 }
4940 else
4941 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
4942
4943 regmatch->rmm_ic = p_ic;
4944 regmatch->rmm_maxcol = 0;
4945}
4946
4947/*
4948 * Display a file name when vimgrep is running.
4949 */
4950 static void
4951vgr_display_fname(char_u *fname)
4952{
4953 char_u *p;
4954
4955 msg_start();
4956 p = msg_strtrunc(fname, TRUE);
4957 if (p == NULL)
4958 msg_outtrans(fname);
4959 else
4960 {
4961 msg_outtrans(p);
4962 vim_free(p);
4963 }
4964 msg_clr_eos();
4965 msg_didout = FALSE; /* overwrite this message */
4966 msg_nowait = TRUE; /* don't wait for this message */
4967 msg_col = 0;
4968 out_flush();
4969}
4970
4971/*
4972 * Load a dummy buffer to search for a pattern using vimgrep.
4973 */
4974 static buf_T *
4975vgr_load_dummy_buf(
4976 char_u *fname,
4977 char_u *dirname_start,
4978 char_u *dirname_now)
4979{
4980 int save_mls;
4981#if defined(FEAT_SYN_HL)
4982 char_u *save_ei = NULL;
4983#endif
4984 buf_T *buf;
4985
4986#if defined(FEAT_SYN_HL)
4987 /* Don't do Filetype autocommands to avoid loading syntax and
4988 * indent scripts, a great speed improvement. */
4989 save_ei = au_event_disable(",Filetype");
4990#endif
4991 /* Don't use modelines here, it's useless. */
4992 save_mls = p_mls;
4993 p_mls = 0;
4994
4995 /* Load file into a buffer, so that 'fileencoding' is detected,
4996 * autocommands applied, etc. */
4997 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
4998
4999 p_mls = save_mls;
5000#if defined(FEAT_SYN_HL)
5001 au_event_restore(save_ei);
5002#endif
5003
5004 return buf;
5005}
5006
5007/*
5008 * Check whether a quickfix/location list valid. Autocmds may remove or change
5009 * a quickfix list when vimgrep is running. If the list is not found, create a
5010 * new list.
5011 */
5012 static int
5013vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005014 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005015 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005016 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005017 char_u *title)
5018{
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005019 /* Verify that the quickfix/location list was not freed by an autocmd */
5020 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005021 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005022 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005023 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005024 /* An autocmd has freed the location list. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005025 EMSG(_(e_loc_list_changed));
5026 return FALSE;
5027 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005028 else
5029 {
5030 /* Quickfix list is not found, create a new one. */
5031 qf_new_list(qi, title);
5032 return TRUE;
5033 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005034 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005035
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005036 if (qf_restore_list(qi, qfid) == FAIL)
5037 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005038
5039 return TRUE;
5040}
5041
5042/*
5043 * Search for a pattern in all the lines in a buffer and add the matching lines
5044 * to a quickfix list.
5045 */
5046 static int
5047vgr_match_buflines(
5048 qf_info_T *qi,
5049 char_u *fname,
5050 buf_T *buf,
5051 regmmatch_T *regmatch,
5052 long tomatch,
5053 int duplicate_name,
5054 int flags)
5055{
5056 int found_match = FALSE;
5057 long lnum;
5058 colnr_T col;
5059
5060 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum)
5061 {
5062 col = 0;
5063 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5064 col, NULL, NULL) > 0)
5065 {
5066 /* Pass the buffer number so that it gets used even for a
5067 * dummy buffer, unless duplicate_name is set, then the
5068 * buffer will be wiped out below. */
5069 if (qf_add_entry(qi,
5070 qi->qf_curlist,
5071 NULL, /* dir */
5072 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005073 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005074 duplicate_name ? 0 : buf->b_fnum,
5075 ml_get_buf(buf,
5076 regmatch->startpos[0].lnum + lnum, FALSE),
5077 regmatch->startpos[0].lnum + lnum,
5078 regmatch->startpos[0].col + 1,
5079 FALSE, /* vis_col */
5080 NULL, /* search pattern */
5081 0, /* nr */
5082 0, /* type */
5083 TRUE /* valid */
5084 ) == FAIL)
5085 {
5086 got_int = TRUE;
5087 break;
5088 }
5089 found_match = TRUE;
5090 if (--tomatch == 0)
5091 break;
5092 if ((flags & VGR_GLOBAL) == 0
5093 || regmatch->endpos[0].lnum > 0)
5094 break;
5095 col = regmatch->endpos[0].col
5096 + (col == regmatch->endpos[0].col);
5097 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5098 break;
5099 }
5100 line_breakcheck();
5101 if (got_int)
5102 break;
5103 }
5104
5105 return found_match;
5106}
5107
5108/*
5109 * Jump to the first match and update the directory.
5110 */
5111 static void
5112vgr_jump_to_match(
5113 qf_info_T *qi,
5114 int forceit,
5115 int *redraw_for_dummy,
5116 buf_T *first_match_buf,
5117 char_u *target_dir)
5118{
5119 buf_T *buf;
5120
5121 buf = curbuf;
5122 qf_jump(qi, 0, 0, forceit);
5123 if (buf != curbuf)
5124 /* If we jumped to another buffer redrawing will already be
5125 * taken care of. */
5126 *redraw_for_dummy = FALSE;
5127
5128 /* Jump to the directory used after loading the buffer. */
5129 if (curbuf == first_match_buf && target_dir != NULL)
5130 {
5131 exarg_T ea;
5132
5133 ea.arg = target_dir;
5134 ea.cmdidx = CMD_lcd;
5135 ex_cd(&ea);
5136 }
5137}
5138
5139/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005140 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005141 * ":vimgrepadd {pattern} file(s)"
5142 * ":lvimgrep {pattern} file(s)"
5143 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005144 */
5145 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005146ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005147{
Bram Moolenaar81695252004-12-29 20:58:21 +00005148 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005149 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005150 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005151 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005152 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005153 char_u *s;
5154 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005155 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005156 qf_info_T *qi = &ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005157 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005158 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005159 buf_T *buf;
5160 int duplicate_name = FALSE;
5161 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005162 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005163 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005164 buf_T *first_match_buf = NULL;
5165 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005166 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005167 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005168 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005169 char_u *dirname_start = NULL;
5170 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005171 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005172 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005173
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005174 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005175 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5176 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005177 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005178#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005179 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005180 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005181#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005182 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005183
Bram Moolenaar39665952018-08-15 20:59:48 +02005184 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005185 {
5186 qi = ll_get_or_alloc_list(curwin);
5187 if (qi == NULL)
5188 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005189 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005190 }
5191
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005192 if (eap->addr_count > 0)
5193 tomatch = eap->line2;
5194 else
5195 tomatch = MAXLNUM;
5196
Bram Moolenaar81695252004-12-29 20:58:21 +00005197 /* Get the search pattern: either white-separated or enclosed in // */
Bram Moolenaar86b68352004-12-27 21:59:20 +00005198 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005199 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005200 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005201 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005202 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005203 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005204 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005205 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005206
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005207 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005208 if (regmatch.regprog == NULL)
5209 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005210
5211 p = skipwhite(p);
5212 if (*p == NUL)
5213 {
5214 EMSG(_("E683: File name missing or invalid pattern"));
5215 goto theend;
5216 }
5217
Bram Moolenaar55b69262017-08-13 13:42:01 +02005218 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005219 && eap->cmdidx != CMD_vimgrepadd
5220 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005221 || qi->qf_curlist == qi->qf_listcount)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005222 /* make place for a new list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005223 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005224
5225 /* parse the list of arguments */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005226 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005227 goto theend;
5228 if (fcount == 0)
5229 {
5230 EMSG(_(e_nomatch));
5231 goto theend;
5232 }
5233
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005234 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5235 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005236 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005237 {
5238 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005239 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005240 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005241
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005242 /* Remember the current directory, because a BufRead autocommand that does
5243 * ":lcd %:p:h" changes the meaning of short path names. */
5244 mch_dirname(dirname_start, MAXPATHL);
5245
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005246 /* Remember the current quickfix list identifier, so that we can check for
5247 * autocommands changing the current quickfix list. */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005248 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005249
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005250 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005251 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005252 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005253 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005254 if (time(NULL) > seconds)
5255 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005256 /* Display the file name every second or so, show the user we are
5257 * working on it. */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005258 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005259 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005260 }
5261
Bram Moolenaar81695252004-12-29 20:58:21 +00005262 buf = buflist_findname_exp(fnames[fi]);
5263 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5264 {
5265 /* Remember that a buffer with this name already exists. */
5266 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005267 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005268 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005269
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005270 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005271 }
5272 else
5273 /* Use existing, loaded buffer. */
5274 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005275
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005276 /* Check whether the quickfix list is still valid. When loading a
5277 * buffer above, autocommands might have changed the quickfix list. */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005278 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005279 {
5280 FreeWild(fcount, fnames);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005281 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005282 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005283 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005284
Bram Moolenaar81695252004-12-29 20:58:21 +00005285 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005286 {
5287 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005288 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005289 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005290 else
5291 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005292 /* Try for a match in all lines of the buffer.
5293 * For ":1vimgrep" look for first match only. */
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005294 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
5295 tomatch, duplicate_name, flags);
5296
Bram Moolenaar81695252004-12-29 20:58:21 +00005297 if (using_dummy)
5298 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005299 if (found_match && first_match_buf == NULL)
5300 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005301 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005302 {
Bram Moolenaar81695252004-12-29 20:58:21 +00005303 /* Never keep a dummy buffer if there is another buffer
5304 * with the same name. */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005305 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005306 buf = NULL;
5307 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005308 else if (!cmdmod.hide
5309 || buf->b_p_bh[0] == 'u' /* "unload" */
5310 || buf->b_p_bh[0] == 'w' /* "wipe" */
5311 || buf->b_p_bh[0] == 'd') /* "delete" */
Bram Moolenaar81695252004-12-29 20:58:21 +00005312 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00005313 /* When no match was found we don't need to remember the
5314 * buffer, wipe it out. If there was a match and it
5315 * wasn't the first one or we won't jump there: only
5316 * unload the buffer.
5317 * Ignore 'hidden' here, because it may lead to having too
5318 * many swap files. */
Bram Moolenaar81695252004-12-29 20:58:21 +00005319 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005320 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005321 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005322 buf = NULL;
5323 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005324 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005325 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005326 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar015102e2016-07-16 18:24:56 +02005327 /* Keeping the buffer, remove the dummy flag. */
5328 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005329 buf = NULL;
5330 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005331 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005332
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005333 if (buf != NULL)
5334 {
Bram Moolenaar015102e2016-07-16 18:24:56 +02005335 /* Keeping the buffer, remove the dummy flag. */
5336 buf->b_flags &= ~BF_DUMMY;
5337
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005338 /* If the buffer is still loaded we need to use the
5339 * directory we jumped to below. */
5340 if (buf == first_match_buf
5341 && target_dir == NULL
5342 && STRCMP(dirname_start, dirname_now) != 0)
5343 target_dir = vim_strsave(dirname_now);
5344
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005345 /* The buffer is still loaded, the Filetype autocommands
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005346 * need to be done now, in that buffer. And the modelines
Bram Moolenaara3227e22006-03-08 21:32:40 +00005347 * need to be done (again). But not the window-local
5348 * options! */
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005349 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005350#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005351 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5352 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005353#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005354 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005355 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005356 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005357 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005358 }
5359 }
5360
5361 FreeWild(fcount, fnames);
5362
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005363 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
5364 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start;
5365 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005366 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005367
Bram Moolenaar864293a2016-06-02 13:40:04 +02005368 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005369
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005370 if (au_name != NULL)
5371 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5372 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3c097222017-12-21 20:54:49 +01005373 /*
5374 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5375 * is still valid.
5376 */
Bram Moolenaar3c097222017-12-21 20:54:49 +01005377 if (!qflist_valid(wp, save_qfid))
5378 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005379
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005380 if (qf_restore_list(qi, save_qfid) == FAIL)
5381 goto theend;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005382
Bram Moolenaar86b68352004-12-27 21:59:20 +00005383 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005384 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005385 {
5386 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005387 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5388 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005389 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005390 else
5391 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005392
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005393 /* If we loaded a dummy buffer into the current window, the autocommands
5394 * may have messed up things, need to redraw and recompute folds. */
5395 if (redraw_for_dummy)
5396 {
5397#ifdef FEAT_FOLDING
5398 foldUpdateAll(curwin);
5399#else
5400 redraw_later(NOT_VALID);
5401#endif
5402 }
5403
Bram Moolenaar86b68352004-12-27 21:59:20 +00005404theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005405 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005406 vim_free(dirname_now);
5407 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005408 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005409 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005410}
5411
5412/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005413 * Restore current working directory to "dirname_start" if they differ, taking
5414 * into account whether it is set locally or globally.
5415 */
5416 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005417restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005418{
5419 char_u *dirname_now = alloc(MAXPATHL);
5420
5421 if (NULL != dirname_now)
5422 {
5423 mch_dirname(dirname_now, MAXPATHL);
5424 if (STRCMP(dirname_start, dirname_now) != 0)
5425 {
5426 /* If the directory has changed, change it back by building up an
5427 * appropriate ex command and executing it. */
5428 exarg_T ea;
5429
5430 ea.arg = dirname_start;
5431 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5432 ex_cd(&ea);
5433 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005434 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005435 }
5436}
5437
5438/*
5439 * Load file "fname" into a dummy buffer and return the buffer pointer,
5440 * placing the directory resulting from the buffer load into the
5441 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5442 * prior to calling this function. Restores directory to "dirname_start" prior
5443 * to returning, if autocmds or the 'autochdir' option have changed it.
5444 *
5445 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5446 * or wipe_dummy_buffer() later!
5447 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005448 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005449 */
5450 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005451load_dummy_buffer(
5452 char_u *fname,
5453 char_u *dirname_start, /* in: old directory */
5454 char_u *resulting_dir) /* out: new directory */
Bram Moolenaar81695252004-12-29 20:58:21 +00005455{
5456 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005457 bufref_T newbufref;
5458 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005459 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005460 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005461 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005462
5463 /* Allocate a buffer without putting it in the buffer list. */
5464 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5465 if (newbuf == NULL)
5466 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005467 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005468
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005469 /* Init the options. */
5470 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5471
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005472 /* need to open the memfile before putting the buffer in a window */
5473 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005474 {
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005475 /* Make sure this buffer isn't wiped out by autocommands. */
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005476 ++newbuf->b_locked;
5477
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005478 /* set curwin/curbuf to buf and save a few things */
5479 aucmd_prepbuf(&aco, newbuf);
5480
5481 /* Need to set the filename for autocommands. */
5482 (void)setfname(curbuf, fname, NULL, FALSE);
5483
Bram Moolenaar81695252004-12-29 20:58:21 +00005484 /* Create swap file now to avoid the ATTENTION message. */
5485 check_need_swap(TRUE);
5486
5487 /* Remove the "dummy" flag, otherwise autocommands may not
5488 * work. */
5489 curbuf->b_flags &= ~BF_DUMMY;
5490
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005491 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005492 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005493 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005494 NULL, READ_NEW | READ_DUMMY);
5495 --newbuf->b_locked;
5496 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005497 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005498 && !(curbuf->b_flags & BF_NEW))
5499 {
5500 failed = FALSE;
5501 if (curbuf != newbuf)
5502 {
Bram Moolenaar0785ccf2010-11-24 16:32:05 +01005503 /* Bloody autocommands changed the buffer! Can happen when
5504 * using netrw and editing a remote file. Use the current
5505 * buffer instead, delete the dummy one after restoring the
5506 * window stuff. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005507 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005508 newbuf = curbuf;
5509 }
5510 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005511
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005512 /* restore curwin/curbuf and a few other things */
5513 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005514 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5515 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005516
5517 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5518 * skip it. */
5519 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005520 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005521
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005522 /*
5523 * When autocommands/'autochdir' option changed directory: go back.
5524 * Let the caller know what the resulting dir was first, in case it is
5525 * important.
5526 */
5527 mch_dirname(resulting_dir, MAXPATHL);
5528 restore_start_dir(dirname_start);
5529
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005530 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005531 return NULL;
5532 if (failed)
5533 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005534 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005535 return NULL;
5536 }
5537 return newbuf;
5538}
5539
5540/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005541 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5542 * directory to "dirname_start" prior to returning, if autocmds or the
5543 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005544 */
5545 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005546wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005547{
5548 if (curbuf != buf) /* safety check */
Bram Moolenaard68071d2006-05-02 22:08:30 +00005549 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005550#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005551 cleanup_T cs;
5552
5553 /* Reset the error/interrupt/exception state here so that aborting()
5554 * returns FALSE when wiping out the buffer. Otherwise it doesn't
5555 * work when got_int is set. */
5556 enter_cleanup(&cs);
5557#endif
5558
Bram Moolenaar81695252004-12-29 20:58:21 +00005559 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005560
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005561#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005562 /* Restore the error/interrupt/exception state if not discarded by a
5563 * new aborting error, interrupt, or uncaught exception. */
5564 leave_cleanup(&cs);
5565#endif
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005566 /* When autocommands/'autochdir' option changed directory: go back. */
5567 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005568 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005569}
5570
5571/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005572 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5573 * directory to "dirname_start" prior to returning, if autocmds or the
5574 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005575 */
5576 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005577unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005578{
5579 if (curbuf != buf) /* safety check */
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005580 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005581 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005582
5583 /* When autocommands/'autochdir' option changed directory: go back. */
5584 restore_start_dir(dirname_start);
5585 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005586}
5587
Bram Moolenaar05159a02005-02-26 23:04:13 +00005588#if defined(FEAT_EVAL) || defined(PROTO)
5589/*
5590 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005591 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005592 */
5593 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005594get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005595{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005596 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005597 dict_T *dict;
5598 char_u buf[2];
5599 qfline_T *qfp;
5600 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005601 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005602
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005603 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005604 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005605 qi = &ql_info;
5606 if (wp != NULL)
5607 {
5608 qi = GET_LOC_LIST(wp);
5609 if (qi == NULL)
5610 return FAIL;
5611 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005612 }
5613
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005614 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005615 qf_idx = qi->qf_curlist;
5616
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005617 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005618 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005619
Bram Moolenaard823fa92016-08-12 16:29:27 +02005620 qfp = qi->qf_lists[qf_idx].qf_start;
5621 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005622 {
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005623 /* Handle entries with a non-existing buffer number. */
5624 bufnum = qfp->qf_fnum;
5625 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5626 bufnum = 0;
5627
Bram Moolenaar05159a02005-02-26 23:04:13 +00005628 if ((dict = dict_alloc()) == NULL)
5629 return FAIL;
5630 if (list_append_dict(list, dict) == FAIL)
5631 return FAIL;
5632
5633 buf[0] = qfp->qf_type;
5634 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005635 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5636 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5637 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5638 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5639 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5640 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5641 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5642 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5643 || dict_add_string(dict, "type", buf) == FAIL
5644 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005645 return FAIL;
5646
5647 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005648 if (qfp == NULL)
5649 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005650 }
5651 return OK;
5652}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005653
5654/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005655 * Flags used by getqflist()/getloclist() to determine which fields to return.
5656 */
5657enum {
5658 QF_GETLIST_NONE = 0x0,
5659 QF_GETLIST_TITLE = 0x1,
5660 QF_GETLIST_ITEMS = 0x2,
5661 QF_GETLIST_NR = 0x4,
5662 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005663 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005664 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005665 QF_GETLIST_IDX = 0x40,
5666 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005667 QF_GETLIST_TICK = 0x100,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005668 QF_GETLIST_ALL = 0x1FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005669};
5670
5671/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005672 * Parse text from 'di' and return the quickfix list items.
5673 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005674 */
5675 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005676qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005677{
5678 int status = FAIL;
5679 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005680 char_u *errorformat = p_efm;
5681 dictitem_T *efm_di;
5682 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005683
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005684 /* Only a List value is supported */
5685 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005686 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005687 /* If errorformat is supplied then use it, otherwise use the 'efm'
5688 * option setting
5689 */
5690 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5691 {
5692 if (efm_di->di_tv.v_type != VAR_STRING ||
5693 efm_di->di_tv.vval.v_string == NULL)
5694 return FAIL;
5695 errorformat = efm_di->di_tv.vval.v_string;
5696 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005697
Bram Moolenaar36538222017-09-02 19:51:44 +02005698 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005699 if (l == NULL)
5700 return FAIL;
5701
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005702 qi = ll_new_list();
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005703 if (qi != NULL)
5704 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005705 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005706 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5707 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005708 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005709 qf_free(qi, 0);
5710 }
5711 free(qi);
5712 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005713 dict_add_list(retdict, "items", l);
5714 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005715 }
5716
5717 return status;
5718}
5719
5720/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005721 * Return the quickfix/location list window identifier in the current tabpage.
5722 */
5723 static int
5724qf_winid(qf_info_T *qi)
5725{
5726 win_T *win;
5727
5728 /* The quickfix window can be opened even if the quickfix list is not set
5729 * using ":copen". This is not true for location lists. */
5730 if (qi == NULL)
5731 return 0;
5732 win = qf_find_win(qi);
5733 if (win != NULL)
5734 return win->w_id;
5735 return 0;
5736}
5737
5738/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005739 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005740 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005741 static int
5742qf_getprop_keys2flags(dict_T *what)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005743{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005744 int flags = QF_GETLIST_NONE;
5745
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005746 if (dict_find(what, (char_u *)"all", -1) != NULL)
5747 flags |= QF_GETLIST_ALL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005748
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005749 if (dict_find(what, (char_u *)"title", -1) != NULL)
5750 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005751
Bram Moolenaara6d48492017-12-12 22:45:31 +01005752 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5753 flags |= QF_GETLIST_NR;
5754
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005755 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5756 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005757
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005758 if (dict_find(what, (char_u *)"context", -1) != NULL)
5759 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005760
Bram Moolenaara6d48492017-12-12 22:45:31 +01005761 if (dict_find(what, (char_u *)"id", -1) != NULL)
5762 flags |= QF_GETLIST_ID;
5763
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005764 if (dict_find(what, (char_u *)"items", -1) != NULL)
5765 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005766
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005767 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5768 flags |= QF_GETLIST_IDX;
5769
5770 if (dict_find(what, (char_u *)"size", -1) != NULL)
5771 flags |= QF_GETLIST_SIZE;
5772
Bram Moolenaarb254af32017-12-18 19:48:58 +01005773 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5774 flags |= QF_GETLIST_TICK;
5775
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005776 return flags;
5777}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005778
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005779/*
5780 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5781 * If 'nr' and 'id' are not present in 'what' then return the current
5782 * quickfix list index.
5783 * If 'nr' is zero then return the current quickfix list index.
5784 * If 'nr' is '$' then return the last quickfix list index.
5785 * If 'id' is present then return the index of the quickfix list with that id.
5786 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5787 * Return -1, if quickfix list is not present or if the stack is empty.
5788 */
5789 static int
5790qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5791{
5792 int qf_idx;
5793 dictitem_T *di;
5794
5795 qf_idx = qi->qf_curlist; /* default is the current list */
5796 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5797 {
5798 /* Use the specified quickfix/location list */
5799 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005800 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005801 /* for zero use the current list */
5802 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005803 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005804 qf_idx = di->di_tv.vval.v_number - 1;
5805 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005806 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005807 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005808 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005809 else if (di->di_tv.v_type == VAR_STRING
5810 && di->di_tv.vval.v_string != NULL
5811 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
5812 /* Get the last quickfix list number */
5813 qf_idx = qi->qf_listcount - 1;
5814 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005815 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005816 }
5817
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005818 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5819 {
5820 /* Look for a list with the specified id */
5821 if (di->di_tv.v_type == VAR_NUMBER)
5822 {
5823 /*
5824 * For zero, use the current list or the list specified by 'nr'
5825 */
5826 if (di->di_tv.vval.v_number != 0)
5827 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5828 }
5829 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005830 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005831 }
5832
5833 return qf_idx;
5834}
5835
5836/*
5837 * Return default values for quickfix list properties in retdict.
5838 */
5839 static int
5840qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5841{
5842 int status = OK;
5843
5844 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005845 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005846 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5847 {
5848 list_T *l = list_alloc();
5849 if (l != NULL)
5850 status = dict_add_list(retdict, "items", l);
5851 else
5852 status = FAIL;
5853 }
5854 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005855 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005856 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005857 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005858 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005859 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005860 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005861 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005862 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005863 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005864 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005865 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005866 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005867 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005868
5869 return status;
5870}
5871
5872/*
5873 * Return the quickfix list title as 'title' in retdict
5874 */
5875 static int
5876qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5877{
Bram Moolenaare0be1672018-07-08 16:50:37 +02005878 return dict_add_string(retdict, "title", qi->qf_lists[qf_idx].qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005879}
5880
5881/*
5882 * Return the quickfix list items/entries as 'items' in retdict
5883 */
5884 static int
5885qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5886{
5887 int status = OK;
5888 list_T *l = list_alloc();
5889 if (l != NULL)
5890 {
5891 (void)get_errorlist(qi, NULL, qf_idx, l);
5892 dict_add_list(retdict, "items", l);
5893 }
5894 else
5895 status = FAIL;
5896
5897 return status;
5898}
5899
5900/*
5901 * Return the quickfix list context (if any) as 'context' in retdict.
5902 */
5903 static int
5904qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5905{
5906 int status;
5907 dictitem_T *di;
5908
5909 if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5910 {
5911 di = dictitem_alloc((char_u *)"context");
5912 if (di != NULL)
5913 {
5914 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5915 status = dict_add(retdict, di);
5916 if (status == FAIL)
5917 dictitem_free(di);
5918 }
5919 else
5920 status = FAIL;
5921 }
5922 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02005923 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005924
5925 return status;
5926}
5927
5928/*
5929 * Return the quickfix list index as 'idx' in retdict
5930 */
5931 static int
5932qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5933{
5934 int idx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005935 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005936 /* For empty lists, qf_index is set to 1 */
5937 idx = 0;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005938 return dict_add_number(retdict, "idx", idx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005939}
5940
5941/*
5942 * Return quickfix/location list details (title) as a
5943 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5944 * then current list is used. Otherwise the specified list is used.
5945 */
5946 int
5947qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5948{
5949 qf_info_T *qi = &ql_info;
5950 int status = OK;
5951 int qf_idx;
5952 dictitem_T *di;
5953 int flags = QF_GETLIST_NONE;
5954
5955 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5956 return qf_get_list_from_lines(what, di, retdict);
5957
5958 if (wp != NULL)
5959 qi = GET_LOC_LIST(wp);
5960
5961 flags = qf_getprop_keys2flags(what);
5962
5963 if (qi != NULL && qi->qf_listcount != 0)
5964 qf_idx = qf_getprop_qfidx(qi, what);
5965
Bram Moolenaara6d48492017-12-12 22:45:31 +01005966 /* List is not present or is empty */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005967 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005968 return qf_getprop_defaults(qi, flags, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01005969
Bram Moolenaard823fa92016-08-12 16:29:27 +02005970 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005971 status = qf_getprop_title(qi, qf_idx, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005972 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005973 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005974 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005975 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005976 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005977 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005978 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005979 status = qf_getprop_ctx(qi, qf_idx, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005980 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005981 status = dict_add_number(retdict, "id", qi->qf_lists[qf_idx].qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005982 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005983 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005984 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005985 status = dict_add_number(retdict, "size",
5986 qi->qf_lists[qf_idx].qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005987 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02005988 status = dict_add_number(retdict, "changedtick",
5989 qi->qf_lists[qf_idx].qf_changedtick);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005990
Bram Moolenaard823fa92016-08-12 16:29:27 +02005991 return status;
5992}
5993
5994/*
5995 * Add list of entries to quickfix/location list. Each list entry is
5996 * a dictionary with item information.
5997 */
5998 static int
5999qf_add_entries(
6000 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006001 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006002 list_T *list,
6003 char_u *title,
6004 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006005{
6006 listitem_T *li;
6007 dict_T *d;
Bram Moolenaard76ce852018-05-01 15:02:04 +02006008 char_u *filename, *module, *pattern, *text, *type;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006009 int bufnum;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006010 long lnum;
6011 int col, nr;
6012 int vcol;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006013 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006014 int valid, status;
6015 int retval = OK;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006016 int did_bufnr_emsg = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006017
Bram Moolenaara3921f42017-06-04 15:30:34 +02006018 if (action == ' ' || qf_idx == qi->qf_listcount)
6019 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006020 /* make place for a new list */
Bram Moolenaar94116152012-11-28 17:41:59 +01006021 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006022 qf_idx = qi->qf_curlist;
6023 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006024 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006025 /* Adding to existing list, use last entry. */
Bram Moolenaara3921f42017-06-04 15:30:34 +02006026 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006027 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006028 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006029 qf_free_items(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006030 qf_store_title(qi, qf_idx, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006031 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006032
6033 for (li = list->lv_first; li != NULL; li = li->li_next)
6034 {
6035 if (li->li_tv.v_type != VAR_DICT)
6036 continue; /* Skip non-dict items */
6037
6038 d = li->li_tv.vval.v_dict;
6039 if (d == NULL)
6040 continue;
6041
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006042 filename = get_dict_string(d, (char_u *)"filename", TRUE);
Bram Moolenaard76ce852018-05-01 15:02:04 +02006043 module = get_dict_string(d, (char_u *)"module", TRUE);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006044 bufnum = (int)get_dict_number(d, (char_u *)"bufnr");
6045 lnum = (int)get_dict_number(d, (char_u *)"lnum");
6046 col = (int)get_dict_number(d, (char_u *)"col");
6047 vcol = (int)get_dict_number(d, (char_u *)"vcol");
6048 nr = (int)get_dict_number(d, (char_u *)"nr");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006049 type = get_dict_string(d, (char_u *)"type", TRUE);
6050 pattern = get_dict_string(d, (char_u *)"pattern", TRUE);
6051 text = get_dict_string(d, (char_u *)"text", TRUE);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006052 if (text == NULL)
6053 text = vim_strsave((char_u *)"");
6054
6055 valid = TRUE;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006056 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006057 valid = FALSE;
6058
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006059 /* Mark entries with non-existing buffer number as not valid. Give the
6060 * error message only once. */
6061 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6062 {
6063 if (!did_bufnr_emsg)
6064 {
6065 did_bufnr_emsg = TRUE;
6066 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6067 }
6068 valid = FALSE;
6069 bufnum = 0;
6070 }
6071
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02006072 /* If the 'valid' field is present it overrules the detected value. */
6073 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
6074 valid = (int)get_dict_number(d, (char_u *)"valid");
6075
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02006076 status = qf_add_entry(qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006077 qf_idx,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006078 NULL, /* dir */
6079 filename,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006080 module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00006081 bufnum,
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006082 text,
6083 lnum,
6084 col,
6085 vcol, /* vis_col */
6086 pattern, /* search pattern */
6087 nr,
6088 type == NULL ? NUL : *type,
6089 valid);
6090
6091 vim_free(filename);
Bram Moolenaard76ce852018-05-01 15:02:04 +02006092 vim_free(module);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006093 vim_free(pattern);
6094 vim_free(text);
6095 vim_free(type);
6096
6097 if (status == FAIL)
6098 {
6099 retval = FAIL;
6100 break;
6101 }
6102 }
6103
Bram Moolenaara3921f42017-06-04 15:30:34 +02006104 if (qi->qf_lists[qf_idx].qf_index == 0)
Bram Moolenaard236ac02011-05-05 17:14:14 +02006105 /* no valid entry */
Bram Moolenaara3921f42017-06-04 15:30:34 +02006106 qi->qf_lists[qf_idx].qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006107 else
Bram Moolenaara3921f42017-06-04 15:30:34 +02006108 qi->qf_lists[qf_idx].qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006109 if (action != 'a')
6110 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006111 qi->qf_lists[qf_idx].qf_ptr =
6112 qi->qf_lists[qf_idx].qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006113 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara3921f42017-06-04 15:30:34 +02006114 qi->qf_lists[qf_idx].qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006115 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006116
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006117 /* Don't update the cursor in quickfix window when appending entries */
Bram Moolenaar864293a2016-06-02 13:40:04 +02006118 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006119
6120 return retval;
6121}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006122
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006123/*
6124 * Get the quickfix list index from 'nr' or 'id'
6125 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006126 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006127qf_setprop_get_qfidx(
6128 qf_info_T *qi,
6129 dict_T *what,
6130 int action,
6131 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006132{
6133 dictitem_T *di;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006134 int qf_idx = qi->qf_curlist; /* default is the current list */
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006135
Bram Moolenaard823fa92016-08-12 16:29:27 +02006136 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6137 {
6138 /* Use the specified quickfix/location list */
6139 if (di->di_tv.v_type == VAR_NUMBER)
6140 {
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006141 /* for zero use the current list */
6142 if (di->di_tv.vval.v_number != 0)
6143 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006144
Bram Moolenaar55b69262017-08-13 13:42:01 +02006145 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6146 {
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006147 /*
6148 * When creating a new list, accept qf_idx pointing to the next
Bram Moolenaar55b69262017-08-13 13:42:01 +02006149 * non-available list and add the new list at the end of the
6150 * stack.
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006151 */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006152 *newlist = TRUE;
6153 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006154 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006155 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006156 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006157 else if (action != ' ')
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006158 *newlist = FALSE; /* use the specified list */
Bram Moolenaar55b69262017-08-13 13:42:01 +02006159 }
6160 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006161 && di->di_tv.vval.v_string != NULL
6162 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006163 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006164 if (qi->qf_listcount > 0)
6165 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006166 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006167 qf_idx = 0;
6168 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006169 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006170 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006171 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006172 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006173 }
6174
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006175 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006176 {
6177 /* Use the quickfix/location list with the specified id */
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006178 if (di->di_tv.v_type != VAR_NUMBER)
6179 return INVALID_QFIDX;
6180
6181 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006182 }
6183
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006184 return qf_idx;
6185}
6186
6187/*
6188 * Set the quickfix list title.
6189 */
6190 static int
6191qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6192{
6193 if (di->di_tv.v_type != VAR_STRING)
6194 return FAIL;
6195
6196 vim_free(qi->qf_lists[qf_idx].qf_title);
6197 qi->qf_lists[qf_idx].qf_title =
6198 get_dict_string(what, (char_u *)"title", TRUE);
6199 if (qf_idx == qi->qf_curlist)
6200 qf_update_win_titlevar(qi);
6201
6202 return OK;
6203}
6204
6205/*
6206 * Set quickfix list items/entries.
6207 */
6208 static int
6209qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6210{
6211 int retval = FAIL;
6212 char_u *title_save;
6213
6214 if (di->di_tv.v_type != VAR_LIST)
6215 return FAIL;
6216
6217 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6218 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6219 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006220 vim_free(title_save);
6221
6222 return retval;
6223}
6224
6225/*
6226 * Set quickfix list items/entries from a list of lines.
6227 */
6228 static int
6229qf_setprop_items_from_lines(
6230 qf_info_T *qi,
6231 int qf_idx,
6232 dict_T *what,
6233 dictitem_T *di,
6234 int action)
6235{
6236 char_u *errorformat = p_efm;
6237 dictitem_T *efm_di;
6238 int retval = FAIL;
6239
6240 /* Use the user supplied errorformat settings (if present) */
6241 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6242 {
6243 if (efm_di->di_tv.v_type != VAR_STRING ||
6244 efm_di->di_tv.vval.v_string == NULL)
6245 return FAIL;
6246 errorformat = efm_di->di_tv.vval.v_string;
6247 }
6248
6249 /* Only a List value is supported */
6250 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6251 return FAIL;
6252
6253 if (action == 'r')
6254 qf_free_items(qi, qf_idx);
6255 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6256 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6257 retval = OK;
6258
6259 return retval;
6260}
6261
6262/*
6263 * Set quickfix list context.
6264 */
6265 static int
6266qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di)
6267{
6268 typval_T *ctx;
6269
6270 free_tv(qi->qf_lists[qf_idx].qf_ctx);
6271 ctx = alloc_tv();
6272 if (ctx != NULL)
6273 copy_tv(&di->di_tv, ctx);
6274 qi->qf_lists[qf_idx].qf_ctx = ctx;
6275
6276 return OK;
6277}
6278
6279/*
6280 * Set quickfix/location list properties (title, items, context).
6281 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006282 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006283 */
6284 static int
6285qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6286{
6287 dictitem_T *di;
6288 int retval = FAIL;
6289 int qf_idx;
6290 int newlist = FALSE;
6291
6292 if (action == ' ' || qi->qf_curlist == qi->qf_listcount)
6293 newlist = TRUE;
6294
6295 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
6296 if (qf_idx == INVALID_QFIDX) /* List not found */
6297 return FAIL;
6298
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006299 if (newlist)
6300 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006301 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006302 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006303 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006304 }
6305
6306 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006307 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006308 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006309 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006310 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006311 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006312 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006313 retval = qf_setprop_context(qi, qf_idx, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006314
Bram Moolenaarb254af32017-12-18 19:48:58 +01006315 if (retval == OK)
6316 qf_list_changed(qi, qf_idx);
6317
Bram Moolenaard823fa92016-08-12 16:29:27 +02006318 return retval;
6319}
6320
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006321/*
6322 * Find the non-location list window with the specified location list.
6323 */
6324 static win_T *
6325find_win_with_ll(qf_info_T *qi)
6326{
6327 win_T *wp = NULL;
6328
6329 FOR_ALL_WINDOWS(wp)
6330 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6331 return wp;
6332
6333 return NULL;
6334}
6335
6336/*
6337 * Free the entire quickfix/location list stack.
6338 * If the quickfix/location list window is open, then clear it.
6339 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006340 static void
6341qf_free_stack(win_T *wp, qf_info_T *qi)
6342{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006343 win_T *qfwin = qf_find_win(qi);
6344 win_T *llwin = NULL;
6345 win_T *orig_wp = wp;
6346
6347 if (qfwin != NULL)
6348 {
6349 /* If the quickfix/location list window is open, then clear it */
6350 if (qi->qf_curlist < qi->qf_listcount)
6351 qf_free(qi, qi->qf_curlist);
6352 qf_update_buffer(qi, NULL);
6353 }
6354
6355 if (wp != NULL && IS_LL_WINDOW(wp))
6356 {
6357 /* If in the location list window, then use the non-location list
6358 * window with this location list (if present)
6359 */
6360 llwin = find_win_with_ll(qi);
6361 if (llwin != NULL)
6362 wp = llwin;
6363 }
6364
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006365 qf_free_all(wp);
6366 if (wp == NULL)
6367 {
6368 /* quickfix list */
6369 qi->qf_curlist = 0;
6370 qi->qf_listcount = 0;
6371 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006372 else if (IS_LL_WINDOW(orig_wp))
6373 {
6374 /* If the location list window is open, then create a new empty
6375 * location list */
6376 qf_info_T *new_ll = ll_new_list();
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006377
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006378 /* first free the list reference in the location list window */
6379 ll_free_all(&orig_wp->w_llist_ref);
6380
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006381 orig_wp->w_llist_ref = new_ll;
6382 if (llwin != NULL)
6383 {
6384 llwin->w_llist = new_ll;
6385 new_ll->qf_refcount++;
6386 }
6387 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006388}
6389
Bram Moolenaard823fa92016-08-12 16:29:27 +02006390/*
6391 * Populate the quickfix list with the items supplied in the list
6392 * of dictionaries. "title" will be copied to w:quickfix_title.
6393 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6394 */
6395 int
6396set_errorlist(
6397 win_T *wp,
6398 list_T *list,
6399 int action,
6400 char_u *title,
6401 dict_T *what)
6402{
6403 qf_info_T *qi = &ql_info;
6404 int retval = OK;
6405
6406 if (wp != NULL)
6407 {
6408 qi = ll_get_or_alloc_list(wp);
6409 if (qi == NULL)
6410 return FAIL;
6411 }
6412
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006413 if (action == 'f')
6414 {
6415 /* Free the entire quickfix or location list stack */
6416 qf_free_stack(wp, qi);
6417 }
6418 else if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006419 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006420 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006421 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006422 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006423 if (retval == OK)
6424 qf_list_changed(qi, qi->qf_curlist);
6425 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006426
6427 return retval;
6428}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006429
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006430/*
6431 * Mark the context as in use for all the lists in a quickfix stack.
6432 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006433 static int
6434mark_quickfix_ctx(qf_info_T *qi, int copyID)
6435{
6436 int i;
6437 int abort = FALSE;
6438 typval_T *ctx;
6439
6440 for (i = 0; i < LISTCOUNT && !abort; ++i)
6441 {
6442 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006443 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6444 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006445 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6446 }
6447
6448 return abort;
6449}
6450
6451/*
6452 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006453 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006454 */
6455 int
6456set_ref_in_quickfix(int copyID)
6457{
6458 int abort = FALSE;
6459 tabpage_T *tp;
6460 win_T *win;
6461
6462 abort = mark_quickfix_ctx(&ql_info, copyID);
6463 if (abort)
6464 return abort;
6465
6466 FOR_ALL_TAB_WINDOWS(tp, win)
6467 {
6468 if (win->w_llist != NULL)
6469 {
6470 abort = mark_quickfix_ctx(win->w_llist, copyID);
6471 if (abort)
6472 return abort;
6473 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006474 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6475 {
6476 /* In a location list window and none of the other windows is
6477 * referring to this location list. Mark the location list
6478 * context as still in use.
6479 */
6480 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6481 if (abort)
6482 return abort;
6483 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006484 }
6485
6486 return abort;
6487}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006488#endif
6489
Bram Moolenaar81695252004-12-29 20:58:21 +00006490/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006491 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006492 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006493 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006494 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006495 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006496 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006497 */
6498 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006499ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006500{
6501 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006502 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006503 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006504 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006505 int_u save_qfid;
6506 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006507
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006508 switch (eap->cmdidx)
6509 {
6510 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6511 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6512 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6513 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6514 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6515 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6516 default: break;
6517 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006518 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6519 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006520 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006521#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006522 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006523 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006524#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006525 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006526
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006527 /* Must come after autocommands. */
Bram Moolenaar39665952018-08-15 20:59:48 +02006528 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006529 {
6530 qi = ll_get_or_alloc_list(curwin);
6531 if (qi == NULL)
6532 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006533 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006534 }
6535
Bram Moolenaar86b68352004-12-27 21:59:20 +00006536 if (*eap->arg == NUL)
6537 buf = curbuf;
6538 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6539 buf = buflist_findnr(atoi((char *)eap->arg));
6540 if (buf == NULL)
6541 EMSG(_(e_invarg));
6542 else if (buf->b_ml.ml_mfp == NULL)
6543 EMSG(_("E681: Buffer is not loaded"));
6544 else
6545 {
6546 if (eap->addr_count == 0)
6547 {
6548 eap->line1 = 1;
6549 eap->line2 = buf->b_ml.ml_line_count;
6550 }
6551 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6552 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6553 EMSG(_(e_invrange));
6554 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006555 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006556 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006557
6558 if (buf->b_sfname)
6559 {
6560 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6561 (char *)qf_title, (char *)buf->b_sfname);
6562 qf_title = IObuff;
6563 }
6564
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006565 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006566 (eap->cmdidx != CMD_caddbuffer
6567 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006568 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006569 qf_title, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006570 if (res >= 0)
6571 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006572
6573 // Remember the current quickfix list identifier, so that we can
6574 // check for autocommands changing the current quickfix list.
6575 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006576 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006577 {
6578 buf_T *curbuf_old = curbuf;
6579
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006580 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6581 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006582 if (curbuf != curbuf_old)
6583 // Autocommands changed buffer, don't jump now, "qi" may
6584 // be invalid.
6585 res = 0;
6586 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006587 // Jump to the first error for a new list and if autocmds didn't
6588 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006589 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006590 eap->cmdidx == CMD_lbuffer)
6591 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006592 // display the first error
6593 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar754b5602006-02-09 23:53:20 +00006594 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006595 }
6596}
6597
Bram Moolenaar1e015462005-09-25 22:16:38 +00006598#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006599/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006600 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6601 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006602 */
6603 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006604ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006605{
6606 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006607 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006608 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006609 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006610 int_u save_qfid;
6611 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006612
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006613 switch (eap->cmdidx)
6614 {
6615 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6616 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6617 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6618 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6619 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6620 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6621 default: break;
6622 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006623 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6624 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006625 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006626#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006627 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006628 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006629#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006630 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006631
Bram Moolenaar39665952018-08-15 20:59:48 +02006632 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006633 {
6634 qi = ll_get_or_alloc_list(curwin);
6635 if (qi == NULL)
6636 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006637 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006638 }
6639
Bram Moolenaar4770d092006-01-12 23:22:24 +00006640 /* Evaluate the expression. When the result is a string or a list we can
6641 * use it to fill the errorlist. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006642 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006643 if (tv != NULL)
6644 {
6645 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6646 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6647 {
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006648 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006649 (eap->cmdidx != CMD_caddexpr
6650 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006651 (linenr_T)0, (linenr_T)0,
6652 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006653 if (res >= 0)
6654 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006655
6656 // Remember the current quickfix list identifier, so that we can
6657 // check for autocommands changing the current quickfix list.
6658 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006659 if (au_name != NULL)
6660 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6661 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006662
6663 // Jump to the first error for a new list and if autocmds didn't
6664 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006665 if (res > 0 && (eap->cmdidx == CMD_cexpr
6666 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006667 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006668 // display the first error
6669 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006670 }
6671 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006672 EMSG(_("E777: String or List expected"));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006673 free_tv(tv);
6674 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006675}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006676#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006677
6678/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006679 * Get the location list for ":lhelpgrep"
6680 */
6681 static qf_info_T *
6682hgr_get_ll(int *new_ll)
6683{
6684 win_T *wp;
6685 qf_info_T *qi;
6686
6687 /* If the current window is a help window, then use it */
6688 if (bt_help(curwin->w_buffer))
6689 wp = curwin;
6690 else
6691 /* Find an existing help window */
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006692 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006693
6694 if (wp == NULL) /* Help window not found */
6695 qi = NULL;
6696 else
6697 qi = wp->w_llist;
6698
6699 if (qi == NULL)
6700 {
6701 /* Allocate a new location list for help text matches */
6702 if ((qi = ll_new_list()) == NULL)
6703 return NULL;
6704 *new_ll = TRUE;
6705 }
6706
6707 return qi;
6708}
6709
6710/*
6711 * Search for a pattern in a help file.
6712 */
6713 static void
6714hgr_search_file(
6715 qf_info_T *qi,
6716 char_u *fname,
6717#ifdef FEAT_MBYTE
6718 vimconv_T *p_vc,
6719#endif
6720 regmatch_T *p_regmatch)
6721{
6722 FILE *fd;
6723 long lnum;
6724
6725 fd = mch_fopen((char *)fname, "r");
6726 if (fd == NULL)
6727 return;
6728
6729 lnum = 1;
6730 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
6731 {
6732 char_u *line = IObuff;
6733#ifdef FEAT_MBYTE
6734 /* Convert a line if 'encoding' is not utf-8 and
6735 * the line contains a non-ASCII character. */
6736 if (p_vc->vc_type != CONV_NONE
6737 && has_non_ascii(IObuff))
6738 {
6739 line = string_convert(p_vc, IObuff, NULL);
6740 if (line == NULL)
6741 line = IObuff;
6742 }
6743#endif
6744
6745 if (vim_regexec(p_regmatch, line, (colnr_T)0))
6746 {
6747 int l = (int)STRLEN(line);
6748
6749 /* remove trailing CR, LF, spaces, etc. */
6750 while (l > 0 && line[l - 1] <= ' ')
6751 line[--l] = NUL;
6752
6753 if (qf_add_entry(qi,
6754 qi->qf_curlist,
6755 NULL, /* dir */
6756 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02006757 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006758 0,
6759 line,
6760 lnum,
6761 (int)(p_regmatch->startp[0] - line)
6762 + 1, /* col */
6763 FALSE, /* vis_col */
6764 NULL, /* search pattern */
6765 0, /* nr */
6766 1, /* type */
6767 TRUE /* valid */
6768 ) == FAIL)
6769 {
6770 got_int = TRUE;
6771#ifdef FEAT_MBYTE
6772 if (line != IObuff)
6773 vim_free(line);
6774#endif
6775 break;
6776 }
6777 }
6778#ifdef FEAT_MBYTE
6779 if (line != IObuff)
6780 vim_free(line);
6781#endif
6782 ++lnum;
6783 line_breakcheck();
6784 }
6785 fclose(fd);
6786}
6787
6788/*
6789 * Search for a pattern in all the help files in the doc directory under
6790 * the given directory.
6791 */
6792 static void
6793hgr_search_files_in_dir(
6794 qf_info_T *qi,
6795 char_u *dirname,
6796 regmatch_T *p_regmatch
6797#ifdef FEAT_MBYTE
6798 , vimconv_T *p_vc
6799#endif
6800#ifdef FEAT_MULTI_LANG
6801 , char_u *lang
6802#endif
6803 )
6804{
6805 int fcount;
6806 char_u **fnames;
6807 int fi;
6808
6809 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */
6810 add_pathsep(dirname);
6811 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
6812 if (gen_expand_wildcards(1, &dirname, &fcount,
6813 &fnames, EW_FILE|EW_SILENT) == OK
6814 && fcount > 0)
6815 {
6816 for (fi = 0; fi < fcount && !got_int; ++fi)
6817 {
6818#ifdef FEAT_MULTI_LANG
6819 /* Skip files for a different language. */
6820 if (lang != NULL
6821 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02006822 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006823 && !(STRNICMP(lang, "en", 2) == 0
6824 && STRNICMP("txt", fnames[fi]
6825 + STRLEN(fnames[fi]) - 3, 3) == 0))
6826 continue;
6827#endif
6828
6829 hgr_search_file(qi, fnames[fi],
6830#ifdef FEAT_MBYTE
6831 p_vc,
6832#endif
6833 p_regmatch);
6834 }
6835 FreeWild(fcount, fnames);
6836 }
6837}
6838
6839/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006840 * Search for a pattern in all the help files in the 'runtimepath'
6841 * and add the matches to a quickfix list.
6842 * 'arg' is the language specifier. If supplied, then only matches in the
6843 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006844 */
6845 static void
6846hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *arg)
6847{
6848 char_u *p;
6849#ifdef FEAT_MULTI_LANG
6850 char_u *lang;
6851#endif
6852
6853#ifdef FEAT_MBYTE
6854 vimconv_T vc;
6855
6856 /* Help files are in utf-8 or latin1, convert lines when 'encoding'
6857 * differs. */
6858 vc.vc_type = CONV_NONE;
6859 if (!enc_utf8)
6860 convert_setup(&vc, (char_u *)"utf-8", p_enc);
6861#endif
6862
6863#ifdef FEAT_MULTI_LANG
6864 /* Check for a specified language */
6865 lang = check_help_lang(arg);
6866#endif
6867
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006868 /* Go through all the directories in 'runtimepath' */
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006869 p = p_rtp;
6870 while (*p != NUL && !got_int)
6871 {
6872 copy_option_part(&p, NameBuff, MAXPATHL, ",");
6873
6874 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
6875#ifdef FEAT_MBYTE
6876 , &vc
6877#endif
6878#ifdef FEAT_MULTI_LANG
6879 , lang
6880#endif
6881 );
6882 }
6883
6884#ifdef FEAT_MBYTE
6885 if (vc.vc_type != CONV_NONE)
6886 convert_setup(&vc, NULL, NULL);
6887#endif
6888}
6889
6890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 * ":helpgrep {pattern}"
6892 */
6893 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006894ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
6896 regmatch_T regmatch;
6897 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006898 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006899 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006900 char_u *au_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901
Bram Moolenaar73633f82012-01-20 13:39:07 +01006902 switch (eap->cmdidx)
6903 {
6904 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
6905 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
6906 default: break;
6907 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006908 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6909 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01006910 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006911#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006912 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01006913 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01006914#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006915 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006916
6917 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6918 save_cpo = p_cpo;
6919 p_cpo = empty_option;
6920
Bram Moolenaar39665952018-08-15 20:59:48 +02006921 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006922 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006923 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006924 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006925 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006926 }
6927
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
6929 regmatch.rm_ic = FALSE;
6930 if (regmatch.regprog != NULL)
6931 {
6932 /* create a new quickfix list */
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006933 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006935 hgr_search_in_rtp(qi, &regmatch, eap->arg);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01006936
Bram Moolenaar473de612013-06-08 18:19:48 +02006937 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006939 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
6940 qi->qf_lists[qi->qf_curlist].qf_ptr =
6941 qi->qf_lists[qi->qf_curlist].qf_start;
6942 qi->qf_lists[qi->qf_curlist].qf_index = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
6944
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006945 if (p_cpo == empty_option)
6946 p_cpo = save_cpo;
6947 else
6948 /* Darn, some plugin changed the value. */
6949 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950
Bram Moolenaarb254af32017-12-18 19:48:58 +01006951 qf_list_changed(qi, qi->qf_curlist);
Bram Moolenaar864293a2016-06-02 13:40:04 +02006952 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
Bram Moolenaar73633f82012-01-20 13:39:07 +01006954 if (au_name != NULL)
6955 {
6956 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6957 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02006958 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL)
Bram Moolenaar73633f82012-01-20 13:39:07 +01006959 /* autocommands made "qi" invalid */
6960 return;
6961 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01006962
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 /* Jump to first match. */
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006964 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006965 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006966 else
6967 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006968
6969 if (eap->cmdidx == CMD_lhelpgrep)
6970 {
6971 /* If the help window is not opened or if it already points to the
Bram Moolenaar754b5602006-02-09 23:53:20 +00006972 * correct location list, then free the new location list. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006973 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006974 {
6975 if (new_qi)
6976 ll_free_all(&qi);
6977 }
6978 else if (curwin->w_llist == NULL)
6979 curwin->w_llist = qi;
6980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981}
6982
6983#endif /* FEAT_QUICKFIX */