blob: 899b63689cfc9a3b00a67374ff86175fee4f8454 [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 Moolenaar00bf8cd2018-10-07 20:26:20 +020030 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
36 char_u *qf_module; // module name for this error
37 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
42 // :helpgrep
43 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/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010053 * Quickfix list type.
54 */
55typedef enum
56{
57 QFLT_QUICKFIX, // Quickfix list - global list
58 QFLT_LOCATION, // Location list - per window list
59 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
60} qfltype_T;
61
62/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020063 * Quickfix/Location list definition
64 * Contains a list of entries (qfline_T). qf_start points to the first entry
65 * and qf_last points to the last entry. qf_count contains the list size.
66 *
67 * Usually the list contains one or more entries. But an empty list can be
68 * created using setqflist()/setloclist() with a title and/or user context
69 * information and entries can be added later using setqflist()/setloclist().
70 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000071typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000072{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020073 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010074 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020075 qfline_T *qf_start; // pointer to the first error
76 qfline_T *qf_last; // pointer to the last error
77 qfline_T *qf_ptr; // pointer to the current error
78 int qf_count; // number of errors (0 means empty list)
79 int qf_index; // current index in the error list
80 int qf_nonevalid; // TRUE if not a single valid entry found
81 char_u *qf_title; // title derived from the command that created
82 // the error list or set by setqflist
83 typval_T *qf_ctx; // context set by setqflist/setloclist
Bram Moolenaara7df8c72017-07-19 13:23:06 +020084
85 struct dir_stack_T *qf_dir_stack;
86 char_u *qf_directory;
87 struct dir_stack_T *qf_file_stack;
88 char_u *qf_currfile;
89 int qf_multiline;
90 int qf_multiignore;
91 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010092 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000093} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020095/*
96 * Quickfix/Location list stack definition
97 * Contains a list of quickfix/location lists (qf_list_T)
98 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000099struct qf_info_S
100{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200101 // Count of references to this list. Used only for location lists.
102 // When a location list window reference this list, qf_refcount
103 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
104 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000105 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200106 int qf_listcount; // current number of lists
107 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000108 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100109 qfltype_T qfl_type; // type of list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110};
111
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200112static qf_info_T ql_info; // global quickfix list
113static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200115#define FMT_PATTERNS 11 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
117/*
118 * Structure used to hold the info of one part of 'errorformat'
119 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000120typedef struct efm_S efm_T;
121struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200123 regprog_T *prog; // pre-formatted part of 'errorformat'
124 efm_T *next; // pointer to next (NULL if last)
125 char_u addr[FMT_PATTERNS]; // indices of used % patterns
126 char_u prefix; // prefix of this format line:
127 // 'D' enter directory
128 // 'X' leave directory
129 // 'A' start of multi-line message
130 // 'E' error message
131 // 'W' warning message
132 // 'I' informational message
133 // 'C' continuation line
134 // 'Z' end of multi-line message
135 // 'G' general, unspecific message
136 // 'P' push file (partial) message
137 // 'Q' pop/quit file (partial) message
138 // 'O' overread (partial) message
139 char_u flags; // additional flags given in prefix
140 // '-' do not include this line
141 // '+' include whole line in message
142 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143};
144
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200145// List of location lists to be deleted.
146// Used to delay the deletion of locations lists by autocmds.
147typedef struct qf_delq_S
148{
149 struct qf_delq_S *next;
150 qf_info_T *qi;
151} qf_delq_T;
152static qf_delq_T *qf_delq_head = NULL;
153
154// Counter to prevent autocmds from freeing up location lists when they are
155// still being used.
156static int quickfix_busy = 0;
157
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200158static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100159
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaard76ce852018-05-01 15:02:04 +0200161static 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 Moolenaarfe15b7d2018-09-18 22:50:06 +0200162static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200164static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200165static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100166static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200167static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100169static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static win_T *qf_find_win(qf_info_T *qi);
171static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200172static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200173static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
175static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
176static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
177static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200179// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000180#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200181// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000182#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200183
184// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100185#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
186#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
187#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
188#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200189
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000190/*
191 * Return location list for window 'wp'
192 * For location list window, return the referenced location list
193 */
194#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200197 * Looking up a buffer can be slow if there are many. Remember the last one
198 * to make this a lot faster if there are multiple matches in the same file.
199 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200200static char_u *qf_last_bufname = NULL;
201static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200202
Bram Moolenaar3c097222017-12-21 20:54:49 +0100203static char *e_loc_list_changed =
204 N_("E926: Current location list was changed");
205
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200206/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200207 * Maximum number of bytes allowed per line while reading a errorfile.
208 */
209#define LINE_MAXLEN 4096
210
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200211static struct fmtpattern
212{
213 char_u convchar;
214 char *pattern;
215} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200216 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200217 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200218 {'n', "\\d\\+"},
219 {'l', "\\d\\+"},
220 {'c', "\\d\\+"},
221 {'t', "."},
222 {'m', ".\\+"},
223 {'r', ".*"},
224 {'p', "[- .]*"},
225 {'v', "\\d\\+"},
226 {'s', ".\\+"},
227 {'o', ".\\+"}
228 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200229
230/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200231 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200232 * See fmt_pat definition above for the list of supported patterns. The
233 * pattern specifier is supplied in "efmpat". The converted pattern is stored
234 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200235 */
236 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200237efmpat_to_regpat(
238 char_u *efmpat,
239 char_u *regpat,
240 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200241 int idx,
242 int round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200243 char_u *errmsg)
244{
245 char_u *srcptr;
246
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200247 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200248 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200249 // Each errorformat pattern can occur only once
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200250 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200251 _("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200252 EMSG(errmsg);
253 return NULL;
254 }
255 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200256 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200257 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200258 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200259 {
260 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200261 _("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200262 EMSG(errmsg);
263 return NULL;
264 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200265 efminfo->addr[idx] = (char_u)++round;
266 *regpat++ = '\\';
267 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200268#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200269 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200270 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200271 // Also match "c:" in the file name, even when
272 // checking for a colon next: "%f:".
273 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200274 STRCPY(regpat, "\\%(\\a:\\)\\=");
275 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200276 }
277#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200278 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200279 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200280 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200281 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200282 // A file name may contain spaces, but this isn't
283 // in "\f". For "%f:%l:%m" there may be a ":" in
284 // the file name. Use ".\{-1,}x" instead (x is
285 // the next character), the requirement that :999:
286 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200287 STRCPY(regpat, ".\\{-1,}");
288 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200289 }
290 else
291 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200292 // File name followed by '\\' or '%': include as
293 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200294 STRCPY(regpat, "\\f\\+");
295 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200296 }
297 }
298 else
299 {
300 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200301 while ((*regpat = *srcptr++) != NUL)
302 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200303 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200304 *regpat++ = '\\';
305 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200306
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200307 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200308}
309
310/*
311 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200312 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200313 */
314 static char_u *
315scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200316 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200317 char_u *efm,
318 int len,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200319 char_u *regpat,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200320 char_u *errmsg)
321{
322 char_u *efmp = *pefmp;
323
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200324 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200325 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200326 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200327 {
328 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200329 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200330 if (efmp < efm + len)
331 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200332 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200333 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200334 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200335 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200336 if (efmp == efm + len)
337 {
338 EMSG(_("E374: Missing ] in format string"));
339 return NULL;
340 }
341 }
342 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200343 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200344 *regpat++ = *++efmp;
345 *regpat++ = '\\';
346 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200347 }
348 else
349 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200350 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200351 sprintf((char *)errmsg,
352 _("E375: Unsupported %%%c in format string"), *efmp);
353 EMSG(errmsg);
354 return NULL;
355 }
356
357 *pefmp = efmp;
358
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200359 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200360}
361
362/*
363 * Analyze/parse an errorformat prefix.
364 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200365 static char_u *
366efm_analyze_prefix(char_u *efmp, efm_T *efminfo, char_u *errmsg)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200367{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200368 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200369 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200370 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200371 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200372 else
373 {
374 sprintf((char *)errmsg,
375 _("E376: Invalid %%%c in format string prefix"), *efmp);
376 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200377 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200378 }
379
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200380 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200381}
382
383/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200384 * Converts a 'errorformat' string part in 'efm' to a regular expression
385 * pattern. The resulting regex pattern is returned in "regpat". Additional
386 * information about the 'erroformat' pattern is returned in "fmt_ptr".
387 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200388 */
389 static int
390efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200391 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200392 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200393 efm_T *fmt_ptr,
394 char_u *regpat,
395 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200396{
397 char_u *ptr;
398 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200399 int round;
400 int idx = 0;
401
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200402 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200403 ptr = regpat;
404 *ptr++ = '^';
405 round = 0;
406 for (efmp = efm; efmp < efm + len; ++efmp)
407 {
408 if (*efmp == '%')
409 {
410 ++efmp;
411 for (idx = 0; idx < FMT_PATTERNS; ++idx)
412 if (fmt_pat[idx].convchar == *efmp)
413 break;
414 if (idx < FMT_PATTERNS)
415 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200416 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200417 errmsg);
418 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200419 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200420 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200421 }
422 else if (*efmp == '*')
423 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200424 ++efmp;
425 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr, errmsg);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200426 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200427 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200428 }
429 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200430 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200431 else if (*efmp == '#')
432 *ptr++ = '*';
433 else if (*efmp == '>')
434 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200435 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200436 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200437 // prefix is allowed only at the beginning of the errorformat
438 // option part
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200439 efmp = efm_analyze_prefix(efmp, fmt_ptr, errmsg);
440 if (efmp == NULL)
441 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200442 }
443 else
444 {
445 sprintf((char *)errmsg,
446 _("E377: Invalid %%%c in format string"), *efmp);
447 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200448 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200449 }
450 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200451 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200452 {
453 if (*efmp == '\\' && efmp + 1 < efm + len)
454 ++efmp;
455 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200456 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200457 if (*efmp)
458 *ptr++ = *efmp;
459 }
460 }
461 *ptr++ = '$';
462 *ptr = NUL;
463
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200464 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200465}
466
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200467/*
468 * Free the 'errorformat' information list
469 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200470 static void
471free_efm_list(efm_T **efm_first)
472{
473 efm_T *efm_ptr;
474
475 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
476 {
477 *efm_first = efm_ptr->next;
478 vim_regfree(efm_ptr->prog);
479 vim_free(efm_ptr);
480 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100481 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200482}
483
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200484/*
485 * Compute the size of the buffer used to convert a 'errorformat' pattern into
486 * a regular expression pattern.
487 */
488 static int
489efm_regpat_bufsz(char_u *efm)
490{
491 int sz;
492 int i;
493
494 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
495 for (i = FMT_PATTERNS; i > 0; )
496 sz += (int)STRLEN(fmt_pat[--i].pattern);
497#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200498 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200499#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200500 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200501#endif
502
503 return sz;
504}
505
506/*
507 * Return the length of a 'errorformat' option part (separated by ",").
508 */
509 static int
510efm_option_part_len(char_u *efm)
511{
512 int len;
513
514 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
515 if (efm[len] == '\\' && efm[len + 1] != NUL)
516 ++len;
517
518 return len;
519}
520
521/*
522 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
523 * are parsed and converted to regular expressions. Returns information about
524 * the parsed 'errorformat' option.
525 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200526 static efm_T *
527parse_efm_option(char_u *efm)
528{
529 char_u *errmsg = NULL;
530 int errmsglen;
531 efm_T *fmt_ptr = NULL;
532 efm_T *fmt_first = NULL;
533 efm_T *fmt_last = NULL;
534 char_u *fmtstr = NULL;
535 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200536 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200537
538 errmsglen = CMDBUFFSIZE + 1;
539 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
540 if (errmsg == NULL)
541 goto parse_efm_end;
542
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200543 // Each part of the format string is copied and modified from errorformat
544 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200545
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200546 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200547 sz = efm_regpat_bufsz(efm);
548 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200549 goto parse_efm_error;
550
551 while (efm[0] != NUL)
552 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200553 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200554 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
555 if (fmt_ptr == NULL)
556 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200557 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200558 fmt_first = fmt_ptr;
559 else
560 fmt_last->next = fmt_ptr;
561 fmt_last = fmt_ptr;
562
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200563 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200564 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200565
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200566 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200567 goto parse_efm_error;
568 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
569 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200570 // Advance to next part
571 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200572 }
573
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200574 if (fmt_first == NULL) // nothing found
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200575 EMSG(_("E378: 'errorformat' contains no pattern"));
576
577 goto parse_efm_end;
578
579parse_efm_error:
580 free_efm_list(&fmt_first);
581
582parse_efm_end:
583 vim_free(fmtstr);
584 vim_free(errmsg);
585
586 return fmt_first;
587}
588
Bram Moolenaare0d37972016-07-15 22:36:01 +0200589enum {
590 QF_FAIL = 0,
591 QF_OK = 1,
592 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200593 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200594 QF_IGNORE_LINE = 4,
595 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200596};
597
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200598/*
599 * State information used to parse lines and add entries to a quickfix/location
600 * list.
601 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200602typedef struct {
603 char_u *linebuf;
604 int linelen;
605 char_u *growbuf;
606 int growbufsiz;
607 FILE *fd;
608 typval_T *tv;
609 char_u *p_str;
610 listitem_T *p_li;
611 buf_T *buf;
612 linenr_T buflnum;
613 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100614 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200615} qfstate_T;
616
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200617/*
618 * Allocate more memory for the line buffer used for parsing lines.
619 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200620 static char_u *
621qf_grow_linebuf(qfstate_T *state, int newsz)
622{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200623 char_u *p;
624
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200625 // If the line exceeds LINE_MAXLEN exclude the last
626 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200627 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
628 if (state->growbuf == NULL)
629 {
630 state->growbuf = alloc(state->linelen + 1);
631 if (state->growbuf == NULL)
632 return NULL;
633 state->growbufsiz = state->linelen;
634 }
635 else if (state->linelen > state->growbufsiz)
636 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200637 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200638 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200639 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200640 state->growbufsiz = state->linelen;
641 }
642 return state->growbuf;
643}
644
645/*
646 * Get the next string (separated by newline) from state->p_str.
647 */
648 static int
649qf_get_next_str_line(qfstate_T *state)
650{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200651 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200652 char_u *p_str = state->p_str;
653 char_u *p;
654 int len;
655
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200656 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200657 return QF_END_OF_INPUT;
658
659 p = vim_strchr(p_str, '\n');
660 if (p != NULL)
661 len = (int)(p - p_str) + 1;
662 else
663 len = (int)STRLEN(p_str);
664
665 if (len > IOSIZE - 2)
666 {
667 state->linebuf = qf_grow_linebuf(state, len);
668 if (state->linebuf == NULL)
669 return QF_NOMEM;
670 }
671 else
672 {
673 state->linebuf = IObuff;
674 state->linelen = len;
675 }
676 vim_strncpy(state->linebuf, p_str, state->linelen);
677
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200678 // Increment using len in order to discard the rest of the
679 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200680 p_str += len;
681 state->p_str = p_str;
682
683 return QF_OK;
684}
685
686/*
687 * Get the next string from state->p_Li.
688 */
689 static int
690qf_get_next_list_line(qfstate_T *state)
691{
692 listitem_T *p_li = state->p_li;
693 int len;
694
695 while (p_li != NULL
696 && (p_li->li_tv.v_type != VAR_STRING
697 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200698 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200699
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200700 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200701 {
702 state->p_li = NULL;
703 return QF_END_OF_INPUT;
704 }
705
706 len = (int)STRLEN(p_li->li_tv.vval.v_string);
707 if (len > IOSIZE - 2)
708 {
709 state->linebuf = qf_grow_linebuf(state, len);
710 if (state->linebuf == NULL)
711 return QF_NOMEM;
712 }
713 else
714 {
715 state->linebuf = IObuff;
716 state->linelen = len;
717 }
718
719 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
720
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200721 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200722 return QF_OK;
723}
724
725/*
726 * Get the next string from state->buf.
727 */
728 static int
729qf_get_next_buf_line(qfstate_T *state)
730{
731 char_u *p_buf = NULL;
732 int len;
733
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200734 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200735 if (state->buflnum > state->lnumlast)
736 return QF_END_OF_INPUT;
737
738 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
739 state->buflnum += 1;
740
741 len = (int)STRLEN(p_buf);
742 if (len > IOSIZE - 2)
743 {
744 state->linebuf = qf_grow_linebuf(state, len);
745 if (state->linebuf == NULL)
746 return QF_NOMEM;
747 }
748 else
749 {
750 state->linebuf = IObuff;
751 state->linelen = len;
752 }
753 vim_strncpy(state->linebuf, p_buf, state->linelen);
754
755 return QF_OK;
756}
757
758/*
759 * Get the next string from file state->fd.
760 */
761 static int
762qf_get_next_file_line(qfstate_T *state)
763{
764 int discard;
765 int growbuflen;
766
767 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
768 return QF_END_OF_INPUT;
769
770 discard = FALSE;
771 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200772 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200773 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200774 // The current line exceeds IObuff, continue reading using
775 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200776 if (state->growbuf == NULL)
777 {
778 state->growbufsiz = 2 * (IOSIZE - 1);
779 state->growbuf = alloc(state->growbufsiz);
780 if (state->growbuf == NULL)
781 return QF_NOMEM;
782 }
783
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200784 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200785 memcpy(state->growbuf, IObuff, IOSIZE - 1);
786 growbuflen = state->linelen;
787
788 for (;;)
789 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200790 char_u *p;
791
Bram Moolenaare0d37972016-07-15 22:36:01 +0200792 if (fgets((char *)state->growbuf + growbuflen,
793 state->growbufsiz - growbuflen, state->fd) == NULL)
794 break;
795 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
796 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200797 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200798 break;
799 if (state->growbufsiz == LINE_MAXLEN)
800 {
801 discard = TRUE;
802 break;
803 }
804
805 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
806 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200807 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200808 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200809 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200810 }
811
812 while (discard)
813 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200814 // The current line is longer than LINE_MAXLEN, continue
815 // reading but discard everything until EOL or EOF is
816 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200817 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
818 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200819 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200820 break;
821 }
822
823 state->linebuf = state->growbuf;
824 state->linelen = growbuflen;
825 }
826 else
827 state->linebuf = IObuff;
828
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100829#ifdef FEAT_MBYTE
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200830 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200831 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
832 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100833 char_u *line;
834
835 line = string_convert(&state->vc, state->linebuf, &state->linelen);
836 if (line != NULL)
837 {
838 if (state->linelen < IOSIZE)
839 {
840 STRCPY(state->linebuf, line);
841 vim_free(line);
842 }
843 else
844 {
845 vim_free(state->growbuf);
846 state->linebuf = state->growbuf = line;
847 state->growbufsiz = state->linelen < LINE_MAXLEN
848 ? state->linelen : LINE_MAXLEN;
849 }
850 }
851 }
852#endif
853
Bram Moolenaare0d37972016-07-15 22:36:01 +0200854 return QF_OK;
855}
856
857/*
858 * Get the next string from a file/buffer/list/string.
859 */
860 static int
861qf_get_nextline(qfstate_T *state)
862{
863 int status = QF_FAIL;
864
865 if (state->fd == NULL)
866 {
867 if (state->tv != NULL)
868 {
869 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200870 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200871 status = qf_get_next_str_line(state);
872 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200873 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200874 status = qf_get_next_list_line(state);
875 }
876 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200877 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200878 status = qf_get_next_buf_line(state);
879 }
880 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200881 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200882 status = qf_get_next_file_line(state);
883
884 if (status != QF_OK)
885 return status;
886
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200887 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200888 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200889 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200890 state->linebuf[state->linelen - 1] = NUL;
891#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200892 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
893 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200894#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200895 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200896
897#ifdef FEAT_MBYTE
898 remove_bom(state->linebuf);
899#endif
900
901 return QF_OK;
902}
903
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200904typedef struct {
905 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200906 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200907 char_u *errmsg;
908 int errmsglen;
909 long lnum;
910 int col;
911 char_u use_viscol;
912 char_u *pattern;
913 int enr;
914 int type;
915 int valid;
916} qffields_T;
917
918/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200919 * Parse the match for filename ('%f') pattern in regmatch.
920 * Return the matched value in "fields->namebuf".
921 */
922 static int
923qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
924{
925 int c;
926
927 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
928 return QF_FAIL;
929
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200930 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200931 c = *rmp->endp[midx];
932 *rmp->endp[midx] = NUL;
933 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
934 *rmp->endp[midx] = c;
935
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200936 // For separate filename patterns (%O, %P and %Q), the specified file
937 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200938 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
939 && mch_getperm(fields->namebuf) == -1)
940 return QF_FAIL;
941
942 return QF_OK;
943}
944
945/*
946 * Parse the match for error number ('%n') pattern in regmatch.
947 * Return the matched value in "fields->enr".
948 */
949 static int
950qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
951{
952 if (rmp->startp[midx] == NULL)
953 return QF_FAIL;
954 fields->enr = (int)atol((char *)rmp->startp[midx]);
955 return QF_OK;
956}
957
958/*
959 * Parse the match for line number (%l') pattern in regmatch.
960 * Return the matched value in "fields->lnum".
961 */
962 static int
963qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
964{
965 if (rmp->startp[midx] == NULL)
966 return QF_FAIL;
967 fields->lnum = atol((char *)rmp->startp[midx]);
968 return QF_OK;
969}
970
971/*
972 * Parse the match for column number ('%c') pattern in regmatch.
973 * Return the matched value in "fields->col".
974 */
975 static int
976qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
977{
978 if (rmp->startp[midx] == NULL)
979 return QF_FAIL;
980 fields->col = (int)atol((char *)rmp->startp[midx]);
981 return QF_OK;
982}
983
984/*
985 * Parse the match for error type ('%t') pattern in regmatch.
986 * Return the matched value in "fields->type".
987 */
988 static int
989qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
990{
991 if (rmp->startp[midx] == NULL)
992 return QF_FAIL;
993 fields->type = *rmp->startp[midx];
994 return QF_OK;
995}
996
997/*
998 * Parse the match for '%+' format pattern. The whole matching line is included
999 * in the error string. Return the matched line in "fields->errmsg".
1000 */
1001 static int
1002qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
1003{
1004 char_u *p;
1005
1006 if (linelen >= fields->errmsglen)
1007 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001008 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001009 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1010 return QF_NOMEM;
1011 fields->errmsg = p;
1012 fields->errmsglen = linelen + 1;
1013 }
1014 vim_strncpy(fields->errmsg, linebuf, linelen);
1015 return QF_OK;
1016}
1017
1018/*
1019 * Parse the match for error message ('%m') pattern in regmatch.
1020 * Return the matched value in "fields->errmsg".
1021 */
1022 static int
1023qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1024{
1025 char_u *p;
1026 int len;
1027
1028 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1029 return QF_FAIL;
1030 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1031 if (len >= fields->errmsglen)
1032 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001033 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001034 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1035 return QF_NOMEM;
1036 fields->errmsg = p;
1037 fields->errmsglen = len + 1;
1038 }
1039 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1040 return QF_OK;
1041}
1042
1043/*
1044 * Parse the match for rest of a single-line file message ('%r') pattern.
1045 * Return the matched value in "tail".
1046 */
1047 static int
1048qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1049{
1050 if (rmp->startp[midx] == NULL)
1051 return QF_FAIL;
1052 *tail = rmp->startp[midx];
1053 return QF_OK;
1054}
1055
1056/*
1057 * Parse the match for the pointer line ('%p') pattern in regmatch.
1058 * Return the matched value in "fields->col".
1059 */
1060 static int
1061qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1062{
1063 char_u *match_ptr;
1064
1065 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1066 return QF_FAIL;
1067 fields->col = 0;
1068 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1069 ++match_ptr)
1070 {
1071 ++fields->col;
1072 if (*match_ptr == TAB)
1073 {
1074 fields->col += 7;
1075 fields->col -= fields->col % 8;
1076 }
1077 }
1078 ++fields->col;
1079 fields->use_viscol = TRUE;
1080 return QF_OK;
1081}
1082
1083/*
1084 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1085 * Return the matched value in "fields->col".
1086 */
1087 static int
1088qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1089{
1090 if (rmp->startp[midx] == NULL)
1091 return QF_FAIL;
1092 fields->col = (int)atol((char *)rmp->startp[midx]);
1093 fields->use_viscol = TRUE;
1094 return QF_OK;
1095}
1096
1097/*
1098 * Parse the match for the search text ('%s') pattern in regmatch.
1099 * Return the matched value in "fields->pattern".
1100 */
1101 static int
1102qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1103{
1104 int len;
1105
1106 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1107 return QF_FAIL;
1108 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1109 if (len > CMDBUFFSIZE - 5)
1110 len = CMDBUFFSIZE - 5;
1111 STRCPY(fields->pattern, "^\\V");
1112 STRNCAT(fields->pattern, rmp->startp[midx], len);
1113 fields->pattern[len + 3] = '\\';
1114 fields->pattern[len + 4] = '$';
1115 fields->pattern[len + 5] = NUL;
1116 return QF_OK;
1117}
1118
1119/*
1120 * Parse the match for the module ('%o') pattern in regmatch.
1121 * Return the matched value in "fields->module".
1122 */
1123 static int
1124qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1125{
1126 int len;
1127
1128 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1129 return QF_FAIL;
1130 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1131 if (len > CMDBUFFSIZE)
1132 len = CMDBUFFSIZE;
1133 STRNCAT(fields->module, rmp->startp[midx], len);
1134 return QF_OK;
1135}
1136
1137/*
1138 * 'errorformat' format pattern parser functions.
1139 * The '%f' and '%r' formats are parsed differently from other formats.
1140 * See qf_parse_match() for details.
1141 */
1142static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1143{
1144 NULL,
1145 qf_parse_fmt_n,
1146 qf_parse_fmt_l,
1147 qf_parse_fmt_c,
1148 qf_parse_fmt_t,
1149 qf_parse_fmt_m,
1150 NULL,
1151 qf_parse_fmt_p,
1152 qf_parse_fmt_v,
1153 qf_parse_fmt_s,
1154 qf_parse_fmt_o
1155};
1156
1157/*
1158 * Parse the error format pattern matches in "regmatch" and set the values in
1159 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1160 * match. Returns QF_OK if all the matches are successfully parsed. On
1161 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001162 */
1163 static int
1164qf_parse_match(
1165 char_u *linebuf,
1166 int linelen,
1167 efm_T *fmt_ptr,
1168 regmatch_T *regmatch,
1169 qffields_T *fields,
1170 int qf_multiline,
1171 int qf_multiscan,
1172 char_u **tail)
1173{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001174 int idx = fmt_ptr->prefix;
1175 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001176 int midx;
1177 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001178
1179 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1180 return QF_FAIL;
1181 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1182 fields->type = idx;
1183 else
1184 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001185
1186 // Extract error message data from matched line.
1187 // We check for an actual submatch, because "\[" and "\]" in
1188 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001189 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001190 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001191 status = QF_OK;
1192 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001193 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001194 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1195 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001196 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001197 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001198 status = qf_parse_fmt_plus(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001199 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001200 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001201 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001202 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001203 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001204 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001205 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001206
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001207 if (status != QF_OK)
1208 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001209 }
1210
1211 return QF_OK;
1212}
1213
1214/*
1215 * Parse an error line in 'linebuf' using a single error format string in
1216 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1217 * Returns QF_OK if the efm format matches completely and the fields are
1218 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1219 */
1220 static int
1221qf_parse_get_fields(
1222 char_u *linebuf,
1223 int linelen,
1224 efm_T *fmt_ptr,
1225 qffields_T *fields,
1226 int qf_multiline,
1227 int qf_multiscan,
1228 char_u **tail)
1229{
1230 regmatch_T regmatch;
1231 int status = QF_FAIL;
1232 int r;
1233
1234 if (qf_multiscan &&
1235 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1236 return QF_FAIL;
1237
1238 fields->namebuf[0] = NUL;
1239 fields->module[0] = NUL;
1240 fields->pattern[0] = NUL;
1241 if (!qf_multiscan)
1242 fields->errmsg[0] = NUL;
1243 fields->lnum = 0;
1244 fields->col = 0;
1245 fields->use_viscol = FALSE;
1246 fields->enr = -1;
1247 fields->type = 0;
1248 *tail = NULL;
1249
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001250 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001251 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001252 regmatch.regprog = fmt_ptr->prog;
1253 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1254 fmt_ptr->prog = regmatch.regprog;
1255 if (r)
1256 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1257 fields, qf_multiline, qf_multiscan, tail);
1258
1259 return status;
1260}
1261
1262/*
1263 * Parse directory error format prefixes (%D and %X).
1264 * Push and pop directories from the directory stack when scanning directory
1265 * names.
1266 */
1267 static int
1268qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1269{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001270 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001271 {
1272 if (*fields->namebuf == NUL)
1273 {
1274 EMSG(_("E379: Missing or empty directory name"));
1275 return QF_FAIL;
1276 }
1277 qfl->qf_directory =
1278 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1279 if (qfl->qf_directory == NULL)
1280 return QF_FAIL;
1281 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001282 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001283 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1284
1285 return QF_OK;
1286}
1287
1288/*
1289 * Parse global file name error format prefixes (%O, %P and %Q).
1290 */
1291 static int
1292qf_parse_file_pfx(
1293 int idx,
1294 qffields_T *fields,
1295 qf_list_T *qfl,
1296 char_u *tail)
1297{
1298 fields->valid = FALSE;
1299 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1300 {
1301 if (*fields->namebuf && idx == 'P')
1302 qfl->qf_currfile =
1303 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1304 else if (idx == 'Q')
1305 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1306 *fields->namebuf = NUL;
1307 if (tail && *tail)
1308 {
1309 STRMOVE(IObuff, skipwhite(tail));
1310 qfl->qf_multiscan = TRUE;
1311 return QF_MULTISCAN;
1312 }
1313 }
1314
1315 return QF_OK;
1316}
1317
1318/*
1319 * Parse a non-error line (a line which doesn't match any of the error
1320 * format in 'efm').
1321 */
1322 static int
1323qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1324{
1325 char_u *p;
1326
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001327 fields->namebuf[0] = NUL; // no match found, remove file name
1328 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001329 fields->valid = FALSE;
1330 if (linelen >= fields->errmsglen)
1331 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001332 // linelen + null terminator
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001333 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1334 return QF_NOMEM;
1335 fields->errmsg = p;
1336 fields->errmsglen = linelen + 1;
1337 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001338 // copy whole line to error message
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001339 vim_strncpy(fields->errmsg, linebuf, linelen);
1340
1341 return QF_OK;
1342}
1343
1344/*
1345 * Parse multi-line error format prefixes (%C and %Z)
1346 */
1347 static int
1348qf_parse_multiline_pfx(
1349 qf_info_T *qi,
1350 int qf_idx,
1351 int idx,
1352 qf_list_T *qfl,
1353 qffields_T *fields)
1354{
1355 char_u *ptr;
1356 int len;
1357
1358 if (!qfl->qf_multiignore)
1359 {
1360 qfline_T *qfprev = qfl->qf_last;
1361
1362 if (qfprev == NULL)
1363 return QF_FAIL;
1364 if (*fields->errmsg && !qfl->qf_multiignore)
1365 {
1366 len = (int)STRLEN(qfprev->qf_text);
1367 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1368 == NULL)
1369 return QF_FAIL;
1370 STRCPY(ptr, qfprev->qf_text);
1371 vim_free(qfprev->qf_text);
1372 qfprev->qf_text = ptr;
1373 *(ptr += len) = '\n';
1374 STRCPY(++ptr, fields->errmsg);
1375 }
1376 if (qfprev->qf_nr == -1)
1377 qfprev->qf_nr = fields->enr;
1378 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001379 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001380 qfprev->qf_type = fields->type;
1381
1382 if (!qfprev->qf_lnum)
1383 qfprev->qf_lnum = fields->lnum;
1384 if (!qfprev->qf_col)
1385 qfprev->qf_col = fields->col;
1386 qfprev->qf_viscol = fields->use_viscol;
1387 if (!qfprev->qf_fnum)
1388 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1389 qfl->qf_directory,
1390 *fields->namebuf || qfl->qf_directory != NULL
1391 ? fields->namebuf
1392 : qfl->qf_currfile != NULL && fields->valid
1393 ? qfl->qf_currfile : 0);
1394 }
1395 if (idx == 'Z')
1396 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1397 line_breakcheck();
1398
1399 return QF_IGNORE_LINE;
1400}
1401
1402/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001403 * Parse a line and get the quickfix fields.
1404 * Return the QF_ status.
1405 */
1406 static int
1407qf_parse_line(
1408 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001409 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001410 char_u *linebuf,
1411 int linelen,
1412 efm_T *fmt_first,
1413 qffields_T *fields)
1414{
1415 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001416 int idx = 0;
1417 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001418 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001419 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001420
Bram Moolenaare333e792018-04-08 13:27:39 +02001421restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001422 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001423 if (fmt_start == NULL)
1424 fmt_ptr = fmt_first;
1425 else
1426 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001427 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001428 fmt_ptr = fmt_start;
1429 fmt_start = NULL;
1430 }
1431
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001432 // Try to match each part of 'errorformat' until we find a complete
1433 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001434 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001435 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1436 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001437 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001438 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1439 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1440 if (status == QF_NOMEM)
1441 return status;
1442 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001443 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001444 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001445 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001446
1447 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1448 {
1449 if (fmt_ptr != NULL)
1450 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001451 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001452 status = qf_parse_dir_pfx(idx, fields, qfl);
1453 if (status != QF_OK)
1454 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001455 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001456
1457 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1458 if (status != QF_OK)
1459 return status;
1460
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001461 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001462 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001463 }
1464 else if (fmt_ptr != NULL)
1465 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001466 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001467 if (fmt_ptr->conthere)
1468 fmt_start = fmt_ptr;
1469
1470 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1471 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001472 qfl->qf_multiline = TRUE; // start of a multi-line message
1473 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001474 }
1475 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001476 { // continuation of multi-line msg
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001477 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1478 if (status != QF_OK)
1479 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001480 }
1481 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001482 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001483 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1484 if (status == QF_MULTISCAN)
1485 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001486 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001487 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001488 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001489 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001490 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001491 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001492 return QF_IGNORE_LINE;
1493 }
1494 }
1495
1496 return QF_OK;
1497}
1498
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001499/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001500 * Returns TRUE if the specified quickfix/location stack is empty
1501 */
1502 static int
1503qf_stack_empty(qf_info_T *qi)
1504{
1505 return qi == NULL || qi->qf_listcount <= 0;
1506}
1507
1508/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001509 * Returns TRUE if the specified quickfix/location list is empty.
1510 */
1511 static int
1512qf_list_empty(qf_info_T *qi, int qf_idx)
1513{
1514 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1515 return TRUE;
1516 return qi->qf_lists[qf_idx].qf_count <= 0;
1517}
1518
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001519/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001520 * Allocate the fields used for parsing lines and populating a quickfix list.
1521 */
1522 static int
1523qf_alloc_fields(qffields_T *pfields)
1524{
1525 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1526 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1527 pfields->errmsglen = CMDBUFFSIZE + 1;
1528 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1529 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1530 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1531 || pfields->pattern == NULL || pfields->module == NULL)
1532 return FAIL;
1533
1534 return OK;
1535}
1536
1537/*
1538 * Free the fields used for parsing lines and populating a quickfix list.
1539 */
1540 static void
1541qf_free_fields(qffields_T *pfields)
1542{
1543 vim_free(pfields->namebuf);
1544 vim_free(pfields->module);
1545 vim_free(pfields->errmsg);
1546 vim_free(pfields->pattern);
1547}
1548
1549/*
1550 * Setup the state information used for parsing lines and populating a
1551 * quickfix list.
1552 */
1553 static int
1554qf_setup_state(
1555 qfstate_T *pstate,
1556 char_u *enc,
1557 char_u *efile,
1558 typval_T *tv,
1559 buf_T *buf,
1560 linenr_T lnumfirst,
1561 linenr_T lnumlast)
1562{
1563#ifdef FEAT_MBYTE
1564 pstate->vc.vc_type = CONV_NONE;
1565 if (enc != NULL && *enc != NUL)
1566 convert_setup(&pstate->vc, enc, p_enc);
1567#endif
1568
1569 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1570 {
1571 EMSG2(_(e_openerrf), efile);
1572 return FAIL;
1573 }
1574
1575 if (tv != NULL)
1576 {
1577 if (tv->v_type == VAR_STRING)
1578 pstate->p_str = tv->vval.v_string;
1579 else if (tv->v_type == VAR_LIST)
1580 pstate->p_li = tv->vval.v_list->lv_first;
1581 pstate->tv = tv;
1582 }
1583 pstate->buf = buf;
1584 pstate->buflnum = lnumfirst;
1585 pstate->lnumlast = lnumlast;
1586
1587 return OK;
1588}
1589
1590/*
1591 * Cleanup the state information used for parsing lines and populating a
1592 * quickfix list.
1593 */
1594 static void
1595qf_cleanup_state(qfstate_T *pstate)
1596{
1597 if (pstate->fd != NULL)
1598 fclose(pstate->fd);
1599
1600 vim_free(pstate->growbuf);
1601#ifdef FEAT_MBYTE
1602 if (pstate->vc.vc_type != CONV_NONE)
1603 convert_setup(&pstate->vc, NULL, NULL);
1604#endif
1605}
1606
1607/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001608 * Read the errorfile "efile" into memory, line by line, building the error
1609 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001610 * Alternative: when "efile" is NULL read errors from buffer "buf".
1611 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001612 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001613 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1614 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001615 * Return -1 for error, number of errors for success.
1616 */
1617 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001618qf_init_ext(
1619 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001620 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001621 char_u *efile,
1622 buf_T *buf,
1623 typval_T *tv,
1624 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001625 int newlist, // TRUE: start a new error list
1626 linenr_T lnumfirst, // first line number to use
1627 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001628 char_u *qf_title,
1629 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001630{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001631 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001632 qfstate_T state;
1633 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001634 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001635 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001636 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001638 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001639 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001640 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001642 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001643 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001644
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001645 vim_memset(&state, 0, sizeof(state));
1646 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001647 if ((qf_alloc_fields(&fields) == FAIL) ||
1648 (qf_setup_state(&state, enc, efile, tv, buf,
1649 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 goto qf_init_end;
1651
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001652 if (newlist || qf_idx == qi->qf_listcount)
1653 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001654 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001655 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001656 qf_idx = qi->qf_curlist;
1657 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001658 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001659 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001660 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001661 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001662 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001663 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001666 qfl = &qi->qf_lists[qf_idx];
1667
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001668 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001669 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001670 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 else
1672 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001674 // If the errorformat didn't change between calls, then reuse the
1675 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001676 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1677 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001678 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001679 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001680 free_efm_list(&fmt_first);
1681
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001682 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001683 fmt_first = parse_efm_option(efm);
1684 if (fmt_first != NULL)
1685 last_efm = vim_strsave(efm);
1686 }
1687
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001688 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001691 // got_int is reset here, because it was probably set when killing the
1692 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 got_int = FALSE;
1694
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001695 // Read the lines in the error file one by one.
1696 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001697 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001699 // Get the next line from a file/buffer/list/string
Bram Moolenaare0d37972016-07-15 22:36:01 +02001700 status = qf_get_nextline(&state);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001701 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001702 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001703 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001704 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001706 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1707 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001708 if (status == QF_FAIL)
1709 goto error2;
1710 if (status == QF_NOMEM)
1711 goto qf_init_end;
1712 if (status == QF_IGNORE_LINE)
1713 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001715 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001716 qf_idx,
1717 qfl->qf_directory,
1718 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001719 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001720 : ((qfl->qf_currfile != NULL && fields.valid)
1721 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001722 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001723 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001724 fields.errmsg,
1725 fields.lnum,
1726 fields.col,
1727 fields.use_viscol,
1728 fields.pattern,
1729 fields.enr,
1730 fields.type,
1731 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 goto error2;
1733 line_breakcheck();
1734 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001735 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001737 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001739 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001740 qfl->qf_ptr = qfl->qf_start;
1741 qfl->qf_index = 1;
1742 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
1744 else
1745 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001746 qfl->qf_nonevalid = FALSE;
1747 if (qfl->qf_ptr == NULL)
1748 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001750 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001751 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001752 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
1754 EMSG(_(e_readerrf));
1755error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001756 if (!adding)
1757 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001758 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001759 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001760 qi->qf_listcount--;
1761 if (qi->qf_curlist > 0)
1762 --qi->qf_curlist;
1763 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001764qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001765 if (qf_idx == qi->qf_curlist)
1766 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001767 qf_cleanup_state(&state);
1768 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
1770 return retval;
1771}
1772
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001773/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001774 * Read the errorfile "efile" into memory, line by line, building the error
1775 * list. Set the error list's title to qf_title.
1776 * Return -1 for error, number of errors for success.
1777 */
1778 int
1779qf_init(win_T *wp,
1780 char_u *efile,
1781 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001782 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001783 char_u *qf_title,
1784 char_u *enc)
1785{
1786 qf_info_T *qi = &ql_info;
1787
1788 if (wp != NULL)
1789 {
1790 qi = ll_get_or_alloc_list(wp);
1791 if (qi == NULL)
1792 return FAIL;
1793 }
1794
1795 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1796 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1797}
1798
1799/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001800 * Set the title of the specified quickfix list. Frees the previous title.
1801 * Prepends ':' to the title.
1802 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001803 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001804qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001805{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001806 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001807
Bram Moolenaarfb604092014-07-23 15:55:00 +02001808 if (title != NULL)
1809 {
1810 char_u *p = alloc((int)STRLEN(title) + 2);
1811
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001812 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001813 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001814 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001815 }
1816}
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001819 * The title of a quickfix/location list is set, by default, to the command
1820 * that created the quickfix list with the ":" prefix.
1821 * Create a quickfix list title string by prepending ":" to a user command.
1822 * Returns a pointer to a static buffer with the title.
1823 */
1824 static char_u *
1825qf_cmdtitle(char_u *cmd)
1826{
1827 static char_u qftitle_str[IOSIZE];
1828
1829 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1830 return qftitle_str;
1831}
1832
1833/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001834 * Prepare for adding a new quickfix list. If the current list is in the
1835 * middle of the stack, then all the following lists are freed and then
1836 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 */
1838 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001839qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
1841 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001842 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001844 // If the current entry is not the last entry, delete entries beyond
1845 // the current entry. This makes it possible to browse in a tree-like
1846 // way with ":grep'.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001847 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001848 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001850 // When the stack is full, remove to oldest entry
1851 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001852 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001854 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001856 qi->qf_lists[i - 1] = qi->qf_lists[i];
1857 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 }
1859 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001860 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001861 qfl = &qi->qf_lists[qi->qf_curlist];
1862 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1863 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001864 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001865 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866}
1867
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001868/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001869 * Queue location list stack delete request.
1870 */
1871 static void
1872locstack_queue_delreq(qf_info_T *qi)
1873{
1874 qf_delq_T *q;
1875
1876 q = (qf_delq_T *)alloc((unsigned)sizeof(qf_delq_T));
1877 if (q != NULL)
1878 {
1879 q->qi = qi;
1880 q->next = qf_delq_head;
1881 qf_delq_head = q;
1882 }
1883}
1884
1885/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001886 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001887 */
1888 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001889ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001890{
1891 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001892 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001893
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001894 qi = *pqi;
1895 if (qi == NULL)
1896 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001897 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001898
1899 qi->qf_refcount--;
1900 if (qi->qf_refcount < 1)
1901 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001902 // No references to this location list.
1903 // If the location list is still in use, then queue the delete request
1904 // to be processed later.
1905 if (quickfix_busy > 0)
1906 locstack_queue_delreq(qi);
1907 else
1908 {
1909 for (i = 0; i < qi->qf_listcount; ++i)
1910 qf_free(&qi->qf_lists[i]);
1911 vim_free(qi);
1912 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001913 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001914}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001915
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001916/*
1917 * Free all the quickfix/location lists in the stack.
1918 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001919 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001920qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001921{
1922 int i;
1923 qf_info_T *qi = &ql_info;
1924
1925 if (wp != NULL)
1926 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001927 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001928 ll_free_all(&wp->w_llist);
1929 ll_free_all(&wp->w_llist_ref);
1930 }
1931 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001932 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001933 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001934 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001935}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001936
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001938 * Delay freeing of location list stacks when the quickfix code is running.
1939 * Used to avoid problems with autocmds freeing location list stacks when the
1940 * quickfix code is still referencing the stack.
1941 * Must always call decr_quickfix_busy() exactly once after this.
1942 */
1943 static void
1944incr_quickfix_busy(void)
1945{
1946 quickfix_busy++;
1947}
1948
1949/*
1950 * Safe to free location list stacks. Process any delayed delete requests.
1951 */
1952 static void
1953decr_quickfix_busy(void)
1954{
1955 if (--quickfix_busy == 0)
1956 {
1957 // No longer referencing the location lists. Process all the pending
1958 // delete requests.
1959 while (qf_delq_head != NULL)
1960 {
1961 qf_delq_T *q = qf_delq_head;
1962
1963 qf_delq_head = q->next;
1964 ll_free_all(&q->qi);
1965 vim_free(q);
1966 }
1967 }
1968#ifdef ABORT_ON_INTERNAL_ERROR
1969 if (quickfix_busy < 0)
1970 {
1971 EMSG("quickfix_busy has become negative");
1972 abort();
1973 }
1974#endif
1975}
1976
1977#if defined(EXITFREE) || defined(PROTO)
1978 void
1979check_quickfix_busy(void)
1980{
1981 if (quickfix_busy != 0)
1982 {
1983 EMSGN("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
1984# ifdef ABORT_ON_INTERNAL_ERROR
1985 abort();
1986# endif
1987 }
1988}
1989#endif
1990
1991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 * Add an entry to the end of the list of errors.
1993 * Returns OK or FAIL.
1994 */
1995 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001996qf_add_entry(
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001997 qf_info_T *qi, // quickfix list
1998 int qf_idx, // list index
1999 char_u *dir, // optional directory name
2000 char_u *fname, // file name or NULL
2001 char_u *module, // module name or NULL
2002 int bufnum, // buffer number or zero
2003 char_u *mesg, // message
2004 long lnum, // line number
2005 int col, // column
2006 int vis_col, // using visual column
2007 char_u *pattern, // search pattern
2008 int nr, // error number
2009 int type, // type character
2010 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002012 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002013 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002014 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002016 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002018 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002019 {
2020 buf_T *buf = buflist_findnr(bufnum);
2021
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002022 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002023 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002024 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002025 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002026 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002027 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002028 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2030 {
2031 vim_free(qfp);
2032 return FAIL;
2033 }
2034 qfp->qf_lnum = lnum;
2035 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002036 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002037 if (pattern == NULL || *pattern == NUL)
2038 qfp->qf_pattern = NULL;
2039 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2040 {
2041 vim_free(qfp->qf_text);
2042 vim_free(qfp);
2043 return FAIL;
2044 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002045 if (module == NULL || *module == NUL)
2046 qfp->qf_module = NULL;
2047 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2048 {
2049 vim_free(qfp->qf_text);
2050 vim_free(qfp->qf_pattern);
2051 vim_free(qfp);
2052 return FAIL;
2053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002055 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 type = 0;
2057 qfp->qf_type = type;
2058 qfp->qf_valid = valid;
2059
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002060 lastp = &qfl->qf_last;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002061 if (qf_list_empty(qi, qf_idx)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002063 qfl->qf_start = qfp;
2064 qfl->qf_ptr = qfp;
2065 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002066 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
2068 else
2069 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002070 qfp->qf_prev = *lastp;
2071 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002073 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002075 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002076 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002077 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002079 qfl->qf_index = qfl->qf_count;
2080 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082
2083 return OK;
2084}
2085
2086/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002087 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002088 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002089 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002090qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002091{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002092 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002093
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002094 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002095 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002096 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002097 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002098 qi->qfl_type = qfltype;
2099 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002100 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002101}
2102
2103/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002104 * Return the location list stack for window 'wp'.
2105 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002106 */
2107 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002108ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002109{
2110 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002111 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002112 return wp->w_llist_ref;
2113
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002114 // For a non-location list window, w_llist_ref should not point to a
2115 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002116 ll_free_all(&wp->w_llist_ref);
2117
2118 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002119 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002120 return wp->w_llist;
2121}
2122
2123/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002124 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2125 */
2126 static int
2127copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2128{
2129 int i;
2130 qfline_T *from_qfp;
2131 qfline_T *prevp;
2132
2133 // copy all the location entries in this list
2134 for (i = 0, from_qfp = from_qfl->qf_start;
2135 i < from_qfl->qf_count && from_qfp != NULL;
2136 ++i, from_qfp = from_qfp->qf_next)
2137 {
2138 if (qf_add_entry(to_qi,
2139 to_qi->qf_curlist,
2140 NULL,
2141 NULL,
2142 from_qfp->qf_module,
2143 0,
2144 from_qfp->qf_text,
2145 from_qfp->qf_lnum,
2146 from_qfp->qf_col,
2147 from_qfp->qf_viscol,
2148 from_qfp->qf_pattern,
2149 from_qfp->qf_nr,
2150 0,
2151 from_qfp->qf_valid) == FAIL)
2152 return FAIL;
2153
2154 // qf_add_entry() will not set the qf_num field, as the
2155 // directory and file names are not supplied. So the qf_fnum
2156 // field is copied here.
2157 prevp = to_qfl->qf_last;
2158 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2159 prevp->qf_type = from_qfp->qf_type; // error type
2160 if (from_qfl->qf_ptr == from_qfp)
2161 to_qfl->qf_ptr = prevp; // current location
2162 }
2163
2164 return OK;
2165}
2166
2167/*
2168 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2169 */
2170 static int
2171copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2172{
2173 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002174 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002175 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2176 to_qfl->qf_count = 0;
2177 to_qfl->qf_index = 0;
2178 to_qfl->qf_start = NULL;
2179 to_qfl->qf_last = NULL;
2180 to_qfl->qf_ptr = NULL;
2181 if (from_qfl->qf_title != NULL)
2182 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2183 else
2184 to_qfl->qf_title = NULL;
2185 if (from_qfl->qf_ctx != NULL)
2186 {
2187 to_qfl->qf_ctx = alloc_tv();
2188 if (to_qfl->qf_ctx != NULL)
2189 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2190 }
2191 else
2192 to_qfl->qf_ctx = NULL;
2193
2194 if (from_qfl->qf_count)
2195 if (copy_loclist_entries(from_qfl, to_qfl, to_qi) == FAIL)
2196 return FAIL;
2197
2198 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2199
2200 // Assign a new ID for the location list
2201 to_qfl->qf_id = ++last_qf_id;
2202 to_qfl->qf_changedtick = 0L;
2203
2204 // When no valid entries are present in the list, qf_ptr points to
2205 // the first item in the list
2206 if (to_qfl->qf_nonevalid)
2207 {
2208 to_qfl->qf_ptr = to_qfl->qf_start;
2209 to_qfl->qf_index = 1;
2210 }
2211
2212 return OK;
2213}
2214
2215/*
2216 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002217 */
2218 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002219copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002220{
2221 qf_info_T *qi;
2222 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002223
Bram Moolenaar09037502018-09-25 22:08:14 +02002224 // When copying from a location list window, copy the referenced
2225 // location list. For other windows, copy the location list for
2226 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002227 if (IS_LL_WINDOW(from))
2228 qi = from->w_llist_ref;
2229 else
2230 qi = from->w_llist;
2231
Bram Moolenaar09037502018-09-25 22:08:14 +02002232 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002233 return;
2234
Bram Moolenaar09037502018-09-25 22:08:14 +02002235 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002236 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002237 return;
2238
2239 to->w_llist->qf_listcount = qi->qf_listcount;
2240
Bram Moolenaar09037502018-09-25 22:08:14 +02002241 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002242 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002243 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002244 to->w_llist->qf_curlist = idx;
2245
Bram Moolenaar09037502018-09-25 22:08:14 +02002246 if (copy_loclist(&qi->qf_lists[idx],
2247 &to->w_llist->qf_lists[idx], to->w_llist) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002248 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002249 qf_free_all(to);
2250 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002251 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002252 }
2253
Bram Moolenaar09037502018-09-25 22:08:14 +02002254 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002255}
2256
2257/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002258 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002259 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 */
2261 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002262qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002264 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar82404332016-07-10 17:00:38 +02002265 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002266 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002267 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002268
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002269 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
Bram Moolenaare60acc12011-05-10 16:41:25 +02002272#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002273 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002274#endif
2275#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002276 if (directory != NULL)
2277 slash_adjust(directory);
2278 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002279#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002280 if (directory != NULL && !vim_isAbsName(fname)
2281 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2282 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002283 // Here we check if the file really exists.
2284 // This should normally be true, but if make works without
2285 // "leaving directory"-messages we might have missed a
2286 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002287 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002290 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002291 if (directory)
2292 ptr = concat_fnames(directory, fname, TRUE);
2293 else
2294 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002296 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002297 bufname = ptr;
2298 }
2299 else
2300 bufname = fname;
2301
2302 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002303 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002304 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002305 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002306 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002308 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002309 {
2310 vim_free(qf_last_bufname);
2311 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2312 if (bufname == ptr)
2313 qf_last_bufname = bufname;
2314 else
2315 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002316 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002317 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002318 if (buf == NULL)
2319 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002320
Bram Moolenaarc1542742016-07-20 21:44:37 +02002321 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002322 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002323 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324}
2325
2326/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002327 * Push dirbuf onto the directory stack and return pointer to actual dir or
2328 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 */
2330 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002331qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332{
2333 struct dir_stack_T *ds_new;
2334 struct dir_stack_T *ds_ptr;
2335
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002336 // allocate new stack element and hook it in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2338 if (ds_new == NULL)
2339 return NULL;
2340
2341 ds_new->next = *stackptr;
2342 *stackptr = ds_new;
2343
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002344 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (vim_isAbsName(dirbuf)
2346 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002347 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 (*stackptr)->dirname = vim_strsave(dirbuf);
2349 else
2350 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002351 // Okay we don't have an absolute path.
2352 // dirbuf must be a subdir of one of the directories on the stack.
2353 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 ds_new = (*stackptr)->next;
2355 (*stackptr)->dirname = NULL;
2356 while (ds_new)
2357 {
2358 vim_free((*stackptr)->dirname);
2359 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2360 TRUE);
2361 if (mch_isdir((*stackptr)->dirname) == TRUE)
2362 break;
2363
2364 ds_new = ds_new->next;
2365 }
2366
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002367 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 while ((*stackptr)->next != ds_new)
2369 {
2370 ds_ptr = (*stackptr)->next;
2371 (*stackptr)->next = (*stackptr)->next->next;
2372 vim_free(ds_ptr->dirname);
2373 vim_free(ds_ptr);
2374 }
2375
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002376 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 if (ds_new == NULL)
2378 {
2379 vim_free((*stackptr)->dirname);
2380 (*stackptr)->dirname = vim_strsave(dirbuf);
2381 }
2382 }
2383
2384 if ((*stackptr)->dirname != NULL)
2385 return (*stackptr)->dirname;
2386 else
2387 {
2388 ds_ptr = *stackptr;
2389 *stackptr = (*stackptr)->next;
2390 vim_free(ds_ptr);
2391 return NULL;
2392 }
2393}
2394
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395/*
2396 * pop dirbuf from the directory stack and return previous directory or NULL if
2397 * stack is empty
2398 */
2399 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002400qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401{
2402 struct dir_stack_T *ds_ptr;
2403
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002404 // TODO: Should we check if dirbuf is the directory on top of the stack?
2405 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002407 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 if (*stackptr != NULL)
2409 {
2410 ds_ptr = *stackptr;
2411 *stackptr = (*stackptr)->next;
2412 vim_free(ds_ptr->dirname);
2413 vim_free(ds_ptr);
2414 }
2415
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002416 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 return *stackptr ? (*stackptr)->dirname : NULL;
2418}
2419
2420/*
2421 * clean up directory stack
2422 */
2423 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002424qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425{
2426 struct dir_stack_T *ds_ptr;
2427
2428 while ((ds_ptr = *stackptr) != NULL)
2429 {
2430 *stackptr = (*stackptr)->next;
2431 vim_free(ds_ptr->dirname);
2432 vim_free(ds_ptr);
2433 }
2434}
2435
2436/*
2437 * Check in which directory of the directory stack the given file can be
2438 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002439 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 * Cleans up intermediate directory entries.
2441 *
2442 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002443 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 * ./
2445 * ./aa
2446 * ./aa/bb
2447 * ./bb
2448 * ./bb/x.c
2449 * and make says:
2450 * making all in aa
2451 * making all in bb
2452 * x.c:9: Error
2453 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2454 * qf_guess_filepath will return NULL.
2455 */
2456 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002457qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
2459 struct dir_stack_T *ds_ptr;
2460 struct dir_stack_T *ds_tmp;
2461 char_u *fullname;
2462
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002463 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002464 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 return NULL;
2466
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002467 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 fullname = NULL;
2469 while (ds_ptr)
2470 {
2471 vim_free(fullname);
2472 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2473
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002474 // If concat_fnames failed, just go on. The worst thing that can happen
2475 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2477 break;
2478
2479 ds_ptr = ds_ptr->next;
2480 }
2481
2482 vim_free(fullname);
2483
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002484 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002485 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002487 ds_tmp = qfl->qf_dir_stack->next;
2488 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 vim_free(ds_tmp->dirname);
2490 vim_free(ds_tmp);
2491 }
2492
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002493 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494}
2495
2496/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002497 * Returns TRUE if a quickfix/location list with the given identifier exists.
2498 */
2499 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002500qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002501{
2502 qf_info_T *qi = &ql_info;
2503 int i;
2504
2505 if (wp != NULL)
2506 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002507 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002508 if (qi == NULL)
2509 return FALSE;
2510 }
2511
2512 for (i = 0; i < qi->qf_listcount; ++i)
2513 if (qi->qf_lists[i].qf_id == qf_id)
2514 return TRUE;
2515
2516 return FALSE;
2517}
2518
2519/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002520 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002521 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002522 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002523 * Similar to location list.
2524 */
2525 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002526is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002527{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002528 qfline_T *qfp;
2529 int i;
2530
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002531 // Search for the entry in the current list
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002532 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2533 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002534 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002535 break;
2536
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002537 if (i == qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002538 return FALSE;
2539
2540 return TRUE;
2541}
2542
2543/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002544 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002545 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002546 */
2547 static qfline_T *
2548get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002549 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002550 qfline_T *qf_ptr,
2551 int *qf_index,
2552 int dir)
2553{
2554 int idx;
2555 int old_qf_fnum;
2556
2557 idx = *qf_index;
2558 old_qf_fnum = qf_ptr->qf_fnum;
2559
2560 do
2561 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002562 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002563 return NULL;
2564 ++idx;
2565 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002566 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002567 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2568
2569 *qf_index = idx;
2570 return qf_ptr;
2571}
2572
2573/*
2574 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002575 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002576 */
2577 static qfline_T *
2578get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002579 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002580 qfline_T *qf_ptr,
2581 int *qf_index,
2582 int dir)
2583{
2584 int idx;
2585 int old_qf_fnum;
2586
2587 idx = *qf_index;
2588 old_qf_fnum = qf_ptr->qf_fnum;
2589
2590 do
2591 {
2592 if (idx == 1 || qf_ptr->qf_prev == NULL)
2593 return NULL;
2594 --idx;
2595 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002596 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002597 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2598
2599 *qf_index = idx;
2600 return qf_ptr;
2601}
2602
2603/*
2604 * Get the n'th (errornr) previous/next valid entry from the current entry in
2605 * the quickfix list.
2606 * dir == FORWARD or FORWARD_FILE: next valid entry
2607 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2608 */
2609 static qfline_T *
2610get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002611 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002612 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002613 int dir,
2614 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002615{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002616 qfline_T *qf_ptr = qfl->qf_ptr;
2617 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002618 qfline_T *prev_qf_ptr;
2619 int prev_index;
2620 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2621 char_u *err = e_no_more_items;
2622
2623 while (errornr--)
2624 {
2625 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002626 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002627
2628 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002629 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002630 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002631 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002632 if (qf_ptr == NULL)
2633 {
2634 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002635 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002636 if (err != NULL)
2637 {
2638 EMSG(_(err));
2639 return NULL;
2640 }
2641 break;
2642 }
2643
2644 err = NULL;
2645 }
2646
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002647 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002648 return qf_ptr;
2649}
2650
2651/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002652 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2653 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002654 */
2655 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002656get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002657{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002658 qfline_T *qf_ptr = qfl->qf_ptr;
2659 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002660
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002661 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002662 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2663 {
2664 --qf_idx;
2665 qf_ptr = qf_ptr->qf_prev;
2666 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002667 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002668 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2669 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002670 {
2671 ++qf_idx;
2672 qf_ptr = qf_ptr->qf_next;
2673 }
2674
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002675 *new_qfidx = qf_idx;
2676 return qf_ptr;
2677}
2678
2679/*
2680 * Get a entry specied by 'errornr' and 'dir' from the current
2681 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2682 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2683 * Returns a pointer to the entry and the index of the new entry is stored in
2684 * 'new_qfidx'.
2685 */
2686 static qfline_T *
2687qf_get_entry(
2688 qf_list_T *qfl,
2689 int errornr,
2690 int dir,
2691 int *new_qfidx)
2692{
2693 qfline_T *qf_ptr = qfl->qf_ptr;
2694 int qfidx = qfl->qf_index;
2695
2696 if (dir != 0) // next/prev valid entry
2697 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2698 else if (errornr != 0) // go to specified number
2699 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2700
2701 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002702 return qf_ptr;
2703}
2704
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002705/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002706 * Find a window displaying a Vim help file.
2707 */
2708 static win_T *
2709qf_find_help_win(void)
2710{
2711 win_T *wp;
2712
2713 FOR_ALL_WINDOWS(wp)
2714 if (bt_help(wp->w_buffer))
2715 return wp;
2716
2717 return NULL;
2718}
2719
2720/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002721 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2722 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002723 */
2724 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002725jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002726{
2727 win_T *wp;
2728 int flags;
2729
Bram Moolenaarb2443732018-11-11 22:50:27 +01002730 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002731 wp = NULL;
2732 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002733 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002734 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2735 win_enter(wp, TRUE);
2736 else
2737 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002738 // Split off help window; put it at far top if no position
2739 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002740 flags = WSP_HELP;
2741 if (cmdmod.split == 0 && curwin->w_width != Columns
2742 && curwin->w_width < 80)
2743 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002744 // If the user asks to open a new window, then copy the location list.
2745 // Otherwise, don't copy the location list.
2746 if (IS_LL_STACK(qi) && !newwin)
2747 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002748
2749 if (win_split(0, flags) == FAIL)
2750 return FAIL;
2751
2752 *opened_window = TRUE;
2753
2754 if (curwin->w_height < p_hh)
2755 win_setheight((int)p_hh);
2756
Bram Moolenaarb2443732018-11-11 22:50:27 +01002757 // When using location list, the new window should use the supplied
2758 // location list. If the user asks to open a new window, then the new
2759 // window will get a copy of the location list.
2760 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002761 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002762 curwin->w_llist = qi;
2763 qi->qf_refcount++;
2764 }
2765 }
2766
2767 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002768 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002769
2770 return OK;
2771}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002772
2773/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002774 * Find a non-quickfix window using the given location list.
2775 * Returns NULL if a matching window is not found.
2776 */
2777 static win_T *
2778qf_find_win_with_loclist(qf_info_T *ll)
2779{
2780 win_T *wp;
2781
2782 FOR_ALL_WINDOWS(wp)
2783 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2784 return wp;
2785
2786 return NULL;
2787}
2788
2789/*
2790 * Find a window containing a normal buffer
2791 */
2792 static win_T *
2793qf_find_win_with_normal_buf(void)
2794{
2795 win_T *wp;
2796
2797 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002798 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002799 return wp;
2800
2801 return NULL;
2802}
2803
2804/*
2805 * Go to a window in any tabpage containing the specified file. Returns TRUE
2806 * if successfully jumped to the window. Otherwise returns FALSE.
2807 */
2808 static int
2809qf_goto_tabwin_with_file(int fnum)
2810{
2811 tabpage_T *tp;
2812 win_T *wp;
2813
2814 FOR_ALL_TAB_WINDOWS(tp, wp)
2815 if (wp->w_buffer->b_fnum == fnum)
2816 {
2817 goto_tabpage_win(tp, wp);
2818 return TRUE;
2819 }
2820
2821 return FALSE;
2822}
2823
2824/*
2825 * Create a new window to show a file above the quickfix window. Called when
2826 * only the quickfix window is present.
2827 */
2828 static int
2829qf_open_new_file_win(qf_info_T *ll_ref)
2830{
2831 int flags;
2832
2833 flags = WSP_ABOVE;
2834 if (ll_ref != NULL)
2835 flags |= WSP_NEWLOC;
2836 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002837 return FAIL; // not enough room for window
2838 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002839 swb_flags = 0;
2840 RESET_BINDING(curwin);
2841 if (ll_ref != NULL)
2842 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002843 // The new window should use the location list from the
2844 // location list window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002845 curwin->w_llist = ll_ref;
2846 ll_ref->qf_refcount++;
2847 }
2848 return OK;
2849}
2850
2851/*
2852 * Go to a window that shows the right buffer. If the window is not found, go
2853 * to the window just above the location list window. This is used for opening
2854 * a file from a location window and not from a quickfix window. If some usable
2855 * window is previously found, then it is supplied in 'use_win'.
2856 */
2857 static void
2858qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2859{
2860 win_T *win = use_win;
2861
2862 if (win == NULL)
2863 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002864 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002865 FOR_ALL_WINDOWS(win)
2866 if (win->w_buffer->b_fnum == qf_fnum)
2867 break;
2868 if (win == NULL)
2869 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002870 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002871 win = curwin;
2872 do
2873 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002874 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002875 break;
2876 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002877 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002878 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002879 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002880 } while (win != curwin);
2881 }
2882 }
2883 win_goto(win);
2884
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002885 // If the location list for the window is not set, then set it
2886 // to the location list from the location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002887 if (win->w_llist == NULL)
2888 {
2889 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002890 if (ll_ref != NULL)
2891 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002892 }
2893}
2894
2895/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002896 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2897 * not found, then go to the window just above the quickfix window. This is
2898 * used for opening a file from a quickfix window and not from a location
2899 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002900 */
2901 static void
2902qf_goto_win_with_qfl_file(int qf_fnum)
2903{
2904 win_T *win;
2905 win_T *altwin;
2906
2907 win = curwin;
2908 altwin = NULL;
2909 for (;;)
2910 {
2911 if (win->w_buffer->b_fnum == qf_fnum)
2912 break;
2913 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002914 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002915 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002916 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002917
2918 if (IS_QF_WINDOW(win))
2919 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002920 // Didn't find it, go to the window before the quickfix
2921 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002922 if (altwin != NULL)
2923 win = altwin;
2924 else if (curwin->w_prev != NULL)
2925 win = curwin->w_prev;
2926 else
2927 win = curwin->w_next;
2928 break;
2929 }
2930
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002931 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02002932 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002933 altwin = win;
2934 }
2935
2936 win_goto(win);
2937}
2938
2939/*
2940 * Find a suitable window for opening a file (qf_fnum) from the
2941 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01002942 * window, jump to it. Otherwise open a new window to display the file. If
2943 * 'newwin' is TRUE, then always open a new window. This is called from either
2944 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002945 */
2946 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002947qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002948{
2949 win_T *usable_win_ptr = NULL;
2950 int usable_win;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002951 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002952 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002953
2954 usable_win = 0;
2955
Bram Moolenaarb2443732018-11-11 22:50:27 +01002956 // If opening a new window, then don't use the location list referred by
2957 // the current window. Otherwise two windows will refer to the same
2958 // location list.
2959 if (!newwin)
2960 ll_ref = curwin->w_llist_ref;
2961
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002962 if (ll_ref != NULL)
2963 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002964 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002965 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2966 if (usable_win_ptr != NULL)
2967 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002968 }
2969
2970 if (!usable_win)
2971 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002972 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002973 win = qf_find_win_with_normal_buf();
2974 if (win != NULL)
2975 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002976 }
2977
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002978 // If no usable window is found and 'switchbuf' contains "usetab"
2979 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002980 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002981 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002982
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002983 // If there is only one window and it is the quickfix window, create a
2984 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01002985 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002986 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002987 if (qf_open_new_file_win(ll_ref) != OK)
2988 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002989 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002990 }
2991 else
2992 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002993 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002994 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002995 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002996 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002997 }
2998
2999 return OK;
3000}
3001
3002/*
3003 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003004 * Returns OK if successfully edited the file, FAIL on failing to open the
3005 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3006 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003007 */
3008 static int
3009qf_jump_edit_buffer(
3010 qf_info_T *qi,
3011 qfline_T *qf_ptr,
3012 int forceit,
3013 win_T *oldwin,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003014 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003015{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003016 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003017 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003018 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003019 int old_qf_curlist = qi->qf_curlist;
3020 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003021
3022 if (qf_ptr->qf_type == 1)
3023 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003024 // Open help file (do_ecmd() will set b_help flag, readfile() will
3025 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003026 if (!can_abandon(curbuf, forceit))
3027 {
3028 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003029 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003030 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003031
3032 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3033 ECMD_HIDE + ECMD_SET_HELP,
3034 oldwin == curwin ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003035 }
3036 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003037 retval = buflist_getfile(qf_ptr->qf_fnum,
3038 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003039
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003040 // If a location list, check whether the associated window is still
3041 // present.
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003042 if (qfl_type == QFLT_LOCATION && !win_valid_any_tab(oldwin))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003043 {
3044 EMSG(_("E924: Current window was closed"));
3045 *opened_window = FALSE;
3046 return NOTDONE;
3047 }
3048
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003049 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003050 {
3051 EMSG(_("E925: Current quickfix was changed"));
3052 return NOTDONE;
3053 }
3054
3055 if (old_qf_curlist != qi->qf_curlist
3056 || !is_qf_entry_present(qfl, qf_ptr))
3057 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003058 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003059 EMSG(_("E925: Current quickfix was changed"));
3060 else
3061 EMSG(_(e_loc_list_changed));
3062 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003063 }
3064
3065 return retval;
3066}
3067
3068/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003069 * Go to the error line in the current file using either line/column number or
3070 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003071 */
3072 static void
3073qf_jump_goto_line(
3074 linenr_T qf_lnum,
3075 int qf_col,
3076 char_u qf_viscol,
3077 char_u *qf_pattern)
3078{
3079 linenr_T i;
3080 char_u *line;
3081 colnr_T screen_col;
3082 colnr_T char_col;
3083
3084 if (qf_pattern == NULL)
3085 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003086 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003087 i = qf_lnum;
3088 if (i > 0)
3089 {
3090 if (i > curbuf->b_ml.ml_line_count)
3091 i = curbuf->b_ml.ml_line_count;
3092 curwin->w_cursor.lnum = i;
3093 }
3094 if (qf_col > 0)
3095 {
3096 curwin->w_cursor.col = qf_col - 1;
3097#ifdef FEAT_VIRTUALEDIT
3098 curwin->w_cursor.coladd = 0;
3099#endif
3100 if (qf_viscol == TRUE)
3101 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003102 // Check each character from the beginning of the error
3103 // line up to the error column. For each tab character
3104 // found, reduce the error column value by the length of
3105 // a tab character.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003106 line = ml_get_curline();
3107 screen_col = 0;
3108 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3109 {
3110 if (*line == NUL)
3111 break;
3112 if (*line++ == '\t')
3113 {
3114 curwin->w_cursor.col -= 7 - (screen_col % 8);
3115 screen_col += 8 - (screen_col % 8);
3116 }
3117 else
3118 ++screen_col;
3119 }
3120 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003121 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003122 check_cursor();
3123 }
3124 else
3125 beginline(BL_WHITE | BL_FIX);
3126 }
3127 else
3128 {
3129 pos_T save_cursor;
3130
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003131 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003132 save_cursor = curwin->w_cursor;
3133 curwin->w_cursor.lnum = 0;
3134 if (!do_search(NULL, '/', qf_pattern, (long)1,
3135 SEARCH_KEEP, NULL, NULL))
3136 curwin->w_cursor = save_cursor;
3137 }
3138}
3139
3140/*
3141 * Display quickfix list index and size message
3142 */
3143 static void
3144qf_jump_print_msg(
3145 qf_info_T *qi,
3146 int qf_index,
3147 qfline_T *qf_ptr,
3148 buf_T *old_curbuf,
3149 linenr_T old_lnum)
3150{
3151 linenr_T i;
3152 int len;
3153
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003154 // Update the screen before showing the message, unless the screen
3155 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003156 if (!msg_scrolled)
3157 update_topline_redraw();
3158 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3159 qi->qf_lists[qi->qf_curlist].qf_count,
3160 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3161 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003162 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003163 len = (int)STRLEN(IObuff);
3164 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3165
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003166 // Output the message. Overwrite to avoid scrolling when the 'O'
3167 // flag is present in 'shortmess'; But when not jumping, print the
3168 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003169 i = msg_scroll;
3170 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3171 msg_scroll = TRUE;
3172 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3173 msg_scroll = FALSE;
3174 msg_attr_keep(IObuff, 0, TRUE);
3175 msg_scroll = i;
3176}
3177
3178/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003179 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003180 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3181 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003182 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3183 * able to jump/open a window. Returns NOTDONE if a file is not associated
3184 * with the entry.
3185 */
3186 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003187qf_jump_open_window(
3188 qf_info_T *qi,
3189 qfline_T *qf_ptr,
3190 int newwin,
3191 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003192{
3193 // For ":helpgrep" find a help window or open one.
3194 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003195 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003196 return FAIL;
3197
3198 // If currently in the quickfix window, find another window to show the
3199 // file in.
3200 if (bt_quickfix(curbuf) && !*opened_window)
3201 {
3202 // If there is no file specified, we don't know where to go.
3203 // But do advance, otherwise ":cn" gets stuck.
3204 if (qf_ptr->qf_fnum == 0)
3205 return NOTDONE;
3206
Bram Moolenaarb2443732018-11-11 22:50:27 +01003207 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3208 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003209 return FAIL;
3210 }
3211
3212 return OK;
3213}
3214
3215/*
3216 * Edit a selected file from the quickfix/location list and jump to a
3217 * particular line/column, adjust the folds and display a message about the
3218 * jump.
3219 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3220 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3221 * the file.
3222 */
3223 static int
3224qf_jump_to_buffer(
3225 qf_info_T *qi,
3226 int qf_index,
3227 qfline_T *qf_ptr,
3228 int forceit,
3229 win_T *oldwin,
3230 int *opened_window,
3231 int openfold,
3232 int print_message)
3233{
3234 buf_T *old_curbuf;
3235 linenr_T old_lnum;
3236 int retval = OK;
3237
3238 // If there is a file name, read the wanted file if needed, and check
3239 // autowrite etc.
3240 old_curbuf = curbuf;
3241 old_lnum = curwin->w_cursor.lnum;
3242
3243 if (qf_ptr->qf_fnum != 0)
3244 {
3245 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3246 opened_window);
3247 if (retval != OK)
3248 return retval;
3249 }
3250
3251 // When not switched to another buffer, still need to set pc mark
3252 if (curbuf == old_curbuf)
3253 setpcmark();
3254
3255 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3256 qf_ptr->qf_pattern);
3257
3258#ifdef FEAT_FOLDING
3259 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3260 foldOpenCursor();
3261#endif
3262 if (print_message)
3263 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3264
3265 return retval;
3266}
3267
3268/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003269 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 */
3271 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003272qf_jump(qf_info_T *qi,
3273 int dir,
3274 int errornr,
3275 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003277 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3278}
3279
3280/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003281 * Jump to a quickfix line.
3282 * If dir == 0 go to entry "errornr".
3283 * If dir == FORWARD go "errornr" valid entries forward.
3284 * If dir == BACKWARD go "errornr" valid entries backward.
3285 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3286 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3287 * else if "errornr" is zero, redisplay the same line
3288 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003289 * If 'newwin' is TRUE, then open the file in a new window.
3290 */
3291 void
3292qf_jump_newwin(qf_info_T *qi,
3293 int dir,
3294 int errornr,
3295 int forceit,
3296 int newwin)
3297{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003298 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003299 qfline_T *qf_ptr;
3300 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003303 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003304 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003306 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003309 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003311 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003313 if (qi == NULL)
3314 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003315
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003316 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 {
3318 EMSG(_(e_quickfix));
3319 return;
3320 }
3321
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003322 qfl = &qi->qf_lists[qi->qf_curlist];
3323
3324 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003326 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003328
3329 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3330 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003332 qf_ptr = old_qf_ptr;
3333 qf_index = old_qf_index;
3334 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003337 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003338 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003339 // No need to print the error message if it's visible in the error
3340 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 print_message = FALSE;
3342
Bram Moolenaarb2443732018-11-11 22:50:27 +01003343 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003344 if (retval == FAIL)
3345 goto failed;
3346 if (retval == NOTDONE)
3347 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003348
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003349 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, oldwin,
3350 &opened_window, old_KeyTyped, print_message);
3351 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003353 // Quickfix/location list is freed by an autocmd
3354 qi = NULL;
3355 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003358 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003361 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003362 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003364 // Couldn't open file, so put index back where it was. This could
3365 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 qf_ptr = old_qf_ptr;
3368 qf_index = old_qf_index;
3369 }
3370 }
3371theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003372 if (qi != NULL)
3373 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003374 qfl->qf_ptr = qf_ptr;
3375 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 if (p_swb != old_swb && opened_window)
3378 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003379 // Restore old 'switchbuf' value, but not when an autocommand or
3380 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003384 swb_flags = old_swb_flags;
3385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 else
3387 free_string_option(old_swb);
3388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389}
3390
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003391// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003392static int qfFileAttr;
3393static int qfSepAttr;
3394static int qfLineAttr;
3395
3396/*
3397 * Display information about a single entry from the quickfix/location list.
3398 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003399 * 'cursel' will be set to TRUE for the currently selected entry in the
3400 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003401 */
3402 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003403qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003404{
3405 char_u *fname;
3406 buf_T *buf;
3407 int filter_entry;
3408
3409 fname = NULL;
3410 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3411 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3412 (char *)qfp->qf_module);
3413 else {
3414 if (qfp->qf_fnum != 0
3415 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3416 {
3417 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003418 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003419 fname = gettail(fname);
3420 }
3421 if (fname == NULL)
3422 sprintf((char *)IObuff, "%2d", qf_idx);
3423 else
3424 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3425 qf_idx, (char *)fname);
3426 }
3427
3428 // Support for filtering entries using :filter /pat/ clist
3429 // Match against the module name, file name, search pattern and
3430 // text of the entry.
3431 filter_entry = TRUE;
3432 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3433 filter_entry &= message_filtered(qfp->qf_module);
3434 if (filter_entry && fname != NULL)
3435 filter_entry &= message_filtered(fname);
3436 if (filter_entry && qfp->qf_pattern != NULL)
3437 filter_entry &= message_filtered(qfp->qf_pattern);
3438 if (filter_entry)
3439 filter_entry &= message_filtered(qfp->qf_text);
3440 if (filter_entry)
3441 return;
3442
3443 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003444 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003445
3446 if (qfp->qf_lnum != 0)
3447 msg_puts_attr((char_u *)":", qfSepAttr);
3448 if (qfp->qf_lnum == 0)
3449 IObuff[0] = NUL;
3450 else if (qfp->qf_col == 0)
3451 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3452 else
3453 sprintf((char *)IObuff, "%ld col %d",
3454 qfp->qf_lnum, qfp->qf_col);
3455 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3456 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3457 msg_puts_attr(IObuff, qfLineAttr);
3458 msg_puts_attr((char_u *)":", qfSepAttr);
3459 if (qfp->qf_pattern != NULL)
3460 {
3461 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3462 msg_puts(IObuff);
3463 msg_puts_attr((char_u *)":", qfSepAttr);
3464 }
3465 msg_puts((char_u *)" ");
3466
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003467 // Remove newlines and leading whitespace from the text. For an
3468 // unrecognized line keep the indent, the compiler may mark a word
3469 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003470 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3471 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3472 IObuff, IOSIZE);
3473 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003474 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003475}
3476
3477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003479 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 */
3481 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003482qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003484 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003485 qfline_T *qfp;
3486 int i;
3487 int idx1 = 1;
3488 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003489 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003490 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003491 int all = eap->forceit; // if not :cl!, only show
3492 // recognised errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003493 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
Bram Moolenaar39665952018-08-15 20:59:48 +02003495 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003496 {
3497 qi = GET_LOC_LIST(curwin);
3498 if (qi == NULL)
3499 {
3500 EMSG(_(e_loclist));
3501 return;
3502 }
3503 }
3504
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003505 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
3507 EMSG(_(e_quickfix));
3508 return;
3509 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003510 if (*arg == '+')
3511 {
3512 ++arg;
3513 plus = TRUE;
3514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3516 {
3517 EMSG(_(e_trailing));
3518 return;
3519 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003520 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003521 if (plus)
3522 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003523 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003524 idx2 = i + idx1;
3525 idx1 = i;
3526 }
3527 else
3528 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003529 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003530 if (idx1 < 0)
3531 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3532 if (idx2 < 0)
3533 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003536 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003537 shorten_fnames(FALSE);
3538
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003539 // Get the attributes for the different quickfix highlight items. Note
3540 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003541 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3542 if (qfFileAttr == 0)
3543 qfFileAttr = HL_ATTR(HLF_D);
3544 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3545 if (qfSepAttr == 0)
3546 qfSepAttr = HL_ATTR(HLF_D);
3547 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3548 if (qfLineAttr == 0)
3549 qfLineAttr = HL_ATTR(HLF_N);
3550
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003551 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003553 qfp = qfl->qf_start;
3554 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
3556 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3557 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003558 if (got_int)
3559 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003560
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003561 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003563
3564 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003565 if (qfp == NULL)
3566 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003567 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 ui_breakcheck();
3569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570}
3571
3572/*
3573 * Remove newlines and leading whitespace from an error message.
3574 * Put the result in "buf[bufsize]".
3575 */
3576 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003577qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
3579 int i;
3580 char_u *p = text;
3581
3582 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3583 {
3584 if (*p == '\n')
3585 {
3586 buf[i] = ' ';
3587 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003588 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 break;
3590 }
3591 else
3592 buf[i] = *p++;
3593 }
3594 buf[i] = NUL;
3595}
3596
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003597/*
3598 * Display information (list number, list size and the title) about a
3599 * quickfix/location list.
3600 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003601 static void
3602qf_msg(qf_info_T *qi, int which, char *lead)
3603{
3604 char *title = (char *)qi->qf_lists[which].qf_title;
3605 int count = qi->qf_lists[which].qf_count;
3606 char_u buf[IOSIZE];
3607
3608 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3609 lead,
3610 which + 1,
3611 qi->qf_listcount,
3612 count);
3613
3614 if (title != NULL)
3615 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003616 size_t len = STRLEN(buf);
3617
3618 if (len < 34)
3619 {
3620 vim_memset(buf + len, ' ', 34 - len);
3621 buf[34] = NUL;
3622 }
3623 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003624 }
3625 trunc_string(buf, buf, Columns - 1, IOSIZE);
3626 msg(buf);
3627}
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629/*
3630 * ":colder [count]": Up in the quickfix stack.
3631 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003632 * ":lolder [count]": Up in the location list stack.
3633 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 */
3635 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003636qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003638 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 int count;
3640
Bram Moolenaar39665952018-08-15 20:59:48 +02003641 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003642 {
3643 qi = GET_LOC_LIST(curwin);
3644 if (qi == NULL)
3645 {
3646 EMSG(_(e_loclist));
3647 return;
3648 }
3649 }
3650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 if (eap->addr_count != 0)
3652 count = eap->line2;
3653 else
3654 count = 1;
3655 while (count--)
3656 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003657 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003659 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
3661 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003662 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003664 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
3666 else
3667 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003668 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
3670 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003671 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003673 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
3675 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003676 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003677 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678}
3679
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003680/*
3681 * Display the information about all the quickfix/location lists in the stack
3682 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003683 void
3684qf_history(exarg_T *eap)
3685{
3686 qf_info_T *qi = &ql_info;
3687 int i;
3688
Bram Moolenaar39665952018-08-15 20:59:48 +02003689 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003690 qi = GET_LOC_LIST(curwin);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003691 if (qf_stack_empty(qi))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003692 MSG(_("No entries"));
3693 else
3694 for (i = 0; i < qi->qf_listcount; ++i)
3695 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3696}
3697
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003699 * Free all the entries in the error list "idx". Note that other information
3700 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 */
3702 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003703qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003705 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003706 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003707 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003709 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003711 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003712 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003713 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003714 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003715 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003716 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003717 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003718 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003719 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003720 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003721 // Somehow qf_count may have an incorrect value, set it to 1
3722 // to avoid crashing when it's wrong.
3723 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003724 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003725 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003726 qfl->qf_start = qfpnext;
3727 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003729
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003730 qfl->qf_index = 0;
3731 qfl->qf_start = NULL;
3732 qfl->qf_last = NULL;
3733 qfl->qf_ptr = NULL;
3734 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003735
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003736 qf_clean_dir_stack(&qfl->qf_dir_stack);
3737 qfl->qf_directory = NULL;
3738 qf_clean_dir_stack(&qfl->qf_file_stack);
3739 qfl->qf_currfile = NULL;
3740 qfl->qf_multiline = FALSE;
3741 qfl->qf_multiignore = FALSE;
3742 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743}
3744
3745/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003746 * Free error list "idx". Frees all the entries in the quickfix list,
3747 * associated context information and the title.
3748 */
3749 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003750qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003751{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003752 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003753
Bram Moolenaard23a8232018-02-10 18:45:26 +01003754 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003755 free_tv(qfl->qf_ctx);
3756 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003757 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003758 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003759}
3760
3761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 * qf_mark_adjust: adjust marks
3763 */
3764 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003765qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003766 win_T *wp,
3767 linenr_T line1,
3768 linenr_T line2,
3769 long amount,
3770 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003772 int i;
3773 qfline_T *qfp;
3774 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003775 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003776 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003777 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
Bram Moolenaarc1542742016-07-20 21:44:37 +02003779 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003780 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003781 if (wp != NULL)
3782 {
3783 if (wp->w_llist == NULL)
3784 return;
3785 qi = wp->w_llist;
3786 }
3787
3788 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003789 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003790 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003791 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3792 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 if (qfp->qf_fnum == curbuf->b_fnum)
3794 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003795 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3797 {
3798 if (amount == MAXLNUM)
3799 qfp->qf_cleared = TRUE;
3800 else
3801 qfp->qf_lnum += amount;
3802 }
3803 else if (amount_after && qfp->qf_lnum > line2)
3804 qfp->qf_lnum += amount_after;
3805 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003806
3807 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003808 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809}
3810
3811/*
3812 * Make a nice message out of the error character and the error number:
3813 * char number message
3814 * e or E 0 " error"
3815 * w or W 0 " warning"
3816 * i or I 0 " info"
3817 * 0 0 ""
3818 * other 0 " c"
3819 * e or E n " error n"
3820 * w or W n " warning n"
3821 * i or I n " info n"
3822 * 0 n " error n"
3823 * other n " c n"
3824 * 1 x "" :helpgrep
3825 */
3826 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003827qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828{
3829 static char_u buf[20];
3830 static char_u cc[3];
3831 char_u *p;
3832
3833 if (c == 'W' || c == 'w')
3834 p = (char_u *)" warning";
3835 else if (c == 'I' || c == 'i')
3836 p = (char_u *)" info";
3837 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3838 p = (char_u *)" error";
3839 else if (c == 0 || c == 1)
3840 p = (char_u *)"";
3841 else
3842 {
3843 cc[0] = ' ';
3844 cc[1] = c;
3845 cc[2] = NUL;
3846 p = cc;
3847 }
3848
3849 if (nr <= 0)
3850 return p;
3851
3852 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3853 return buf;
3854}
3855
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003857 * When "split" is FALSE: Open the entry/result under the cursor.
3858 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3859 */
3860 void
3861qf_view_result(int split)
3862{
3863 qf_info_T *qi = &ql_info;
3864
3865 if (!bt_quickfix(curbuf))
3866 return;
3867
3868 if (IS_LL_WINDOW(curwin))
3869 qi = GET_LOC_LIST(curwin);
3870
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003871 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003872 {
3873 EMSG(_(e_quickfix));
3874 return;
3875 }
3876
3877 if (split)
3878 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003879 // Open the selected entry in a new window
3880 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3881 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003882 return;
3883 }
3884
3885 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3886}
3887
3888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 * ":cwindow": open the quickfix window if we have errors to display,
3890 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003891 * ":lwindow": open the location list window if we have locations to display,
3892 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 */
3894 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003895ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003897 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003898 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 win_T *win;
3900
Bram Moolenaar39665952018-08-15 20:59:48 +02003901 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003902 {
3903 qi = GET_LOC_LIST(curwin);
3904 if (qi == NULL)
3905 return;
3906 }
3907
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003908 qfl = &qi->qf_lists[qi->qf_curlist];
3909
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003910 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003911 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003913 // If a quickfix window is open but we have no errors to display,
3914 // close the window. If a quickfix window is not open, then open
3915 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003916 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003917 || qfl->qf_nonevalid
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003918 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 {
3920 if (win != NULL)
3921 ex_cclose(eap);
3922 }
3923 else if (win == NULL)
3924 ex_copen(eap);
3925}
3926
3927/*
3928 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003929 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003932ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003934 win_T *win = NULL;
3935 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
Bram Moolenaar39665952018-08-15 20:59:48 +02003937 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003938 {
3939 qi = GET_LOC_LIST(curwin);
3940 if (qi == NULL)
3941 return;
3942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003944 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003945 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 if (win != NULL)
3947 win_close(win, FALSE);
3948}
3949
3950/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003951 * Set "w:quickfix_title" if "qi" has a title.
3952 */
3953 static void
3954qf_set_title_var(qf_list_T *qfl)
3955{
3956 if (qfl->qf_title != NULL)
3957 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3958}
3959
3960/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003961 * Goto a quickfix or location list window (if present).
3962 * Returns OK if the window is found, FAIL otherwise.
3963 */
3964 static int
3965qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3966{
3967 win_T *win;
3968
3969 win = qf_find_win(qi);
3970 if (win == NULL)
3971 return FAIL;
3972
3973 win_goto(win);
3974 if (resize)
3975 {
3976 if (vertsplit)
3977 {
3978 if (sz != win->w_width)
3979 win_setwidth(sz);
3980 }
3981 else if (sz != win->w_height)
3982 win_setheight(sz);
3983 }
3984
3985 return OK;
3986}
3987
3988/*
3989 * Open a new quickfix or location list window, load the quickfix buffer and
3990 * set the appropriate options for the window.
3991 * Returns FAIL if the window could not be opened.
3992 */
3993 static int
3994qf_open_new_cwindow(qf_info_T *qi, int height)
3995{
3996 buf_T *qf_buf;
3997 win_T *oldwin = curwin;
3998 tabpage_T *prevtab = curtab;
3999 int flags = 0;
4000 win_T *win;
4001
4002 qf_buf = qf_find_buf(qi);
4003
4004 // The current window becomes the previous window afterwards.
4005 win = curwin;
4006
4007 if (IS_QF_STACK(qi) && cmdmod.split == 0)
4008 // Create the new quickfix window at the very bottom, except when
4009 // :belowright or :aboveleft is used.
4010 win_goto(lastwin);
4011 // Default is to open the window below the current window
4012 if (cmdmod.split == 0)
4013 flags = WSP_BELOW;
4014 flags |= WSP_NEWLOC;
4015 if (win_split(height, flags) == FAIL)
4016 return FAIL; // not enough room for window
4017 RESET_BINDING(curwin);
4018
4019 if (IS_LL_STACK(qi))
4020 {
4021 // For the location list window, create a reference to the
4022 // location list from the window 'win'.
4023 curwin->w_llist_ref = win->w_llist;
4024 win->w_llist->qf_refcount++;
4025 }
4026
4027 if (oldwin != curwin)
4028 oldwin = NULL; // don't store info when in another window
4029 if (qf_buf != NULL)
4030 {
4031 // Use the existing quickfix buffer
4032 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4033 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4034 }
4035 else
4036 {
4037 // Create a new quickfix buffer
4038 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4039
4040 // switch off 'swapfile'
4041 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4042 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4043 OPT_LOCAL);
4044 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
4045 RESET_BINDING(curwin);
4046#ifdef FEAT_DIFF
4047 curwin->w_p_diff = FALSE;
4048#endif
4049#ifdef FEAT_FOLDING
4050 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4051 OPT_LOCAL);
4052#endif
4053 }
4054
4055 // Only set the height when still in the same tab page and there is no
4056 // window to the side.
4057 if (curtab == prevtab && curwin->w_width == Columns)
4058 win_setheight(height);
4059 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4060 if (win_valid(win))
4061 prevwin = win;
4062
4063 return OK;
4064}
4065
4066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004068 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 */
4070 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004071ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004073 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004074 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004076 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004077 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
Bram Moolenaar39665952018-08-15 20:59:48 +02004079 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004080 {
4081 qi = GET_LOC_LIST(curwin);
4082 if (qi == NULL)
4083 {
4084 EMSG(_(e_loclist));
4085 return;
4086 }
4087 }
4088
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004089 incr_quickfix_busy();
4090
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 if (eap->addr_count != 0)
4092 height = eap->line2;
4093 else
4094 height = QF_WINHEIGHT;
4095
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004096 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097#ifdef FEAT_GUI
4098 need_mouse_correct = TRUE;
4099#endif
4100
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004101 // Find an existing quickfix window, or open a new one.
4102 if (cmdmod.tab == 0)
4103 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4104 cmdmod.split & WSP_VERT);
4105 if (status == FAIL)
4106 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004107 {
4108 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004109 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004112 qfl = &qi->qf_lists[qi->qf_curlist];
4113 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004114 // Save the current index here, as updating the quickfix buffer may free
4115 // the quickfix list
4116 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004117
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004118 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004119 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004121 decr_quickfix_busy();
4122
4123 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 curwin->w_cursor.col = 0;
4125 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004126 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127}
4128
4129/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004130 * Move the cursor in the quickfix window to "lnum".
4131 */
4132 static void
4133qf_win_goto(win_T *win, linenr_T lnum)
4134{
4135 win_T *old_curwin = curwin;
4136
4137 curwin = win;
4138 curbuf = win->w_buffer;
4139 curwin->w_cursor.lnum = lnum;
4140 curwin->w_cursor.col = 0;
4141#ifdef FEAT_VIRTUALEDIT
4142 curwin->w_cursor.coladd = 0;
4143#endif
4144 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004145 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004146 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004147 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004148 curwin = old_curwin;
4149 curbuf = curwin->w_buffer;
4150}
4151
4152/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004153 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004154 */
4155 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004156ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004157{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004158 qf_info_T *qi = &ql_info;
4159 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004160
Bram Moolenaar39665952018-08-15 20:59:48 +02004161 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004162 {
4163 qi = GET_LOC_LIST(curwin);
4164 if (qi == NULL)
4165 {
4166 EMSG(_(e_loclist));
4167 return;
4168 }
4169 }
4170
4171 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004172 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4173 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4174}
4175
4176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 * Return the number of the current entry (line number in the quickfix
4178 * window).
4179 */
4180 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004181qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004183 qf_info_T *qi = &ql_info;
4184
4185 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004186 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004187 qi = wp->w_llist_ref;
4188
4189 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190}
4191
4192/*
4193 * Update the cursor position in the quickfix window to the current error.
4194 * Return TRUE if there is a quickfix window.
4195 */
4196 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004197qf_win_pos_update(
4198 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004199 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200{
4201 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004202 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004204 // Put the cursor on the current error in the quickfix window, so that
4205 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004206 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 if (win != NULL
4208 && qf_index <= win->w_buffer->b_ml.ml_line_count
4209 && old_qf_index != qf_index)
4210 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 if (qf_index > old_qf_index)
4212 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004213 win->w_redraw_top = old_qf_index;
4214 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 }
4216 else
4217 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004218 win->w_redraw_top = qf_index;
4219 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004221 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
4223 return win != NULL;
4224}
4225
4226/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004227 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004228 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004229 */
4230 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004231is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004232{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004233 // A window displaying the quickfix buffer will have the w_llist_ref field
4234 // set to NULL.
4235 // A window displaying a location list buffer will have the w_llist_ref
4236 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004237 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004238 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4239 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004240 return TRUE;
4241
4242 return FALSE;
4243}
4244
4245/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004246 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004247 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004248 */
4249 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004250qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004251{
4252 win_T *win;
4253
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004254 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004255 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004256 return win;
4257 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004258}
4259
4260/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004261 * Find a quickfix buffer.
4262 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 */
4264 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004265qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004267 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004268 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269
Bram Moolenaar9c102382006-05-03 21:26:49 +00004270 FOR_ALL_TAB_WINDOWS(tp, win)
4271 if (is_qf_win(win, qi))
4272 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004273
Bram Moolenaar9c102382006-05-03 21:26:49 +00004274 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275}
4276
4277/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004278 * Update the w:quickfix_title variable in the quickfix/location list window
4279 */
4280 static void
4281qf_update_win_titlevar(qf_info_T *qi)
4282{
4283 win_T *win;
4284 win_T *curwin_save;
4285
4286 if ((win = qf_find_win(qi)) != NULL)
4287 {
4288 curwin_save = curwin;
4289 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004290 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004291 curwin = curwin_save;
4292 }
4293}
4294
4295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 * Find the quickfix buffer. If it exists, update the contents.
4297 */
4298 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004299qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300{
4301 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004302 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004305 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004306 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 if (buf != NULL)
4308 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004309 linenr_T old_line_count = buf->b_ml.ml_line_count;
4310
4311 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004312 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004313 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314
Bram Moolenaard823fa92016-08-12 16:29:27 +02004315 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004316
Bram Moolenaar864293a2016-06-02 13:40:04 +02004317 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004318 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004319
Bram Moolenaar864293a2016-06-02 13:40:04 +02004320 if (old_last == NULL)
4321 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004322 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004323
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004324 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004325 aucmd_restbuf(&aco);
4326 }
4327
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004328 // Only redraw when added lines are visible. This avoids flickering
4329 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004330 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4331 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 }
4333}
4334
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004335/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004336 * Add an error line to the quickfix buffer.
4337 */
4338 static int
4339qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4340{
4341 int len;
4342 buf_T *errbuf;
4343
4344 if (qfp->qf_module != NULL)
4345 {
4346 STRCPY(IObuff, qfp->qf_module);
4347 len = (int)STRLEN(IObuff);
4348 }
4349 else if (qfp->qf_fnum != 0
4350 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4351 && errbuf->b_fname != NULL)
4352 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004353 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004354 STRCPY(IObuff, gettail(errbuf->b_fname));
4355 else
4356 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004357 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004358 if (errbuf->b_sfname == NULL
4359 || mch_isFullName(errbuf->b_sfname))
4360 {
4361 if (*dirname == NUL)
4362 mch_dirname(dirname, MAXPATHL);
4363 shorten_buf_fname(errbuf, dirname, FALSE);
4364 }
4365 STRCPY(IObuff, errbuf->b_fname);
4366 }
4367 len = (int)STRLEN(IObuff);
4368 }
4369 else
4370 len = 0;
4371 IObuff[len++] = '|';
4372
4373 if (qfp->qf_lnum > 0)
4374 {
4375 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4376 len += (int)STRLEN(IObuff + len);
4377
4378 if (qfp->qf_col > 0)
4379 {
4380 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4381 len += (int)STRLEN(IObuff + len);
4382 }
4383
4384 sprintf((char *)IObuff + len, "%s",
4385 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4386 len += (int)STRLEN(IObuff + len);
4387 }
4388 else if (qfp->qf_pattern != NULL)
4389 {
4390 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4391 len += (int)STRLEN(IObuff + len);
4392 }
4393 IObuff[len++] = '|';
4394 IObuff[len++] = ' ';
4395
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004396 // Remove newlines and leading whitespace from the text.
4397 // For an unrecognized line keep the indent, the compiler may
4398 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004399 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4400 IObuff + len, IOSIZE - len);
4401
4402 if (ml_append_buf(buf, lnum, IObuff,
4403 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4404 return FAIL;
4405
4406 return OK;
4407}
4408
4409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 * Fill current buffer with quickfix errors, replacing any previous contents.
4411 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004412 * If "old_last" is not NULL append the items after this one.
4413 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4414 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 */
4416 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004417qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004419 linenr_T lnum;
4420 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004421 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422
Bram Moolenaar864293a2016-06-02 13:40:04 +02004423 if (old_last == NULL)
4424 {
4425 if (buf != curbuf)
4426 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004427 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004428 return;
4429 }
4430
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004431 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004432 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4433 (void)ml_delete((linenr_T)1, FALSE);
4434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004436 // Check if there is anything to display
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004437 if (!qf_stack_empty(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004439 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4440 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004441
4442 *dirname = NUL;
4443
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004444 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004445 if (old_last == NULL)
4446 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004447 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004448 lnum = 0;
4449 }
4450 else
4451 {
4452 qfp = old_last->qf_next;
4453 lnum = buf->b_ml.ml_line_count;
4454 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004455 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004457 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004459
Bram Moolenaar864293a2016-06-02 13:40:04 +02004460 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004462 if (qfp == NULL)
4463 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004465
4466 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004467 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004468 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004471 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 check_lnums(TRUE);
4473
Bram Moolenaar864293a2016-06-02 13:40:04 +02004474 if (old_last == NULL)
4475 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004476 // Set the 'filetype' to "qf" each time after filling the buffer.
4477 // This resembles reading a file into a buffer, it's more logical when
4478 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004479 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004480 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4481 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004483 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004484 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004486 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004488 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004489 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004490
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004491 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004492 redraw_curbuf_later(NOT_VALID);
4493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004495 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 KeyTyped = old_KeyTyped;
4497}
4498
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004499/*
4500 * For every change made to the quickfix list, update the changed tick.
4501 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004502 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004503qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004504{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004505 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004506}
4507
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004509 * Return the quickfix/location list number with the given identifier.
4510 * Returns -1 if list is not found.
4511 */
4512 static int
4513qf_id2nr(qf_info_T *qi, int_u qfid)
4514{
4515 int qf_idx;
4516
4517 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4518 if (qi->qf_lists[qf_idx].qf_id == qfid)
4519 return qf_idx;
4520 return INVALID_QFIDX;
4521}
4522
4523/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004524 * If the current list is not "save_qfid" and we can find the list with that ID
4525 * then make it the current list.
4526 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004527 * Returns OK if successfully restored the list. Returns FAIL if the list with
4528 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004529 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004530 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004531qf_restore_list(qf_info_T *qi, int_u save_qfid)
4532{
4533 int curlist;
4534
4535 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4536 {
4537 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004538 if (curlist < 0)
4539 // list is not present
4540 return FAIL;
4541 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004542 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004543 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004544}
4545
4546/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004547 * Jump to the first entry if there is one.
4548 */
4549 static void
4550qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4551{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004552 if (qf_restore_list(qi, save_qfid) == FAIL)
4553 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004554
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004555 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004556 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004557 qf_jump(qi, 0, 0, forceit);
4558}
4559
4560/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004561 * Return TRUE when using ":vimgrep" for ":grep".
4562 */
4563 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004564grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004565{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004566 return ((cmdidx == CMD_grep
4567 || cmdidx == CMD_lgrep
4568 || cmdidx == CMD_grepadd
4569 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004570 && STRCMP("internal",
4571 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4572}
4573
4574/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004575 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004577 static char_u *
4578make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004580 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004581 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004582 case CMD_make: return (char_u *)"make";
4583 case CMD_lmake: return (char_u *)"lmake";
4584 case CMD_grep: return (char_u *)"grep";
4585 case CMD_lgrep: return (char_u *)"lgrep";
4586 case CMD_grepadd: return (char_u *)"grepadd";
4587 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4588 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590}
4591
4592/*
4593 * Return the name for the errorfile, in allocated memory.
4594 * Find a new unique name when 'makeef' contains "##".
4595 * Returns NULL for error.
4596 */
4597 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004598get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599{
4600 char_u *p;
4601 char_u *name;
4602 static int start = -1;
4603 static int off = 0;
4604#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004605 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606#endif
4607
4608 if (*p_mef == NUL)
4609 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004610 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 if (name == NULL)
4612 EMSG(_(e_notmp));
4613 return name;
4614 }
4615
4616 for (p = p_mef; *p; ++p)
4617 if (p[0] == '#' && p[1] == '#')
4618 break;
4619
4620 if (*p == NUL)
4621 return vim_strsave(p_mef);
4622
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004623 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 for (;;)
4625 {
4626 if (start == -1)
4627 start = mch_get_pid();
4628 else
4629 off += 19;
4630
4631 name = alloc((unsigned)STRLEN(p_mef) + 30);
4632 if (name == NULL)
4633 break;
4634 STRCPY(name, p_mef);
4635 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4636 STRCAT(name, p + 2);
4637 if (mch_getperm(name) < 0
4638#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004639 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 && mch_lstat((char *)name, &sb) < 0
4641#endif
4642 )
4643 break;
4644 vim_free(name);
4645 }
4646 return name;
4647}
4648
4649/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004650 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4651 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4652 */
4653 static char_u *
4654make_get_fullcmd(char_u *makecmd, char_u *fname)
4655{
4656 char_u *cmd;
4657 unsigned len;
4658
4659 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4660 if (*p_sp != NUL)
4661 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4662 cmd = alloc(len);
4663 if (cmd == NULL)
4664 return NULL;
4665 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4666 (char *)p_shq);
4667
4668 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4669 if (*p_sp != NUL)
4670 append_redir(cmd, len, p_sp, fname);
4671
4672 // Display the fully formed command. Output a newline if there's something
4673 // else than the :make command that was typed (in which case the cursor is
4674 // in column 0).
4675 if (msg_col == 0)
4676 msg_didout = FALSE;
4677 msg_start();
4678 MSG_PUTS(":!");
4679 msg_outtrans(cmd); // show what we are doing
4680
4681 return cmd;
4682}
4683
4684/*
4685 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4686 */
4687 void
4688ex_make(exarg_T *eap)
4689{
4690 char_u *fname;
4691 char_u *cmd;
4692 char_u *enc = NULL;
4693 win_T *wp = NULL;
4694 qf_info_T *qi = &ql_info;
4695 int res;
4696 char_u *au_name = NULL;
4697 int_u save_qfid;
4698
4699 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4700 if (grep_internal(eap->cmdidx))
4701 {
4702 ex_vimgrep(eap);
4703 return;
4704 }
4705
4706 au_name = make_get_auname(eap->cmdidx);
4707 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4708 curbuf->b_fname, TRUE, curbuf))
4709 {
4710#ifdef FEAT_EVAL
4711 if (aborting())
4712 return;
4713#endif
4714 }
4715#ifdef FEAT_MBYTE
4716 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4717#endif
4718
4719 if (is_loclist_cmd(eap->cmdidx))
4720 wp = curwin;
4721
4722 autowrite_all();
4723 fname = get_mef_name();
4724 if (fname == NULL)
4725 return;
4726 mch_remove(fname); // in case it's not unique
4727
4728 cmd = make_get_fullcmd(eap->arg, fname);
4729 if (cmd == NULL)
4730 return;
4731
4732 // let the shell know if we are redirecting output or not
4733 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4734
4735#ifdef AMIGA
4736 out_flush();
4737 // read window status report and redraw before message
4738 (void)char_avail();
4739#endif
4740
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004741 incr_quickfix_busy();
4742
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004743 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4744 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4745 (eap->cmdidx != CMD_grepadd
4746 && eap->cmdidx != CMD_lgrepadd),
4747 qf_cmdtitle(*eap->cmdlinep), enc);
4748 if (wp != NULL)
4749 {
4750 qi = GET_LOC_LIST(wp);
4751 if (qi == NULL)
4752 goto cleanup;
4753 }
4754 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004755 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004756
4757 // Remember the current quickfix list identifier, so that we can
4758 // check for autocommands changing the current quickfix list.
4759 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4760 if (au_name != NULL)
4761 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4762 curbuf->b_fname, TRUE, curbuf);
4763 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4764 // display the first error
4765 qf_jump_first(qi, save_qfid, FALSE);
4766
4767cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004768 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004769 mch_remove(fname);
4770 vim_free(fname);
4771 vim_free(cmd);
4772}
4773
4774/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004775 * Returns the number of valid entries in the current quickfix/location list.
4776 */
4777 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004778qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004779{
4780 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004781 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004782 qfline_T *qfp;
4783 int i, sz = 0;
4784 int prev_fnum = 0;
4785
Bram Moolenaar39665952018-08-15 20:59:48 +02004786 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004787 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004788 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004789 qi = GET_LOC_LIST(curwin);
4790 if (qi == NULL)
4791 return 0;
4792 }
4793
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004794 qfl = &qi->qf_lists[qi->qf_curlist];
4795 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004796 ++i, qfp = qfp->qf_next)
4797 {
4798 if (qfp->qf_valid)
4799 {
4800 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004801 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004802 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4803 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004804 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004805 sz++;
4806 prev_fnum = qfp->qf_fnum;
4807 }
4808 }
4809 }
4810
4811 return sz;
4812}
4813
4814/*
4815 * Returns the current index of the quickfix/location list.
4816 * Returns 0 if there is an error.
4817 */
4818 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004819qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004820{
4821 qf_info_T *qi = &ql_info;
4822
Bram Moolenaar39665952018-08-15 20:59:48 +02004823 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004824 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004825 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004826 qi = GET_LOC_LIST(curwin);
4827 if (qi == NULL)
4828 return 0;
4829 }
4830
4831 return qi->qf_lists[qi->qf_curlist].qf_index;
4832}
4833
4834/*
4835 * Returns the current index in the quickfix/location list (counting only valid
4836 * entries). If no valid entries are in the list, then returns 1.
4837 */
4838 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004839qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004840{
4841 qf_info_T *qi = &ql_info;
4842 qf_list_T *qfl;
4843 qfline_T *qfp;
4844 int i, eidx = 0;
4845 int prev_fnum = 0;
4846
Bram Moolenaar39665952018-08-15 20:59:48 +02004847 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004848 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004849 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004850 qi = GET_LOC_LIST(curwin);
4851 if (qi == NULL)
4852 return 1;
4853 }
4854
4855 qfl = &qi->qf_lists[qi->qf_curlist];
4856 qfp = qfl->qf_start;
4857
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004858 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004859 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4860 return 1;
4861
4862 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4863 {
4864 if (qfp->qf_valid)
4865 {
4866 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4867 {
4868 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4869 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004870 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004871 eidx++;
4872 prev_fnum = qfp->qf_fnum;
4873 }
4874 }
4875 else
4876 eidx++;
4877 }
4878 }
4879
4880 return eidx ? eidx : 1;
4881}
4882
4883/*
4884 * Get the 'n'th valid error entry in the quickfix or location list.
4885 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4886 * For :cdo and :ldo returns the 'n'th valid error entry.
4887 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4888 */
4889 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004890qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004891{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004892 qfline_T *qfp = qfl->qf_start;
4893 int i, eidx;
4894 int prev_fnum = 0;
4895
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004896 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004897 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4898 return 1;
4899
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004900 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004901 i++, qfp = qfp->qf_next)
4902 {
4903 if (qfp->qf_valid)
4904 {
4905 if (fdo)
4906 {
4907 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4908 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004909 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004910 eidx++;
4911 prev_fnum = qfp->qf_fnum;
4912 }
4913 }
4914 else
4915 eidx++;
4916 }
4917
4918 if (eidx == n)
4919 break;
4920 }
4921
4922 if (i <= qfl->qf_count)
4923 return i;
4924 else
4925 return 1;
4926}
4927
4928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004930 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004931 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 */
4933 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004934ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004936 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004937 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004938
Bram Moolenaar39665952018-08-15 20:59:48 +02004939 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004940 {
4941 qi = GET_LOC_LIST(curwin);
4942 if (qi == NULL)
4943 {
4944 EMSG(_(e_loclist));
4945 return;
4946 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004947 }
4948
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004949 if (eap->addr_count > 0)
4950 errornr = (int)eap->line2;
4951 else
4952 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004953 switch (eap->cmdidx)
4954 {
4955 case CMD_cc: case CMD_ll:
4956 errornr = 0;
4957 break;
4958 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4959 case CMD_lfirst:
4960 errornr = 1;
4961 break;
4962 default:
4963 errornr = 32767;
4964 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004965 }
4966
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004967 // For cdo and ldo commands, jump to the nth valid error.
4968 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02004969 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4970 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004971 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004972 eap->addr_count > 0 ? (int)eap->line1 : 1,
4973 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4974
4975 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976}
4977
4978/*
4979 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004980 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004981 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 */
4983 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004984ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004986 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004987 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004988 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004989
Bram Moolenaar39665952018-08-15 20:59:48 +02004990 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004991 {
4992 qi = GET_LOC_LIST(curwin);
4993 if (qi == NULL)
4994 {
4995 EMSG(_(e_loclist));
4996 return;
4997 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004998 }
4999
Bram Moolenaar55b69262017-08-13 13:42:01 +02005000 if (eap->addr_count > 0
5001 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5002 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005003 errornr = (int)eap->line2;
5004 else
5005 errornr = 1;
5006
Bram Moolenaar39665952018-08-15 20:59:48 +02005007 // Depending on the command jump to either next or previous entry/file.
5008 switch (eap->cmdidx)
5009 {
5010 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5011 dir = FORWARD;
5012 break;
5013 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5014 case CMD_lNext:
5015 dir = BACKWARD;
5016 break;
5017 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5018 dir = FORWARD_FILE;
5019 break;
5020 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5021 dir = BACKWARD_FILE;
5022 break;
5023 default:
5024 dir = FORWARD;
5025 break;
5026 }
5027
5028 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029}
5030
5031/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005032 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005033 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 */
5035 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005036ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005038 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005039 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005040 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005041 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005042 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005043 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005044
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005045 switch (eap->cmdidx)
5046 {
5047 case CMD_cfile: au_name = (char_u *)"cfile"; break;
5048 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
5049 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
5050 case CMD_lfile: au_name = (char_u *)"lfile"; break;
5051 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
5052 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
5053 default: break;
5054 }
5055 if (au_name != NULL)
5056 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005057#ifdef FEAT_MBYTE
5058 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
5059#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02005060#ifdef FEAT_BROWSE
5061 if (cmdmod.browse)
5062 {
5063 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005064 NULL, NULL,
5065 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005066 if (browse_file == NULL)
5067 return;
5068 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5069 vim_free(browse_file);
5070 }
5071 else
5072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005074 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005075
Bram Moolenaar39665952018-08-15 20:59:48 +02005076 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005077 wp = curwin;
5078
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005079 incr_quickfix_busy();
5080
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005081 // This function is used by the :cfile, :cgetfile and :caddfile
5082 // commands.
5083 // :cfile always creates a new quickfix list and jumps to the
5084 // first error.
5085 // :cgetfile creates a new quickfix list but doesn't jump to the
5086 // first error.
5087 // :caddfile adds to an existing quickfix list. If there is no
5088 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005089 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005090 && eap->cmdidx != CMD_laddfile),
5091 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005092 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005093 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005094 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005095 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005096 {
5097 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005098 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005099 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005100 }
5101 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005102 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005103 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005104 if (au_name != NULL)
5105 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005106
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005107 // Jump to the first error for a new list and if autocmds didn't
5108 // free the list.
5109 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5110 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005111 // display the first error
5112 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005113
5114 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005115}
5116
5117/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005118 * Return the vimgrep autocmd name.
5119 */
5120 static char_u *
5121vgr_get_auname(cmdidx_T cmdidx)
5122{
5123 switch (cmdidx)
5124 {
5125 case CMD_vimgrep: return (char_u *)"vimgrep";
5126 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5127 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5128 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5129 case CMD_grep: return (char_u *)"grep";
5130 case CMD_lgrep: return (char_u *)"lgrep";
5131 case CMD_grepadd: return (char_u *)"grepadd";
5132 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5133 default: return NULL;
5134 }
5135}
5136
5137/*
5138 * Initialize the regmatch used by vimgrep for pattern "s".
5139 */
5140 static void
5141vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5142{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005143 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005144 regmatch->regprog = NULL;
5145
5146 if (s == NULL || *s == NUL)
5147 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005148 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005149 if (last_search_pat() == NULL)
5150 {
5151 EMSG(_(e_noprevre));
5152 return;
5153 }
5154 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5155 }
5156 else
5157 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5158
5159 regmatch->rmm_ic = p_ic;
5160 regmatch->rmm_maxcol = 0;
5161}
5162
5163/*
5164 * Display a file name when vimgrep is running.
5165 */
5166 static void
5167vgr_display_fname(char_u *fname)
5168{
5169 char_u *p;
5170
5171 msg_start();
5172 p = msg_strtrunc(fname, TRUE);
5173 if (p == NULL)
5174 msg_outtrans(fname);
5175 else
5176 {
5177 msg_outtrans(p);
5178 vim_free(p);
5179 }
5180 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005181 msg_didout = FALSE; // overwrite this message
5182 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005183 msg_col = 0;
5184 out_flush();
5185}
5186
5187/*
5188 * Load a dummy buffer to search for a pattern using vimgrep.
5189 */
5190 static buf_T *
5191vgr_load_dummy_buf(
5192 char_u *fname,
5193 char_u *dirname_start,
5194 char_u *dirname_now)
5195{
5196 int save_mls;
5197#if defined(FEAT_SYN_HL)
5198 char_u *save_ei = NULL;
5199#endif
5200 buf_T *buf;
5201
5202#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005203 // Don't do Filetype autocommands to avoid loading syntax and
5204 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005205 save_ei = au_event_disable(",Filetype");
5206#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005207 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005208 save_mls = p_mls;
5209 p_mls = 0;
5210
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005211 // Load file into a buffer, so that 'fileencoding' is detected,
5212 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005213 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5214
5215 p_mls = save_mls;
5216#if defined(FEAT_SYN_HL)
5217 au_event_restore(save_ei);
5218#endif
5219
5220 return buf;
5221}
5222
5223/*
5224 * Check whether a quickfix/location list valid. Autocmds may remove or change
5225 * a quickfix list when vimgrep is running. If the list is not found, create a
5226 * new list.
5227 */
5228 static int
5229vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005230 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005231 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005232 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005233 char_u *title)
5234{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005235 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005236 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005237 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005238 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005239 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005240 // An autocmd has freed the location list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005241 EMSG(_(e_loc_list_changed));
5242 return FALSE;
5243 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005244 else
5245 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005246 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005247 qf_new_list(qi, title);
5248 return TRUE;
5249 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005250 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005251
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005252 if (qf_restore_list(qi, qfid) == FAIL)
5253 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005254
5255 return TRUE;
5256}
5257
5258/*
5259 * Search for a pattern in all the lines in a buffer and add the matching lines
5260 * to a quickfix list.
5261 */
5262 static int
5263vgr_match_buflines(
5264 qf_info_T *qi,
5265 char_u *fname,
5266 buf_T *buf,
5267 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005268 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005269 int duplicate_name,
5270 int flags)
5271{
5272 int found_match = FALSE;
5273 long lnum;
5274 colnr_T col;
5275
Bram Moolenaar1c299432018-10-28 14:36:09 +01005276 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005277 {
5278 col = 0;
5279 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5280 col, NULL, NULL) > 0)
5281 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005282 // Pass the buffer number so that it gets used even for a
5283 // dummy buffer, unless duplicate_name is set, then the
5284 // buffer will be wiped out below.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005285 if (qf_add_entry(qi,
5286 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005287 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005288 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005289 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005290 duplicate_name ? 0 : buf->b_fnum,
5291 ml_get_buf(buf,
5292 regmatch->startpos[0].lnum + lnum, FALSE),
5293 regmatch->startpos[0].lnum + lnum,
5294 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005295 FALSE, // vis_col
5296 NULL, // search pattern
5297 0, // nr
5298 0, // type
5299 TRUE // valid
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005300 ) == FAIL)
5301 {
5302 got_int = TRUE;
5303 break;
5304 }
5305 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005306 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005307 break;
5308 if ((flags & VGR_GLOBAL) == 0
5309 || regmatch->endpos[0].lnum > 0)
5310 break;
5311 col = regmatch->endpos[0].col
5312 + (col == regmatch->endpos[0].col);
5313 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5314 break;
5315 }
5316 line_breakcheck();
5317 if (got_int)
5318 break;
5319 }
5320
5321 return found_match;
5322}
5323
5324/*
5325 * Jump to the first match and update the directory.
5326 */
5327 static void
5328vgr_jump_to_match(
5329 qf_info_T *qi,
5330 int forceit,
5331 int *redraw_for_dummy,
5332 buf_T *first_match_buf,
5333 char_u *target_dir)
5334{
5335 buf_T *buf;
5336
5337 buf = curbuf;
5338 qf_jump(qi, 0, 0, forceit);
5339 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005340 // If we jumped to another buffer redrawing will already be
5341 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005342 *redraw_for_dummy = FALSE;
5343
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005344 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005345 if (curbuf == first_match_buf && target_dir != NULL)
5346 {
5347 exarg_T ea;
5348
5349 ea.arg = target_dir;
5350 ea.cmdidx = CMD_lcd;
5351 ex_cd(&ea);
5352 }
5353}
5354
5355/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005356 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005357 * ":vimgrepadd {pattern} file(s)"
5358 * ":lvimgrep {pattern} file(s)"
5359 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005360 */
5361 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005362ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005363{
Bram Moolenaar81695252004-12-29 20:58:21 +00005364 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005365 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005366 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005367 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005368 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005369 char_u *s;
5370 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005371 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005372 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005373 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005374 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005375 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005376 buf_T *buf;
5377 int duplicate_name = FALSE;
5378 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005379 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005380 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005381 buf_T *first_match_buf = NULL;
5382 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005383 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005384 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005385 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005386 char_u *dirname_start = NULL;
5387 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005388 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005389 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005390
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005391 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005392 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5393 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005394 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005395#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005396 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005397 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005398#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005399 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005400
Bram Moolenaar39665952018-08-15 20:59:48 +02005401 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005402 {
5403 qi = ll_get_or_alloc_list(curwin);
5404 if (qi == NULL)
5405 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005406 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005407 }
5408
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005409 if (eap->addr_count > 0)
5410 tomatch = eap->line2;
5411 else
5412 tomatch = MAXLNUM;
5413
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005414 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005415 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005416 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005417 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005418 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005419 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005420 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005421 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005422 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005423
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005424 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005425 if (regmatch.regprog == NULL)
5426 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005427
5428 p = skipwhite(p);
5429 if (*p == NUL)
5430 {
5431 EMSG(_("E683: File name missing or invalid pattern"));
5432 goto theend;
5433 }
5434
Bram Moolenaar55b69262017-08-13 13:42:01 +02005435 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005436 && eap->cmdidx != CMD_vimgrepadd
5437 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005438 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005439 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005440 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005441
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005442 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005443 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005444 goto theend;
5445 if (fcount == 0)
5446 {
5447 EMSG(_(e_nomatch));
5448 goto theend;
5449 }
5450
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005451 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5452 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005453 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005454 {
5455 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005456 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005457 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005458
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005459 // Remember the current directory, because a BufRead autocommand that does
5460 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005461 mch_dirname(dirname_start, MAXPATHL);
5462
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005463 incr_quickfix_busy();
5464
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005465 // Remember the current quickfix list identifier, so that we can check for
5466 // autocommands changing the current quickfix list.
Bram Moolenaar3c097222017-12-21 20:54:49 +01005467 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005468
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005469 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005470 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005471 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005472 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005473 if (time(NULL) > seconds)
5474 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005475 // Display the file name every second or so, show the user we are
5476 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005477 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005478 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005479 }
5480
Bram Moolenaar81695252004-12-29 20:58:21 +00005481 buf = buflist_findname_exp(fnames[fi]);
5482 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5483 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005484 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005485 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005486 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005487 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005488
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005489 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005490 }
5491 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005492 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005493 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005494
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005495 // Check whether the quickfix list is still valid. When loading a
5496 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005497 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005498 {
5499 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005500 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005501 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005502 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005503 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005504
Bram Moolenaar81695252004-12-29 20:58:21 +00005505 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005506 {
5507 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005508 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005509 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005510 else
5511 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005512 // Try for a match in all lines of the buffer.
5513 // For ":1vimgrep" look for first match only.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005514 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005515 &tomatch, duplicate_name, flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005516
Bram Moolenaar81695252004-12-29 20:58:21 +00005517 if (using_dummy)
5518 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005519 if (found_match && first_match_buf == NULL)
5520 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005521 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005522 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005523 // Never keep a dummy buffer if there is another buffer
5524 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005525 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005526 buf = NULL;
5527 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005528 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005529 || buf->b_p_bh[0] == 'u' // "unload"
5530 || buf->b_p_bh[0] == 'w' // "wipe"
5531 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005532 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005533 // When no match was found we don't need to remember the
5534 // buffer, wipe it out. If there was a match and it
5535 // wasn't the first one or we won't jump there: only
5536 // unload the buffer.
5537 // Ignore 'hidden' here, because it may lead to having too
5538 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005539 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005540 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005541 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005542 buf = NULL;
5543 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005544 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005545 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005546 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005547 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005548 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005549 buf = NULL;
5550 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005551 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005552
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005553 if (buf != NULL)
5554 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005555 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005556 buf->b_flags &= ~BF_DUMMY;
5557
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005558 // If the buffer is still loaded we need to use the
5559 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005560 if (buf == first_match_buf
5561 && target_dir == NULL
5562 && STRCMP(dirname_start, dirname_now) != 0)
5563 target_dir = vim_strsave(dirname_now);
5564
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005565 // The buffer is still loaded, the Filetype autocommands
5566 // need to be done now, in that buffer. And the modelines
5567 // need to be done (again). But not the window-local
5568 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005569 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005570#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005571 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5572 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005573#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005574 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005575 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005576 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005577 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005578 }
5579 }
5580
5581 FreeWild(fcount, fnames);
5582
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005583 qfl = &qi->qf_lists[qi->qf_curlist];
5584 qfl->qf_nonevalid = FALSE;
5585 qfl->qf_ptr = qfl->qf_start;
5586 qfl->qf_index = 1;
5587 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005588
Bram Moolenaar864293a2016-06-02 13:40:04 +02005589 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005590
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005591 if (au_name != NULL)
5592 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5593 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005594 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5595 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005596 if (!qflist_valid(wp, save_qfid)
5597 || qf_restore_list(qi, save_qfid) == FAIL)
5598 {
5599 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005600 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005601 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005602
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005603 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005604 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005605 {
5606 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005607 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5608 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005609 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005610 else
5611 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005612
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005613 decr_quickfix_busy();
5614
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005615 // If we loaded a dummy buffer into the current window, the autocommands
5616 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005617 if (redraw_for_dummy)
5618 {
5619#ifdef FEAT_FOLDING
5620 foldUpdateAll(curwin);
5621#else
5622 redraw_later(NOT_VALID);
5623#endif
5624 }
5625
Bram Moolenaar86b68352004-12-27 21:59:20 +00005626theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005627 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005628 vim_free(dirname_now);
5629 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005630 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005631 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005632}
5633
5634/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005635 * Restore current working directory to "dirname_start" if they differ, taking
5636 * into account whether it is set locally or globally.
5637 */
5638 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005639restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005640{
5641 char_u *dirname_now = alloc(MAXPATHL);
5642
5643 if (NULL != dirname_now)
5644 {
5645 mch_dirname(dirname_now, MAXPATHL);
5646 if (STRCMP(dirname_start, dirname_now) != 0)
5647 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005648 // If the directory has changed, change it back by building up an
5649 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005650 exarg_T ea;
5651
5652 ea.arg = dirname_start;
5653 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5654 ex_cd(&ea);
5655 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005656 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005657 }
5658}
5659
5660/*
5661 * Load file "fname" into a dummy buffer and return the buffer pointer,
5662 * placing the directory resulting from the buffer load into the
5663 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5664 * prior to calling this function. Restores directory to "dirname_start" prior
5665 * to returning, if autocmds or the 'autochdir' option have changed it.
5666 *
5667 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5668 * or wipe_dummy_buffer() later!
5669 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005670 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005671 */
5672 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005673load_dummy_buffer(
5674 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005675 char_u *dirname_start, // in: old directory
5676 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005677{
5678 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005679 bufref_T newbufref;
5680 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005681 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005682 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005683 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005684
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005685 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005686 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5687 if (newbuf == NULL)
5688 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005689 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005690
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005691 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005692 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5693
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005694 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005695 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005696 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005697 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005698 ++newbuf->b_locked;
5699
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005700 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005701 aucmd_prepbuf(&aco, newbuf);
5702
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005703 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005704 (void)setfname(curbuf, fname, NULL, FALSE);
5705
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005706 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005707 check_need_swap(TRUE);
5708
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005709 // Remove the "dummy" flag, otherwise autocommands may not
5710 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005711 curbuf->b_flags &= ~BF_DUMMY;
5712
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005713 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005714 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005715 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005716 NULL, READ_NEW | READ_DUMMY);
5717 --newbuf->b_locked;
5718 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005719 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005720 && !(curbuf->b_flags & BF_NEW))
5721 {
5722 failed = FALSE;
5723 if (curbuf != newbuf)
5724 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005725 // Bloody autocommands changed the buffer! Can happen when
5726 // using netrw and editing a remote file. Use the current
5727 // buffer instead, delete the dummy one after restoring the
5728 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005729 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005730 newbuf = curbuf;
5731 }
5732 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005733
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005734 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005735 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005736 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5737 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005738
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005739 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5740 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005741 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005742 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005743
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005744 // When autocommands/'autochdir' option changed directory: go back.
5745 // Let the caller know what the resulting dir was first, in case it is
5746 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005747 mch_dirname(resulting_dir, MAXPATHL);
5748 restore_start_dir(dirname_start);
5749
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005750 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005751 return NULL;
5752 if (failed)
5753 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005754 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005755 return NULL;
5756 }
5757 return newbuf;
5758}
5759
5760/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005761 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5762 * directory to "dirname_start" prior to returning, if autocmds or the
5763 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005764 */
5765 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005766wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005767{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005768 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005769 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005770#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005771 cleanup_T cs;
5772
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005773 // Reset the error/interrupt/exception state here so that aborting()
5774 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5775 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005776 enter_cleanup(&cs);
5777#endif
5778
Bram Moolenaar81695252004-12-29 20:58:21 +00005779 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005780
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005781#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005782 // Restore the error/interrupt/exception state if not discarded by a
5783 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005784 leave_cleanup(&cs);
5785#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005786 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005787 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005788 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005789}
5790
5791/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005792 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5793 * directory to "dirname_start" prior to returning, if autocmds or the
5794 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005795 */
5796 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005797unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005798{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005799 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005800 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005801 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005802
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005803 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005804 restore_start_dir(dirname_start);
5805 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005806}
5807
Bram Moolenaar05159a02005-02-26 23:04:13 +00005808#if defined(FEAT_EVAL) || defined(PROTO)
5809/*
5810 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005811 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005812 */
5813 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005814get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005815{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005816 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005817 dict_T *dict;
5818 char_u buf[2];
5819 qfline_T *qfp;
5820 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005821 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005822
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005823 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005824 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005825 qi = &ql_info;
5826 if (wp != NULL)
5827 {
5828 qi = GET_LOC_LIST(wp);
5829 if (qi == NULL)
5830 return FAIL;
5831 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005832 }
5833
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005834 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005835 qf_idx = qi->qf_curlist;
5836
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005837 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005838 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005839
Bram Moolenaard823fa92016-08-12 16:29:27 +02005840 qfp = qi->qf_lists[qf_idx].qf_start;
5841 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005842 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005843 // Handle entries with a non-existing buffer number.
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005844 bufnum = qfp->qf_fnum;
5845 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5846 bufnum = 0;
5847
Bram Moolenaar05159a02005-02-26 23:04:13 +00005848 if ((dict = dict_alloc()) == NULL)
5849 return FAIL;
5850 if (list_append_dict(list, dict) == FAIL)
5851 return FAIL;
5852
5853 buf[0] = qfp->qf_type;
5854 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005855 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5856 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5857 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5858 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5859 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5860 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5861 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5862 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5863 || dict_add_string(dict, "type", buf) == FAIL
5864 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005865 return FAIL;
5866
5867 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005868 if (qfp == NULL)
5869 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005870 }
5871 return OK;
5872}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005873
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005874// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005875enum {
5876 QF_GETLIST_NONE = 0x0,
5877 QF_GETLIST_TITLE = 0x1,
5878 QF_GETLIST_ITEMS = 0x2,
5879 QF_GETLIST_NR = 0x4,
5880 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005881 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005882 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005883 QF_GETLIST_IDX = 0x40,
5884 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005885 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005886 QF_GETLIST_FILEWINID = 0x200,
5887 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005888};
5889
5890/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005891 * Parse text from 'di' and return the quickfix list items.
5892 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005893 */
5894 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005895qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005896{
5897 int status = FAIL;
5898 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005899 char_u *errorformat = p_efm;
5900 dictitem_T *efm_di;
5901 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005902
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005903 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005904 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005905 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005906 // If errorformat is supplied then use it, otherwise use the 'efm'
5907 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005908 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5909 {
5910 if (efm_di->di_tv.v_type != VAR_STRING ||
5911 efm_di->di_tv.vval.v_string == NULL)
5912 return FAIL;
5913 errorformat = efm_di->di_tv.vval.v_string;
5914 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005915
Bram Moolenaar36538222017-09-02 19:51:44 +02005916 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005917 if (l == NULL)
5918 return FAIL;
5919
Bram Moolenaar2d67d302018-11-16 18:46:02 +01005920 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005921 if (qi != NULL)
5922 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005923 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005924 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5925 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005926 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005927 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005928 }
5929 free(qi);
5930 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005931 dict_add_list(retdict, "items", l);
5932 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005933 }
5934
5935 return status;
5936}
5937
5938/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005939 * Return the quickfix/location list window identifier in the current tabpage.
5940 */
5941 static int
5942qf_winid(qf_info_T *qi)
5943{
5944 win_T *win;
5945
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005946 // The quickfix window can be opened even if the quickfix list is not set
5947 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005948 if (qi == NULL)
5949 return 0;
5950 win = qf_find_win(qi);
5951 if (win != NULL)
5952 return win->w_id;
5953 return 0;
5954}
5955
5956/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005957 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005958 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005959 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005960qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005961{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005962 int flags = QF_GETLIST_NONE;
5963
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005964 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005965 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005966 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005967 if (!loclist)
5968 // File window ID is applicable only to location list windows
5969 flags &= ~ QF_GETLIST_FILEWINID;
5970 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005971
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005972 if (dict_find(what, (char_u *)"title", -1) != NULL)
5973 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005974
Bram Moolenaara6d48492017-12-12 22:45:31 +01005975 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5976 flags |= QF_GETLIST_NR;
5977
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005978 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5979 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005980
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005981 if (dict_find(what, (char_u *)"context", -1) != NULL)
5982 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005983
Bram Moolenaara6d48492017-12-12 22:45:31 +01005984 if (dict_find(what, (char_u *)"id", -1) != NULL)
5985 flags |= QF_GETLIST_ID;
5986
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005987 if (dict_find(what, (char_u *)"items", -1) != NULL)
5988 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005989
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005990 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5991 flags |= QF_GETLIST_IDX;
5992
5993 if (dict_find(what, (char_u *)"size", -1) != NULL)
5994 flags |= QF_GETLIST_SIZE;
5995
Bram Moolenaarb254af32017-12-18 19:48:58 +01005996 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5997 flags |= QF_GETLIST_TICK;
5998
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005999 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6000 flags |= QF_GETLIST_FILEWINID;
6001
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006002 return flags;
6003}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006004
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006005/*
6006 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6007 * If 'nr' and 'id' are not present in 'what' then return the current
6008 * quickfix list index.
6009 * If 'nr' is zero then return the current quickfix list index.
6010 * If 'nr' is '$' then return the last quickfix list index.
6011 * If 'id' is present then return the index of the quickfix list with that id.
6012 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6013 * Return -1, if quickfix list is not present or if the stack is empty.
6014 */
6015 static int
6016qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6017{
6018 int qf_idx;
6019 dictitem_T *di;
6020
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006021 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006022 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6023 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006024 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006025 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006026 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006027 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006028 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006029 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006030 qf_idx = di->di_tv.vval.v_number - 1;
6031 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006032 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006033 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006034 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006035 else if (di->di_tv.v_type == VAR_STRING
6036 && di->di_tv.vval.v_string != NULL
6037 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006038 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006039 qf_idx = qi->qf_listcount - 1;
6040 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006041 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006042 }
6043
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006044 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6045 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006046 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006047 if (di->di_tv.v_type == VAR_NUMBER)
6048 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006049 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006050 if (di->di_tv.vval.v_number != 0)
6051 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6052 }
6053 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006054 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006055 }
6056
6057 return qf_idx;
6058}
6059
6060/*
6061 * Return default values for quickfix list properties in retdict.
6062 */
6063 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006064qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006065{
6066 int status = OK;
6067
6068 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006069 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006070 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6071 {
6072 list_T *l = list_alloc();
6073 if (l != NULL)
6074 status = dict_add_list(retdict, "items", l);
6075 else
6076 status = FAIL;
6077 }
6078 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006079 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006080 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006081 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006082 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006083 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006084 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006085 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006086 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006087 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006088 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006089 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006090 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006091 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006092 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006093 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006094
6095 return status;
6096}
6097
6098/*
6099 * Return the quickfix list title as 'title' in retdict
6100 */
6101 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006102qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006103{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006104 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006105}
6106
6107/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006108 * Returns the identifier of the window used to display files from a location
6109 * list. If there is no associated window, then returns 0. Useful only when
6110 * called from a location list window.
6111 */
6112 static int
6113qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6114{
6115 int winid = 0;
6116
6117 if (wp != NULL && IS_LL_WINDOW(wp))
6118 {
6119 win_T *ll_wp = qf_find_win_with_loclist(qi);
6120 if (ll_wp != NULL)
6121 winid = ll_wp->w_id;
6122 }
6123
6124 return dict_add_number(retdict, "filewinid", winid);
6125}
6126
6127/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006128 * Return the quickfix list items/entries as 'items' in retdict
6129 */
6130 static int
6131qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6132{
6133 int status = OK;
6134 list_T *l = list_alloc();
6135 if (l != NULL)
6136 {
6137 (void)get_errorlist(qi, NULL, qf_idx, l);
6138 dict_add_list(retdict, "items", l);
6139 }
6140 else
6141 status = FAIL;
6142
6143 return status;
6144}
6145
6146/*
6147 * Return the quickfix list context (if any) as 'context' in retdict.
6148 */
6149 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006150qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006151{
6152 int status;
6153 dictitem_T *di;
6154
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006155 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006156 {
6157 di = dictitem_alloc((char_u *)"context");
6158 if (di != NULL)
6159 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006160 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006161 status = dict_add(retdict, di);
6162 if (status == FAIL)
6163 dictitem_free(di);
6164 }
6165 else
6166 status = FAIL;
6167 }
6168 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006169 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006170
6171 return status;
6172}
6173
6174/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006175 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006176 */
6177 static int
6178qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6179{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006180 int curidx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006181 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006182 // For empty lists, current index is set to 0
6183 curidx = 0;
6184 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006185}
6186
6187/*
6188 * Return quickfix/location list details (title) as a
6189 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6190 * then current list is used. Otherwise the specified list is used.
6191 */
6192 int
6193qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6194{
6195 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006196 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006197 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006198 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006199 dictitem_T *di;
6200 int flags = QF_GETLIST_NONE;
6201
6202 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6203 return qf_get_list_from_lines(what, di, retdict);
6204
6205 if (wp != NULL)
6206 qi = GET_LOC_LIST(wp);
6207
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006208 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006209
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006210 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006211 qf_idx = qf_getprop_qfidx(qi, what);
6212
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006213 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006214 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006215 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006216
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006217 qfl = &qi->qf_lists[qf_idx];
6218
Bram Moolenaard823fa92016-08-12 16:29:27 +02006219 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006220 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006221 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006222 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006223 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006224 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006225 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006226 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006227 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006228 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006229 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006230 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006231 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006232 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006233 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006234 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006235 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006236 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006237 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6238 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006239
Bram Moolenaard823fa92016-08-12 16:29:27 +02006240 return status;
6241}
6242
6243/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006244 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006245 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6246 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006247 */
6248 static int
6249qf_add_entry_from_dict(
6250 qf_info_T *qi,
6251 int qf_idx,
6252 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006253 int first_entry,
6254 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006255{
6256 static int did_bufnr_emsg;
6257 char_u *filename, *module, *pattern, *text, *type;
6258 int bufnum, valid, status, col, vcol, nr;
6259 long lnum;
6260
6261 if (first_entry)
6262 did_bufnr_emsg = FALSE;
6263
Bram Moolenaar8f667172018-12-14 15:38:31 +01006264 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6265 module = dict_get_string(d, (char_u *)"module", TRUE);
6266 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6267 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6268 col = (int)dict_get_number(d, (char_u *)"col");
6269 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6270 nr = (int)dict_get_number(d, (char_u *)"nr");
6271 type = dict_get_string(d, (char_u *)"type", TRUE);
6272 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6273 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006274 if (text == NULL)
6275 text = vim_strsave((char_u *)"");
6276
6277 valid = TRUE;
6278 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6279 valid = FALSE;
6280
6281 // Mark entries with non-existing buffer number as not valid. Give the
6282 // error message only once.
6283 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6284 {
6285 if (!did_bufnr_emsg)
6286 {
6287 did_bufnr_emsg = TRUE;
6288 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6289 }
6290 valid = FALSE;
6291 bufnum = 0;
6292 }
6293
6294 // If the 'valid' field is present it overrules the detected value.
6295 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006296 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006297
6298 status = qf_add_entry(qi,
6299 qf_idx,
6300 NULL, // dir
6301 filename,
6302 module,
6303 bufnum,
6304 text,
6305 lnum,
6306 col,
6307 vcol, // vis_col
6308 pattern, // search pattern
6309 nr,
6310 type == NULL ? NUL : *type,
6311 valid);
6312
6313 vim_free(filename);
6314 vim_free(module);
6315 vim_free(pattern);
6316 vim_free(text);
6317 vim_free(type);
6318
Bram Moolenaar9752c722018-12-22 16:49:34 +01006319 if (valid)
6320 *valid_entry = TRUE;
6321
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006322 return status;
6323}
6324
6325/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006326 * Add list of entries to quickfix/location list. Each list entry is
6327 * a dictionary with item information.
6328 */
6329 static int
6330qf_add_entries(
6331 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006332 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006333 list_T *list,
6334 char_u *title,
6335 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006336{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006337 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006338 listitem_T *li;
6339 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006340 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006341 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006342 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006343
Bram Moolenaara3921f42017-06-04 15:30:34 +02006344 if (action == ' ' || qf_idx == qi->qf_listcount)
6345 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006346 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006347 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006348 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006349 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006350 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006351 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006352 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006353 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006354 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006355 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006356 qf_free_items(qfl);
6357 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006358 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006359
6360 for (li = list->lv_first; li != NULL; li = li->li_next)
6361 {
6362 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006363 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006364
6365 d = li->li_tv.vval.v_dict;
6366 if (d == NULL)
6367 continue;
6368
Bram Moolenaar9752c722018-12-22 16:49:34 +01006369 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first,
6370 &valid_entry);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006371 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006372 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006373 }
6374
Bram Moolenaar9752c722018-12-22 16:49:34 +01006375 // Check if any valid error entries are added to the list.
6376 if (valid_entry)
6377 qfl->qf_nonevalid = FALSE;
6378 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006379 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006380 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006381
6382 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006383 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006384 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006385
6386 // Update the current error index if not appending to the list or if the
6387 // list was empty before and it is not empty now.
6388 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qi, qf_idx))
6389 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006390
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006391 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006392 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006393
6394 return retval;
6395}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006396
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006397/*
6398 * Get the quickfix list index from 'nr' or 'id'
6399 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006400 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006401qf_setprop_get_qfidx(
6402 qf_info_T *qi,
6403 dict_T *what,
6404 int action,
6405 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006406{
6407 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006408 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006409
Bram Moolenaard823fa92016-08-12 16:29:27 +02006410 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6411 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006412 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006413 if (di->di_tv.v_type == VAR_NUMBER)
6414 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006415 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006416 if (di->di_tv.vval.v_number != 0)
6417 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006418
Bram Moolenaar55b69262017-08-13 13:42:01 +02006419 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6420 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006421 // When creating a new list, accept qf_idx pointing to the next
6422 // non-available list and add the new list at the end of the
6423 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006424 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006425 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006426 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006427 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006428 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006429 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006430 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006431 }
6432 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006433 && di->di_tv.vval.v_string != NULL
6434 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006435 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006436 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006437 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006438 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006439 qf_idx = 0;
6440 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006441 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006442 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006443 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006444 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006445 }
6446
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006447 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006448 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006449 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006450 if (di->di_tv.v_type != VAR_NUMBER)
6451 return INVALID_QFIDX;
6452
6453 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006454 }
6455
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006456 return qf_idx;
6457}
6458
6459/*
6460 * Set the quickfix list title.
6461 */
6462 static int
6463qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6464{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006465 qf_list_T *qfl = &qi->qf_lists[qf_idx];
6466
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006467 if (di->di_tv.v_type != VAR_STRING)
6468 return FAIL;
6469
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006470 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01006471 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006472 if (qf_idx == qi->qf_curlist)
6473 qf_update_win_titlevar(qi);
6474
6475 return OK;
6476}
6477
6478/*
6479 * Set quickfix list items/entries.
6480 */
6481 static int
6482qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6483{
6484 int retval = FAIL;
6485 char_u *title_save;
6486
6487 if (di->di_tv.v_type != VAR_LIST)
6488 return FAIL;
6489
6490 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6491 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6492 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006493 vim_free(title_save);
6494
6495 return retval;
6496}
6497
6498/*
6499 * Set quickfix list items/entries from a list of lines.
6500 */
6501 static int
6502qf_setprop_items_from_lines(
6503 qf_info_T *qi,
6504 int qf_idx,
6505 dict_T *what,
6506 dictitem_T *di,
6507 int action)
6508{
6509 char_u *errorformat = p_efm;
6510 dictitem_T *efm_di;
6511 int retval = FAIL;
6512
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006513 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006514 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6515 {
6516 if (efm_di->di_tv.v_type != VAR_STRING ||
6517 efm_di->di_tv.vval.v_string == NULL)
6518 return FAIL;
6519 errorformat = efm_di->di_tv.vval.v_string;
6520 }
6521
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006522 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006523 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6524 return FAIL;
6525
6526 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006527 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006528 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6529 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6530 retval = OK;
6531
6532 return retval;
6533}
6534
6535/*
6536 * Set quickfix list context.
6537 */
6538 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006539qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006540{
6541 typval_T *ctx;
6542
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006543 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006544 ctx = alloc_tv();
6545 if (ctx != NULL)
6546 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006547 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006548
6549 return OK;
6550}
6551
6552/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006553 * Set the current index in the specified quickfix list
6554 */
6555 static int
6556qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
6557{
6558 int denote = FALSE;
6559 int newidx;
6560 int old_qfidx;
6561 qfline_T *qf_ptr;
6562
6563 // If the specified index is '$', then use the last entry
6564 if (di->di_tv.v_type == VAR_STRING
6565 && di->di_tv.vval.v_string != NULL
6566 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
6567 newidx = qfl->qf_count;
6568 else
6569 {
6570 // Otherwise use the specified index
6571 newidx = tv_get_number_chk(&di->di_tv, &denote);
6572 if (denote)
6573 return FAIL;
6574 }
6575
6576 if (newidx < 1) // sanity check
6577 return FAIL;
6578 if (newidx > qfl->qf_count)
6579 newidx = qfl->qf_count;
6580
6581 old_qfidx = qfl->qf_index;
6582 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
6583 if (qf_ptr == NULL)
6584 return FAIL;
6585 qfl->qf_ptr = qf_ptr;
6586 qfl->qf_index = newidx;
6587
6588 // If the current list is modified and it is displayed in the quickfix
6589 // window, then Update it.
6590 if (qi->qf_lists[qi->qf_curlist].qf_id == qfl->qf_id)
6591 qf_win_pos_update(qi, old_qfidx);
6592
6593 return OK;
6594}
6595
6596/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006597 * Set quickfix/location list properties (title, items, context).
6598 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006599 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006600 */
6601 static int
6602qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6603{
6604 dictitem_T *di;
6605 int retval = FAIL;
6606 int qf_idx;
6607 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006608 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006609
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006610 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006611 newlist = TRUE;
6612
6613 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006614 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006615 return FAIL;
6616
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006617 if (newlist)
6618 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006619 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006620 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006621 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006622 }
6623
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006624 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaard823fa92016-08-12 16:29:27 +02006625 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006626 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006627 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006628 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006629 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006630 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006631 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006632 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006633 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6634 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006635
Bram Moolenaarb254af32017-12-18 19:48:58 +01006636 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006637 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006638
Bram Moolenaard823fa92016-08-12 16:29:27 +02006639 return retval;
6640}
6641
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006642/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006643 * Find the non-location list window with the specified location list stack in
6644 * the current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006645 */
6646 static win_T *
6647find_win_with_ll(qf_info_T *qi)
6648{
6649 win_T *wp = NULL;
6650
6651 FOR_ALL_WINDOWS(wp)
6652 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6653 return wp;
6654
6655 return NULL;
6656}
6657
6658/*
6659 * Free the entire quickfix/location list stack.
6660 * If the quickfix/location list window is open, then clear it.
6661 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006662 static void
6663qf_free_stack(win_T *wp, qf_info_T *qi)
6664{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006665 win_T *qfwin = qf_find_win(qi);
6666 win_T *llwin = NULL;
6667 win_T *orig_wp = wp;
6668
6669 if (qfwin != NULL)
6670 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006671 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006672 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006673 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006674 qf_update_buffer(qi, NULL);
6675 }
6676
6677 if (wp != NULL && IS_LL_WINDOW(wp))
6678 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006679 // If in the location list window, then use the non-location list
6680 // window with this location list (if present)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006681 llwin = find_win_with_ll(qi);
6682 if (llwin != NULL)
6683 wp = llwin;
6684 }
6685
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006686 qf_free_all(wp);
6687 if (wp == NULL)
6688 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006689 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006690 qi->qf_curlist = 0;
6691 qi->qf_listcount = 0;
6692 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006693 else if (IS_LL_WINDOW(orig_wp))
6694 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006695 // If the location list window is open, then create a new empty
6696 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006697 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006698
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006699 // first free the list reference in the location list window
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006700 ll_free_all(&orig_wp->w_llist_ref);
6701
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006702 orig_wp->w_llist_ref = new_ll;
6703 if (llwin != NULL)
6704 {
6705 llwin->w_llist = new_ll;
6706 new_ll->qf_refcount++;
6707 }
6708 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006709}
6710
Bram Moolenaard823fa92016-08-12 16:29:27 +02006711/*
6712 * Populate the quickfix list with the items supplied in the list
6713 * of dictionaries. "title" will be copied to w:quickfix_title.
6714 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6715 */
6716 int
6717set_errorlist(
6718 win_T *wp,
6719 list_T *list,
6720 int action,
6721 char_u *title,
6722 dict_T *what)
6723{
6724 qf_info_T *qi = &ql_info;
6725 int retval = OK;
6726
6727 if (wp != NULL)
6728 {
6729 qi = ll_get_or_alloc_list(wp);
6730 if (qi == NULL)
6731 return FAIL;
6732 }
6733
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006734 if (action == 'f')
6735 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006736 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006737 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006738 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006739 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006740
6741 incr_quickfix_busy();
6742
6743 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006744 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006745 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006746 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006747 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006748 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006749 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006750 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006751
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006752 decr_quickfix_busy();
6753
Bram Moolenaard823fa92016-08-12 16:29:27 +02006754 return retval;
6755}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006756
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006757/*
6758 * Mark the context as in use for all the lists in a quickfix stack.
6759 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006760 static int
6761mark_quickfix_ctx(qf_info_T *qi, int copyID)
6762{
6763 int i;
6764 int abort = FALSE;
6765 typval_T *ctx;
6766
6767 for (i = 0; i < LISTCOUNT && !abort; ++i)
6768 {
6769 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006770 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6771 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006772 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6773 }
6774
6775 return abort;
6776}
6777
6778/*
6779 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006780 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006781 */
6782 int
6783set_ref_in_quickfix(int copyID)
6784{
6785 int abort = FALSE;
6786 tabpage_T *tp;
6787 win_T *win;
6788
6789 abort = mark_quickfix_ctx(&ql_info, copyID);
6790 if (abort)
6791 return abort;
6792
6793 FOR_ALL_TAB_WINDOWS(tp, win)
6794 {
6795 if (win->w_llist != NULL)
6796 {
6797 abort = mark_quickfix_ctx(win->w_llist, copyID);
6798 if (abort)
6799 return abort;
6800 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006801 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6802 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006803 // In a location list window and none of the other windows is
6804 // referring to this location list. Mark the location list
6805 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006806 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6807 if (abort)
6808 return abort;
6809 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006810 }
6811
6812 return abort;
6813}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006814#endif
6815
Bram Moolenaar81695252004-12-29 20:58:21 +00006816/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006817 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006818 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006819 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006820 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006821 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006822 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006823 */
6824 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006825ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006826{
6827 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006828 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006829 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006830 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006831 int_u save_qfid;
6832 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006833
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006834 switch (eap->cmdidx)
6835 {
6836 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6837 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6838 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6839 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6840 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6841 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6842 default: break;
6843 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006844 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6845 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006846 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006847#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006848 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006849 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006850#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006851 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006852
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006853 // Must come after autocommands.
Bram Moolenaar39665952018-08-15 20:59:48 +02006854 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006855 {
6856 qi = ll_get_or_alloc_list(curwin);
6857 if (qi == NULL)
6858 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006859 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006860 }
6861
Bram Moolenaar86b68352004-12-27 21:59:20 +00006862 if (*eap->arg == NUL)
6863 buf = curbuf;
6864 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6865 buf = buflist_findnr(atoi((char *)eap->arg));
6866 if (buf == NULL)
6867 EMSG(_(e_invarg));
6868 else if (buf->b_ml.ml_mfp == NULL)
6869 EMSG(_("E681: Buffer is not loaded"));
6870 else
6871 {
6872 if (eap->addr_count == 0)
6873 {
6874 eap->line1 = 1;
6875 eap->line2 = buf->b_ml.ml_line_count;
6876 }
6877 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6878 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6879 EMSG(_(e_invrange));
6880 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006881 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006882 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006883
6884 if (buf->b_sfname)
6885 {
6886 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6887 (char *)qf_title, (char *)buf->b_sfname);
6888 qf_title = IObuff;
6889 }
6890
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006891 incr_quickfix_busy();
6892
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006893 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006894 (eap->cmdidx != CMD_caddbuffer
6895 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006896 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006897 qf_title, NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006898 if (qf_stack_empty(qi))
6899 {
6900 decr_quickfix_busy();
6901 return;
6902 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006903 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006904 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006905
6906 // Remember the current quickfix list identifier, so that we can
6907 // check for autocommands changing the current quickfix list.
6908 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006909 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006910 {
6911 buf_T *curbuf_old = curbuf;
6912
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006913 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6914 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006915 if (curbuf != curbuf_old)
6916 // Autocommands changed buffer, don't jump now, "qi" may
6917 // be invalid.
6918 res = 0;
6919 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006920 // Jump to the first error for a new list and if autocmds didn't
6921 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006922 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006923 eap->cmdidx == CMD_lbuffer)
6924 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006925 // display the first error
6926 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006927
6928 decr_quickfix_busy();
Bram Moolenaar754b5602006-02-09 23:53:20 +00006929 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006930 }
6931}
6932
Bram Moolenaar1e015462005-09-25 22:16:38 +00006933#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006934/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006935 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6936 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006937 */
6938 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006939ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006940{
6941 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006942 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006943 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006944 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006945 int_u save_qfid;
6946 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006947
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006948 switch (eap->cmdidx)
6949 {
6950 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6951 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6952 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6953 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6954 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6955 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6956 default: break;
6957 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006958 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6959 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006960 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006961#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006962 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006963 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006964#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006965 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006966
Bram Moolenaar39665952018-08-15 20:59:48 +02006967 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006968 {
6969 qi = ll_get_or_alloc_list(curwin);
6970 if (qi == NULL)
6971 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006972 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006973 }
6974
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006975 // Evaluate the expression. When the result is a string or a list we can
6976 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006977 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006978 if (tv != NULL)
6979 {
6980 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6981 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6982 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006983 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006984 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006985 (eap->cmdidx != CMD_caddexpr
6986 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006987 (linenr_T)0, (linenr_T)0,
6988 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006989 if (qf_stack_empty(qi))
6990 {
6991 decr_quickfix_busy();
6992 goto cleanup;
6993 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006994 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006995 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006996
6997 // Remember the current quickfix list identifier, so that we can
6998 // check for autocommands changing the current quickfix list.
6999 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007000 if (au_name != NULL)
7001 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7002 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007003
7004 // Jump to the first error for a new list and if autocmds didn't
7005 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02007006 if (res > 0 && (eap->cmdidx == CMD_cexpr
7007 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007008 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02007009 // display the first error
7010 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007011 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00007012 }
7013 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007014 EMSG(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007015cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00007016 free_tv(tv);
7017 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007018}
Bram Moolenaar1e015462005-09-25 22:16:38 +00007019#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007020
7021/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007022 * Get the location list for ":lhelpgrep"
7023 */
7024 static qf_info_T *
7025hgr_get_ll(int *new_ll)
7026{
7027 win_T *wp;
7028 qf_info_T *qi;
7029
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007030 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007031 if (bt_help(curwin->w_buffer))
7032 wp = curwin;
7033 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007034 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007035 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007036
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007037 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007038 qi = NULL;
7039 else
7040 qi = wp->w_llist;
7041
7042 if (qi == NULL)
7043 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007044 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007045 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007046 return NULL;
7047 *new_ll = TRUE;
7048 }
7049
7050 return qi;
7051}
7052
7053/*
7054 * Search for a pattern in a help file.
7055 */
7056 static void
7057hgr_search_file(
7058 qf_info_T *qi,
7059 char_u *fname,
7060#ifdef FEAT_MBYTE
7061 vimconv_T *p_vc,
7062#endif
7063 regmatch_T *p_regmatch)
7064{
7065 FILE *fd;
7066 long lnum;
7067
7068 fd = mch_fopen((char *)fname, "r");
7069 if (fd == NULL)
7070 return;
7071
7072 lnum = 1;
7073 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7074 {
7075 char_u *line = IObuff;
7076#ifdef FEAT_MBYTE
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007077 // Convert a line if 'encoding' is not utf-8 and
7078 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007079 if (p_vc->vc_type != CONV_NONE
7080 && has_non_ascii(IObuff))
7081 {
7082 line = string_convert(p_vc, IObuff, NULL);
7083 if (line == NULL)
7084 line = IObuff;
7085 }
7086#endif
7087
7088 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7089 {
7090 int l = (int)STRLEN(line);
7091
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007092 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007093 while (l > 0 && line[l - 1] <= ' ')
7094 line[--l] = NUL;
7095
7096 if (qf_add_entry(qi,
7097 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007098 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007099 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007100 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007101 0,
7102 line,
7103 lnum,
7104 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007105 + 1, // col
7106 FALSE, // vis_col
7107 NULL, // search pattern
7108 0, // nr
7109 1, // type
7110 TRUE // valid
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007111 ) == FAIL)
7112 {
7113 got_int = TRUE;
7114#ifdef FEAT_MBYTE
7115 if (line != IObuff)
7116 vim_free(line);
7117#endif
7118 break;
7119 }
7120 }
7121#ifdef FEAT_MBYTE
7122 if (line != IObuff)
7123 vim_free(line);
7124#endif
7125 ++lnum;
7126 line_breakcheck();
7127 }
7128 fclose(fd);
7129}
7130
7131/*
7132 * Search for a pattern in all the help files in the doc directory under
7133 * the given directory.
7134 */
7135 static void
7136hgr_search_files_in_dir(
7137 qf_info_T *qi,
7138 char_u *dirname,
7139 regmatch_T *p_regmatch
7140#ifdef FEAT_MBYTE
7141 , vimconv_T *p_vc
7142#endif
7143#ifdef FEAT_MULTI_LANG
7144 , char_u *lang
7145#endif
7146 )
7147{
7148 int fcount;
7149 char_u **fnames;
7150 int fi;
7151
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007152 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007153 add_pathsep(dirname);
7154 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7155 if (gen_expand_wildcards(1, &dirname, &fcount,
7156 &fnames, EW_FILE|EW_SILENT) == OK
7157 && fcount > 0)
7158 {
7159 for (fi = 0; fi < fcount && !got_int; ++fi)
7160 {
7161#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007162 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007163 if (lang != NULL
7164 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007165 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007166 && !(STRNICMP(lang, "en", 2) == 0
7167 && STRNICMP("txt", fnames[fi]
7168 + STRLEN(fnames[fi]) - 3, 3) == 0))
7169 continue;
7170#endif
7171
7172 hgr_search_file(qi, fnames[fi],
7173#ifdef FEAT_MBYTE
7174 p_vc,
7175#endif
7176 p_regmatch);
7177 }
7178 FreeWild(fcount, fnames);
7179 }
7180}
7181
7182/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007183 * Search for a pattern in all the help files in the 'runtimepath'
7184 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007185 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007186 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007187 */
7188 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007189hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007190{
7191 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007192
7193#ifdef FEAT_MBYTE
7194 vimconv_T vc;
7195
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007196 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7197 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007198 vc.vc_type = CONV_NONE;
7199 if (!enc_utf8)
7200 convert_setup(&vc, (char_u *)"utf-8", p_enc);
7201#endif
7202
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007203 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007204 p = p_rtp;
7205 while (*p != NUL && !got_int)
7206 {
7207 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7208
7209 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
7210#ifdef FEAT_MBYTE
7211 , &vc
7212#endif
7213#ifdef FEAT_MULTI_LANG
7214 , lang
7215#endif
7216 );
7217 }
7218
7219#ifdef FEAT_MBYTE
7220 if (vc.vc_type != CONV_NONE)
7221 convert_setup(&vc, NULL, NULL);
7222#endif
7223}
7224
7225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 * ":helpgrep {pattern}"
7227 */
7228 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007229ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230{
7231 regmatch_T regmatch;
7232 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007233 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007234 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007235 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007236 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
Bram Moolenaar73633f82012-01-20 13:39:07 +01007238 switch (eap->cmdidx)
7239 {
7240 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7241 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7242 default: break;
7243 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007244 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7245 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007246 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007247#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007248 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007249 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007250#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007251 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007252
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007253 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01007254 save_cpo = p_cpo;
7255 p_cpo = empty_option;
7256
Bram Moolenaar39665952018-08-15 20:59:48 +02007257 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007258 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007259 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007260 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007261 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007262 }
7263
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007264 incr_quickfix_busy();
7265
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007266#ifdef FEAT_MULTI_LANG
7267 // Check for a specified language
7268 lang = check_help_lang(eap->arg);
7269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7271 regmatch.rm_ic = FALSE;
7272 if (regmatch.regprog != NULL)
7273 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007274 qf_list_T *qfl;
7275
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007276 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007277 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007279 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007280
Bram Moolenaar473de612013-06-08 18:19:48 +02007281 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007283 qfl = &qi->qf_lists[qi->qf_curlist];
7284 qfl->qf_nonevalid = FALSE;
7285 qfl->qf_ptr = qfl->qf_start;
7286 qfl->qf_index = 1;
7287 qf_list_changed(qfl);
7288 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 }
7290
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007291 if (p_cpo == empty_option)
7292 p_cpo = save_cpo;
7293 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007294 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007295 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
Bram Moolenaar73633f82012-01-20 13:39:07 +01007297 if (au_name != NULL)
7298 {
7299 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7300 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007301 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007302 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007303 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007304 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007305 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007306 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007307 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007308
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007309 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007310 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007311 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007312 else
7313 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007314
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007315 decr_quickfix_busy();
7316
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007317 if (eap->cmdidx == CMD_lhelpgrep)
7318 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007319 // If the help window is not opened or if it already points to the
7320 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007321 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007322 {
7323 if (new_qi)
7324 ll_free_all(&qi);
7325 }
7326 else if (curwin->w_llist == NULL)
7327 curwin->w_llist = qi;
7328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329}
7330
7331#endif /* FEAT_QUICKFIX */