blob: 25eea1a4767f9e069573d8c76a1879a7ca859003 [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 Moolenaaree8188f2019-02-05 21:23:04 +010051#define INVALID_QFBUFNR (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020053/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010054 * Quickfix list type.
55 */
56typedef enum
57{
58 QFLT_QUICKFIX, // Quickfix list - global list
59 QFLT_LOCATION, // Location list - per window list
60 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
61} qfltype_T;
62
63/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020064 * Quickfix/Location list definition
65 * Contains a list of entries (qfline_T). qf_start points to the first entry
66 * and qf_last points to the last entry. qf_count contains the list size.
67 *
68 * Usually the list contains one or more entries. But an empty list can be
69 * created using setqflist()/setloclist() with a title and/or user context
70 * information and entries can be added later using setqflist()/setloclist().
71 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000072typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000073{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020074 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010075 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020076 qfline_T *qf_start; // pointer to the first error
77 qfline_T *qf_last; // pointer to the last error
78 qfline_T *qf_ptr; // pointer to the current error
79 int qf_count; // number of errors (0 means empty list)
80 int qf_index; // current index in the error list
81 int qf_nonevalid; // TRUE if not a single valid entry found
82 char_u *qf_title; // title derived from the command that created
83 // the error list or set by setqflist
84 typval_T *qf_ctx; // context set by setqflist/setloclist
Bram Moolenaara7df8c72017-07-19 13:23:06 +020085
86 struct dir_stack_T *qf_dir_stack;
87 char_u *qf_directory;
88 struct dir_stack_T *qf_file_stack;
89 char_u *qf_currfile;
90 int qf_multiline;
91 int qf_multiignore;
92 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010093 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000094} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000095
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020096/*
97 * Quickfix/Location list stack definition
98 * Contains a list of quickfix/location lists (qf_list_T)
99 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000100struct qf_info_S
101{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200102 // Count of references to this list. Used only for location lists.
103 // When a location list window reference this list, qf_refcount
104 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
105 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000106 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200107 int qf_listcount; // current number of lists
108 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000109 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100110 qfltype_T qfl_type; // type of list
Bram Moolenaaree8188f2019-02-05 21:23:04 +0100111 int qf_bufnr; // quickfix window buffer number
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000112};
113
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200114static qf_info_T ql_info; // global quickfix list
115static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200117#define FMT_PATTERNS 11 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
120 * Structure used to hold the info of one part of 'errorformat'
121 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000122typedef struct efm_S efm_T;
123struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200125 regprog_T *prog; // pre-formatted part of 'errorformat'
126 efm_T *next; // pointer to next (NULL if last)
127 char_u addr[FMT_PATTERNS]; // indices of used % patterns
128 char_u prefix; // prefix of this format line:
129 // 'D' enter directory
130 // 'X' leave directory
131 // 'A' start of multi-line message
132 // 'E' error message
133 // 'W' warning message
134 // 'I' informational message
135 // 'C' continuation line
136 // 'Z' end of multi-line message
137 // 'G' general, unspecific message
138 // 'P' push file (partial) message
139 // 'Q' pop/quit file (partial) message
140 // 'O' overread (partial) message
141 char_u flags; // additional flags given in prefix
142 // '-' do not include this line
143 // '+' include whole line in message
144 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145};
146
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200147// List of location lists to be deleted.
148// Used to delay the deletion of locations lists by autocmds.
149typedef struct qf_delq_S
150{
151 struct qf_delq_S *next;
152 qf_info_T *qi;
153} qf_delq_T;
154static qf_delq_T *qf_delq_head = NULL;
155
156// Counter to prevent autocmds from freeing up location lists when they are
157// still being used.
158static int quickfix_busy = 0;
159
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200160static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100161
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100162static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100163static int qf_add_entry(qf_list_T *qfl, 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 +0200164static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100166static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200167static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200169static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100172static win_T *qf_find_win(qf_info_T *qi);
173static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200174static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +0100175static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
177static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
178static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
179static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200181// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000182#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200183// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000184#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200185
186// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100187#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
188#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
189#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
190#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200191
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000192/*
193 * Return location list for window 'wp'
194 * For location list window, return the referenced location list
195 */
196#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
197
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200199 * Looking up a buffer can be slow if there are many. Remember the last one
200 * to make this a lot faster if there are multiple matches in the same file.
201 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200202static char_u *qf_last_bufname = NULL;
203static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200204
Bram Moolenaar3c097222017-12-21 20:54:49 +0100205static char *e_loc_list_changed =
206 N_("E926: Current location list was changed");
207
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200208/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200209 * Maximum number of bytes allowed per line while reading a errorfile.
210 */
211#define LINE_MAXLEN 4096
212
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200213static struct fmtpattern
214{
215 char_u convchar;
216 char *pattern;
217} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200218 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200219 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200220 {'n', "\\d\\+"},
221 {'l', "\\d\\+"},
222 {'c', "\\d\\+"},
223 {'t', "."},
224 {'m', ".\\+"},
225 {'r', ".*"},
226 {'p', "[- .]*"},
227 {'v', "\\d\\+"},
228 {'s', ".\\+"},
229 {'o', ".\\+"}
230 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200231
232/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200233 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200234 * See fmt_pat definition above for the list of supported patterns. The
235 * pattern specifier is supplied in "efmpat". The converted pattern is stored
236 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200237 */
238 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200239efmpat_to_regpat(
240 char_u *efmpat,
241 char_u *regpat,
242 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200243 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100244 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200245{
246 char_u *srcptr;
247
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200248 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200249 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200250 // Each errorformat pattern can occur only once
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100251 semsg(_("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200252 return NULL;
253 }
254 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200255 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200256 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200257 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100259 semsg(_("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200260 return NULL;
261 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200262 efminfo->addr[idx] = (char_u)++round;
263 *regpat++ = '\\';
264 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200265#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200266 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200267 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200268 // Also match "c:" in the file name, even when
269 // checking for a colon next: "%f:".
270 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200271 STRCPY(regpat, "\\%(\\a:\\)\\=");
272 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200273 }
274#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200275 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200276 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200277 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200278 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200279 // A file name may contain spaces, but this isn't
280 // in "\f". For "%f:%l:%m" there may be a ":" in
281 // the file name. Use ".\{-1,}x" instead (x is
282 // the next character), the requirement that :999:
283 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200284 STRCPY(regpat, ".\\{-1,}");
285 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200286 }
287 else
288 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200289 // File name followed by '\\' or '%': include as
290 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200291 STRCPY(regpat, "\\f\\+");
292 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200293 }
294 }
295 else
296 {
297 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200298 while ((*regpat = *srcptr++) != NUL)
299 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200300 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200301 *regpat++ = '\\';
302 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200303
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200304 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200305}
306
307/*
308 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200309 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200310 */
311 static char_u *
312scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200313 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200314 char_u *efm,
315 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100316 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200317{
318 char_u *efmp = *pefmp;
319
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200320 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200321 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200322 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200323 {
324 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200325 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200326 if (efmp < efm + len)
327 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200328 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200329 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200330 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200331 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200332 if (efmp == efm + len)
333 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100334 emsg(_("E374: Missing ] in format string"));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200335 return NULL;
336 }
337 }
338 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200339 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200340 *regpat++ = *++efmp;
341 *regpat++ = '\\';
342 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200343 }
344 else
345 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200346 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100347 semsg(_("E375: Unsupported %%%c in format string"), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200348 return NULL;
349 }
350
351 *pefmp = efmp;
352
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200353 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200354}
355
356/*
357 * Analyze/parse an errorformat prefix.
358 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200359 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100360efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200361{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200362 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200363 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200364 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200365 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200366 else
367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100368 semsg(_("E376: Invalid %%%c in format string prefix"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200369 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200370 }
371
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200372 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200373}
374
375/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200376 * Converts a 'errorformat' string part in 'efm' to a regular expression
377 * pattern. The resulting regex pattern is returned in "regpat". Additional
378 * information about the 'erroformat' pattern is returned in "fmt_ptr".
379 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200380 */
381 static int
382efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200383 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200384 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200385 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100386 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200387{
388 char_u *ptr;
389 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200390 int round;
391 int idx = 0;
392
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200393 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200394 ptr = regpat;
395 *ptr++ = '^';
396 round = 0;
397 for (efmp = efm; efmp < efm + len; ++efmp)
398 {
399 if (*efmp == '%')
400 {
401 ++efmp;
402 for (idx = 0; idx < FMT_PATTERNS; ++idx)
403 if (fmt_pat[idx].convchar == *efmp)
404 break;
405 if (idx < FMT_PATTERNS)
406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100407 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200408 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200409 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200410 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200411 }
412 else if (*efmp == '*')
413 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200414 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100415 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200416 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200417 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200418 }
419 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200420 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200421 else if (*efmp == '#')
422 *ptr++ = '*';
423 else if (*efmp == '>')
424 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200425 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200426 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200427 // prefix is allowed only at the beginning of the errorformat
428 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100429 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200430 if (efmp == NULL)
431 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200432 }
433 else
434 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100435 semsg(_("E377: Invalid %%%c in format string"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200436 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200437 }
438 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200439 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200440 {
441 if (*efmp == '\\' && efmp + 1 < efm + len)
442 ++efmp;
443 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200444 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200445 if (*efmp)
446 *ptr++ = *efmp;
447 }
448 }
449 *ptr++ = '$';
450 *ptr = NUL;
451
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200452 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200453}
454
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200455/*
456 * Free the 'errorformat' information list
457 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200458 static void
459free_efm_list(efm_T **efm_first)
460{
461 efm_T *efm_ptr;
462
463 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
464 {
465 *efm_first = efm_ptr->next;
466 vim_regfree(efm_ptr->prog);
467 vim_free(efm_ptr);
468 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100469 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200470}
471
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200472/*
473 * Compute the size of the buffer used to convert a 'errorformat' pattern into
474 * a regular expression pattern.
475 */
476 static int
477efm_regpat_bufsz(char_u *efm)
478{
479 int sz;
480 int i;
481
482 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
483 for (i = FMT_PATTERNS; i > 0; )
484 sz += (int)STRLEN(fmt_pat[--i].pattern);
485#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200486 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200487#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200488 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200489#endif
490
491 return sz;
492}
493
494/*
495 * Return the length of a 'errorformat' option part (separated by ",").
496 */
497 static int
498efm_option_part_len(char_u *efm)
499{
500 int len;
501
502 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
503 if (efm[len] == '\\' && efm[len + 1] != NUL)
504 ++len;
505
506 return len;
507}
508
509/*
510 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
511 * are parsed and converted to regular expressions. Returns information about
512 * the parsed 'errorformat' option.
513 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200514 static efm_T *
515parse_efm_option(char_u *efm)
516{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200517 efm_T *fmt_ptr = NULL;
518 efm_T *fmt_first = NULL;
519 efm_T *fmt_last = NULL;
520 char_u *fmtstr = NULL;
521 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200522 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200523
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200524 // Each part of the format string is copied and modified from errorformat
525 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200526
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200527 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200528 sz = efm_regpat_bufsz(efm);
529 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200530 goto parse_efm_error;
531
532 while (efm[0] != NUL)
533 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200534 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200535 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
536 if (fmt_ptr == NULL)
537 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200538 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200539 fmt_first = fmt_ptr;
540 else
541 fmt_last->next = fmt_ptr;
542 fmt_last = fmt_ptr;
543
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200544 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200545 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200546
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100547 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200548 goto parse_efm_error;
549 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
550 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200551 // Advance to next part
552 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200553 }
554
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200555 if (fmt_first == NULL) // nothing found
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100556 emsg(_("E378: 'errorformat' contains no pattern"));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200557
558 goto parse_efm_end;
559
560parse_efm_error:
561 free_efm_list(&fmt_first);
562
563parse_efm_end:
564 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200565
566 return fmt_first;
567}
568
Bram Moolenaare0d37972016-07-15 22:36:01 +0200569enum {
570 QF_FAIL = 0,
571 QF_OK = 1,
572 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200573 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200574 QF_IGNORE_LINE = 4,
575 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200576};
577
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200578/*
579 * State information used to parse lines and add entries to a quickfix/location
580 * list.
581 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200582typedef struct {
583 char_u *linebuf;
584 int linelen;
585 char_u *growbuf;
586 int growbufsiz;
587 FILE *fd;
588 typval_T *tv;
589 char_u *p_str;
590 listitem_T *p_li;
591 buf_T *buf;
592 linenr_T buflnum;
593 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100594 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200595} qfstate_T;
596
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200597/*
598 * Allocate more memory for the line buffer used for parsing lines.
599 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200600 static char_u *
601qf_grow_linebuf(qfstate_T *state, int newsz)
602{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200603 char_u *p;
604
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200605 // If the line exceeds LINE_MAXLEN exclude the last
606 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200607 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
608 if (state->growbuf == NULL)
609 {
610 state->growbuf = alloc(state->linelen + 1);
611 if (state->growbuf == NULL)
612 return NULL;
613 state->growbufsiz = state->linelen;
614 }
615 else if (state->linelen > state->growbufsiz)
616 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200617 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200618 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200619 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200620 state->growbufsiz = state->linelen;
621 }
622 return state->growbuf;
623}
624
625/*
626 * Get the next string (separated by newline) from state->p_str.
627 */
628 static int
629qf_get_next_str_line(qfstate_T *state)
630{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200631 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200632 char_u *p_str = state->p_str;
633 char_u *p;
634 int len;
635
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200636 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200637 return QF_END_OF_INPUT;
638
639 p = vim_strchr(p_str, '\n');
640 if (p != NULL)
641 len = (int)(p - p_str) + 1;
642 else
643 len = (int)STRLEN(p_str);
644
645 if (len > IOSIZE - 2)
646 {
647 state->linebuf = qf_grow_linebuf(state, len);
648 if (state->linebuf == NULL)
649 return QF_NOMEM;
650 }
651 else
652 {
653 state->linebuf = IObuff;
654 state->linelen = len;
655 }
656 vim_strncpy(state->linebuf, p_str, state->linelen);
657
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200658 // Increment using len in order to discard the rest of the
659 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200660 p_str += len;
661 state->p_str = p_str;
662
663 return QF_OK;
664}
665
666/*
667 * Get the next string from state->p_Li.
668 */
669 static int
670qf_get_next_list_line(qfstate_T *state)
671{
672 listitem_T *p_li = state->p_li;
673 int len;
674
675 while (p_li != NULL
676 && (p_li->li_tv.v_type != VAR_STRING
677 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200678 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200679
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200680 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200681 {
682 state->p_li = NULL;
683 return QF_END_OF_INPUT;
684 }
685
686 len = (int)STRLEN(p_li->li_tv.vval.v_string);
687 if (len > IOSIZE - 2)
688 {
689 state->linebuf = qf_grow_linebuf(state, len);
690 if (state->linebuf == NULL)
691 return QF_NOMEM;
692 }
693 else
694 {
695 state->linebuf = IObuff;
696 state->linelen = len;
697 }
698
699 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
700
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200701 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200702 return QF_OK;
703}
704
705/*
706 * Get the next string from state->buf.
707 */
708 static int
709qf_get_next_buf_line(qfstate_T *state)
710{
711 char_u *p_buf = NULL;
712 int len;
713
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200714 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200715 if (state->buflnum > state->lnumlast)
716 return QF_END_OF_INPUT;
717
718 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
719 state->buflnum += 1;
720
721 len = (int)STRLEN(p_buf);
722 if (len > IOSIZE - 2)
723 {
724 state->linebuf = qf_grow_linebuf(state, len);
725 if (state->linebuf == NULL)
726 return QF_NOMEM;
727 }
728 else
729 {
730 state->linebuf = IObuff;
731 state->linelen = len;
732 }
733 vim_strncpy(state->linebuf, p_buf, state->linelen);
734
735 return QF_OK;
736}
737
738/*
739 * Get the next string from file state->fd.
740 */
741 static int
742qf_get_next_file_line(qfstate_T *state)
743{
744 int discard;
745 int growbuflen;
746
747 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
748 return QF_END_OF_INPUT;
749
750 discard = FALSE;
751 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200752 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200753 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200754 // The current line exceeds IObuff, continue reading using
755 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200756 if (state->growbuf == NULL)
757 {
758 state->growbufsiz = 2 * (IOSIZE - 1);
759 state->growbuf = alloc(state->growbufsiz);
760 if (state->growbuf == NULL)
761 return QF_NOMEM;
762 }
763
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200764 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200765 memcpy(state->growbuf, IObuff, IOSIZE - 1);
766 growbuflen = state->linelen;
767
768 for (;;)
769 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200770 char_u *p;
771
Bram Moolenaare0d37972016-07-15 22:36:01 +0200772 if (fgets((char *)state->growbuf + growbuflen,
773 state->growbufsiz - growbuflen, state->fd) == NULL)
774 break;
775 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
776 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200777 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200778 break;
779 if (state->growbufsiz == LINE_MAXLEN)
780 {
781 discard = TRUE;
782 break;
783 }
784
785 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
786 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200787 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200788 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200789 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200790 }
791
792 while (discard)
793 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200794 // The current line is longer than LINE_MAXLEN, continue
795 // reading but discard everything until EOL or EOF is
796 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200797 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
798 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200799 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200800 break;
801 }
802
803 state->linebuf = state->growbuf;
804 state->linelen = growbuflen;
805 }
806 else
807 state->linebuf = IObuff;
808
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200809 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200810 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
811 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100812 char_u *line;
813
814 line = string_convert(&state->vc, state->linebuf, &state->linelen);
815 if (line != NULL)
816 {
817 if (state->linelen < IOSIZE)
818 {
819 STRCPY(state->linebuf, line);
820 vim_free(line);
821 }
822 else
823 {
824 vim_free(state->growbuf);
825 state->linebuf = state->growbuf = line;
826 state->growbufsiz = state->linelen < LINE_MAXLEN
827 ? state->linelen : LINE_MAXLEN;
828 }
829 }
830 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100831
Bram Moolenaare0d37972016-07-15 22:36:01 +0200832 return QF_OK;
833}
834
835/*
836 * Get the next string from a file/buffer/list/string.
837 */
838 static int
839qf_get_nextline(qfstate_T *state)
840{
841 int status = QF_FAIL;
842
843 if (state->fd == NULL)
844 {
845 if (state->tv != NULL)
846 {
847 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200848 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200849 status = qf_get_next_str_line(state);
850 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200851 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200852 status = qf_get_next_list_line(state);
853 }
854 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200855 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200856 status = qf_get_next_buf_line(state);
857 }
858 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200859 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200860 status = qf_get_next_file_line(state);
861
862 if (status != QF_OK)
863 return status;
864
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200865 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200866 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200867 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200868 state->linebuf[state->linelen - 1] = NUL;
869#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200870 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
871 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200872#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200873 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200874
Bram Moolenaare0d37972016-07-15 22:36:01 +0200875 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200876
877 return QF_OK;
878}
879
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200880typedef struct {
881 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200882 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200883 char_u *errmsg;
884 int errmsglen;
885 long lnum;
886 int col;
887 char_u use_viscol;
888 char_u *pattern;
889 int enr;
890 int type;
891 int valid;
892} qffields_T;
893
894/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200895 * Parse the match for filename ('%f') pattern in regmatch.
896 * Return the matched value in "fields->namebuf".
897 */
898 static int
899qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
900{
901 int c;
902
903 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
904 return QF_FAIL;
905
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200906 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200907 c = *rmp->endp[midx];
908 *rmp->endp[midx] = NUL;
909 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
910 *rmp->endp[midx] = c;
911
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200912 // For separate filename patterns (%O, %P and %Q), the specified file
913 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200914 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
915 && mch_getperm(fields->namebuf) == -1)
916 return QF_FAIL;
917
918 return QF_OK;
919}
920
921/*
922 * Parse the match for error number ('%n') pattern in regmatch.
923 * Return the matched value in "fields->enr".
924 */
925 static int
926qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
927{
928 if (rmp->startp[midx] == NULL)
929 return QF_FAIL;
930 fields->enr = (int)atol((char *)rmp->startp[midx]);
931 return QF_OK;
932}
933
934/*
935 * Parse the match for line number (%l') pattern in regmatch.
936 * Return the matched value in "fields->lnum".
937 */
938 static int
939qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
940{
941 if (rmp->startp[midx] == NULL)
942 return QF_FAIL;
943 fields->lnum = atol((char *)rmp->startp[midx]);
944 return QF_OK;
945}
946
947/*
948 * Parse the match for column number ('%c') pattern in regmatch.
949 * Return the matched value in "fields->col".
950 */
951 static int
952qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
953{
954 if (rmp->startp[midx] == NULL)
955 return QF_FAIL;
956 fields->col = (int)atol((char *)rmp->startp[midx]);
957 return QF_OK;
958}
959
960/*
961 * Parse the match for error type ('%t') pattern in regmatch.
962 * Return the matched value in "fields->type".
963 */
964 static int
965qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
966{
967 if (rmp->startp[midx] == NULL)
968 return QF_FAIL;
969 fields->type = *rmp->startp[midx];
970 return QF_OK;
971}
972
973/*
974 * Parse the match for '%+' format pattern. The whole matching line is included
975 * in the error string. Return the matched line in "fields->errmsg".
976 */
977 static int
978qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
979{
980 char_u *p;
981
982 if (linelen >= fields->errmsglen)
983 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200984 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200985 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
986 return QF_NOMEM;
987 fields->errmsg = p;
988 fields->errmsglen = linelen + 1;
989 }
990 vim_strncpy(fields->errmsg, linebuf, linelen);
991 return QF_OK;
992}
993
994/*
995 * Parse the match for error message ('%m') pattern in regmatch.
996 * Return the matched value in "fields->errmsg".
997 */
998 static int
999qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1000{
1001 char_u *p;
1002 int len;
1003
1004 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1005 return QF_FAIL;
1006 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1007 if (len >= fields->errmsglen)
1008 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001009 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001010 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1011 return QF_NOMEM;
1012 fields->errmsg = p;
1013 fields->errmsglen = len + 1;
1014 }
1015 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1016 return QF_OK;
1017}
1018
1019/*
1020 * Parse the match for rest of a single-line file message ('%r') pattern.
1021 * Return the matched value in "tail".
1022 */
1023 static int
1024qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1025{
1026 if (rmp->startp[midx] == NULL)
1027 return QF_FAIL;
1028 *tail = rmp->startp[midx];
1029 return QF_OK;
1030}
1031
1032/*
1033 * Parse the match for the pointer line ('%p') pattern in regmatch.
1034 * Return the matched value in "fields->col".
1035 */
1036 static int
1037qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1038{
1039 char_u *match_ptr;
1040
1041 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1042 return QF_FAIL;
1043 fields->col = 0;
1044 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1045 ++match_ptr)
1046 {
1047 ++fields->col;
1048 if (*match_ptr == TAB)
1049 {
1050 fields->col += 7;
1051 fields->col -= fields->col % 8;
1052 }
1053 }
1054 ++fields->col;
1055 fields->use_viscol = TRUE;
1056 return QF_OK;
1057}
1058
1059/*
1060 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1061 * Return the matched value in "fields->col".
1062 */
1063 static int
1064qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1065{
1066 if (rmp->startp[midx] == NULL)
1067 return QF_FAIL;
1068 fields->col = (int)atol((char *)rmp->startp[midx]);
1069 fields->use_viscol = TRUE;
1070 return QF_OK;
1071}
1072
1073/*
1074 * Parse the match for the search text ('%s') pattern in regmatch.
1075 * Return the matched value in "fields->pattern".
1076 */
1077 static int
1078qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1079{
1080 int len;
1081
1082 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1083 return QF_FAIL;
1084 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1085 if (len > CMDBUFFSIZE - 5)
1086 len = CMDBUFFSIZE - 5;
1087 STRCPY(fields->pattern, "^\\V");
1088 STRNCAT(fields->pattern, rmp->startp[midx], len);
1089 fields->pattern[len + 3] = '\\';
1090 fields->pattern[len + 4] = '$';
1091 fields->pattern[len + 5] = NUL;
1092 return QF_OK;
1093}
1094
1095/*
1096 * Parse the match for the module ('%o') pattern in regmatch.
1097 * Return the matched value in "fields->module".
1098 */
1099 static int
1100qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1101{
1102 int len;
1103
1104 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1105 return QF_FAIL;
1106 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1107 if (len > CMDBUFFSIZE)
1108 len = CMDBUFFSIZE;
1109 STRNCAT(fields->module, rmp->startp[midx], len);
1110 return QF_OK;
1111}
1112
1113/*
1114 * 'errorformat' format pattern parser functions.
1115 * The '%f' and '%r' formats are parsed differently from other formats.
1116 * See qf_parse_match() for details.
1117 */
1118static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1119{
1120 NULL,
1121 qf_parse_fmt_n,
1122 qf_parse_fmt_l,
1123 qf_parse_fmt_c,
1124 qf_parse_fmt_t,
1125 qf_parse_fmt_m,
1126 NULL,
1127 qf_parse_fmt_p,
1128 qf_parse_fmt_v,
1129 qf_parse_fmt_s,
1130 qf_parse_fmt_o
1131};
1132
1133/*
1134 * Parse the error format pattern matches in "regmatch" and set the values in
1135 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1136 * match. Returns QF_OK if all the matches are successfully parsed. On
1137 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001138 */
1139 static int
1140qf_parse_match(
1141 char_u *linebuf,
1142 int linelen,
1143 efm_T *fmt_ptr,
1144 regmatch_T *regmatch,
1145 qffields_T *fields,
1146 int qf_multiline,
1147 int qf_multiscan,
1148 char_u **tail)
1149{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001150 int idx = fmt_ptr->prefix;
1151 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001152 int midx;
1153 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001154
1155 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1156 return QF_FAIL;
1157 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1158 fields->type = idx;
1159 else
1160 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001161
1162 // Extract error message data from matched line.
1163 // We check for an actual submatch, because "\[" and "\]" in
1164 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001165 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001166 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001167 status = QF_OK;
1168 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001169 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001170 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1171 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001172 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001173 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001174 status = qf_parse_fmt_plus(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001175 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001176 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001177 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001178 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001179 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001180 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001181 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001182
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001183 if (status != QF_OK)
1184 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001185 }
1186
1187 return QF_OK;
1188}
1189
1190/*
1191 * Parse an error line in 'linebuf' using a single error format string in
1192 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1193 * Returns QF_OK if the efm format matches completely and the fields are
1194 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1195 */
1196 static int
1197qf_parse_get_fields(
1198 char_u *linebuf,
1199 int linelen,
1200 efm_T *fmt_ptr,
1201 qffields_T *fields,
1202 int qf_multiline,
1203 int qf_multiscan,
1204 char_u **tail)
1205{
1206 regmatch_T regmatch;
1207 int status = QF_FAIL;
1208 int r;
1209
1210 if (qf_multiscan &&
1211 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1212 return QF_FAIL;
1213
1214 fields->namebuf[0] = NUL;
1215 fields->module[0] = NUL;
1216 fields->pattern[0] = NUL;
1217 if (!qf_multiscan)
1218 fields->errmsg[0] = NUL;
1219 fields->lnum = 0;
1220 fields->col = 0;
1221 fields->use_viscol = FALSE;
1222 fields->enr = -1;
1223 fields->type = 0;
1224 *tail = NULL;
1225
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001226 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001227 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001228 regmatch.regprog = fmt_ptr->prog;
1229 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1230 fmt_ptr->prog = regmatch.regprog;
1231 if (r)
1232 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1233 fields, qf_multiline, qf_multiscan, tail);
1234
1235 return status;
1236}
1237
1238/*
1239 * Parse directory error format prefixes (%D and %X).
1240 * Push and pop directories from the directory stack when scanning directory
1241 * names.
1242 */
1243 static int
1244qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1245{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001246 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001247 {
1248 if (*fields->namebuf == NUL)
1249 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001250 emsg(_("E379: Missing or empty directory name"));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001251 return QF_FAIL;
1252 }
1253 qfl->qf_directory =
1254 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1255 if (qfl->qf_directory == NULL)
1256 return QF_FAIL;
1257 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001258 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001259 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1260
1261 return QF_OK;
1262}
1263
1264/*
1265 * Parse global file name error format prefixes (%O, %P and %Q).
1266 */
1267 static int
1268qf_parse_file_pfx(
1269 int idx,
1270 qffields_T *fields,
1271 qf_list_T *qfl,
1272 char_u *tail)
1273{
1274 fields->valid = FALSE;
1275 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1276 {
1277 if (*fields->namebuf && idx == 'P')
1278 qfl->qf_currfile =
1279 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1280 else if (idx == 'Q')
1281 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1282 *fields->namebuf = NUL;
1283 if (tail && *tail)
1284 {
1285 STRMOVE(IObuff, skipwhite(tail));
1286 qfl->qf_multiscan = TRUE;
1287 return QF_MULTISCAN;
1288 }
1289 }
1290
1291 return QF_OK;
1292}
1293
1294/*
1295 * Parse a non-error line (a line which doesn't match any of the error
1296 * format in 'efm').
1297 */
1298 static int
1299qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1300{
1301 char_u *p;
1302
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001303 fields->namebuf[0] = NUL; // no match found, remove file name
1304 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001305 fields->valid = FALSE;
1306 if (linelen >= fields->errmsglen)
1307 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001308 // linelen + null terminator
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001309 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1310 return QF_NOMEM;
1311 fields->errmsg = p;
1312 fields->errmsglen = linelen + 1;
1313 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001314 // copy whole line to error message
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001315 vim_strncpy(fields->errmsg, linebuf, linelen);
1316
1317 return QF_OK;
1318}
1319
1320/*
1321 * Parse multi-line error format prefixes (%C and %Z)
1322 */
1323 static int
1324qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001325 int idx,
1326 qf_list_T *qfl,
1327 qffields_T *fields)
1328{
1329 char_u *ptr;
1330 int len;
1331
1332 if (!qfl->qf_multiignore)
1333 {
1334 qfline_T *qfprev = qfl->qf_last;
1335
1336 if (qfprev == NULL)
1337 return QF_FAIL;
1338 if (*fields->errmsg && !qfl->qf_multiignore)
1339 {
1340 len = (int)STRLEN(qfprev->qf_text);
1341 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1342 == NULL)
1343 return QF_FAIL;
1344 STRCPY(ptr, qfprev->qf_text);
1345 vim_free(qfprev->qf_text);
1346 qfprev->qf_text = ptr;
1347 *(ptr += len) = '\n';
1348 STRCPY(++ptr, fields->errmsg);
1349 }
1350 if (qfprev->qf_nr == -1)
1351 qfprev->qf_nr = fields->enr;
1352 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001353 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001354 qfprev->qf_type = fields->type;
1355
1356 if (!qfprev->qf_lnum)
1357 qfprev->qf_lnum = fields->lnum;
1358 if (!qfprev->qf_col)
1359 qfprev->qf_col = fields->col;
1360 qfprev->qf_viscol = fields->use_viscol;
1361 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001362 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001363 qfl->qf_directory,
1364 *fields->namebuf || qfl->qf_directory != NULL
1365 ? fields->namebuf
1366 : qfl->qf_currfile != NULL && fields->valid
1367 ? qfl->qf_currfile : 0);
1368 }
1369 if (idx == 'Z')
1370 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1371 line_breakcheck();
1372
1373 return QF_IGNORE_LINE;
1374}
1375
1376/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001377 * Parse a line and get the quickfix fields.
1378 * Return the QF_ status.
1379 */
1380 static int
1381qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001382 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001383 char_u *linebuf,
1384 int linelen,
1385 efm_T *fmt_first,
1386 qffields_T *fields)
1387{
1388 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001389 int idx = 0;
1390 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001391 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001392
Bram Moolenaare333e792018-04-08 13:27:39 +02001393restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001394 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001395 if (fmt_start == NULL)
1396 fmt_ptr = fmt_first;
1397 else
1398 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001399 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001400 fmt_ptr = fmt_start;
1401 fmt_start = NULL;
1402 }
1403
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001404 // Try to match each part of 'errorformat' until we find a complete
1405 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001406 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001407 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1408 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001409 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001410 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1411 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1412 if (status == QF_NOMEM)
1413 return status;
1414 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001415 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001416 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001417 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001418
1419 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1420 {
1421 if (fmt_ptr != NULL)
1422 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001423 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001424 status = qf_parse_dir_pfx(idx, fields, qfl);
1425 if (status != QF_OK)
1426 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001427 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001428
1429 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1430 if (status != QF_OK)
1431 return status;
1432
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001433 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001434 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001435 }
1436 else if (fmt_ptr != NULL)
1437 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001438 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001439 if (fmt_ptr->conthere)
1440 fmt_start = fmt_ptr;
1441
1442 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1443 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001444 qfl->qf_multiline = TRUE; // start of a multi-line message
1445 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001446 }
1447 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001448 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001449 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001450 if (status != QF_OK)
1451 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001452 }
1453 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001454 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001455 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1456 if (status == QF_MULTISCAN)
1457 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001458 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001459 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001460 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001461 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001462 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001463 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001464 return QF_IGNORE_LINE;
1465 }
1466 }
1467
1468 return QF_OK;
1469}
1470
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001471/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001472 * Returns TRUE if the specified quickfix/location stack is empty
1473 */
1474 static int
1475qf_stack_empty(qf_info_T *qi)
1476{
1477 return qi == NULL || qi->qf_listcount <= 0;
1478}
1479
1480/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001481 * Returns TRUE if the specified quickfix/location list is empty.
1482 */
1483 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001484qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001485{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001486 return qfl == NULL || qfl->qf_count <= 0;
1487}
1488
1489/*
1490 * Return a pointer to a list in the specified quickfix stack
1491 */
1492 static qf_list_T *
1493qf_get_list(qf_info_T *qi, int idx)
1494{
1495 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001496}
1497
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001498/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001499 * Allocate the fields used for parsing lines and populating a quickfix list.
1500 */
1501 static int
1502qf_alloc_fields(qffields_T *pfields)
1503{
1504 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1505 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1506 pfields->errmsglen = CMDBUFFSIZE + 1;
1507 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1508 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1509 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1510 || pfields->pattern == NULL || pfields->module == NULL)
1511 return FAIL;
1512
1513 return OK;
1514}
1515
1516/*
1517 * Free the fields used for parsing lines and populating a quickfix list.
1518 */
1519 static void
1520qf_free_fields(qffields_T *pfields)
1521{
1522 vim_free(pfields->namebuf);
1523 vim_free(pfields->module);
1524 vim_free(pfields->errmsg);
1525 vim_free(pfields->pattern);
1526}
1527
1528/*
1529 * Setup the state information used for parsing lines and populating a
1530 * quickfix list.
1531 */
1532 static int
1533qf_setup_state(
1534 qfstate_T *pstate,
1535 char_u *enc,
1536 char_u *efile,
1537 typval_T *tv,
1538 buf_T *buf,
1539 linenr_T lnumfirst,
1540 linenr_T lnumlast)
1541{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001542 pstate->vc.vc_type = CONV_NONE;
1543 if (enc != NULL && *enc != NUL)
1544 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001545
1546 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1547 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001548 semsg(_(e_openerrf), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001549 return FAIL;
1550 }
1551
1552 if (tv != NULL)
1553 {
1554 if (tv->v_type == VAR_STRING)
1555 pstate->p_str = tv->vval.v_string;
1556 else if (tv->v_type == VAR_LIST)
1557 pstate->p_li = tv->vval.v_list->lv_first;
1558 pstate->tv = tv;
1559 }
1560 pstate->buf = buf;
1561 pstate->buflnum = lnumfirst;
1562 pstate->lnumlast = lnumlast;
1563
1564 return OK;
1565}
1566
1567/*
1568 * Cleanup the state information used for parsing lines and populating a
1569 * quickfix list.
1570 */
1571 static void
1572qf_cleanup_state(qfstate_T *pstate)
1573{
1574 if (pstate->fd != NULL)
1575 fclose(pstate->fd);
1576
1577 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001578 if (pstate->vc.vc_type != CONV_NONE)
1579 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001580}
1581
1582/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001583 * Read the errorfile "efile" into memory, line by line, building the error
1584 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001585 * Alternative: when "efile" is NULL read errors from buffer "buf".
1586 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001587 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001588 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1589 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001590 * Return -1 for error, number of errors for success.
1591 */
1592 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001593qf_init_ext(
1594 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001595 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001596 char_u *efile,
1597 buf_T *buf,
1598 typval_T *tv,
1599 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001600 int newlist, // TRUE: start a new error list
1601 linenr_T lnumfirst, // first line number to use
1602 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001603 char_u *qf_title,
1604 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001605{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001606 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001607 qfstate_T state;
1608 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001609 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001610 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001611 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001613 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001614 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001615 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001617 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001618 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001619
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001620 vim_memset(&state, 0, sizeof(state));
1621 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001622 if ((qf_alloc_fields(&fields) == FAIL) ||
1623 (qf_setup_state(&state, enc, efile, tv, buf,
1624 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 goto qf_init_end;
1626
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001627 if (newlist || qf_idx == qi->qf_listcount)
1628 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001629 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001630 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001631 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001632 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001633 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001634 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001635 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001636 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001637 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001638 qfl = qf_get_list(qi, qf_idx);
1639 if (!qf_list_empty(qfl))
1640 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001643 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001644 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001645 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 else
1647 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001649 // If the errorformat didn't change between calls, then reuse the
1650 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001651 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1652 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001653 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001654 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001655 free_efm_list(&fmt_first);
1656
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001657 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001658 fmt_first = parse_efm_option(efm);
1659 if (fmt_first != NULL)
1660 last_efm = vim_strsave(efm);
1661 }
1662
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001663 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001666 // got_int is reset here, because it was probably set when killing the
1667 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 got_int = FALSE;
1669
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001670 // Read the lines in the error file one by one.
1671 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001672 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001674 // Get the next line from a file/buffer/list/string
Bram Moolenaare0d37972016-07-15 22:36:01 +02001675 status = qf_get_nextline(&state);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001676 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001677 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001678 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001679 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
Bram Moolenaar0398e002019-03-21 21:12:49 +01001681 status = qf_parse_line(qfl, state.linebuf, state.linelen,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001682 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001683 if (status == QF_FAIL)
1684 goto error2;
1685 if (status == QF_NOMEM)
1686 goto qf_init_end;
1687 if (status == QF_IGNORE_LINE)
1688 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689
Bram Moolenaar0398e002019-03-21 21:12:49 +01001690 if (qf_add_entry(qfl,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001691 qfl->qf_directory,
1692 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001693 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001694 : ((qfl->qf_currfile != NULL && fields.valid)
1695 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001696 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001697 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001698 fields.errmsg,
1699 fields.lnum,
1700 fields.col,
1701 fields.use_viscol,
1702 fields.pattern,
1703 fields.enr,
1704 fields.type,
1705 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 goto error2;
1707 line_breakcheck();
1708 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001709 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001711 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001713 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001714 qfl->qf_ptr = qfl->qf_start;
1715 qfl->qf_index = 1;
1716 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 }
1718 else
1719 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001720 qfl->qf_nonevalid = FALSE;
1721 if (qfl->qf_ptr == NULL)
1722 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001724 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001725 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001726 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001728 emsg(_(e_readerrf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001730 if (!adding)
1731 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001732 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001733 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001734 qi->qf_listcount--;
1735 if (qi->qf_curlist > 0)
1736 --qi->qf_curlist;
1737 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001738qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001739 if (qf_idx == qi->qf_curlist)
1740 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001741 qf_cleanup_state(&state);
1742 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743
1744 return retval;
1745}
1746
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001747/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001748 * Read the errorfile "efile" into memory, line by line, building the error
1749 * list. Set the error list's title to qf_title.
1750 * Return -1 for error, number of errors for success.
1751 */
1752 int
1753qf_init(win_T *wp,
1754 char_u *efile,
1755 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001756 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001757 char_u *qf_title,
1758 char_u *enc)
1759{
1760 qf_info_T *qi = &ql_info;
1761
1762 if (wp != NULL)
1763 {
1764 qi = ll_get_or_alloc_list(wp);
1765 if (qi == NULL)
1766 return FAIL;
1767 }
1768
1769 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1770 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1771}
1772
1773/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001774 * Set the title of the specified quickfix list. Frees the previous title.
1775 * Prepends ':' to the title.
1776 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001777 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001778qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001779{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001780 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001781
Bram Moolenaarfb604092014-07-23 15:55:00 +02001782 if (title != NULL)
1783 {
1784 char_u *p = alloc((int)STRLEN(title) + 2);
1785
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001786 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001787 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001788 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001789 }
1790}
1791
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001793 * The title of a quickfix/location list is set, by default, to the command
1794 * that created the quickfix list with the ":" prefix.
1795 * Create a quickfix list title string by prepending ":" to a user command.
1796 * Returns a pointer to a static buffer with the title.
1797 */
1798 static char_u *
1799qf_cmdtitle(char_u *cmd)
1800{
1801 static char_u qftitle_str[IOSIZE];
1802
1803 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1804 return qftitle_str;
1805}
1806
1807/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001808 * Return a pointer to the current list in the specified quickfix stack
1809 */
1810 static qf_list_T *
1811qf_get_curlist(qf_info_T *qi)
1812{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001813 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001814}
1815
1816/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001817 * Prepare for adding a new quickfix list. If the current list is in the
1818 * middle of the stack, then all the following lists are freed and then
1819 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 */
1821 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001822qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001825 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001827 // If the current entry is not the last entry, delete entries beyond
1828 // the current entry. This makes it possible to browse in a tree-like
1829 // way with ":grep'.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001830 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001831 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001833 // When the stack is full, remove to oldest entry
1834 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001835 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001837 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001839 qi->qf_lists[i - 1] = qi->qf_lists[i];
1840 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001843 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001844 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001845 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1846 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001847 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001848 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849}
1850
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001851/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001852 * Queue location list stack delete request.
1853 */
1854 static void
1855locstack_queue_delreq(qf_info_T *qi)
1856{
1857 qf_delq_T *q;
1858
1859 q = (qf_delq_T *)alloc((unsigned)sizeof(qf_delq_T));
1860 if (q != NULL)
1861 {
1862 q->qi = qi;
1863 q->next = qf_delq_head;
1864 qf_delq_head = q;
1865 }
1866}
1867
1868/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001869 * Return the global quickfix stack window buffer number.
1870 */
1871 int
1872qf_stack_get_bufnr(void)
1873{
1874 return ql_info.qf_bufnr;
1875}
1876
1877/*
1878 * Wipe the quickfix window buffer (if present) for the specified
1879 * quickfix/location list.
1880 */
1881 static void
1882wipe_qf_buffer(qf_info_T *qi)
1883{
1884 buf_T *qfbuf;
1885
1886 if (qi->qf_bufnr != INVALID_QFBUFNR)
1887 {
1888 qfbuf = buflist_findnr(qi->qf_bufnr);
1889 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1890 {
1891 // If the quickfix buffer is not loaded in any window, then
1892 // wipe the buffer.
1893 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE);
1894 qi->qf_bufnr = INVALID_QFBUFNR;
1895 }
1896 }
1897}
1898
1899/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001900 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001901 */
1902 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001903ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001904{
1905 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001906 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001907
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001908 qi = *pqi;
1909 if (qi == NULL)
1910 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001911 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001912
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001913 // If the location list is still in use, then queue the delete request
1914 // to be processed later.
1915 if (quickfix_busy > 0)
1916 {
1917 locstack_queue_delreq(qi);
1918 return;
1919 }
1920
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001921 qi->qf_refcount--;
1922 if (qi->qf_refcount < 1)
1923 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001924 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001925 // If the quickfix window buffer is loaded, then wipe it
1926 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001927
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001928 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001929 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001930 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001931 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001932}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001933
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001934/*
1935 * Free all the quickfix/location lists in the stack.
1936 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001937 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001938qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001939{
1940 int i;
1941 qf_info_T *qi = &ql_info;
1942
1943 if (wp != NULL)
1944 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001945 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001946 ll_free_all(&wp->w_llist);
1947 ll_free_all(&wp->w_llist_ref);
1948 }
1949 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001950 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001951 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001952 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001953}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001956 * Delay freeing of location list stacks when the quickfix code is running.
1957 * Used to avoid problems with autocmds freeing location list stacks when the
1958 * quickfix code is still referencing the stack.
1959 * Must always call decr_quickfix_busy() exactly once after this.
1960 */
1961 static void
1962incr_quickfix_busy(void)
1963{
1964 quickfix_busy++;
1965}
1966
1967/*
1968 * Safe to free location list stacks. Process any delayed delete requests.
1969 */
1970 static void
1971decr_quickfix_busy(void)
1972{
1973 if (--quickfix_busy == 0)
1974 {
1975 // No longer referencing the location lists. Process all the pending
1976 // delete requests.
1977 while (qf_delq_head != NULL)
1978 {
1979 qf_delq_T *q = qf_delq_head;
1980
1981 qf_delq_head = q->next;
1982 ll_free_all(&q->qi);
1983 vim_free(q);
1984 }
1985 }
1986#ifdef ABORT_ON_INTERNAL_ERROR
1987 if (quickfix_busy < 0)
1988 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001989 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001990 abort();
1991 }
1992#endif
1993}
1994
1995#if defined(EXITFREE) || defined(PROTO)
1996 void
1997check_quickfix_busy(void)
1998{
1999 if (quickfix_busy != 0)
2000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002001 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002002# ifdef ABORT_ON_INTERNAL_ERROR
2003 abort();
2004# endif
2005 }
2006}
2007#endif
2008
2009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 * Add an entry to the end of the list of errors.
2011 * Returns OK or FAIL.
2012 */
2013 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002014qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002015 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002016 char_u *dir, // optional directory name
2017 char_u *fname, // file name or NULL
2018 char_u *module, // module name or NULL
2019 int bufnum, // buffer number or zero
2020 char_u *mesg, // message
2021 long lnum, // line number
2022 int col, // column
2023 int vis_col, // using visual column
2024 char_u *pattern, // search pattern
2025 int nr, // error number
2026 int type, // type character
2027 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002029 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002030 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002032 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002034 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002035 {
2036 buf_T *buf = buflist_findnr(bufnum);
2037
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002038 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002039 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002040 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002041 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002042 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002043 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002044 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2046 {
2047 vim_free(qfp);
2048 return FAIL;
2049 }
2050 qfp->qf_lnum = lnum;
2051 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002052 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002053 if (pattern == NULL || *pattern == NUL)
2054 qfp->qf_pattern = NULL;
2055 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2056 {
2057 vim_free(qfp->qf_text);
2058 vim_free(qfp);
2059 return FAIL;
2060 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002061 if (module == NULL || *module == NUL)
2062 qfp->qf_module = NULL;
2063 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2064 {
2065 vim_free(qfp->qf_text);
2066 vim_free(qfp->qf_pattern);
2067 vim_free(qfp);
2068 return FAIL;
2069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002071 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 type = 0;
2073 qfp->qf_type = type;
2074 qfp->qf_valid = valid;
2075
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002076 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002077 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002079 qfl->qf_start = qfp;
2080 qfl->qf_ptr = qfp;
2081 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002082 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
2084 else
2085 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002086 qfp->qf_prev = *lastp;
2087 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002089 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002091 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002092 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002093 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002095 qfl->qf_index = qfl->qf_count;
2096 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 }
2098
2099 return OK;
2100}
2101
2102/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002103 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002104 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002105 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002106qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002107{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002108 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002109
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002110 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002111 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002112 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002113 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002114 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002115 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002116 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002117 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002118}
2119
2120/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002121 * Return the location list stack for window 'wp'.
2122 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002123 */
2124 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002125ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002126{
2127 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002128 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002129 return wp->w_llist_ref;
2130
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002131 // For a non-location list window, w_llist_ref should not point to a
2132 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002133 ll_free_all(&wp->w_llist_ref);
2134
2135 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002136 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002137 return wp->w_llist;
2138}
2139
2140/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002141 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2142 */
2143 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002144copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002145{
2146 int i;
2147 qfline_T *from_qfp;
2148 qfline_T *prevp;
2149
2150 // copy all the location entries in this list
2151 for (i = 0, from_qfp = from_qfl->qf_start;
2152 i < from_qfl->qf_count && from_qfp != NULL;
2153 ++i, from_qfp = from_qfp->qf_next)
2154 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002155 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002156 NULL,
2157 NULL,
2158 from_qfp->qf_module,
2159 0,
2160 from_qfp->qf_text,
2161 from_qfp->qf_lnum,
2162 from_qfp->qf_col,
2163 from_qfp->qf_viscol,
2164 from_qfp->qf_pattern,
2165 from_qfp->qf_nr,
2166 0,
2167 from_qfp->qf_valid) == FAIL)
2168 return FAIL;
2169
2170 // qf_add_entry() will not set the qf_num field, as the
2171 // directory and file names are not supplied. So the qf_fnum
2172 // field is copied here.
2173 prevp = to_qfl->qf_last;
2174 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2175 prevp->qf_type = from_qfp->qf_type; // error type
2176 if (from_qfl->qf_ptr == from_qfp)
2177 to_qfl->qf_ptr = prevp; // current location
2178 }
2179
2180 return OK;
2181}
2182
2183/*
2184 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2185 */
2186 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002187copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002188{
2189 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002190 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002191 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2192 to_qfl->qf_count = 0;
2193 to_qfl->qf_index = 0;
2194 to_qfl->qf_start = NULL;
2195 to_qfl->qf_last = NULL;
2196 to_qfl->qf_ptr = NULL;
2197 if (from_qfl->qf_title != NULL)
2198 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2199 else
2200 to_qfl->qf_title = NULL;
2201 if (from_qfl->qf_ctx != NULL)
2202 {
2203 to_qfl->qf_ctx = alloc_tv();
2204 if (to_qfl->qf_ctx != NULL)
2205 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2206 }
2207 else
2208 to_qfl->qf_ctx = NULL;
2209
2210 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002211 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002212 return FAIL;
2213
2214 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2215
2216 // Assign a new ID for the location list
2217 to_qfl->qf_id = ++last_qf_id;
2218 to_qfl->qf_changedtick = 0L;
2219
2220 // When no valid entries are present in the list, qf_ptr points to
2221 // the first item in the list
2222 if (to_qfl->qf_nonevalid)
2223 {
2224 to_qfl->qf_ptr = to_qfl->qf_start;
2225 to_qfl->qf_index = 1;
2226 }
2227
2228 return OK;
2229}
2230
2231/*
2232 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002233 */
2234 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002235copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002236{
2237 qf_info_T *qi;
2238 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002239
Bram Moolenaar09037502018-09-25 22:08:14 +02002240 // When copying from a location list window, copy the referenced
2241 // location list. For other windows, copy the location list for
2242 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002243 if (IS_LL_WINDOW(from))
2244 qi = from->w_llist_ref;
2245 else
2246 qi = from->w_llist;
2247
Bram Moolenaar09037502018-09-25 22:08:14 +02002248 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002249 return;
2250
Bram Moolenaar09037502018-09-25 22:08:14 +02002251 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002252 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002253 return;
2254
2255 to->w_llist->qf_listcount = qi->qf_listcount;
2256
Bram Moolenaar09037502018-09-25 22:08:14 +02002257 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002258 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002259 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002260 to->w_llist->qf_curlist = idx;
2261
Bram Moolenaar0398e002019-03-21 21:12:49 +01002262 if (copy_loclist(qf_get_list(qi, idx),
2263 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002264 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002265 qf_free_all(to);
2266 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002267 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002268 }
2269
Bram Moolenaar09037502018-09-25 22:08:14 +02002270 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002271}
2272
2273/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002274 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002275 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 */
2277 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002278qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
Bram Moolenaar82404332016-07-10 17:00:38 +02002280 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002281 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002282 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002283
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002284 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
Bram Moolenaare60acc12011-05-10 16:41:25 +02002287#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002288 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002289#endif
2290#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002291 if (directory != NULL)
2292 slash_adjust(directory);
2293 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002294#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002295 if (directory != NULL && !vim_isAbsName(fname)
2296 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2297 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002298 // Here we check if the file really exists.
2299 // This should normally be true, but if make works without
2300 // "leaving directory"-messages we might have missed a
2301 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002302 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002305 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002306 if (directory)
2307 ptr = concat_fnames(directory, fname, TRUE);
2308 else
2309 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002311 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002312 bufname = ptr;
2313 }
2314 else
2315 bufname = fname;
2316
2317 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002318 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002319 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002320 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002321 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002323 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002324 {
2325 vim_free(qf_last_bufname);
2326 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2327 if (bufname == ptr)
2328 qf_last_bufname = bufname;
2329 else
2330 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002331 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002332 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002333 if (buf == NULL)
2334 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002335
Bram Moolenaarc1542742016-07-20 21:44:37 +02002336 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002337 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002338 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339}
2340
2341/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002342 * Push dirbuf onto the directory stack and return pointer to actual dir or
2343 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 */
2345 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002346qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347{
2348 struct dir_stack_T *ds_new;
2349 struct dir_stack_T *ds_ptr;
2350
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002351 // allocate new stack element and hook it in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2353 if (ds_new == NULL)
2354 return NULL;
2355
2356 ds_new->next = *stackptr;
2357 *stackptr = ds_new;
2358
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002359 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 if (vim_isAbsName(dirbuf)
2361 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002362 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 (*stackptr)->dirname = vim_strsave(dirbuf);
2364 else
2365 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002366 // Okay we don't have an absolute path.
2367 // dirbuf must be a subdir of one of the directories on the stack.
2368 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 ds_new = (*stackptr)->next;
2370 (*stackptr)->dirname = NULL;
2371 while (ds_new)
2372 {
2373 vim_free((*stackptr)->dirname);
2374 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2375 TRUE);
2376 if (mch_isdir((*stackptr)->dirname) == TRUE)
2377 break;
2378
2379 ds_new = ds_new->next;
2380 }
2381
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002382 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 while ((*stackptr)->next != ds_new)
2384 {
2385 ds_ptr = (*stackptr)->next;
2386 (*stackptr)->next = (*stackptr)->next->next;
2387 vim_free(ds_ptr->dirname);
2388 vim_free(ds_ptr);
2389 }
2390
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002391 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 if (ds_new == NULL)
2393 {
2394 vim_free((*stackptr)->dirname);
2395 (*stackptr)->dirname = vim_strsave(dirbuf);
2396 }
2397 }
2398
2399 if ((*stackptr)->dirname != NULL)
2400 return (*stackptr)->dirname;
2401 else
2402 {
2403 ds_ptr = *stackptr;
2404 *stackptr = (*stackptr)->next;
2405 vim_free(ds_ptr);
2406 return NULL;
2407 }
2408}
2409
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410/*
2411 * pop dirbuf from the directory stack and return previous directory or NULL if
2412 * stack is empty
2413 */
2414 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002415qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416{
2417 struct dir_stack_T *ds_ptr;
2418
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002419 // TODO: Should we check if dirbuf is the directory on top of the stack?
2420 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002422 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 if (*stackptr != NULL)
2424 {
2425 ds_ptr = *stackptr;
2426 *stackptr = (*stackptr)->next;
2427 vim_free(ds_ptr->dirname);
2428 vim_free(ds_ptr);
2429 }
2430
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002431 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 return *stackptr ? (*stackptr)->dirname : NULL;
2433}
2434
2435/*
2436 * clean up directory stack
2437 */
2438 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002439qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
2441 struct dir_stack_T *ds_ptr;
2442
2443 while ((ds_ptr = *stackptr) != NULL)
2444 {
2445 *stackptr = (*stackptr)->next;
2446 vim_free(ds_ptr->dirname);
2447 vim_free(ds_ptr);
2448 }
2449}
2450
2451/*
2452 * Check in which directory of the directory stack the given file can be
2453 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002454 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 * Cleans up intermediate directory entries.
2456 *
2457 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002458 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 * ./
2460 * ./aa
2461 * ./aa/bb
2462 * ./bb
2463 * ./bb/x.c
2464 * and make says:
2465 * making all in aa
2466 * making all in bb
2467 * x.c:9: Error
2468 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2469 * qf_guess_filepath will return NULL.
2470 */
2471 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002472qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473{
2474 struct dir_stack_T *ds_ptr;
2475 struct dir_stack_T *ds_tmp;
2476 char_u *fullname;
2477
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002478 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002479 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 return NULL;
2481
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002482 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 fullname = NULL;
2484 while (ds_ptr)
2485 {
2486 vim_free(fullname);
2487 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2488
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002489 // If concat_fnames failed, just go on. The worst thing that can happen
2490 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2492 break;
2493
2494 ds_ptr = ds_ptr->next;
2495 }
2496
2497 vim_free(fullname);
2498
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002499 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002500 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002502 ds_tmp = qfl->qf_dir_stack->next;
2503 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 vim_free(ds_tmp->dirname);
2505 vim_free(ds_tmp);
2506 }
2507
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002508 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509}
2510
2511/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002512 * Returns TRUE if a quickfix/location list with the given identifier exists.
2513 */
2514 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002515qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002516{
2517 qf_info_T *qi = &ql_info;
2518 int i;
2519
2520 if (wp != NULL)
2521 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002522 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002523 if (qi == NULL)
2524 return FALSE;
2525 }
2526
2527 for (i = 0; i < qi->qf_listcount; ++i)
2528 if (qi->qf_lists[i].qf_id == qf_id)
2529 return TRUE;
2530
2531 return FALSE;
2532}
2533
2534/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002535 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002536 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002537 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002538 * Similar to location list.
2539 */
2540 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002541is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002542{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002543 qfline_T *qfp;
2544 int i;
2545
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002546 // Search for the entry in the current list
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002547 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2548 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002549 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002550 break;
2551
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002552 if (i == qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002553 return FALSE;
2554
2555 return TRUE;
2556}
2557
2558/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002559 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002560 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002561 */
2562 static qfline_T *
2563get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002564 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002565 qfline_T *qf_ptr,
2566 int *qf_index,
2567 int dir)
2568{
2569 int idx;
2570 int old_qf_fnum;
2571
2572 idx = *qf_index;
2573 old_qf_fnum = qf_ptr->qf_fnum;
2574
2575 do
2576 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002577 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002578 return NULL;
2579 ++idx;
2580 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002581 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002582 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2583
2584 *qf_index = idx;
2585 return qf_ptr;
2586}
2587
2588/*
2589 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002590 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002591 */
2592 static qfline_T *
2593get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002594 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002595 qfline_T *qf_ptr,
2596 int *qf_index,
2597 int dir)
2598{
2599 int idx;
2600 int old_qf_fnum;
2601
2602 idx = *qf_index;
2603 old_qf_fnum = qf_ptr->qf_fnum;
2604
2605 do
2606 {
2607 if (idx == 1 || qf_ptr->qf_prev == NULL)
2608 return NULL;
2609 --idx;
2610 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002611 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002612 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2613
2614 *qf_index = idx;
2615 return qf_ptr;
2616}
2617
2618/*
2619 * Get the n'th (errornr) previous/next valid entry from the current entry in
2620 * the quickfix list.
2621 * dir == FORWARD or FORWARD_FILE: next valid entry
2622 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2623 */
2624 static qfline_T *
2625get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002626 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002627 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002628 int dir,
2629 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002630{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002631 qfline_T *qf_ptr = qfl->qf_ptr;
2632 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002633 qfline_T *prev_qf_ptr;
2634 int prev_index;
2635 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2636 char_u *err = e_no_more_items;
2637
2638 while (errornr--)
2639 {
2640 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002641 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002642
2643 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002644 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002645 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002646 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002647 if (qf_ptr == NULL)
2648 {
2649 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002650 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002651 if (err != NULL)
2652 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002653 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002654 return NULL;
2655 }
2656 break;
2657 }
2658
2659 err = NULL;
2660 }
2661
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002662 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002663 return qf_ptr;
2664}
2665
2666/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002667 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2668 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002669 */
2670 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002671get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002672{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002673 qfline_T *qf_ptr = qfl->qf_ptr;
2674 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002675
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002676 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002677 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2678 {
2679 --qf_idx;
2680 qf_ptr = qf_ptr->qf_prev;
2681 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002682 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002683 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2684 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002685 {
2686 ++qf_idx;
2687 qf_ptr = qf_ptr->qf_next;
2688 }
2689
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002690 *new_qfidx = qf_idx;
2691 return qf_ptr;
2692}
2693
2694/*
2695 * Get a entry specied by 'errornr' and 'dir' from the current
2696 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2697 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2698 * Returns a pointer to the entry and the index of the new entry is stored in
2699 * 'new_qfidx'.
2700 */
2701 static qfline_T *
2702qf_get_entry(
2703 qf_list_T *qfl,
2704 int errornr,
2705 int dir,
2706 int *new_qfidx)
2707{
2708 qfline_T *qf_ptr = qfl->qf_ptr;
2709 int qfidx = qfl->qf_index;
2710
2711 if (dir != 0) // next/prev valid entry
2712 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2713 else if (errornr != 0) // go to specified number
2714 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2715
2716 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002717 return qf_ptr;
2718}
2719
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002720/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002721 * Find a window displaying a Vim help file.
2722 */
2723 static win_T *
2724qf_find_help_win(void)
2725{
2726 win_T *wp;
2727
2728 FOR_ALL_WINDOWS(wp)
2729 if (bt_help(wp->w_buffer))
2730 return wp;
2731
2732 return NULL;
2733}
2734
2735/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002736 * Set the location list for the specified window to 'qi'.
2737 */
2738 static void
2739win_set_loclist(win_T *wp, qf_info_T *qi)
2740{
2741 wp->w_llist = qi;
2742 qi->qf_refcount++;
2743}
2744
2745/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002746 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2747 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002748 */
2749 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002750jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002751{
2752 win_T *wp;
2753 int flags;
2754
Bram Moolenaarb2443732018-11-11 22:50:27 +01002755 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002756 wp = NULL;
2757 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002758 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002759 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2760 win_enter(wp, TRUE);
2761 else
2762 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002763 // Split off help window; put it at far top if no position
2764 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002765 flags = WSP_HELP;
2766 if (cmdmod.split == 0 && curwin->w_width != Columns
2767 && curwin->w_width < 80)
2768 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002769 // If the user asks to open a new window, then copy the location list.
2770 // Otherwise, don't copy the location list.
2771 if (IS_LL_STACK(qi) && !newwin)
2772 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002773
2774 if (win_split(0, flags) == FAIL)
2775 return FAIL;
2776
2777 *opened_window = TRUE;
2778
2779 if (curwin->w_height < p_hh)
2780 win_setheight((int)p_hh);
2781
Bram Moolenaarb2443732018-11-11 22:50:27 +01002782 // When using location list, the new window should use the supplied
2783 // location list. If the user asks to open a new window, then the new
2784 // window will get a copy of the location list.
2785 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002786 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002787 }
2788
2789 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002790 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002791
2792 return OK;
2793}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002794
2795/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002796 * Find a non-quickfix window in the current tabpage using the given location
2797 * list stack.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002798 * Returns NULL if a matching window is not found.
2799 */
2800 static win_T *
2801qf_find_win_with_loclist(qf_info_T *ll)
2802{
2803 win_T *wp;
2804
2805 FOR_ALL_WINDOWS(wp)
2806 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2807 return wp;
2808
2809 return NULL;
2810}
2811
2812/*
2813 * Find a window containing a normal buffer
2814 */
2815 static win_T *
2816qf_find_win_with_normal_buf(void)
2817{
2818 win_T *wp;
2819
2820 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002821 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002822 return wp;
2823
2824 return NULL;
2825}
2826
2827/*
2828 * Go to a window in any tabpage containing the specified file. Returns TRUE
2829 * if successfully jumped to the window. Otherwise returns FALSE.
2830 */
2831 static int
2832qf_goto_tabwin_with_file(int fnum)
2833{
2834 tabpage_T *tp;
2835 win_T *wp;
2836
2837 FOR_ALL_TAB_WINDOWS(tp, wp)
2838 if (wp->w_buffer->b_fnum == fnum)
2839 {
2840 goto_tabpage_win(tp, wp);
2841 return TRUE;
2842 }
2843
2844 return FALSE;
2845}
2846
2847/*
2848 * Create a new window to show a file above the quickfix window. Called when
2849 * only the quickfix window is present.
2850 */
2851 static int
2852qf_open_new_file_win(qf_info_T *ll_ref)
2853{
2854 int flags;
2855
2856 flags = WSP_ABOVE;
2857 if (ll_ref != NULL)
2858 flags |= WSP_NEWLOC;
2859 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002860 return FAIL; // not enough room for window
2861 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002862 swb_flags = 0;
2863 RESET_BINDING(curwin);
2864 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002865 // The new window should use the location list from the
2866 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002867 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002868 return OK;
2869}
2870
2871/*
2872 * Go to a window that shows the right buffer. If the window is not found, go
2873 * to the window just above the location list window. This is used for opening
2874 * a file from a location window and not from a quickfix window. If some usable
2875 * window is previously found, then it is supplied in 'use_win'.
2876 */
2877 static void
2878qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2879{
2880 win_T *win = use_win;
2881
2882 if (win == NULL)
2883 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002884 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002885 FOR_ALL_WINDOWS(win)
2886 if (win->w_buffer->b_fnum == qf_fnum)
2887 break;
2888 if (win == NULL)
2889 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002890 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002891 win = curwin;
2892 do
2893 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002894 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002895 break;
2896 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002897 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002898 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002899 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002900 } while (win != curwin);
2901 }
2902 }
2903 win_goto(win);
2904
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002905 // If the location list for the window is not set, then set it
2906 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01002907 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002908 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002909}
2910
2911/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002912 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2913 * not found, then go to the window just above the quickfix window. This is
2914 * used for opening a file from a quickfix window and not from a location
2915 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002916 */
2917 static void
2918qf_goto_win_with_qfl_file(int qf_fnum)
2919{
2920 win_T *win;
2921 win_T *altwin;
2922
2923 win = curwin;
2924 altwin = NULL;
2925 for (;;)
2926 {
2927 if (win->w_buffer->b_fnum == qf_fnum)
2928 break;
2929 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002930 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002931 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002932 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002933
2934 if (IS_QF_WINDOW(win))
2935 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002936 // Didn't find it, go to the window before the quickfix
2937 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002938 if (altwin != NULL)
2939 win = altwin;
2940 else if (curwin->w_prev != NULL)
2941 win = curwin->w_prev;
2942 else
2943 win = curwin->w_next;
2944 break;
2945 }
2946
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002947 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02002948 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002949 altwin = win;
2950 }
2951
2952 win_goto(win);
2953}
2954
2955/*
2956 * Find a suitable window for opening a file (qf_fnum) from the
2957 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01002958 * window, jump to it. Otherwise open a new window to display the file. If
2959 * 'newwin' is TRUE, then always open a new window. This is called from either
2960 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002961 */
2962 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002963qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002964{
2965 win_T *usable_win_ptr = NULL;
2966 int usable_win;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002967 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002968 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002969
2970 usable_win = 0;
2971
Bram Moolenaarb2443732018-11-11 22:50:27 +01002972 // If opening a new window, then don't use the location list referred by
2973 // the current window. Otherwise two windows will refer to the same
2974 // location list.
2975 if (!newwin)
2976 ll_ref = curwin->w_llist_ref;
2977
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002978 if (ll_ref != NULL)
2979 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002980 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002981 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2982 if (usable_win_ptr != NULL)
2983 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002984 }
2985
2986 if (!usable_win)
2987 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002988 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002989 win = qf_find_win_with_normal_buf();
2990 if (win != NULL)
2991 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002992 }
2993
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002994 // If no usable window is found and 'switchbuf' contains "usetab"
2995 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002996 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002997 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002998
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002999 // If there is only one window and it is the quickfix window, create a
3000 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003001 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003002 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003003 if (qf_open_new_file_win(ll_ref) != OK)
3004 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003005 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003006 }
3007 else
3008 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003009 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003010 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003011 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003012 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003013 }
3014
3015 return OK;
3016}
3017
3018/*
3019 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003020 * Returns OK if successfully edited the file, FAIL on failing to open the
3021 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3022 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003023 */
3024 static int
3025qf_jump_edit_buffer(
3026 qf_info_T *qi,
3027 qfline_T *qf_ptr,
3028 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003029 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003030 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003031{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003032 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003033 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003034 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003035 int old_qf_curlist = qi->qf_curlist;
3036 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003037
3038 if (qf_ptr->qf_type == 1)
3039 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003040 // Open help file (do_ecmd() will set b_help flag, readfile() will
3041 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003042 if (!can_abandon(curbuf, forceit))
3043 {
3044 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003045 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003046 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003047
3048 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3049 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003050 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003051 }
3052 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003053 retval = buflist_getfile(qf_ptr->qf_fnum,
3054 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003055
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003056 // If a location list, check whether the associated window is still
3057 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003058 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003059 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003060 win_T *wp = win_id2wp(prev_winid);
3061 if (wp == NULL && curwin->w_llist != qi)
3062 {
3063 emsg(_("E924: Current window was closed"));
3064 *opened_window = FALSE;
3065 return NOTDONE;
3066 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003067 }
3068
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003069 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003070 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003071 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003072 return NOTDONE;
3073 }
3074
3075 if (old_qf_curlist != qi->qf_curlist
3076 || !is_qf_entry_present(qfl, qf_ptr))
3077 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003078 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003079 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003080 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003081 emsg(_(e_loc_list_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003082 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003083 }
3084
3085 return retval;
3086}
3087
3088/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003089 * Go to the error line in the current file using either line/column number or
3090 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003091 */
3092 static void
3093qf_jump_goto_line(
3094 linenr_T qf_lnum,
3095 int qf_col,
3096 char_u qf_viscol,
3097 char_u *qf_pattern)
3098{
3099 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003100
3101 if (qf_pattern == NULL)
3102 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003103 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003104 i = qf_lnum;
3105 if (i > 0)
3106 {
3107 if (i > curbuf->b_ml.ml_line_count)
3108 i = curbuf->b_ml.ml_line_count;
3109 curwin->w_cursor.lnum = i;
3110 }
3111 if (qf_col > 0)
3112 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003113 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003114 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003115 coladvance(qf_col - 1);
3116 else
3117 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003118 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003119 check_cursor();
3120 }
3121 else
3122 beginline(BL_WHITE | BL_FIX);
3123 }
3124 else
3125 {
3126 pos_T save_cursor;
3127
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003128 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003129 save_cursor = curwin->w_cursor;
3130 curwin->w_cursor.lnum = 0;
3131 if (!do_search(NULL, '/', qf_pattern, (long)1,
3132 SEARCH_KEEP, NULL, NULL))
3133 curwin->w_cursor = save_cursor;
3134 }
3135}
3136
3137/*
3138 * Display quickfix list index and size message
3139 */
3140 static void
3141qf_jump_print_msg(
3142 qf_info_T *qi,
3143 int qf_index,
3144 qfline_T *qf_ptr,
3145 buf_T *old_curbuf,
3146 linenr_T old_lnum)
3147{
3148 linenr_T i;
3149 int len;
3150
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003151 // Update the screen before showing the message, unless the screen
3152 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003153 if (!msg_scrolled)
3154 update_topline_redraw();
3155 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003156 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003157 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3158 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003159 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003160 len = (int)STRLEN(IObuff);
3161 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3162
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003163 // Output the message. Overwrite to avoid scrolling when the 'O'
3164 // flag is present in 'shortmess'; But when not jumping, print the
3165 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003166 i = msg_scroll;
3167 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3168 msg_scroll = TRUE;
3169 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3170 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003171 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003172 msg_scroll = i;
3173}
3174
3175/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003176 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003177 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3178 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003179 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3180 * able to jump/open a window. Returns NOTDONE if a file is not associated
3181 * with the entry.
3182 */
3183 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003184qf_jump_open_window(
3185 qf_info_T *qi,
3186 qfline_T *qf_ptr,
3187 int newwin,
3188 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003189{
3190 // For ":helpgrep" find a help window or open one.
3191 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003192 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003193 return FAIL;
3194
3195 // If currently in the quickfix window, find another window to show the
3196 // file in.
3197 if (bt_quickfix(curbuf) && !*opened_window)
3198 {
3199 // If there is no file specified, we don't know where to go.
3200 // But do advance, otherwise ":cn" gets stuck.
3201 if (qf_ptr->qf_fnum == 0)
3202 return NOTDONE;
3203
Bram Moolenaarb2443732018-11-11 22:50:27 +01003204 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3205 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003206 return FAIL;
3207 }
3208
3209 return OK;
3210}
3211
3212/*
3213 * Edit a selected file from the quickfix/location list and jump to a
3214 * particular line/column, adjust the folds and display a message about the
3215 * jump.
3216 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3217 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3218 * the file.
3219 */
3220 static int
3221qf_jump_to_buffer(
3222 qf_info_T *qi,
3223 int qf_index,
3224 qfline_T *qf_ptr,
3225 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003226 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003227 int *opened_window,
3228 int openfold,
3229 int print_message)
3230{
3231 buf_T *old_curbuf;
3232 linenr_T old_lnum;
3233 int retval = OK;
3234
3235 // If there is a file name, read the wanted file if needed, and check
3236 // autowrite etc.
3237 old_curbuf = curbuf;
3238 old_lnum = curwin->w_cursor.lnum;
3239
3240 if (qf_ptr->qf_fnum != 0)
3241 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003242 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003243 opened_window);
3244 if (retval != OK)
3245 return retval;
3246 }
3247
3248 // When not switched to another buffer, still need to set pc mark
3249 if (curbuf == old_curbuf)
3250 setpcmark();
3251
3252 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3253 qf_ptr->qf_pattern);
3254
3255#ifdef FEAT_FOLDING
3256 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3257 foldOpenCursor();
3258#endif
3259 if (print_message)
3260 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3261
3262 return retval;
3263}
3264
3265/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003266 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 */
3268 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003269qf_jump(qf_info_T *qi,
3270 int dir,
3271 int errornr,
3272 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003274 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3275}
3276
3277/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003278 * Jump to a quickfix line.
3279 * If dir == 0 go to entry "errornr".
3280 * If dir == FORWARD go "errornr" valid entries forward.
3281 * If dir == BACKWARD go "errornr" valid entries backward.
3282 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3283 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3284 * else if "errornr" is zero, redisplay the same line
3285 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003286 * If 'newwin' is TRUE, then open the file in a new window.
3287 */
3288 void
3289qf_jump_newwin(qf_info_T *qi,
3290 int dir,
3291 int errornr,
3292 int forceit,
3293 int newwin)
3294{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003295 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003296 qfline_T *qf_ptr;
3297 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003300 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003301 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003302 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003306 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003308 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003310 if (qi == NULL)
3311 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003312
Bram Moolenaar0398e002019-03-21 21:12:49 +01003313 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003315 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 return;
3317 }
3318
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003319 incr_quickfix_busy();
3320
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003321 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003322
3323 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003325 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003327
3328 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3329 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003331 qf_ptr = old_qf_ptr;
3332 qf_index = old_qf_index;
3333 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003336 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003337 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003338 // No need to print the error message if it's visible in the error
3339 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 print_message = FALSE;
3341
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003342 prev_winid = curwin->w_id;
3343
Bram Moolenaarb2443732018-11-11 22:50:27 +01003344 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003345 if (retval == FAIL)
3346 goto failed;
3347 if (retval == NOTDONE)
3348 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003349
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003350 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003351 &opened_window, old_KeyTyped, print_message);
3352 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003354 // Quickfix/location list is freed by an autocmd
3355 qi = NULL;
3356 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003359 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003362 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003363 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003365 // Couldn't open file, so put index back where it was. This could
3366 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 qf_ptr = old_qf_ptr;
3369 qf_index = old_qf_index;
3370 }
3371 }
3372theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003373 if (qi != NULL)
3374 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003375 qfl->qf_ptr = qf_ptr;
3376 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003377 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003378 if (p_swb != old_swb)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003380 // Restore old 'switchbuf' value, but not when an autocommand or
3381 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003385 swb_flags = old_swb_flags;
3386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 else
3388 free_string_option(old_swb);
3389 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003390 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391}
3392
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003393// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003394static int qfFileAttr;
3395static int qfSepAttr;
3396static int qfLineAttr;
3397
3398/*
3399 * Display information about a single entry from the quickfix/location list.
3400 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003401 * 'cursel' will be set to TRUE for the currently selected entry in the
3402 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003403 */
3404 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003405qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003406{
3407 char_u *fname;
3408 buf_T *buf;
3409 int filter_entry;
3410
3411 fname = NULL;
3412 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3413 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3414 (char *)qfp->qf_module);
3415 else {
3416 if (qfp->qf_fnum != 0
3417 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3418 {
3419 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003420 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003421 fname = gettail(fname);
3422 }
3423 if (fname == NULL)
3424 sprintf((char *)IObuff, "%2d", qf_idx);
3425 else
3426 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3427 qf_idx, (char *)fname);
3428 }
3429
3430 // Support for filtering entries using :filter /pat/ clist
3431 // Match against the module name, file name, search pattern and
3432 // text of the entry.
3433 filter_entry = TRUE;
3434 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3435 filter_entry &= message_filtered(qfp->qf_module);
3436 if (filter_entry && fname != NULL)
3437 filter_entry &= message_filtered(fname);
3438 if (filter_entry && qfp->qf_pattern != NULL)
3439 filter_entry &= message_filtered(qfp->qf_pattern);
3440 if (filter_entry)
3441 filter_entry &= message_filtered(qfp->qf_text);
3442 if (filter_entry)
3443 return;
3444
3445 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003446 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003447
3448 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003449 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003450 if (qfp->qf_lnum == 0)
3451 IObuff[0] = NUL;
3452 else if (qfp->qf_col == 0)
3453 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3454 else
3455 sprintf((char *)IObuff, "%ld col %d",
3456 qfp->qf_lnum, qfp->qf_col);
3457 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3458 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003459 msg_puts_attr((char *)IObuff, qfLineAttr);
3460 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003461 if (qfp->qf_pattern != NULL)
3462 {
3463 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003464 msg_puts((char *)IObuff);
3465 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003466 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003467 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003468
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003469 // Remove newlines and leading whitespace from the text. For an
3470 // unrecognized line keep the indent, the compiler may mark a word
3471 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003472 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3473 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3474 IObuff, IOSIZE);
3475 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003476 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003477}
3478
3479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003481 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 */
3483 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003484qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003486 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003487 qfline_T *qfp;
3488 int i;
3489 int idx1 = 1;
3490 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003491 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003492 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003493 int all = eap->forceit; // if not :cl!, only show
3494 // recognised errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003495 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
Bram Moolenaar39665952018-08-15 20:59:48 +02003497 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003498 {
3499 qi = GET_LOC_LIST(curwin);
3500 if (qi == NULL)
3501 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003502 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003503 return;
3504 }
3505 }
3506
Bram Moolenaar0398e002019-03-21 21:12:49 +01003507 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003509 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 return;
3511 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003512 if (*arg == '+')
3513 {
3514 ++arg;
3515 plus = TRUE;
3516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3518 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003519 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 return;
3521 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003522 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003523 if (plus)
3524 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003525 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003526 idx2 = i + idx1;
3527 idx1 = i;
3528 }
3529 else
3530 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003531 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003532 if (idx1 < 0)
3533 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3534 if (idx2 < 0)
3535 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003538 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003539 shorten_fnames(FALSE);
3540
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003541 // Get the attributes for the different quickfix highlight items. Note
3542 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003543 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3544 if (qfFileAttr == 0)
3545 qfFileAttr = HL_ATTR(HLF_D);
3546 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3547 if (qfSepAttr == 0)
3548 qfSepAttr = HL_ATTR(HLF_D);
3549 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3550 if (qfLineAttr == 0)
3551 qfLineAttr = HL_ATTR(HLF_N);
3552
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003553 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003555 qfp = qfl->qf_start;
3556 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
3558 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3559 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003560 if (got_int)
3561 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003562
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003563 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003565
3566 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003567 if (qfp == NULL)
3568 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003569 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 ui_breakcheck();
3571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572}
3573
3574/*
3575 * Remove newlines and leading whitespace from an error message.
3576 * Put the result in "buf[bufsize]".
3577 */
3578 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003579qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580{
3581 int i;
3582 char_u *p = text;
3583
3584 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3585 {
3586 if (*p == '\n')
3587 {
3588 buf[i] = ' ';
3589 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003590 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 break;
3592 }
3593 else
3594 buf[i] = *p++;
3595 }
3596 buf[i] = NUL;
3597}
3598
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003599/*
3600 * Display information (list number, list size and the title) about a
3601 * quickfix/location list.
3602 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003603 static void
3604qf_msg(qf_info_T *qi, int which, char *lead)
3605{
3606 char *title = (char *)qi->qf_lists[which].qf_title;
3607 int count = qi->qf_lists[which].qf_count;
3608 char_u buf[IOSIZE];
3609
3610 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3611 lead,
3612 which + 1,
3613 qi->qf_listcount,
3614 count);
3615
3616 if (title != NULL)
3617 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003618 size_t len = STRLEN(buf);
3619
3620 if (len < 34)
3621 {
3622 vim_memset(buf + len, ' ', 34 - len);
3623 buf[34] = NUL;
3624 }
3625 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003626 }
3627 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003628 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003629}
3630
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631/*
3632 * ":colder [count]": Up in the quickfix stack.
3633 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003634 * ":lolder [count]": Up in the location list stack.
3635 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 */
3637 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003638qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003640 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 int count;
3642
Bram Moolenaar39665952018-08-15 20:59:48 +02003643 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003644 {
3645 qi = GET_LOC_LIST(curwin);
3646 if (qi == NULL)
3647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003648 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003649 return;
3650 }
3651 }
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (eap->addr_count != 0)
3654 count = eap->line2;
3655 else
3656 count = 1;
3657 while (count--)
3658 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003659 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003661 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003663 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003664 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003666 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668 else
3669 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003670 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003672 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003673 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003675 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
3677 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003678 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003679 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680}
3681
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003682/*
3683 * Display the information about all the quickfix/location lists in the stack
3684 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003685 void
3686qf_history(exarg_T *eap)
3687{
3688 qf_info_T *qi = &ql_info;
3689 int i;
3690
Bram Moolenaar39665952018-08-15 20:59:48 +02003691 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003692 qi = GET_LOC_LIST(curwin);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003693 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003694 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003695 else
3696 for (i = 0; i < qi->qf_listcount; ++i)
3697 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3698}
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003701 * Free all the entries in the error list "idx". Note that other information
3702 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 */
3704 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003705qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003707 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003708 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003709 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003711 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003713 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003714 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003715 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003716 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003717 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003718 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003719 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003720 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003721 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003722 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003723 // Somehow qf_count may have an incorrect value, set it to 1
3724 // to avoid crashing when it's wrong.
3725 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003726 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003727 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003728 qfl->qf_start = qfpnext;
3729 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003731
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003732 qfl->qf_index = 0;
3733 qfl->qf_start = NULL;
3734 qfl->qf_last = NULL;
3735 qfl->qf_ptr = NULL;
3736 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003737
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003738 qf_clean_dir_stack(&qfl->qf_dir_stack);
3739 qfl->qf_directory = NULL;
3740 qf_clean_dir_stack(&qfl->qf_file_stack);
3741 qfl->qf_currfile = NULL;
3742 qfl->qf_multiline = FALSE;
3743 qfl->qf_multiignore = FALSE;
3744 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745}
3746
3747/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003748 * Free error list "idx". Frees all the entries in the quickfix list,
3749 * associated context information and the title.
3750 */
3751 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003752qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003753{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003754 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003755
Bram Moolenaard23a8232018-02-10 18:45:26 +01003756 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003757 free_tv(qfl->qf_ctx);
3758 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003759 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003760 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003761}
3762
3763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 * qf_mark_adjust: adjust marks
3765 */
3766 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003767qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003768 win_T *wp,
3769 linenr_T line1,
3770 linenr_T line2,
3771 long amount,
3772 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003774 int i;
3775 qfline_T *qfp;
3776 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003777 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003778 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003779 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780
Bram Moolenaarc1542742016-07-20 21:44:37 +02003781 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003782 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003783 if (wp != NULL)
3784 {
3785 if (wp->w_llist == NULL)
3786 return;
3787 qi = wp->w_llist;
3788 }
3789
3790 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003791 {
3792 qf_list_T *qfl = qf_get_list(qi, idx);
3793
3794 if (!qf_list_empty(qfl))
3795 for (i = 0, qfp = qfl->qf_start;
3796 i < qfl->qf_count && qfp != NULL;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003797 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (qfp->qf_fnum == curbuf->b_fnum)
3799 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003800 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3802 {
3803 if (amount == MAXLNUM)
3804 qfp->qf_cleared = TRUE;
3805 else
3806 qfp->qf_lnum += amount;
3807 }
3808 else if (amount_after && qfp->qf_lnum > line2)
3809 qfp->qf_lnum += amount_after;
3810 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003811 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003812
3813 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003814 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815}
3816
3817/*
3818 * Make a nice message out of the error character and the error number:
3819 * char number message
3820 * e or E 0 " error"
3821 * w or W 0 " warning"
3822 * i or I 0 " info"
3823 * 0 0 ""
3824 * other 0 " c"
3825 * e or E n " error n"
3826 * w or W n " warning n"
3827 * i or I n " info n"
3828 * 0 n " error n"
3829 * other n " c n"
3830 * 1 x "" :helpgrep
3831 */
3832 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003833qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834{
3835 static char_u buf[20];
3836 static char_u cc[3];
3837 char_u *p;
3838
3839 if (c == 'W' || c == 'w')
3840 p = (char_u *)" warning";
3841 else if (c == 'I' || c == 'i')
3842 p = (char_u *)" info";
3843 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3844 p = (char_u *)" error";
3845 else if (c == 0 || c == 1)
3846 p = (char_u *)"";
3847 else
3848 {
3849 cc[0] = ' ';
3850 cc[1] = c;
3851 cc[2] = NUL;
3852 p = cc;
3853 }
3854
3855 if (nr <= 0)
3856 return p;
3857
3858 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3859 return buf;
3860}
3861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003863 * When "split" is FALSE: Open the entry/result under the cursor.
3864 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3865 */
3866 void
3867qf_view_result(int split)
3868{
3869 qf_info_T *qi = &ql_info;
3870
3871 if (!bt_quickfix(curbuf))
3872 return;
3873
3874 if (IS_LL_WINDOW(curwin))
3875 qi = GET_LOC_LIST(curwin);
3876
Bram Moolenaar0398e002019-03-21 21:12:49 +01003877 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003878 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003879 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003880 return;
3881 }
3882
3883 if (split)
3884 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003885 // Open the selected entry in a new window
3886 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3887 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003888 return;
3889 }
3890
3891 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3892}
3893
3894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 * ":cwindow": open the quickfix window if we have errors to display,
3896 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003897 * ":lwindow": open the location list window if we have locations to display,
3898 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 */
3900 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003901ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003903 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003904 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 win_T *win;
3906
Bram Moolenaar39665952018-08-15 20:59:48 +02003907 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003908 {
3909 qi = GET_LOC_LIST(curwin);
3910 if (qi == NULL)
3911 return;
3912 }
3913
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003914 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003915
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003916 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003917 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003919 // If a quickfix window is open but we have no errors to display,
3920 // close the window. If a quickfix window is not open, then open
3921 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003922 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003923 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01003924 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 {
3926 if (win != NULL)
3927 ex_cclose(eap);
3928 }
3929 else if (win == NULL)
3930 ex_copen(eap);
3931}
3932
3933/*
3934 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003935 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003938ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003940 win_T *win = NULL;
3941 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
Bram Moolenaar39665952018-08-15 20:59:48 +02003943 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003944 {
3945 qi = GET_LOC_LIST(curwin);
3946 if (qi == NULL)
3947 return;
3948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003950 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003951 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 if (win != NULL)
3953 win_close(win, FALSE);
3954}
3955
3956/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003957 * Set "w:quickfix_title" if "qi" has a title.
3958 */
3959 static void
3960qf_set_title_var(qf_list_T *qfl)
3961{
3962 if (qfl->qf_title != NULL)
3963 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3964}
3965
3966/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003967 * Goto a quickfix or location list window (if present).
3968 * Returns OK if the window is found, FAIL otherwise.
3969 */
3970 static int
3971qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3972{
3973 win_T *win;
3974
3975 win = qf_find_win(qi);
3976 if (win == NULL)
3977 return FAIL;
3978
3979 win_goto(win);
3980 if (resize)
3981 {
3982 if (vertsplit)
3983 {
3984 if (sz != win->w_width)
3985 win_setwidth(sz);
3986 }
3987 else if (sz != win->w_height)
3988 win_setheight(sz);
3989 }
3990
3991 return OK;
3992}
3993
3994/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01003995 * Set options for the buffer in the quickfix or location list window.
3996 */
3997 static void
3998qf_set_cwindow_options(void)
3999{
4000 // switch off 'swapfile'
4001 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4002 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4003 OPT_LOCAL);
4004 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4005 RESET_BINDING(curwin);
4006#ifdef FEAT_DIFF
4007 curwin->w_p_diff = FALSE;
4008#endif
4009#ifdef FEAT_FOLDING
4010 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4011 OPT_LOCAL);
4012#endif
4013}
4014
4015/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004016 * Open a new quickfix or location list window, load the quickfix buffer and
4017 * set the appropriate options for the window.
4018 * Returns FAIL if the window could not be opened.
4019 */
4020 static int
4021qf_open_new_cwindow(qf_info_T *qi, int height)
4022{
4023 buf_T *qf_buf;
4024 win_T *oldwin = curwin;
4025 tabpage_T *prevtab = curtab;
4026 int flags = 0;
4027 win_T *win;
4028
4029 qf_buf = qf_find_buf(qi);
4030
4031 // The current window becomes the previous window afterwards.
4032 win = curwin;
4033
4034 if (IS_QF_STACK(qi) && cmdmod.split == 0)
4035 // Create the new quickfix window at the very bottom, except when
4036 // :belowright or :aboveleft is used.
4037 win_goto(lastwin);
4038 // Default is to open the window below the current window
4039 if (cmdmod.split == 0)
4040 flags = WSP_BELOW;
4041 flags |= WSP_NEWLOC;
4042 if (win_split(height, flags) == FAIL)
4043 return FAIL; // not enough room for window
4044 RESET_BINDING(curwin);
4045
4046 if (IS_LL_STACK(qi))
4047 {
4048 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004049 // location list stack from the window 'win'.
4050 curwin->w_llist_ref = qi;
4051 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004052 }
4053
4054 if (oldwin != curwin)
4055 oldwin = NULL; // don't store info when in another window
4056 if (qf_buf != NULL)
4057 {
4058 // Use the existing quickfix buffer
4059 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4060 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4061 }
4062 else
4063 {
4064 // Create a new quickfix buffer
4065 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4066
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004067 // save the number of the new buffer
4068 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004069 }
4070
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004071 // Set the options for the quickfix buffer/window (if not already done)
4072 // Do this even if the quickfix buffer was already present, as an autocmd
4073 // might have previously deleted (:bdelete) the quickfix buffer.
4074 if (curbuf->b_p_bt[0] != 'q')
4075 qf_set_cwindow_options();
4076
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004077 // Only set the height when still in the same tab page and there is no
4078 // window to the side.
4079 if (curtab == prevtab && curwin->w_width == Columns)
4080 win_setheight(height);
4081 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4082 if (win_valid(win))
4083 prevwin = win;
4084
4085 return OK;
4086}
4087
4088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004090 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 */
4092 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004093ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004095 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004096 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004098 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004099 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
Bram Moolenaar39665952018-08-15 20:59:48 +02004101 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004102 {
4103 qi = GET_LOC_LIST(curwin);
4104 if (qi == NULL)
4105 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004106 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004107 return;
4108 }
4109 }
4110
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004111 incr_quickfix_busy();
4112
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 if (eap->addr_count != 0)
4114 height = eap->line2;
4115 else
4116 height = QF_WINHEIGHT;
4117
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004118 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119#ifdef FEAT_GUI
4120 need_mouse_correct = TRUE;
4121#endif
4122
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004123 // Find an existing quickfix window, or open a new one.
4124 if (cmdmod.tab == 0)
4125 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4126 cmdmod.split & WSP_VERT);
4127 if (status == FAIL)
4128 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004129 {
4130 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004131 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004134 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004135 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004136 // Save the current index here, as updating the quickfix buffer may free
4137 // the quickfix list
4138 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004139
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004140 // Fill the buffer with the quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004141 qf_fill_buffer(qfl, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004143 decr_quickfix_busy();
4144
4145 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 curwin->w_cursor.col = 0;
4147 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004148 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149}
4150
4151/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004152 * Move the cursor in the quickfix window to "lnum".
4153 */
4154 static void
4155qf_win_goto(win_T *win, linenr_T lnum)
4156{
4157 win_T *old_curwin = curwin;
4158
4159 curwin = win;
4160 curbuf = win->w_buffer;
4161 curwin->w_cursor.lnum = lnum;
4162 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004163 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004164 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004165 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004166 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004167 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004168 curwin = old_curwin;
4169 curbuf = curwin->w_buffer;
4170}
4171
4172/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004173 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004174 */
4175 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004176ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004177{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004178 qf_info_T *qi = &ql_info;
4179 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004180
Bram Moolenaar39665952018-08-15 20:59:48 +02004181 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004182 {
4183 qi = GET_LOC_LIST(curwin);
4184 if (qi == NULL)
4185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004186 emsg(_(e_loclist));
Bram Moolenaar537ef082016-07-09 17:56:19 +02004187 return;
4188 }
4189 }
4190
4191 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004192 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4193 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4194}
4195
4196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 * Return the number of the current entry (line number in the quickfix
4198 * window).
4199 */
4200 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004201qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004203 qf_info_T *qi = &ql_info;
4204
4205 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004206 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004207 qi = wp->w_llist_ref;
4208
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004209 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210}
4211
4212/*
4213 * Update the cursor position in the quickfix window to the current error.
4214 * Return TRUE if there is a quickfix window.
4215 */
4216 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004217qf_win_pos_update(
4218 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004219 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220{
4221 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004222 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004224 // Put the cursor on the current error in the quickfix window, so that
4225 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004226 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 if (win != NULL
4228 && qf_index <= win->w_buffer->b_ml.ml_line_count
4229 && old_qf_index != qf_index)
4230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 if (qf_index > old_qf_index)
4232 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004233 win->w_redraw_top = old_qf_index;
4234 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
4236 else
4237 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004238 win->w_redraw_top = qf_index;
4239 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004241 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 return win != NULL;
4244}
4245
4246/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004247 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004248 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004249 */
4250 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004251is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004252{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004253 // A window displaying the quickfix buffer will have the w_llist_ref field
4254 // set to NULL.
4255 // A window displaying a location list buffer will have the w_llist_ref
4256 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004257 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004258 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4259 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004260 return TRUE;
4261
4262 return FALSE;
4263}
4264
4265/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004266 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004267 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004268 */
4269 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004270qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004271{
4272 win_T *win;
4273
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004274 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004275 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004276 return win;
4277 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004278}
4279
4280/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004281 * Find a quickfix buffer.
4282 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 */
4284 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004285qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004287 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004288 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004290 if (qi->qf_bufnr != INVALID_QFBUFNR)
4291 {
4292 buf_T *qfbuf;
4293 qfbuf = buflist_findnr(qi->qf_bufnr);
4294 if (qfbuf != NULL)
4295 return qfbuf;
4296 // buffer is no longer present
4297 qi->qf_bufnr = INVALID_QFBUFNR;
4298 }
4299
Bram Moolenaar9c102382006-05-03 21:26:49 +00004300 FOR_ALL_TAB_WINDOWS(tp, win)
4301 if (is_qf_win(win, qi))
4302 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004303
Bram Moolenaar9c102382006-05-03 21:26:49 +00004304 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305}
4306
4307/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004308 * Update the w:quickfix_title variable in the quickfix/location list window
4309 */
4310 static void
4311qf_update_win_titlevar(qf_info_T *qi)
4312{
4313 win_T *win;
4314 win_T *curwin_save;
4315
4316 if ((win = qf_find_win(qi)) != NULL)
4317 {
4318 curwin_save = curwin;
4319 curwin = win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004320 qf_set_title_var(qf_get_curlist(qi));
Bram Moolenaard823fa92016-08-12 16:29:27 +02004321 curwin = curwin_save;
4322 }
4323}
4324
4325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 * Find the quickfix buffer. If it exists, update the contents.
4327 */
4328 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004329qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330{
4331 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004332 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004335 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004336 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 if (buf != NULL)
4338 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004339 linenr_T old_line_count = buf->b_ml.ml_line_count;
4340
4341 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004342 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004343 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
Bram Moolenaard823fa92016-08-12 16:29:27 +02004345 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004346
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004347 qf_fill_buffer(qf_get_curlist(qi), buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004348 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004349
Bram Moolenaar864293a2016-06-02 13:40:04 +02004350 if (old_last == NULL)
4351 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004352 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004353
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004354 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004355 aucmd_restbuf(&aco);
4356 }
4357
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004358 // Only redraw when added lines are visible. This avoids flickering
4359 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004360 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4361 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363}
4364
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004365/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004366 * Add an error line to the quickfix buffer.
4367 */
4368 static int
4369qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4370{
4371 int len;
4372 buf_T *errbuf;
4373
4374 if (qfp->qf_module != NULL)
4375 {
4376 STRCPY(IObuff, qfp->qf_module);
4377 len = (int)STRLEN(IObuff);
4378 }
4379 else if (qfp->qf_fnum != 0
4380 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4381 && errbuf->b_fname != NULL)
4382 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004383 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004384 STRCPY(IObuff, gettail(errbuf->b_fname));
4385 else
4386 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004387 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004388 if (errbuf->b_sfname == NULL
4389 || mch_isFullName(errbuf->b_sfname))
4390 {
4391 if (*dirname == NUL)
4392 mch_dirname(dirname, MAXPATHL);
4393 shorten_buf_fname(errbuf, dirname, FALSE);
4394 }
4395 STRCPY(IObuff, errbuf->b_fname);
4396 }
4397 len = (int)STRLEN(IObuff);
4398 }
4399 else
4400 len = 0;
4401 IObuff[len++] = '|';
4402
4403 if (qfp->qf_lnum > 0)
4404 {
4405 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4406 len += (int)STRLEN(IObuff + len);
4407
4408 if (qfp->qf_col > 0)
4409 {
4410 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4411 len += (int)STRLEN(IObuff + len);
4412 }
4413
4414 sprintf((char *)IObuff + len, "%s",
4415 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4416 len += (int)STRLEN(IObuff + len);
4417 }
4418 else if (qfp->qf_pattern != NULL)
4419 {
4420 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4421 len += (int)STRLEN(IObuff + len);
4422 }
4423 IObuff[len++] = '|';
4424 IObuff[len++] = ' ';
4425
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004426 // Remove newlines and leading whitespace from the text.
4427 // For an unrecognized line keep the indent, the compiler may
4428 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004429 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4430 IObuff + len, IOSIZE - len);
4431
4432 if (ml_append_buf(buf, lnum, IObuff,
4433 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4434 return FAIL;
4435
4436 return OK;
4437}
4438
4439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 * Fill current buffer with quickfix errors, replacing any previous contents.
4441 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004442 * If "old_last" is not NULL append the items after this one.
4443 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4444 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 */
4446 static void
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004447qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004449 linenr_T lnum;
4450 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004451 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
Bram Moolenaar864293a2016-06-02 13:40:04 +02004453 if (old_last == NULL)
4454 {
4455 if (buf != curbuf)
4456 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004457 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004458 return;
4459 }
4460
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004461 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004462 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4463 (void)ml_delete((linenr_T)1, FALSE);
4464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004466 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004467 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004469 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004470
4471 *dirname = NUL;
4472
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004473 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004474 if (old_last == NULL)
4475 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004476 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004477 lnum = 0;
4478 }
4479 else
4480 {
4481 qfp = old_last->qf_next;
4482 lnum = buf->b_ml.ml_line_count;
4483 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004484 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004486 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004488
Bram Moolenaar864293a2016-06-02 13:40:04 +02004489 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004491 if (qfp == NULL)
4492 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004494
4495 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004496 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004497 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004500 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 check_lnums(TRUE);
4502
Bram Moolenaar864293a2016-06-02 13:40:04 +02004503 if (old_last == NULL)
4504 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004505 // Set the 'filetype' to "qf" each time after filling the buffer.
4506 // This resembles reading a file into a buffer, it's more logical when
4507 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004508 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004509 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4510 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004512 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004513 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004515 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004517 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004518 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004519
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004520 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004521 redraw_curbuf_later(NOT_VALID);
4522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004524 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 KeyTyped = old_KeyTyped;
4526}
4527
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004528/*
4529 * For every change made to the quickfix list, update the changed tick.
4530 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004531 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004532qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004533{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004534 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004535}
4536
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004538 * Return the quickfix/location list number with the given identifier.
4539 * Returns -1 if list is not found.
4540 */
4541 static int
4542qf_id2nr(qf_info_T *qi, int_u qfid)
4543{
4544 int qf_idx;
4545
4546 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4547 if (qi->qf_lists[qf_idx].qf_id == qfid)
4548 return qf_idx;
4549 return INVALID_QFIDX;
4550}
4551
4552/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004553 * If the current list is not "save_qfid" and we can find the list with that ID
4554 * then make it the current list.
4555 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004556 * Returns OK if successfully restored the list. Returns FAIL if the list with
4557 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004558 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004559 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004560qf_restore_list(qf_info_T *qi, int_u save_qfid)
4561{
4562 int curlist;
4563
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004564 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004565 {
4566 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004567 if (curlist < 0)
4568 // list is not present
4569 return FAIL;
4570 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004571 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004572 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004573}
4574
4575/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004576 * Jump to the first entry if there is one.
4577 */
4578 static void
4579qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4580{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004581 if (qf_restore_list(qi, save_qfid) == FAIL)
4582 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004583
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004584 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004585 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004586 qf_jump(qi, 0, 0, forceit);
4587}
4588
4589/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004590 * Return TRUE when using ":vimgrep" for ":grep".
4591 */
4592 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004593grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004594{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004595 return ((cmdidx == CMD_grep
4596 || cmdidx == CMD_lgrep
4597 || cmdidx == CMD_grepadd
4598 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004599 && STRCMP("internal",
4600 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4601}
4602
4603/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004604 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004606 static char_u *
4607make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004609 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004610 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004611 case CMD_make: return (char_u *)"make";
4612 case CMD_lmake: return (char_u *)"lmake";
4613 case CMD_grep: return (char_u *)"grep";
4614 case CMD_lgrep: return (char_u *)"lgrep";
4615 case CMD_grepadd: return (char_u *)"grepadd";
4616 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4617 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619}
4620
4621/*
4622 * Return the name for the errorfile, in allocated memory.
4623 * Find a new unique name when 'makeef' contains "##".
4624 * Returns NULL for error.
4625 */
4626 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004627get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628{
4629 char_u *p;
4630 char_u *name;
4631 static int start = -1;
4632 static int off = 0;
4633#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004634 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635#endif
4636
4637 if (*p_mef == NUL)
4638 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004639 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004641 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 return name;
4643 }
4644
4645 for (p = p_mef; *p; ++p)
4646 if (p[0] == '#' && p[1] == '#')
4647 break;
4648
4649 if (*p == NUL)
4650 return vim_strsave(p_mef);
4651
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004652 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 for (;;)
4654 {
4655 if (start == -1)
4656 start = mch_get_pid();
4657 else
4658 off += 19;
4659
4660 name = alloc((unsigned)STRLEN(p_mef) + 30);
4661 if (name == NULL)
4662 break;
4663 STRCPY(name, p_mef);
4664 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4665 STRCAT(name, p + 2);
4666 if (mch_getperm(name) < 0
4667#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004668 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 && mch_lstat((char *)name, &sb) < 0
4670#endif
4671 )
4672 break;
4673 vim_free(name);
4674 }
4675 return name;
4676}
4677
4678/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004679 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4680 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4681 */
4682 static char_u *
4683make_get_fullcmd(char_u *makecmd, char_u *fname)
4684{
4685 char_u *cmd;
4686 unsigned len;
4687
4688 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4689 if (*p_sp != NUL)
4690 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4691 cmd = alloc(len);
4692 if (cmd == NULL)
4693 return NULL;
4694 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4695 (char *)p_shq);
4696
4697 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4698 if (*p_sp != NUL)
4699 append_redir(cmd, len, p_sp, fname);
4700
4701 // Display the fully formed command. Output a newline if there's something
4702 // else than the :make command that was typed (in which case the cursor is
4703 // in column 0).
4704 if (msg_col == 0)
4705 msg_didout = FALSE;
4706 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004707 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004708 msg_outtrans(cmd); // show what we are doing
4709
4710 return cmd;
4711}
4712
4713/*
4714 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4715 */
4716 void
4717ex_make(exarg_T *eap)
4718{
4719 char_u *fname;
4720 char_u *cmd;
4721 char_u *enc = NULL;
4722 win_T *wp = NULL;
4723 qf_info_T *qi = &ql_info;
4724 int res;
4725 char_u *au_name = NULL;
4726 int_u save_qfid;
4727
4728 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4729 if (grep_internal(eap->cmdidx))
4730 {
4731 ex_vimgrep(eap);
4732 return;
4733 }
4734
4735 au_name = make_get_auname(eap->cmdidx);
4736 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4737 curbuf->b_fname, TRUE, curbuf))
4738 {
4739#ifdef FEAT_EVAL
4740 if (aborting())
4741 return;
4742#endif
4743 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004744 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004745
4746 if (is_loclist_cmd(eap->cmdidx))
4747 wp = curwin;
4748
4749 autowrite_all();
4750 fname = get_mef_name();
4751 if (fname == NULL)
4752 return;
4753 mch_remove(fname); // in case it's not unique
4754
4755 cmd = make_get_fullcmd(eap->arg, fname);
4756 if (cmd == NULL)
4757 return;
4758
4759 // let the shell know if we are redirecting output or not
4760 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4761
4762#ifdef AMIGA
4763 out_flush();
4764 // read window status report and redraw before message
4765 (void)char_avail();
4766#endif
4767
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004768 incr_quickfix_busy();
4769
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004770 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4771 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4772 (eap->cmdidx != CMD_grepadd
4773 && eap->cmdidx != CMD_lgrepadd),
4774 qf_cmdtitle(*eap->cmdlinep), enc);
4775 if (wp != NULL)
4776 {
4777 qi = GET_LOC_LIST(wp);
4778 if (qi == NULL)
4779 goto cleanup;
4780 }
4781 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004782 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004783
4784 // Remember the current quickfix list identifier, so that we can
4785 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004786 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004787 if (au_name != NULL)
4788 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4789 curbuf->b_fname, TRUE, curbuf);
4790 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4791 // display the first error
4792 qf_jump_first(qi, save_qfid, FALSE);
4793
4794cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004795 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004796 mch_remove(fname);
4797 vim_free(fname);
4798 vim_free(cmd);
4799}
4800
4801/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004802 * Returns the number of valid entries in the current quickfix/location list.
4803 */
4804 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004805qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004806{
4807 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004808 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004809 qfline_T *qfp;
4810 int i, sz = 0;
4811 int prev_fnum = 0;
4812
Bram Moolenaar39665952018-08-15 20:59:48 +02004813 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004814 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004815 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004816 qi = GET_LOC_LIST(curwin);
4817 if (qi == NULL)
4818 return 0;
4819 }
4820
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004821 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004822 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004823 ++i, qfp = qfp->qf_next)
4824 {
4825 if (qfp->qf_valid)
4826 {
4827 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004828 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004829 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4830 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004831 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004832 sz++;
4833 prev_fnum = qfp->qf_fnum;
4834 }
4835 }
4836 }
4837
4838 return sz;
4839}
4840
4841/*
4842 * Returns the current index of the quickfix/location list.
4843 * Returns 0 if there is an error.
4844 */
4845 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004846qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004847{
4848 qf_info_T *qi = &ql_info;
4849
Bram Moolenaar39665952018-08-15 20:59:48 +02004850 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004851 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004852 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004853 qi = GET_LOC_LIST(curwin);
4854 if (qi == NULL)
4855 return 0;
4856 }
4857
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004858 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004859}
4860
4861/*
4862 * Returns the current index in the quickfix/location list (counting only valid
4863 * entries). If no valid entries are in the list, then returns 1.
4864 */
4865 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004866qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004867{
4868 qf_info_T *qi = &ql_info;
4869 qf_list_T *qfl;
4870 qfline_T *qfp;
4871 int i, eidx = 0;
4872 int prev_fnum = 0;
4873
Bram Moolenaar39665952018-08-15 20:59:48 +02004874 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004875 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004876 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004877 qi = GET_LOC_LIST(curwin);
4878 if (qi == NULL)
4879 return 1;
4880 }
4881
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004882 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004883 qfp = qfl->qf_start;
4884
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004885 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004886 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4887 return 1;
4888
4889 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4890 {
4891 if (qfp->qf_valid)
4892 {
4893 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4894 {
4895 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4896 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004897 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004898 eidx++;
4899 prev_fnum = qfp->qf_fnum;
4900 }
4901 }
4902 else
4903 eidx++;
4904 }
4905 }
4906
4907 return eidx ? eidx : 1;
4908}
4909
4910/*
4911 * Get the 'n'th valid error entry in the quickfix or location list.
4912 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4913 * For :cdo and :ldo returns the 'n'th valid error entry.
4914 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4915 */
4916 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004917qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004918{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004919 qfline_T *qfp = qfl->qf_start;
4920 int i, eidx;
4921 int prev_fnum = 0;
4922
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004923 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004924 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4925 return 1;
4926
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004927 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004928 i++, qfp = qfp->qf_next)
4929 {
4930 if (qfp->qf_valid)
4931 {
4932 if (fdo)
4933 {
4934 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4935 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004936 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004937 eidx++;
4938 prev_fnum = qfp->qf_fnum;
4939 }
4940 }
4941 else
4942 eidx++;
4943 }
4944
4945 if (eidx == n)
4946 break;
4947 }
4948
4949 if (i <= qfl->qf_count)
4950 return i;
4951 else
4952 return 1;
4953}
4954
4955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004957 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004958 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 */
4960 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004961ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004963 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004964 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004965
Bram Moolenaar39665952018-08-15 20:59:48 +02004966 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004967 {
4968 qi = GET_LOC_LIST(curwin);
4969 if (qi == NULL)
4970 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004971 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004972 return;
4973 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004974 }
4975
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004976 if (eap->addr_count > 0)
4977 errornr = (int)eap->line2;
4978 else
4979 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004980 switch (eap->cmdidx)
4981 {
4982 case CMD_cc: case CMD_ll:
4983 errornr = 0;
4984 break;
4985 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4986 case CMD_lfirst:
4987 errornr = 1;
4988 break;
4989 default:
4990 errornr = 32767;
4991 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004992 }
4993
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004994 // For cdo and ldo commands, jump to the nth valid error.
4995 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02004996 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4997 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004998 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004999 eap->addr_count > 0 ? (int)eap->line1 : 1,
5000 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5001
5002 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003}
5004
5005/*
5006 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005007 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005008 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 */
5010 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005011ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005013 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005014 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005015 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005016
Bram Moolenaar39665952018-08-15 20:59:48 +02005017 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005018 {
5019 qi = GET_LOC_LIST(curwin);
5020 if (qi == NULL)
5021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005022 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005023 return;
5024 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005025 }
5026
Bram Moolenaar55b69262017-08-13 13:42:01 +02005027 if (eap->addr_count > 0
5028 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5029 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005030 errornr = (int)eap->line2;
5031 else
5032 errornr = 1;
5033
Bram Moolenaar39665952018-08-15 20:59:48 +02005034 // Depending on the command jump to either next or previous entry/file.
5035 switch (eap->cmdidx)
5036 {
5037 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5038 dir = FORWARD;
5039 break;
5040 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5041 case CMD_lNext:
5042 dir = BACKWARD;
5043 break;
5044 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5045 dir = FORWARD_FILE;
5046 break;
5047 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5048 dir = BACKWARD_FILE;
5049 break;
5050 default:
5051 dir = FORWARD;
5052 break;
5053 }
5054
5055 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056}
5057
5058/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005059 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005060 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 */
5062 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005063ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005065 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005066 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005067 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005068 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005069 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005070 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005071
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005072 switch (eap->cmdidx)
5073 {
5074 case CMD_cfile: au_name = (char_u *)"cfile"; break;
5075 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
5076 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
5077 case CMD_lfile: au_name = (char_u *)"lfile"; break;
5078 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
5079 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
5080 default: break;
5081 }
5082 if (au_name != NULL)
5083 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005084 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005085#ifdef FEAT_BROWSE
5086 if (cmdmod.browse)
5087 {
5088 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005089 NULL, NULL,
5090 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005091 if (browse_file == NULL)
5092 return;
5093 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5094 vim_free(browse_file);
5095 }
5096 else
5097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005099 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005100
Bram Moolenaar39665952018-08-15 20:59:48 +02005101 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005102 wp = curwin;
5103
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005104 incr_quickfix_busy();
5105
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005106 // This function is used by the :cfile, :cgetfile and :caddfile
5107 // commands.
5108 // :cfile always creates a new quickfix list and jumps to the
5109 // first error.
5110 // :cgetfile creates a new quickfix list but doesn't jump to the
5111 // first error.
5112 // :caddfile adds to an existing quickfix list. If there is no
5113 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005114 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005115 && eap->cmdidx != CMD_laddfile),
5116 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005117 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005118 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005119 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005120 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005121 {
5122 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005123 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005124 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005125 }
5126 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005127 qf_list_changed(qf_get_curlist(qi));
5128 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005129 if (au_name != NULL)
5130 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005131
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005132 // Jump to the first error for a new list and if autocmds didn't
5133 // free the list.
5134 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5135 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005136 // display the first error
5137 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005138
5139 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005140}
5141
5142/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005143 * Return the vimgrep autocmd name.
5144 */
5145 static char_u *
5146vgr_get_auname(cmdidx_T cmdidx)
5147{
5148 switch (cmdidx)
5149 {
5150 case CMD_vimgrep: return (char_u *)"vimgrep";
5151 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5152 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5153 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5154 case CMD_grep: return (char_u *)"grep";
5155 case CMD_lgrep: return (char_u *)"lgrep";
5156 case CMD_grepadd: return (char_u *)"grepadd";
5157 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5158 default: return NULL;
5159 }
5160}
5161
5162/*
5163 * Initialize the regmatch used by vimgrep for pattern "s".
5164 */
5165 static void
5166vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5167{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005168 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005169 regmatch->regprog = NULL;
5170
5171 if (s == NULL || *s == NUL)
5172 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005173 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005174 if (last_search_pat() == NULL)
5175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005176 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005177 return;
5178 }
5179 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5180 }
5181 else
5182 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5183
5184 regmatch->rmm_ic = p_ic;
5185 regmatch->rmm_maxcol = 0;
5186}
5187
5188/*
5189 * Display a file name when vimgrep is running.
5190 */
5191 static void
5192vgr_display_fname(char_u *fname)
5193{
5194 char_u *p;
5195
5196 msg_start();
5197 p = msg_strtrunc(fname, TRUE);
5198 if (p == NULL)
5199 msg_outtrans(fname);
5200 else
5201 {
5202 msg_outtrans(p);
5203 vim_free(p);
5204 }
5205 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005206 msg_didout = FALSE; // overwrite this message
5207 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005208 msg_col = 0;
5209 out_flush();
5210}
5211
5212/*
5213 * Load a dummy buffer to search for a pattern using vimgrep.
5214 */
5215 static buf_T *
5216vgr_load_dummy_buf(
5217 char_u *fname,
5218 char_u *dirname_start,
5219 char_u *dirname_now)
5220{
5221 int save_mls;
5222#if defined(FEAT_SYN_HL)
5223 char_u *save_ei = NULL;
5224#endif
5225 buf_T *buf;
5226
5227#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005228 // Don't do Filetype autocommands to avoid loading syntax and
5229 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005230 save_ei = au_event_disable(",Filetype");
5231#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005232 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005233 save_mls = p_mls;
5234 p_mls = 0;
5235
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005236 // Load file into a buffer, so that 'fileencoding' is detected,
5237 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005238 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5239
5240 p_mls = save_mls;
5241#if defined(FEAT_SYN_HL)
5242 au_event_restore(save_ei);
5243#endif
5244
5245 return buf;
5246}
5247
5248/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005249 * Check whether a quickfix/location list is valid. Autocmds may remove or
5250 * change a quickfix list when vimgrep is running. If the list is not found,
5251 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005252 */
5253 static int
5254vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005255 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005256 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005257 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005258 char_u *title)
5259{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005260 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005261 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005262 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005263 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005264 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005265 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005266 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005267 return FALSE;
5268 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005269 else
5270 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005271 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005272 qf_new_list(qi, title);
5273 return TRUE;
5274 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005275 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005276
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005277 if (qf_restore_list(qi, qfid) == FAIL)
5278 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005279
5280 return TRUE;
5281}
5282
5283/*
5284 * Search for a pattern in all the lines in a buffer and add the matching lines
5285 * to a quickfix list.
5286 */
5287 static int
5288vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005289 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005290 char_u *fname,
5291 buf_T *buf,
5292 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005293 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005294 int duplicate_name,
5295 int flags)
5296{
5297 int found_match = FALSE;
5298 long lnum;
5299 colnr_T col;
5300
Bram Moolenaar1c299432018-10-28 14:36:09 +01005301 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005302 {
5303 col = 0;
5304 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5305 col, NULL, NULL) > 0)
5306 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005307 // Pass the buffer number so that it gets used even for a
5308 // dummy buffer, unless duplicate_name is set, then the
5309 // buffer will be wiped out below.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005310 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005311 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005312 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005313 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005314 duplicate_name ? 0 : buf->b_fnum,
5315 ml_get_buf(buf,
5316 regmatch->startpos[0].lnum + lnum, FALSE),
5317 regmatch->startpos[0].lnum + lnum,
5318 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005319 FALSE, // vis_col
5320 NULL, // search pattern
5321 0, // nr
5322 0, // type
5323 TRUE // valid
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005324 ) == FAIL)
5325 {
5326 got_int = TRUE;
5327 break;
5328 }
5329 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005330 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005331 break;
5332 if ((flags & VGR_GLOBAL) == 0
5333 || regmatch->endpos[0].lnum > 0)
5334 break;
5335 col = regmatch->endpos[0].col
5336 + (col == regmatch->endpos[0].col);
5337 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5338 break;
5339 }
5340 line_breakcheck();
5341 if (got_int)
5342 break;
5343 }
5344
5345 return found_match;
5346}
5347
5348/*
5349 * Jump to the first match and update the directory.
5350 */
5351 static void
5352vgr_jump_to_match(
5353 qf_info_T *qi,
5354 int forceit,
5355 int *redraw_for_dummy,
5356 buf_T *first_match_buf,
5357 char_u *target_dir)
5358{
5359 buf_T *buf;
5360
5361 buf = curbuf;
5362 qf_jump(qi, 0, 0, forceit);
5363 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005364 // If we jumped to another buffer redrawing will already be
5365 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005366 *redraw_for_dummy = FALSE;
5367
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005368 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005369 if (curbuf == first_match_buf && target_dir != NULL)
5370 {
5371 exarg_T ea;
5372
5373 ea.arg = target_dir;
5374 ea.cmdidx = CMD_lcd;
5375 ex_cd(&ea);
5376 }
5377}
5378
5379/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005380 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005381 * ":vimgrepadd {pattern} file(s)"
5382 * ":lvimgrep {pattern} file(s)"
5383 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005384 */
5385 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005386ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005387{
Bram Moolenaar81695252004-12-29 20:58:21 +00005388 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005389 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005390 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005391 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005392 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005393 char_u *s;
5394 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005395 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005396 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005397 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005398 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005399 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005400 buf_T *buf;
5401 int duplicate_name = FALSE;
5402 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005403 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005404 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005405 buf_T *first_match_buf = NULL;
5406 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005407 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005408 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005409 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005410 char_u *dirname_start = NULL;
5411 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005412 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005413 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005414
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005415 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005416 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5417 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005418 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005419#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005420 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005421 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005422#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005423 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005424
Bram Moolenaar39665952018-08-15 20:59:48 +02005425 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005426 {
5427 qi = ll_get_or_alloc_list(curwin);
5428 if (qi == NULL)
5429 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005430 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005431 }
5432
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005433 if (eap->addr_count > 0)
5434 tomatch = eap->line2;
5435 else
5436 tomatch = MAXLNUM;
5437
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005438 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005439 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005440 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005441 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005442 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005443 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005444 emsg(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005445 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005446 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005447
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005448 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005449 if (regmatch.regprog == NULL)
5450 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005451
5452 p = skipwhite(p);
5453 if (*p == NUL)
5454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005455 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005456 goto theend;
5457 }
5458
Bram Moolenaar55b69262017-08-13 13:42:01 +02005459 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005460 && eap->cmdidx != CMD_vimgrepadd
5461 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005462 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005463 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005464 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005465
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005466 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005467 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005468 goto theend;
5469 if (fcount == 0)
5470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005471 emsg(_(e_nomatch));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005472 goto theend;
5473 }
5474
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005475 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5476 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005477 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005478 {
5479 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005480 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005481 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005482
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005483 // Remember the current directory, because a BufRead autocommand that does
5484 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005485 mch_dirname(dirname_start, MAXPATHL);
5486
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005487 incr_quickfix_busy();
5488
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005489 // Remember the current quickfix list identifier, so that we can check for
5490 // autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005491 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005492
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005493 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005494 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005495 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005496 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005497 if (time(NULL) > seconds)
5498 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005499 // Display the file name every second or so, show the user we are
5500 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005501 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005502 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005503 }
5504
Bram Moolenaar81695252004-12-29 20:58:21 +00005505 buf = buflist_findname_exp(fnames[fi]);
5506 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5507 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005508 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005509 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005510 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005511 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005512
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005513 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005514 }
5515 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005516 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005517 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005518
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005519 // Check whether the quickfix list is still valid. When loading a
5520 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005521 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005522 {
5523 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005524 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005525 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005526 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005527 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005528
Bram Moolenaar81695252004-12-29 20:58:21 +00005529 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005530 {
5531 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005532 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005533 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005534 else
5535 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005536 // Try for a match in all lines of the buffer.
5537 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005538 found_match = vgr_match_buflines(qf_get_curlist(qi),
5539 fname, buf, &regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005540 &tomatch, duplicate_name, flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005541
Bram Moolenaar81695252004-12-29 20:58:21 +00005542 if (using_dummy)
5543 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005544 if (found_match && first_match_buf == NULL)
5545 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005546 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005547 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005548 // Never keep a dummy buffer if there is another buffer
5549 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005550 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005551 buf = NULL;
5552 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005553 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005554 || buf->b_p_bh[0] == 'u' // "unload"
5555 || buf->b_p_bh[0] == 'w' // "wipe"
5556 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005557 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005558 // When no match was found we don't need to remember the
5559 // buffer, wipe it out. If there was a match and it
5560 // wasn't the first one or we won't jump there: only
5561 // unload the buffer.
5562 // Ignore 'hidden' here, because it may lead to having too
5563 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005564 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005565 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005566 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005567 buf = NULL;
5568 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005569 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005570 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005571 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005572 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005573 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005574 buf = NULL;
5575 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005576 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005577
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005578 if (buf != NULL)
5579 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005580 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005581 buf->b_flags &= ~BF_DUMMY;
5582
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005583 // If the buffer is still loaded we need to use the
5584 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005585 if (buf == first_match_buf
5586 && target_dir == NULL
5587 && STRCMP(dirname_start, dirname_now) != 0)
5588 target_dir = vim_strsave(dirname_now);
5589
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005590 // The buffer is still loaded, the Filetype autocommands
5591 // need to be done now, in that buffer. And the modelines
5592 // need to be done (again). But not the window-local
5593 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005594 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005595#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005596 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5597 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005598#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005599 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005600 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005601 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005602 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005603 }
5604 }
5605
5606 FreeWild(fcount, fnames);
5607
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005608 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005609 qfl->qf_nonevalid = FALSE;
5610 qfl->qf_ptr = qfl->qf_start;
5611 qfl->qf_index = 1;
5612 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005613
Bram Moolenaar864293a2016-06-02 13:40:04 +02005614 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005615
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005616 if (au_name != NULL)
5617 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5618 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005619 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5620 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005621 if (!qflist_valid(wp, save_qfid)
5622 || qf_restore_list(qi, save_qfid) == FAIL)
5623 {
5624 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005625 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005626 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005627
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005628 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01005629 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005630 {
5631 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005632 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5633 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005634 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005635 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005636 semsg(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005637
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005638 decr_quickfix_busy();
5639
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005640 // If we loaded a dummy buffer into the current window, the autocommands
5641 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005642 if (redraw_for_dummy)
5643 {
5644#ifdef FEAT_FOLDING
5645 foldUpdateAll(curwin);
5646#else
5647 redraw_later(NOT_VALID);
5648#endif
5649 }
5650
Bram Moolenaar86b68352004-12-27 21:59:20 +00005651theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005652 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005653 vim_free(dirname_now);
5654 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005655 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005656 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005657}
5658
5659/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005660 * Restore current working directory to "dirname_start" if they differ, taking
5661 * into account whether it is set locally or globally.
5662 */
5663 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005664restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005665{
5666 char_u *dirname_now = alloc(MAXPATHL);
5667
5668 if (NULL != dirname_now)
5669 {
5670 mch_dirname(dirname_now, MAXPATHL);
5671 if (STRCMP(dirname_start, dirname_now) != 0)
5672 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005673 // If the directory has changed, change it back by building up an
5674 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005675 exarg_T ea;
5676
5677 ea.arg = dirname_start;
5678 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5679 ex_cd(&ea);
5680 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005681 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005682 }
5683}
5684
5685/*
5686 * Load file "fname" into a dummy buffer and return the buffer pointer,
5687 * placing the directory resulting from the buffer load into the
5688 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5689 * prior to calling this function. Restores directory to "dirname_start" prior
5690 * to returning, if autocmds or the 'autochdir' option have changed it.
5691 *
5692 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5693 * or wipe_dummy_buffer() later!
5694 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005695 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005696 */
5697 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005698load_dummy_buffer(
5699 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005700 char_u *dirname_start, // in: old directory
5701 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005702{
5703 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005704 bufref_T newbufref;
5705 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005706 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005707 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005708 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005709
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005710 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005711 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5712 if (newbuf == NULL)
5713 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005714 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005715
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005716 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005717 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5718
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005719 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005720 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005721 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005722 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005723 ++newbuf->b_locked;
5724
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005725 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005726 aucmd_prepbuf(&aco, newbuf);
5727
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005728 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005729 (void)setfname(curbuf, fname, NULL, FALSE);
5730
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005731 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005732 check_need_swap(TRUE);
5733
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005734 // Remove the "dummy" flag, otherwise autocommands may not
5735 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005736 curbuf->b_flags &= ~BF_DUMMY;
5737
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005738 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005739 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005740 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005741 NULL, READ_NEW | READ_DUMMY);
5742 --newbuf->b_locked;
5743 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005744 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005745 && !(curbuf->b_flags & BF_NEW))
5746 {
5747 failed = FALSE;
5748 if (curbuf != newbuf)
5749 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005750 // Bloody autocommands changed the buffer! Can happen when
5751 // using netrw and editing a remote file. Use the current
5752 // buffer instead, delete the dummy one after restoring the
5753 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005754 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005755 newbuf = curbuf;
5756 }
5757 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005758
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005759 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005760 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005761 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5762 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005763
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005764 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5765 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005766 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005767 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005768
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005769 // When autocommands/'autochdir' option changed directory: go back.
5770 // Let the caller know what the resulting dir was first, in case it is
5771 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005772 mch_dirname(resulting_dir, MAXPATHL);
5773 restore_start_dir(dirname_start);
5774
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005775 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005776 return NULL;
5777 if (failed)
5778 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005779 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005780 return NULL;
5781 }
5782 return newbuf;
5783}
5784
5785/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005786 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5787 * directory to "dirname_start" prior to returning, if autocmds or the
5788 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005789 */
5790 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005791wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005792{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005793 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005794 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005795#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005796 cleanup_T cs;
5797
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005798 // Reset the error/interrupt/exception state here so that aborting()
5799 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5800 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005801 enter_cleanup(&cs);
5802#endif
5803
Bram Moolenaar81695252004-12-29 20:58:21 +00005804 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005805
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005806#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005807 // Restore the error/interrupt/exception state if not discarded by a
5808 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005809 leave_cleanup(&cs);
5810#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005811 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005812 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005813 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005814}
5815
5816/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005817 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5818 * directory to "dirname_start" prior to returning, if autocmds or the
5819 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005820 */
5821 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005822unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005823{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005824 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005825 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005826 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005827
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005828 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005829 restore_start_dir(dirname_start);
5830 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005831}
5832
Bram Moolenaar05159a02005-02-26 23:04:13 +00005833#if defined(FEAT_EVAL) || defined(PROTO)
5834/*
5835 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005836 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005837 */
5838 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005839get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005840{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005841 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01005842 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005843 dict_T *dict;
5844 char_u buf[2];
5845 qfline_T *qfp;
5846 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005847 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005848
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005849 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005850 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005851 qi = &ql_info;
5852 if (wp != NULL)
5853 {
5854 qi = GET_LOC_LIST(wp);
5855 if (qi == NULL)
5856 return FAIL;
5857 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005858 }
5859
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005860 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005861 qf_idx = qi->qf_curlist;
5862
Bram Moolenaar0398e002019-03-21 21:12:49 +01005863 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005864 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005865
Bram Moolenaar0398e002019-03-21 21:12:49 +01005866 qfl = qf_get_list(qi, qf_idx);
5867 if (qf_list_empty(qfl))
5868 return FAIL;
5869
5870 qfp = qfl->qf_start;
5871 for (i = 1; !got_int && i <= qfl->qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005872 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005873 // Handle entries with a non-existing buffer number.
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005874 bufnum = qfp->qf_fnum;
5875 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5876 bufnum = 0;
5877
Bram Moolenaar05159a02005-02-26 23:04:13 +00005878 if ((dict = dict_alloc()) == NULL)
5879 return FAIL;
5880 if (list_append_dict(list, dict) == FAIL)
5881 return FAIL;
5882
5883 buf[0] = qfp->qf_type;
5884 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005885 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5886 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5887 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5888 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5889 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5890 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5891 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5892 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5893 || dict_add_string(dict, "type", buf) == FAIL
5894 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005895 return FAIL;
5896
5897 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005898 if (qfp == NULL)
5899 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005900 }
5901 return OK;
5902}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005903
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005904// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005905enum {
5906 QF_GETLIST_NONE = 0x0,
5907 QF_GETLIST_TITLE = 0x1,
5908 QF_GETLIST_ITEMS = 0x2,
5909 QF_GETLIST_NR = 0x4,
5910 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005911 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005912 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005913 QF_GETLIST_IDX = 0x40,
5914 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005915 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005916 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005917 QF_GETLIST_QFBUFNR = 0x400,
5918 QF_GETLIST_ALL = 0x7FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005919};
5920
5921/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005922 * Parse text from 'di' and return the quickfix list items.
5923 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005924 */
5925 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005926qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005927{
5928 int status = FAIL;
5929 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005930 char_u *errorformat = p_efm;
5931 dictitem_T *efm_di;
5932 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005933
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005934 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005935 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005936 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005937 // If errorformat is supplied then use it, otherwise use the 'efm'
5938 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005939 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5940 {
5941 if (efm_di->di_tv.v_type != VAR_STRING ||
5942 efm_di->di_tv.vval.v_string == NULL)
5943 return FAIL;
5944 errorformat = efm_di->di_tv.vval.v_string;
5945 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005946
Bram Moolenaar36538222017-09-02 19:51:44 +02005947 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005948 if (l == NULL)
5949 return FAIL;
5950
Bram Moolenaar2d67d302018-11-16 18:46:02 +01005951 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005952 if (qi != NULL)
5953 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005954 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005955 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5956 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005957 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005958 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005959 }
5960 free(qi);
5961 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005962 dict_add_list(retdict, "items", l);
5963 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005964 }
5965
5966 return status;
5967}
5968
5969/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005970 * Return the quickfix/location list window identifier in the current tabpage.
5971 */
5972 static int
5973qf_winid(qf_info_T *qi)
5974{
5975 win_T *win;
5976
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005977 // The quickfix window can be opened even if the quickfix list is not set
5978 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005979 if (qi == NULL)
5980 return 0;
5981 win = qf_find_win(qi);
5982 if (win != NULL)
5983 return win->w_id;
5984 return 0;
5985}
5986
5987/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005988 * Returns the number of the buffer displayed in the quickfix/location list
5989 * window. If there is no buffer associated with the list, then returns 0.
5990 */
5991 static int
5992qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
5993{
5994 return dict_add_number(retdict, "qfbufnr",
5995 (qi == NULL) ? 0 : qi->qf_bufnr);
5996}
5997
5998/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005999 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006000 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006001 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006002qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006003{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006004 int flags = QF_GETLIST_NONE;
6005
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006006 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006007 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006008 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006009 if (!loclist)
6010 // File window ID is applicable only to location list windows
6011 flags &= ~ QF_GETLIST_FILEWINID;
6012 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006013
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006014 if (dict_find(what, (char_u *)"title", -1) != NULL)
6015 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006016
Bram Moolenaara6d48492017-12-12 22:45:31 +01006017 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6018 flags |= QF_GETLIST_NR;
6019
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006020 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6021 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006022
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006023 if (dict_find(what, (char_u *)"context", -1) != NULL)
6024 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006025
Bram Moolenaara6d48492017-12-12 22:45:31 +01006026 if (dict_find(what, (char_u *)"id", -1) != NULL)
6027 flags |= QF_GETLIST_ID;
6028
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006029 if (dict_find(what, (char_u *)"items", -1) != NULL)
6030 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006031
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006032 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6033 flags |= QF_GETLIST_IDX;
6034
6035 if (dict_find(what, (char_u *)"size", -1) != NULL)
6036 flags |= QF_GETLIST_SIZE;
6037
Bram Moolenaarb254af32017-12-18 19:48:58 +01006038 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6039 flags |= QF_GETLIST_TICK;
6040
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006041 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6042 flags |= QF_GETLIST_FILEWINID;
6043
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006044 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6045 flags |= QF_GETLIST_QFBUFNR;
6046
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006047 return flags;
6048}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006049
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006050/*
6051 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6052 * If 'nr' and 'id' are not present in 'what' then return the current
6053 * quickfix list index.
6054 * If 'nr' is zero then return the current quickfix list index.
6055 * If 'nr' is '$' then return the last quickfix list index.
6056 * If 'id' is present then return the index of the quickfix list with that id.
6057 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6058 * Return -1, if quickfix list is not present or if the stack is empty.
6059 */
6060 static int
6061qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6062{
6063 int qf_idx;
6064 dictitem_T *di;
6065
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006066 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006067 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6068 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006069 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006070 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006071 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006072 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006073 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006074 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006075 qf_idx = di->di_tv.vval.v_number - 1;
6076 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006077 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006078 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006079 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006080 else if (di->di_tv.v_type == VAR_STRING
6081 && di->di_tv.vval.v_string != NULL
6082 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006083 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006084 qf_idx = qi->qf_listcount - 1;
6085 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006086 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006087 }
6088
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006089 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6090 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006091 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006092 if (di->di_tv.v_type == VAR_NUMBER)
6093 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006094 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006095 if (di->di_tv.vval.v_number != 0)
6096 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6097 }
6098 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006099 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006100 }
6101
6102 return qf_idx;
6103}
6104
6105/*
6106 * Return default values for quickfix list properties in retdict.
6107 */
6108 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006109qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006110{
6111 int status = OK;
6112
6113 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006114 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006115 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6116 {
6117 list_T *l = list_alloc();
6118 if (l != NULL)
6119 status = dict_add_list(retdict, "items", l);
6120 else
6121 status = FAIL;
6122 }
6123 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006124 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006125 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006126 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006127 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006128 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006129 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006130 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006131 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006132 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006133 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006134 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006135 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006136 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006137 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006138 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006139 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6140 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006141
6142 return status;
6143}
6144
6145/*
6146 * Return the quickfix list title as 'title' in retdict
6147 */
6148 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006149qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006150{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006151 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006152}
6153
6154/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006155 * Returns the identifier of the window used to display files from a location
6156 * list. If there is no associated window, then returns 0. Useful only when
6157 * called from a location list window.
6158 */
6159 static int
6160qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6161{
6162 int winid = 0;
6163
6164 if (wp != NULL && IS_LL_WINDOW(wp))
6165 {
6166 win_T *ll_wp = qf_find_win_with_loclist(qi);
6167 if (ll_wp != NULL)
6168 winid = ll_wp->w_id;
6169 }
6170
6171 return dict_add_number(retdict, "filewinid", winid);
6172}
6173
6174/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006175 * Return the quickfix list items/entries as 'items' in retdict
6176 */
6177 static int
6178qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6179{
6180 int status = OK;
6181 list_T *l = list_alloc();
6182 if (l != NULL)
6183 {
6184 (void)get_errorlist(qi, NULL, qf_idx, l);
6185 dict_add_list(retdict, "items", l);
6186 }
6187 else
6188 status = FAIL;
6189
6190 return status;
6191}
6192
6193/*
6194 * Return the quickfix list context (if any) as 'context' in retdict.
6195 */
6196 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006197qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006198{
6199 int status;
6200 dictitem_T *di;
6201
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006202 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006203 {
6204 di = dictitem_alloc((char_u *)"context");
6205 if (di != NULL)
6206 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006207 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006208 status = dict_add(retdict, di);
6209 if (status == FAIL)
6210 dictitem_free(di);
6211 }
6212 else
6213 status = FAIL;
6214 }
6215 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006216 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006217
6218 return status;
6219}
6220
6221/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006222 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006223 */
6224 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01006225qf_getprop_idx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006226{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006227 int curidx = qfl->qf_index;
6228 if (qf_list_empty(qfl))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006229 // For empty lists, current index is set to 0
6230 curidx = 0;
6231 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006232}
6233
6234/*
6235 * Return quickfix/location list details (title) as a
6236 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6237 * then current list is used. Otherwise the specified list is used.
6238 */
6239 int
6240qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6241{
6242 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006243 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006244 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006245 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006246 dictitem_T *di;
6247 int flags = QF_GETLIST_NONE;
6248
6249 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6250 return qf_get_list_from_lines(what, di, retdict);
6251
6252 if (wp != NULL)
6253 qi = GET_LOC_LIST(wp);
6254
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006255 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006256
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006257 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006258 qf_idx = qf_getprop_qfidx(qi, what);
6259
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006260 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006261 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006262 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006263
Bram Moolenaar0398e002019-03-21 21:12:49 +01006264 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006265
Bram Moolenaard823fa92016-08-12 16:29:27 +02006266 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006267 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006268 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006269 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006270 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006271 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006272 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006273 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006274 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006275 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006276 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006277 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006278 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar0398e002019-03-21 21:12:49 +01006279 status = qf_getprop_idx(qfl, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006280 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006281 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006282 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006283 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006284 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6285 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006286 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6287 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006288
Bram Moolenaard823fa92016-08-12 16:29:27 +02006289 return status;
6290}
6291
6292/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006293 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006294 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6295 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006296 */
6297 static int
6298qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01006299 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006300 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006301 int first_entry,
6302 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006303{
6304 static int did_bufnr_emsg;
6305 char_u *filename, *module, *pattern, *text, *type;
6306 int bufnum, valid, status, col, vcol, nr;
6307 long lnum;
6308
6309 if (first_entry)
6310 did_bufnr_emsg = FALSE;
6311
Bram Moolenaar8f667172018-12-14 15:38:31 +01006312 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6313 module = dict_get_string(d, (char_u *)"module", TRUE);
6314 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6315 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6316 col = (int)dict_get_number(d, (char_u *)"col");
6317 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6318 nr = (int)dict_get_number(d, (char_u *)"nr");
6319 type = dict_get_string(d, (char_u *)"type", TRUE);
6320 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6321 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006322 if (text == NULL)
6323 text = vim_strsave((char_u *)"");
6324
6325 valid = TRUE;
6326 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6327 valid = FALSE;
6328
6329 // Mark entries with non-existing buffer number as not valid. Give the
6330 // error message only once.
6331 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6332 {
6333 if (!did_bufnr_emsg)
6334 {
6335 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006336 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006337 }
6338 valid = FALSE;
6339 bufnum = 0;
6340 }
6341
6342 // If the 'valid' field is present it overrules the detected value.
6343 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006344 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006345
Bram Moolenaar0398e002019-03-21 21:12:49 +01006346 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006347 NULL, // dir
6348 filename,
6349 module,
6350 bufnum,
6351 text,
6352 lnum,
6353 col,
6354 vcol, // vis_col
6355 pattern, // search pattern
6356 nr,
6357 type == NULL ? NUL : *type,
6358 valid);
6359
6360 vim_free(filename);
6361 vim_free(module);
6362 vim_free(pattern);
6363 vim_free(text);
6364 vim_free(type);
6365
Bram Moolenaar9752c722018-12-22 16:49:34 +01006366 if (valid)
6367 *valid_entry = TRUE;
6368
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006369 return status;
6370}
6371
6372/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006373 * Add list of entries to quickfix/location list. Each list entry is
6374 * a dictionary with item information.
6375 */
6376 static int
6377qf_add_entries(
6378 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006379 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006380 list_T *list,
6381 char_u *title,
6382 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006383{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006384 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006385 listitem_T *li;
6386 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006387 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006388 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006389 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006390
Bram Moolenaara3921f42017-06-04 15:30:34 +02006391 if (action == ' ' || qf_idx == qi->qf_listcount)
6392 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006393 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006394 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006395 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006396 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006397 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01006398 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006399 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006400 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006401 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006402 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006403 qf_free_items(qfl);
6404 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006405 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006406
6407 for (li = list->lv_first; li != NULL; li = li->li_next)
6408 {
6409 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006410 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006411
6412 d = li->li_tv.vval.v_dict;
6413 if (d == NULL)
6414 continue;
6415
Bram Moolenaar0398e002019-03-21 21:12:49 +01006416 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006417 &valid_entry);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006418 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006419 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006420 }
6421
Bram Moolenaar9752c722018-12-22 16:49:34 +01006422 // Check if any valid error entries are added to the list.
6423 if (valid_entry)
6424 qfl->qf_nonevalid = FALSE;
6425 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006426 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006427 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006428
6429 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006430 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006431 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006432
6433 // Update the current error index if not appending to the list or if the
6434 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006435 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01006436 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006437
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006438 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006439 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006440
6441 return retval;
6442}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006443
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006444/*
6445 * Get the quickfix list index from 'nr' or 'id'
6446 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006447 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006448qf_setprop_get_qfidx(
6449 qf_info_T *qi,
6450 dict_T *what,
6451 int action,
6452 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006453{
6454 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006455 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006456
Bram Moolenaard823fa92016-08-12 16:29:27 +02006457 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6458 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006459 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006460 if (di->di_tv.v_type == VAR_NUMBER)
6461 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006462 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006463 if (di->di_tv.vval.v_number != 0)
6464 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006465
Bram Moolenaar55b69262017-08-13 13:42:01 +02006466 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6467 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006468 // When creating a new list, accept qf_idx pointing to the next
6469 // non-available list and add the new list at the end of the
6470 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006471 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006472 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006473 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006474 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006475 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006476 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006477 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006478 }
6479 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006480 && di->di_tv.vval.v_string != NULL
6481 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006482 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006483 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006484 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006485 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006486 qf_idx = 0;
6487 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006488 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006489 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006490 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006491 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006492 }
6493
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006494 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006495 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006496 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006497 if (di->di_tv.v_type != VAR_NUMBER)
6498 return INVALID_QFIDX;
6499
6500 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006501 }
6502
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006503 return qf_idx;
6504}
6505
6506/*
6507 * Set the quickfix list title.
6508 */
6509 static int
6510qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6511{
Bram Moolenaar0398e002019-03-21 21:12:49 +01006512 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006513
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006514 if (di->di_tv.v_type != VAR_STRING)
6515 return FAIL;
6516
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006517 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01006518 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006519 if (qf_idx == qi->qf_curlist)
6520 qf_update_win_titlevar(qi);
6521
6522 return OK;
6523}
6524
6525/*
6526 * Set quickfix list items/entries.
6527 */
6528 static int
6529qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6530{
6531 int retval = FAIL;
6532 char_u *title_save;
6533
6534 if (di->di_tv.v_type != VAR_LIST)
6535 return FAIL;
6536
6537 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6538 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6539 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006540 vim_free(title_save);
6541
6542 return retval;
6543}
6544
6545/*
6546 * Set quickfix list items/entries from a list of lines.
6547 */
6548 static int
6549qf_setprop_items_from_lines(
6550 qf_info_T *qi,
6551 int qf_idx,
6552 dict_T *what,
6553 dictitem_T *di,
6554 int action)
6555{
6556 char_u *errorformat = p_efm;
6557 dictitem_T *efm_di;
6558 int retval = FAIL;
6559
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006560 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006561 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6562 {
6563 if (efm_di->di_tv.v_type != VAR_STRING ||
6564 efm_di->di_tv.vval.v_string == NULL)
6565 return FAIL;
6566 errorformat = efm_di->di_tv.vval.v_string;
6567 }
6568
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006569 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006570 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6571 return FAIL;
6572
6573 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006574 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006575 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6576 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6577 retval = OK;
6578
6579 return retval;
6580}
6581
6582/*
6583 * Set quickfix list context.
6584 */
6585 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006586qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006587{
6588 typval_T *ctx;
6589
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006590 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006591 ctx = alloc_tv();
6592 if (ctx != NULL)
6593 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006594 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006595
6596 return OK;
6597}
6598
6599/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006600 * Set the current index in the specified quickfix list
6601 */
6602 static int
6603qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
6604{
6605 int denote = FALSE;
6606 int newidx;
6607 int old_qfidx;
6608 qfline_T *qf_ptr;
6609
6610 // If the specified index is '$', then use the last entry
6611 if (di->di_tv.v_type == VAR_STRING
6612 && di->di_tv.vval.v_string != NULL
6613 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
6614 newidx = qfl->qf_count;
6615 else
6616 {
6617 // Otherwise use the specified index
6618 newidx = tv_get_number_chk(&di->di_tv, &denote);
6619 if (denote)
6620 return FAIL;
6621 }
6622
6623 if (newidx < 1) // sanity check
6624 return FAIL;
6625 if (newidx > qfl->qf_count)
6626 newidx = qfl->qf_count;
6627
6628 old_qfidx = qfl->qf_index;
6629 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
6630 if (qf_ptr == NULL)
6631 return FAIL;
6632 qfl->qf_ptr = qf_ptr;
6633 qfl->qf_index = newidx;
6634
6635 // If the current list is modified and it is displayed in the quickfix
6636 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006637 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006638 qf_win_pos_update(qi, old_qfidx);
6639
6640 return OK;
6641}
6642
6643/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006644 * Set quickfix/location list properties (title, items, context).
6645 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006646 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006647 */
6648 static int
6649qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6650{
6651 dictitem_T *di;
6652 int retval = FAIL;
6653 int qf_idx;
6654 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006655 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006656
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006657 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006658 newlist = TRUE;
6659
6660 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006661 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006662 return FAIL;
6663
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006664 if (newlist)
6665 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006666 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006667 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006668 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006669 }
6670
Bram Moolenaar0398e002019-03-21 21:12:49 +01006671 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006672 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006673 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006674 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006675 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006676 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006677 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006678 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006679 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006680 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6681 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006682
Bram Moolenaarb254af32017-12-18 19:48:58 +01006683 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006684 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006685
Bram Moolenaard823fa92016-08-12 16:29:27 +02006686 return retval;
6687}
6688
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006689/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006690 * Free the entire quickfix/location list stack.
6691 * If the quickfix/location list window is open, then clear it.
6692 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006693 static void
6694qf_free_stack(win_T *wp, qf_info_T *qi)
6695{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006696 win_T *qfwin = qf_find_win(qi);
6697 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006698
6699 if (qfwin != NULL)
6700 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006701 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006702 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006703 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006704 qf_update_buffer(qi, NULL);
6705 }
6706
6707 if (wp != NULL && IS_LL_WINDOW(wp))
6708 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006709 // If in the location list window, then use the non-location list
6710 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006711 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006712 if (llwin != NULL)
6713 wp = llwin;
6714 }
6715
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006716 qf_free_all(wp);
6717 if (wp == NULL)
6718 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006719 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006720 qi->qf_curlist = 0;
6721 qi->qf_listcount = 0;
6722 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006723 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006724 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006725 // If the location list window is open, then create a new empty
6726 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006727 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006728 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006729
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006730 // first free the list reference in the location list window
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006731 ll_free_all(&qfwin->w_llist_ref);
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006732
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006733 qfwin->w_llist_ref = new_ll;
6734 if (wp != qfwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006735 win_set_loclist(wp, new_ll);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006736 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006737}
6738
Bram Moolenaard823fa92016-08-12 16:29:27 +02006739/*
6740 * Populate the quickfix list with the items supplied in the list
6741 * of dictionaries. "title" will be copied to w:quickfix_title.
6742 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6743 */
6744 int
6745set_errorlist(
6746 win_T *wp,
6747 list_T *list,
6748 int action,
6749 char_u *title,
6750 dict_T *what)
6751{
6752 qf_info_T *qi = &ql_info;
6753 int retval = OK;
6754
6755 if (wp != NULL)
6756 {
6757 qi = ll_get_or_alloc_list(wp);
6758 if (qi == NULL)
6759 return FAIL;
6760 }
6761
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006762 if (action == 'f')
6763 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006764 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006765 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006766 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006767 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006768
6769 incr_quickfix_busy();
6770
6771 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006772 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006773 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006774 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006775 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006776 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006777 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01006778 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006779
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006780 decr_quickfix_busy();
6781
Bram Moolenaard823fa92016-08-12 16:29:27 +02006782 return retval;
6783}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006784
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006785/*
6786 * Mark the context as in use for all the lists in a quickfix stack.
6787 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006788 static int
6789mark_quickfix_ctx(qf_info_T *qi, int copyID)
6790{
6791 int i;
6792 int abort = FALSE;
6793 typval_T *ctx;
6794
6795 for (i = 0; i < LISTCOUNT && !abort; ++i)
6796 {
6797 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006798 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6799 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006800 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6801 }
6802
6803 return abort;
6804}
6805
6806/*
6807 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006808 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006809 */
6810 int
6811set_ref_in_quickfix(int copyID)
6812{
6813 int abort = FALSE;
6814 tabpage_T *tp;
6815 win_T *win;
6816
6817 abort = mark_quickfix_ctx(&ql_info, copyID);
6818 if (abort)
6819 return abort;
6820
6821 FOR_ALL_TAB_WINDOWS(tp, win)
6822 {
6823 if (win->w_llist != NULL)
6824 {
6825 abort = mark_quickfix_ctx(win->w_llist, copyID);
6826 if (abort)
6827 return abort;
6828 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006829 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6830 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006831 // In a location list window and none of the other windows is
6832 // referring to this location list. Mark the location list
6833 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006834 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6835 if (abort)
6836 return abort;
6837 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006838 }
6839
6840 return abort;
6841}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006842#endif
6843
Bram Moolenaar81695252004-12-29 20:58:21 +00006844/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006845 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006846 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006847 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006848 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006849 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006850 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006851 */
6852 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006853ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006854{
6855 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006856 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006857 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006858 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006859 int_u save_qfid;
6860 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006861
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006862 switch (eap->cmdidx)
6863 {
6864 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6865 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6866 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6867 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6868 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6869 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6870 default: break;
6871 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006872 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6873 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006874 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006875#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006876 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006877 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006878#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006879 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006880
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006881 // Must come after autocommands.
Bram Moolenaar39665952018-08-15 20:59:48 +02006882 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006883 {
6884 qi = ll_get_or_alloc_list(curwin);
6885 if (qi == NULL)
6886 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006887 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006888 }
6889
Bram Moolenaar86b68352004-12-27 21:59:20 +00006890 if (*eap->arg == NUL)
6891 buf = curbuf;
6892 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6893 buf = buflist_findnr(atoi((char *)eap->arg));
6894 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006895 emsg(_(e_invarg));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006896 else if (buf->b_ml.ml_mfp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006897 emsg(_("E681: Buffer is not loaded"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006898 else
6899 {
6900 if (eap->addr_count == 0)
6901 {
6902 eap->line1 = 1;
6903 eap->line2 = buf->b_ml.ml_line_count;
6904 }
6905 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6906 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006907 emsg(_(e_invrange));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006908 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006909 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006910 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006911
6912 if (buf->b_sfname)
6913 {
6914 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6915 (char *)qf_title, (char *)buf->b_sfname);
6916 qf_title = IObuff;
6917 }
6918
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006919 incr_quickfix_busy();
6920
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006921 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006922 (eap->cmdidx != CMD_caddbuffer
6923 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006924 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006925 qf_title, NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006926 if (qf_stack_empty(qi))
6927 {
6928 decr_quickfix_busy();
6929 return;
6930 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006931 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006932 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006933
6934 // Remember the current quickfix list identifier, so that we can
6935 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006936 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006937 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006938 {
6939 buf_T *curbuf_old = curbuf;
6940
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006941 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6942 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006943 if (curbuf != curbuf_old)
6944 // Autocommands changed buffer, don't jump now, "qi" may
6945 // be invalid.
6946 res = 0;
6947 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006948 // Jump to the first error for a new list and if autocmds didn't
6949 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006950 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006951 eap->cmdidx == CMD_lbuffer)
6952 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006953 // display the first error
6954 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006955
6956 decr_quickfix_busy();
Bram Moolenaar754b5602006-02-09 23:53:20 +00006957 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006958 }
6959}
6960
Bram Moolenaar1e015462005-09-25 22:16:38 +00006961#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006962/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006963 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6964 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006965 */
6966 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006967ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006968{
6969 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006970 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006971 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006972 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006973 int_u save_qfid;
6974 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006975
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006976 switch (eap->cmdidx)
6977 {
6978 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6979 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6980 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6981 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6982 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6983 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6984 default: break;
6985 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006986 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6987 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006988 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006989#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006990 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006991 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006992#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006993 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006994
Bram Moolenaar39665952018-08-15 20:59:48 +02006995 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006996 {
6997 qi = ll_get_or_alloc_list(curwin);
6998 if (qi == NULL)
6999 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007000 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007001 }
7002
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007003 // Evaluate the expression. When the result is a string or a list we can
7004 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007005 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007006 if (tv != NULL)
7007 {
7008 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7009 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7010 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007011 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007012 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00007013 (eap->cmdidx != CMD_caddexpr
7014 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007015 (linenr_T)0, (linenr_T)0,
7016 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007017 if (qf_stack_empty(qi))
7018 {
7019 decr_quickfix_busy();
7020 goto cleanup;
7021 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01007022 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007023 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007024
7025 // Remember the current quickfix list identifier, so that we can
7026 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007027 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007028 if (au_name != NULL)
7029 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7030 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007031
7032 // Jump to the first error for a new list and if autocmds didn't
7033 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02007034 if (res > 0 && (eap->cmdidx == CMD_cexpr
7035 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007036 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02007037 // display the first error
7038 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007039 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00007040 }
7041 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007042 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007043cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00007044 free_tv(tv);
7045 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007046}
Bram Moolenaar1e015462005-09-25 22:16:38 +00007047#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007048
7049/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007050 * Get the location list for ":lhelpgrep"
7051 */
7052 static qf_info_T *
7053hgr_get_ll(int *new_ll)
7054{
7055 win_T *wp;
7056 qf_info_T *qi;
7057
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007058 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007059 if (bt_help(curwin->w_buffer))
7060 wp = curwin;
7061 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007062 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007063 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007064
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007065 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007066 qi = NULL;
7067 else
7068 qi = wp->w_llist;
7069
7070 if (qi == NULL)
7071 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007072 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007073 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007074 return NULL;
7075 *new_ll = TRUE;
7076 }
7077
7078 return qi;
7079}
7080
7081/*
7082 * Search for a pattern in a help file.
7083 */
7084 static void
7085hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007086 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007087 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007088 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007089 regmatch_T *p_regmatch)
7090{
7091 FILE *fd;
7092 long lnum;
7093
7094 fd = mch_fopen((char *)fname, "r");
7095 if (fd == NULL)
7096 return;
7097
7098 lnum = 1;
7099 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7100 {
7101 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007102
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007103 // Convert a line if 'encoding' is not utf-8 and
7104 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007105 if (p_vc->vc_type != CONV_NONE
7106 && has_non_ascii(IObuff))
7107 {
7108 line = string_convert(p_vc, IObuff, NULL);
7109 if (line == NULL)
7110 line = IObuff;
7111 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007112
7113 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7114 {
7115 int l = (int)STRLEN(line);
7116
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007117 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007118 while (l > 0 && line[l - 1] <= ' ')
7119 line[--l] = NUL;
7120
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007121 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007122 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007123 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007124 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007125 0,
7126 line,
7127 lnum,
7128 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007129 + 1, // col
7130 FALSE, // vis_col
7131 NULL, // search pattern
7132 0, // nr
7133 1, // type
7134 TRUE // valid
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007135 ) == FAIL)
7136 {
7137 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007138 if (line != IObuff)
7139 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007140 break;
7141 }
7142 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007143 if (line != IObuff)
7144 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007145 ++lnum;
7146 line_breakcheck();
7147 }
7148 fclose(fd);
7149}
7150
7151/*
7152 * Search for a pattern in all the help files in the doc directory under
7153 * the given directory.
7154 */
7155 static void
7156hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007157 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007158 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007159 regmatch_T *p_regmatch,
7160 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007161#ifdef FEAT_MULTI_LANG
7162 , char_u *lang
7163#endif
7164 )
7165{
7166 int fcount;
7167 char_u **fnames;
7168 int fi;
7169
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007170 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007171 add_pathsep(dirname);
7172 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7173 if (gen_expand_wildcards(1, &dirname, &fcount,
7174 &fnames, EW_FILE|EW_SILENT) == OK
7175 && fcount > 0)
7176 {
7177 for (fi = 0; fi < fcount && !got_int; ++fi)
7178 {
7179#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007180 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007181 if (lang != NULL
7182 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007183 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007184 && !(STRNICMP(lang, "en", 2) == 0
7185 && STRNICMP("txt", fnames[fi]
7186 + STRLEN(fnames[fi]) - 3, 3) == 0))
7187 continue;
7188#endif
7189
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007190 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007191 }
7192 FreeWild(fcount, fnames);
7193 }
7194}
7195
7196/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007197 * Search for a pattern in all the help files in the 'runtimepath'
7198 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007199 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007200 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007201 */
7202 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007203hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007204{
7205 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007206
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007207 vimconv_T vc;
7208
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007209 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7210 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007211 vc.vc_type = CONV_NONE;
7212 if (!enc_utf8)
7213 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007214
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007215 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007216 p = p_rtp;
7217 while (*p != NUL && !got_int)
7218 {
7219 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7220
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007221 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007222#ifdef FEAT_MULTI_LANG
7223 , lang
7224#endif
7225 );
7226 }
7227
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007228 if (vc.vc_type != CONV_NONE)
7229 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007230}
7231
7232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 * ":helpgrep {pattern}"
7234 */
7235 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007236ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
7238 regmatch_T regmatch;
7239 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007240 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007241 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007242 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007243 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244
Bram Moolenaar73633f82012-01-20 13:39:07 +01007245 switch (eap->cmdidx)
7246 {
7247 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7248 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7249 default: break;
7250 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007251 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7252 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007253 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007254#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007255 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007256 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007257#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007258 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007259
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007260 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01007261 save_cpo = p_cpo;
7262 p_cpo = empty_option;
7263
Bram Moolenaar39665952018-08-15 20:59:48 +02007264 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007265 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007266 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007267 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007268 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007269 }
7270
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007271 incr_quickfix_busy();
7272
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007273#ifdef FEAT_MULTI_LANG
7274 // Check for a specified language
7275 lang = check_help_lang(eap->arg);
7276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7278 regmatch.rm_ic = FALSE;
7279 if (regmatch.regprog != NULL)
7280 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007281 qf_list_T *qfl;
7282
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007283 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007284 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007285 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01007287 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007288
Bram Moolenaar473de612013-06-08 18:19:48 +02007289 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007291 qfl->qf_nonevalid = FALSE;
7292 qfl->qf_ptr = qfl->qf_start;
7293 qfl->qf_index = 1;
7294 qf_list_changed(qfl);
7295 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 }
7297
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007298 if (p_cpo == empty_option)
7299 p_cpo = save_cpo;
7300 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007301 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007302 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303
Bram Moolenaar73633f82012-01-20 13:39:07 +01007304 if (au_name != NULL)
7305 {
7306 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7307 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007308 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007309 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007310 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007311 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007312 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007313 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007314 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007315
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007316 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007317 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007318 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007319 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007320 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007321
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007322 decr_quickfix_busy();
7323
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007324 if (eap->cmdidx == CMD_lhelpgrep)
7325 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007326 // If the help window is not opened or if it already points to the
7327 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007328 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007329 {
7330 if (new_qi)
7331 ll_free_all(&qi);
7332 }
7333 else if (curwin->w_llist == NULL)
7334 curwin->w_llist = qi;
7335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336}
7337
7338#endif /* FEAT_QUICKFIX */