blob: dba6b63bbe033e6ed46a2ec7d4f001660d822615 [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 Moolenaard76ce852018-05-01 15:02:04 +0200163static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200164static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200166static int qf_get_fnum(qf_info_T *qi, int qf_idx, 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 Moolenaar864293a2016-06-02 13:40:04 +0200175static void qf_fill_buffer(qf_info_T *qi, 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(
1325 qf_info_T *qi,
1326 int qf_idx,
1327 int idx,
1328 qf_list_T *qfl,
1329 qffields_T *fields)
1330{
1331 char_u *ptr;
1332 int len;
1333
1334 if (!qfl->qf_multiignore)
1335 {
1336 qfline_T *qfprev = qfl->qf_last;
1337
1338 if (qfprev == NULL)
1339 return QF_FAIL;
1340 if (*fields->errmsg && !qfl->qf_multiignore)
1341 {
1342 len = (int)STRLEN(qfprev->qf_text);
1343 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1344 == NULL)
1345 return QF_FAIL;
1346 STRCPY(ptr, qfprev->qf_text);
1347 vim_free(qfprev->qf_text);
1348 qfprev->qf_text = ptr;
1349 *(ptr += len) = '\n';
1350 STRCPY(++ptr, fields->errmsg);
1351 }
1352 if (qfprev->qf_nr == -1)
1353 qfprev->qf_nr = fields->enr;
1354 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001355 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001356 qfprev->qf_type = fields->type;
1357
1358 if (!qfprev->qf_lnum)
1359 qfprev->qf_lnum = fields->lnum;
1360 if (!qfprev->qf_col)
1361 qfprev->qf_col = fields->col;
1362 qfprev->qf_viscol = fields->use_viscol;
1363 if (!qfprev->qf_fnum)
1364 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1365 qfl->qf_directory,
1366 *fields->namebuf || qfl->qf_directory != NULL
1367 ? fields->namebuf
1368 : qfl->qf_currfile != NULL && fields->valid
1369 ? qfl->qf_currfile : 0);
1370 }
1371 if (idx == 'Z')
1372 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1373 line_breakcheck();
1374
1375 return QF_IGNORE_LINE;
1376}
1377
1378/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001379 * Parse a line and get the quickfix fields.
1380 * Return the QF_ status.
1381 */
1382 static int
1383qf_parse_line(
1384 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001385 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001386 char_u *linebuf,
1387 int linelen,
1388 efm_T *fmt_first,
1389 qffields_T *fields)
1390{
1391 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001392 int idx = 0;
1393 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001394 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001395 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001396
Bram Moolenaare333e792018-04-08 13:27:39 +02001397restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001398 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001399 if (fmt_start == NULL)
1400 fmt_ptr = fmt_first;
1401 else
1402 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001403 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001404 fmt_ptr = fmt_start;
1405 fmt_start = NULL;
1406 }
1407
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001408 // Try to match each part of 'errorformat' until we find a complete
1409 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001410 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001411 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1412 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001413 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001414 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1415 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1416 if (status == QF_NOMEM)
1417 return status;
1418 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001419 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001420 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001421 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001422
1423 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1424 {
1425 if (fmt_ptr != NULL)
1426 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001427 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001428 status = qf_parse_dir_pfx(idx, fields, qfl);
1429 if (status != QF_OK)
1430 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001431 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001432
1433 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1434 if (status != QF_OK)
1435 return status;
1436
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001437 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001438 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001439 }
1440 else if (fmt_ptr != NULL)
1441 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001442 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001443 if (fmt_ptr->conthere)
1444 fmt_start = fmt_ptr;
1445
1446 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1447 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001448 qfl->qf_multiline = TRUE; // start of a multi-line message
1449 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001450 }
1451 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001452 { // continuation of multi-line msg
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001453 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1454 if (status != QF_OK)
1455 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001456 }
1457 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001458 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001459 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1460 if (status == QF_MULTISCAN)
1461 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001462 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001463 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001464 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001465 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001466 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001467 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001468 return QF_IGNORE_LINE;
1469 }
1470 }
1471
1472 return QF_OK;
1473}
1474
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001475/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001476 * Returns TRUE if the specified quickfix/location stack is empty
1477 */
1478 static int
1479qf_stack_empty(qf_info_T *qi)
1480{
1481 return qi == NULL || qi->qf_listcount <= 0;
1482}
1483
1484/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001485 * Returns TRUE if the specified quickfix/location list is empty.
1486 */
1487 static int
1488qf_list_empty(qf_info_T *qi, int qf_idx)
1489{
1490 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1491 return TRUE;
1492 return qi->qf_lists[qf_idx].qf_count <= 0;
1493}
1494
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001495/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001496 * Allocate the fields used for parsing lines and populating a quickfix list.
1497 */
1498 static int
1499qf_alloc_fields(qffields_T *pfields)
1500{
1501 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1502 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1503 pfields->errmsglen = CMDBUFFSIZE + 1;
1504 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1505 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1506 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1507 || pfields->pattern == NULL || pfields->module == NULL)
1508 return FAIL;
1509
1510 return OK;
1511}
1512
1513/*
1514 * Free the fields used for parsing lines and populating a quickfix list.
1515 */
1516 static void
1517qf_free_fields(qffields_T *pfields)
1518{
1519 vim_free(pfields->namebuf);
1520 vim_free(pfields->module);
1521 vim_free(pfields->errmsg);
1522 vim_free(pfields->pattern);
1523}
1524
1525/*
1526 * Setup the state information used for parsing lines and populating a
1527 * quickfix list.
1528 */
1529 static int
1530qf_setup_state(
1531 qfstate_T *pstate,
1532 char_u *enc,
1533 char_u *efile,
1534 typval_T *tv,
1535 buf_T *buf,
1536 linenr_T lnumfirst,
1537 linenr_T lnumlast)
1538{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001539 pstate->vc.vc_type = CONV_NONE;
1540 if (enc != NULL && *enc != NUL)
1541 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001542
1543 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1544 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001545 semsg(_(e_openerrf), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001546 return FAIL;
1547 }
1548
1549 if (tv != NULL)
1550 {
1551 if (tv->v_type == VAR_STRING)
1552 pstate->p_str = tv->vval.v_string;
1553 else if (tv->v_type == VAR_LIST)
1554 pstate->p_li = tv->vval.v_list->lv_first;
1555 pstate->tv = tv;
1556 }
1557 pstate->buf = buf;
1558 pstate->buflnum = lnumfirst;
1559 pstate->lnumlast = lnumlast;
1560
1561 return OK;
1562}
1563
1564/*
1565 * Cleanup the state information used for parsing lines and populating a
1566 * quickfix list.
1567 */
1568 static void
1569qf_cleanup_state(qfstate_T *pstate)
1570{
1571 if (pstate->fd != NULL)
1572 fclose(pstate->fd);
1573
1574 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001575 if (pstate->vc.vc_type != CONV_NONE)
1576 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001577}
1578
1579/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001580 * Read the errorfile "efile" into memory, line by line, building the error
1581 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001582 * Alternative: when "efile" is NULL read errors from buffer "buf".
1583 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001584 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001585 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1586 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001587 * Return -1 for error, number of errors for success.
1588 */
1589 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001590qf_init_ext(
1591 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001592 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001593 char_u *efile,
1594 buf_T *buf,
1595 typval_T *tv,
1596 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001597 int newlist, // TRUE: start a new error list
1598 linenr_T lnumfirst, // first line number to use
1599 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001600 char_u *qf_title,
1601 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001602{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001603 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001604 qfstate_T state;
1605 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001606 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001607 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001608 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001610 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001611 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001612 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001614 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001615 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001616
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001617 vim_memset(&state, 0, sizeof(state));
1618 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001619 if ((qf_alloc_fields(&fields) == FAIL) ||
1620 (qf_setup_state(&state, enc, efile, tv, buf,
1621 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 goto qf_init_end;
1623
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001624 if (newlist || qf_idx == qi->qf_listcount)
1625 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001626 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001627 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001628 qf_idx = qi->qf_curlist;
1629 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001630 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001631 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001632 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001633 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001634 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001635 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001638 qfl = &qi->qf_lists[qf_idx];
1639
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001640 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001641 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001642 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 else
1644 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001646 // If the errorformat didn't change between calls, then reuse the
1647 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001648 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1649 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001650 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001651 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001652 free_efm_list(&fmt_first);
1653
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001654 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001655 fmt_first = parse_efm_option(efm);
1656 if (fmt_first != NULL)
1657 last_efm = vim_strsave(efm);
1658 }
1659
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001660 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001663 // got_int is reset here, because it was probably set when killing the
1664 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 got_int = FALSE;
1666
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001667 // Read the lines in the error file one by one.
1668 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001669 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001671 // Get the next line from a file/buffer/list/string
Bram Moolenaare0d37972016-07-15 22:36:01 +02001672 status = qf_get_nextline(&state);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001673 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001674 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001675 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001676 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001678 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1679 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001680 if (status == QF_FAIL)
1681 goto error2;
1682 if (status == QF_NOMEM)
1683 goto qf_init_end;
1684 if (status == QF_IGNORE_LINE)
1685 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001687 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001688 qf_idx,
1689 qfl->qf_directory,
1690 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001691 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001692 : ((qfl->qf_currfile != NULL && fields.valid)
1693 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001694 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001695 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001696 fields.errmsg,
1697 fields.lnum,
1698 fields.col,
1699 fields.use_viscol,
1700 fields.pattern,
1701 fields.enr,
1702 fields.type,
1703 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 goto error2;
1705 line_breakcheck();
1706 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001707 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001709 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001711 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001712 qfl->qf_ptr = qfl->qf_start;
1713 qfl->qf_index = 1;
1714 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 }
1716 else
1717 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001718 qfl->qf_nonevalid = FALSE;
1719 if (qfl->qf_ptr == NULL)
1720 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001722 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001723 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001724 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001726 emsg(_(e_readerrf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001728 if (!adding)
1729 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001730 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001731 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001732 qi->qf_listcount--;
1733 if (qi->qf_curlist > 0)
1734 --qi->qf_curlist;
1735 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001736qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001737 if (qf_idx == qi->qf_curlist)
1738 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001739 qf_cleanup_state(&state);
1740 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
1742 return retval;
1743}
1744
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001745/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001746 * Read the errorfile "efile" into memory, line by line, building the error
1747 * list. Set the error list's title to qf_title.
1748 * Return -1 for error, number of errors for success.
1749 */
1750 int
1751qf_init(win_T *wp,
1752 char_u *efile,
1753 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001754 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001755 char_u *qf_title,
1756 char_u *enc)
1757{
1758 qf_info_T *qi = &ql_info;
1759
1760 if (wp != NULL)
1761 {
1762 qi = ll_get_or_alloc_list(wp);
1763 if (qi == NULL)
1764 return FAIL;
1765 }
1766
1767 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1768 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1769}
1770
1771/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001772 * Set the title of the specified quickfix list. Frees the previous title.
1773 * Prepends ':' to the title.
1774 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001775 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001776qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001777{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001778 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001779
Bram Moolenaarfb604092014-07-23 15:55:00 +02001780 if (title != NULL)
1781 {
1782 char_u *p = alloc((int)STRLEN(title) + 2);
1783
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001784 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001785 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001786 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001787 }
1788}
1789
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001791 * The title of a quickfix/location list is set, by default, to the command
1792 * that created the quickfix list with the ":" prefix.
1793 * Create a quickfix list title string by prepending ":" to a user command.
1794 * Returns a pointer to a static buffer with the title.
1795 */
1796 static char_u *
1797qf_cmdtitle(char_u *cmd)
1798{
1799 static char_u qftitle_str[IOSIZE];
1800
1801 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1802 return qftitle_str;
1803}
1804
1805/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001806 * Prepare for adding a new quickfix list. If the current list is in the
1807 * middle of the stack, then all the following lists are freed and then
1808 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 */
1810 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001811qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812{
1813 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001814 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001816 // If the current entry is not the last entry, delete entries beyond
1817 // the current entry. This makes it possible to browse in a tree-like
1818 // way with ":grep'.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001819 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001820 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001822 // When the stack is full, remove to oldest entry
1823 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001824 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001826 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001828 qi->qf_lists[i - 1] = qi->qf_lists[i];
1829 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 }
1831 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001832 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001833 qfl = &qi->qf_lists[qi->qf_curlist];
1834 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1835 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001836 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001837 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838}
1839
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001840/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001841 * Queue location list stack delete request.
1842 */
1843 static void
1844locstack_queue_delreq(qf_info_T *qi)
1845{
1846 qf_delq_T *q;
1847
1848 q = (qf_delq_T *)alloc((unsigned)sizeof(qf_delq_T));
1849 if (q != NULL)
1850 {
1851 q->qi = qi;
1852 q->next = qf_delq_head;
1853 qf_delq_head = q;
1854 }
1855}
1856
1857/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001858 * Return the global quickfix stack window buffer number.
1859 */
1860 int
1861qf_stack_get_bufnr(void)
1862{
1863 return ql_info.qf_bufnr;
1864}
1865
1866/*
1867 * Wipe the quickfix window buffer (if present) for the specified
1868 * quickfix/location list.
1869 */
1870 static void
1871wipe_qf_buffer(qf_info_T *qi)
1872{
1873 buf_T *qfbuf;
1874
1875 if (qi->qf_bufnr != INVALID_QFBUFNR)
1876 {
1877 qfbuf = buflist_findnr(qi->qf_bufnr);
1878 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1879 {
1880 // If the quickfix buffer is not loaded in any window, then
1881 // wipe the buffer.
1882 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE);
1883 qi->qf_bufnr = INVALID_QFBUFNR;
1884 }
1885 }
1886}
1887
1888/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001889 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001890 */
1891 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001892ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001893{
1894 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001895 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001896
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001897 qi = *pqi;
1898 if (qi == NULL)
1899 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001900 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001901
1902 qi->qf_refcount--;
1903 if (qi->qf_refcount < 1)
1904 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001905 // No references to this location list.
1906 // If the location list is still in use, then queue the delete request
1907 // to be processed later.
1908 if (quickfix_busy > 0)
1909 locstack_queue_delreq(qi);
1910 else
1911 {
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001912 // If the quickfix window buffer is loaded, then wipe it
1913 wipe_qf_buffer(qi);
1914
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001915 for (i = 0; i < qi->qf_listcount; ++i)
1916 qf_free(&qi->qf_lists[i]);
1917 vim_free(qi);
1918 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001919 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001920}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001921
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001922/*
1923 * Free all the quickfix/location lists in the stack.
1924 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001925 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001926qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001927{
1928 int i;
1929 qf_info_T *qi = &ql_info;
1930
1931 if (wp != NULL)
1932 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001933 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001934 ll_free_all(&wp->w_llist);
1935 ll_free_all(&wp->w_llist_ref);
1936 }
1937 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001938 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001939 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001940 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001941}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001942
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001944 * Delay freeing of location list stacks when the quickfix code is running.
1945 * Used to avoid problems with autocmds freeing location list stacks when the
1946 * quickfix code is still referencing the stack.
1947 * Must always call decr_quickfix_busy() exactly once after this.
1948 */
1949 static void
1950incr_quickfix_busy(void)
1951{
1952 quickfix_busy++;
1953}
1954
1955/*
1956 * Safe to free location list stacks. Process any delayed delete requests.
1957 */
1958 static void
1959decr_quickfix_busy(void)
1960{
1961 if (--quickfix_busy == 0)
1962 {
1963 // No longer referencing the location lists. Process all the pending
1964 // delete requests.
1965 while (qf_delq_head != NULL)
1966 {
1967 qf_delq_T *q = qf_delq_head;
1968
1969 qf_delq_head = q->next;
1970 ll_free_all(&q->qi);
1971 vim_free(q);
1972 }
1973 }
1974#ifdef ABORT_ON_INTERNAL_ERROR
1975 if (quickfix_busy < 0)
1976 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001977 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001978 abort();
1979 }
1980#endif
1981}
1982
1983#if defined(EXITFREE) || defined(PROTO)
1984 void
1985check_quickfix_busy(void)
1986{
1987 if (quickfix_busy != 0)
1988 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001989 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001990# ifdef ABORT_ON_INTERNAL_ERROR
1991 abort();
1992# endif
1993 }
1994}
1995#endif
1996
1997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 * Add an entry to the end of the list of errors.
1999 * Returns OK or FAIL.
2000 */
2001 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002002qf_add_entry(
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002003 qf_info_T *qi, // quickfix list
2004 int qf_idx, // list index
2005 char_u *dir, // optional directory name
2006 char_u *fname, // file name or NULL
2007 char_u *module, // module name or NULL
2008 int bufnum, // buffer number or zero
2009 char_u *mesg, // message
2010 long lnum, // line number
2011 int col, // column
2012 int vis_col, // using visual column
2013 char_u *pattern, // search pattern
2014 int nr, // error number
2015 int type, // type character
2016 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002018 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002019 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002020 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002022 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002024 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002025 {
2026 buf_T *buf = buflist_findnr(bufnum);
2027
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002028 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002029 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002030 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002031 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002032 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002033 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002034 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2036 {
2037 vim_free(qfp);
2038 return FAIL;
2039 }
2040 qfp->qf_lnum = lnum;
2041 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002042 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002043 if (pattern == NULL || *pattern == NUL)
2044 qfp->qf_pattern = NULL;
2045 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2046 {
2047 vim_free(qfp->qf_text);
2048 vim_free(qfp);
2049 return FAIL;
2050 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002051 if (module == NULL || *module == NUL)
2052 qfp->qf_module = NULL;
2053 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2054 {
2055 vim_free(qfp->qf_text);
2056 vim_free(qfp->qf_pattern);
2057 vim_free(qfp);
2058 return FAIL;
2059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002061 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 type = 0;
2063 qfp->qf_type = type;
2064 qfp->qf_valid = valid;
2065
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002066 lastp = &qfl->qf_last;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002067 if (qf_list_empty(qi, qf_idx)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002069 qfl->qf_start = qfp;
2070 qfl->qf_ptr = qfp;
2071 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002072 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 }
2074 else
2075 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002076 qfp->qf_prev = *lastp;
2077 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002079 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002081 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002082 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002083 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002085 qfl->qf_index = qfl->qf_count;
2086 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 }
2088
2089 return OK;
2090}
2091
2092/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002093 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002094 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002095 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002096qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002097{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002098 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002099
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002100 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002101 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002102 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002103 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002104 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002105 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002106 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002107 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002108}
2109
2110/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002111 * Return the location list stack for window 'wp'.
2112 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002113 */
2114 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002115ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002116{
2117 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002118 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002119 return wp->w_llist_ref;
2120
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002121 // For a non-location list window, w_llist_ref should not point to a
2122 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002123 ll_free_all(&wp->w_llist_ref);
2124
2125 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002126 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002127 return wp->w_llist;
2128}
2129
2130/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002131 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2132 */
2133 static int
2134copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2135{
2136 int i;
2137 qfline_T *from_qfp;
2138 qfline_T *prevp;
2139
2140 // copy all the location entries in this list
2141 for (i = 0, from_qfp = from_qfl->qf_start;
2142 i < from_qfl->qf_count && from_qfp != NULL;
2143 ++i, from_qfp = from_qfp->qf_next)
2144 {
2145 if (qf_add_entry(to_qi,
2146 to_qi->qf_curlist,
2147 NULL,
2148 NULL,
2149 from_qfp->qf_module,
2150 0,
2151 from_qfp->qf_text,
2152 from_qfp->qf_lnum,
2153 from_qfp->qf_col,
2154 from_qfp->qf_viscol,
2155 from_qfp->qf_pattern,
2156 from_qfp->qf_nr,
2157 0,
2158 from_qfp->qf_valid) == FAIL)
2159 return FAIL;
2160
2161 // qf_add_entry() will not set the qf_num field, as the
2162 // directory and file names are not supplied. So the qf_fnum
2163 // field is copied here.
2164 prevp = to_qfl->qf_last;
2165 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2166 prevp->qf_type = from_qfp->qf_type; // error type
2167 if (from_qfl->qf_ptr == from_qfp)
2168 to_qfl->qf_ptr = prevp; // current location
2169 }
2170
2171 return OK;
2172}
2173
2174/*
2175 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2176 */
2177 static int
2178copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2179{
2180 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002181 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002182 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2183 to_qfl->qf_count = 0;
2184 to_qfl->qf_index = 0;
2185 to_qfl->qf_start = NULL;
2186 to_qfl->qf_last = NULL;
2187 to_qfl->qf_ptr = NULL;
2188 if (from_qfl->qf_title != NULL)
2189 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2190 else
2191 to_qfl->qf_title = NULL;
2192 if (from_qfl->qf_ctx != NULL)
2193 {
2194 to_qfl->qf_ctx = alloc_tv();
2195 if (to_qfl->qf_ctx != NULL)
2196 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2197 }
2198 else
2199 to_qfl->qf_ctx = NULL;
2200
2201 if (from_qfl->qf_count)
2202 if (copy_loclist_entries(from_qfl, to_qfl, to_qi) == FAIL)
2203 return FAIL;
2204
2205 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2206
2207 // Assign a new ID for the location list
2208 to_qfl->qf_id = ++last_qf_id;
2209 to_qfl->qf_changedtick = 0L;
2210
2211 // When no valid entries are present in the list, qf_ptr points to
2212 // the first item in the list
2213 if (to_qfl->qf_nonevalid)
2214 {
2215 to_qfl->qf_ptr = to_qfl->qf_start;
2216 to_qfl->qf_index = 1;
2217 }
2218
2219 return OK;
2220}
2221
2222/*
2223 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002224 */
2225 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002226copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002227{
2228 qf_info_T *qi;
2229 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002230
Bram Moolenaar09037502018-09-25 22:08:14 +02002231 // When copying from a location list window, copy the referenced
2232 // location list. For other windows, copy the location list for
2233 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002234 if (IS_LL_WINDOW(from))
2235 qi = from->w_llist_ref;
2236 else
2237 qi = from->w_llist;
2238
Bram Moolenaar09037502018-09-25 22:08:14 +02002239 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002240 return;
2241
Bram Moolenaar09037502018-09-25 22:08:14 +02002242 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002243 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002244 return;
2245
2246 to->w_llist->qf_listcount = qi->qf_listcount;
2247
Bram Moolenaar09037502018-09-25 22:08:14 +02002248 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002249 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002250 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002251 to->w_llist->qf_curlist = idx;
2252
Bram Moolenaar09037502018-09-25 22:08:14 +02002253 if (copy_loclist(&qi->qf_lists[idx],
2254 &to->w_llist->qf_lists[idx], to->w_llist) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002255 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002256 qf_free_all(to);
2257 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002258 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002259 }
2260
Bram Moolenaar09037502018-09-25 22:08:14 +02002261 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002262}
2263
2264/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002265 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002266 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 */
2268 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002269qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002271 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar82404332016-07-10 17:00:38 +02002272 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002273 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002274 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002275
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002276 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
Bram Moolenaare60acc12011-05-10 16:41:25 +02002279#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002280 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002281#endif
2282#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002283 if (directory != NULL)
2284 slash_adjust(directory);
2285 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002286#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002287 if (directory != NULL && !vim_isAbsName(fname)
2288 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2289 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002290 // Here we check if the file really exists.
2291 // This should normally be true, but if make works without
2292 // "leaving directory"-messages we might have missed a
2293 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002294 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002297 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002298 if (directory)
2299 ptr = concat_fnames(directory, fname, TRUE);
2300 else
2301 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002303 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002304 bufname = ptr;
2305 }
2306 else
2307 bufname = fname;
2308
2309 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002310 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002311 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002312 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002313 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002315 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002316 {
2317 vim_free(qf_last_bufname);
2318 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2319 if (bufname == ptr)
2320 qf_last_bufname = bufname;
2321 else
2322 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002323 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002324 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002325 if (buf == NULL)
2326 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002327
Bram Moolenaarc1542742016-07-20 21:44:37 +02002328 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002329 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002330 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331}
2332
2333/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002334 * Push dirbuf onto the directory stack and return pointer to actual dir or
2335 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 */
2337 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002338qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339{
2340 struct dir_stack_T *ds_new;
2341 struct dir_stack_T *ds_ptr;
2342
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002343 // allocate new stack element and hook it in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2345 if (ds_new == NULL)
2346 return NULL;
2347
2348 ds_new->next = *stackptr;
2349 *stackptr = ds_new;
2350
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002351 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (vim_isAbsName(dirbuf)
2353 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002354 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 (*stackptr)->dirname = vim_strsave(dirbuf);
2356 else
2357 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002358 // Okay we don't have an absolute path.
2359 // dirbuf must be a subdir of one of the directories on the stack.
2360 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 ds_new = (*stackptr)->next;
2362 (*stackptr)->dirname = NULL;
2363 while (ds_new)
2364 {
2365 vim_free((*stackptr)->dirname);
2366 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2367 TRUE);
2368 if (mch_isdir((*stackptr)->dirname) == TRUE)
2369 break;
2370
2371 ds_new = ds_new->next;
2372 }
2373
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002374 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 while ((*stackptr)->next != ds_new)
2376 {
2377 ds_ptr = (*stackptr)->next;
2378 (*stackptr)->next = (*stackptr)->next->next;
2379 vim_free(ds_ptr->dirname);
2380 vim_free(ds_ptr);
2381 }
2382
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002383 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 if (ds_new == NULL)
2385 {
2386 vim_free((*stackptr)->dirname);
2387 (*stackptr)->dirname = vim_strsave(dirbuf);
2388 }
2389 }
2390
2391 if ((*stackptr)->dirname != NULL)
2392 return (*stackptr)->dirname;
2393 else
2394 {
2395 ds_ptr = *stackptr;
2396 *stackptr = (*stackptr)->next;
2397 vim_free(ds_ptr);
2398 return NULL;
2399 }
2400}
2401
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402/*
2403 * pop dirbuf from the directory stack and return previous directory or NULL if
2404 * stack is empty
2405 */
2406 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002407qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408{
2409 struct dir_stack_T *ds_ptr;
2410
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002411 // TODO: Should we check if dirbuf is the directory on top of the stack?
2412 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002414 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 if (*stackptr != NULL)
2416 {
2417 ds_ptr = *stackptr;
2418 *stackptr = (*stackptr)->next;
2419 vim_free(ds_ptr->dirname);
2420 vim_free(ds_ptr);
2421 }
2422
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002423 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 return *stackptr ? (*stackptr)->dirname : NULL;
2425}
2426
2427/*
2428 * clean up directory stack
2429 */
2430 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002431qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432{
2433 struct dir_stack_T *ds_ptr;
2434
2435 while ((ds_ptr = *stackptr) != NULL)
2436 {
2437 *stackptr = (*stackptr)->next;
2438 vim_free(ds_ptr->dirname);
2439 vim_free(ds_ptr);
2440 }
2441}
2442
2443/*
2444 * Check in which directory of the directory stack the given file can be
2445 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002446 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 * Cleans up intermediate directory entries.
2448 *
2449 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002450 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 * ./
2452 * ./aa
2453 * ./aa/bb
2454 * ./bb
2455 * ./bb/x.c
2456 * and make says:
2457 * making all in aa
2458 * making all in bb
2459 * x.c:9: Error
2460 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2461 * qf_guess_filepath will return NULL.
2462 */
2463 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002464qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
2466 struct dir_stack_T *ds_ptr;
2467 struct dir_stack_T *ds_tmp;
2468 char_u *fullname;
2469
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002470 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002471 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 return NULL;
2473
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002474 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 fullname = NULL;
2476 while (ds_ptr)
2477 {
2478 vim_free(fullname);
2479 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2480
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002481 // If concat_fnames failed, just go on. The worst thing that can happen
2482 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2484 break;
2485
2486 ds_ptr = ds_ptr->next;
2487 }
2488
2489 vim_free(fullname);
2490
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002491 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002492 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002494 ds_tmp = qfl->qf_dir_stack->next;
2495 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 vim_free(ds_tmp->dirname);
2497 vim_free(ds_tmp);
2498 }
2499
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002500 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501}
2502
2503/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002504 * Returns TRUE if a quickfix/location list with the given identifier exists.
2505 */
2506 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002507qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002508{
2509 qf_info_T *qi = &ql_info;
2510 int i;
2511
2512 if (wp != NULL)
2513 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002514 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002515 if (qi == NULL)
2516 return FALSE;
2517 }
2518
2519 for (i = 0; i < qi->qf_listcount; ++i)
2520 if (qi->qf_lists[i].qf_id == qf_id)
2521 return TRUE;
2522
2523 return FALSE;
2524}
2525
2526/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002527 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002528 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002529 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002530 * Similar to location list.
2531 */
2532 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002533is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002534{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002535 qfline_T *qfp;
2536 int i;
2537
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002538 // Search for the entry in the current list
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002539 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2540 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002541 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002542 break;
2543
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002544 if (i == qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002545 return FALSE;
2546
2547 return TRUE;
2548}
2549
2550/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002551 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002552 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002553 */
2554 static qfline_T *
2555get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002556 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002557 qfline_T *qf_ptr,
2558 int *qf_index,
2559 int dir)
2560{
2561 int idx;
2562 int old_qf_fnum;
2563
2564 idx = *qf_index;
2565 old_qf_fnum = qf_ptr->qf_fnum;
2566
2567 do
2568 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002569 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002570 return NULL;
2571 ++idx;
2572 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002573 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002574 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2575
2576 *qf_index = idx;
2577 return qf_ptr;
2578}
2579
2580/*
2581 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002582 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002583 */
2584 static qfline_T *
2585get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002586 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002587 qfline_T *qf_ptr,
2588 int *qf_index,
2589 int dir)
2590{
2591 int idx;
2592 int old_qf_fnum;
2593
2594 idx = *qf_index;
2595 old_qf_fnum = qf_ptr->qf_fnum;
2596
2597 do
2598 {
2599 if (idx == 1 || qf_ptr->qf_prev == NULL)
2600 return NULL;
2601 --idx;
2602 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002603 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002604 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2605
2606 *qf_index = idx;
2607 return qf_ptr;
2608}
2609
2610/*
2611 * Get the n'th (errornr) previous/next valid entry from the current entry in
2612 * the quickfix list.
2613 * dir == FORWARD or FORWARD_FILE: next valid entry
2614 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2615 */
2616 static qfline_T *
2617get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002618 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002619 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002620 int dir,
2621 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002622{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002623 qfline_T *qf_ptr = qfl->qf_ptr;
2624 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002625 qfline_T *prev_qf_ptr;
2626 int prev_index;
2627 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2628 char_u *err = e_no_more_items;
2629
2630 while (errornr--)
2631 {
2632 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002633 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002634
2635 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002636 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002637 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002638 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002639 if (qf_ptr == NULL)
2640 {
2641 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002642 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002643 if (err != NULL)
2644 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002645 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002646 return NULL;
2647 }
2648 break;
2649 }
2650
2651 err = NULL;
2652 }
2653
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002654 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002655 return qf_ptr;
2656}
2657
2658/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002659 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2660 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002661 */
2662 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002663get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002664{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002665 qfline_T *qf_ptr = qfl->qf_ptr;
2666 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002667
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002668 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002669 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2670 {
2671 --qf_idx;
2672 qf_ptr = qf_ptr->qf_prev;
2673 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002674 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002675 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2676 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002677 {
2678 ++qf_idx;
2679 qf_ptr = qf_ptr->qf_next;
2680 }
2681
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002682 *new_qfidx = qf_idx;
2683 return qf_ptr;
2684}
2685
2686/*
2687 * Get a entry specied by 'errornr' and 'dir' from the current
2688 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2689 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2690 * Returns a pointer to the entry and the index of the new entry is stored in
2691 * 'new_qfidx'.
2692 */
2693 static qfline_T *
2694qf_get_entry(
2695 qf_list_T *qfl,
2696 int errornr,
2697 int dir,
2698 int *new_qfidx)
2699{
2700 qfline_T *qf_ptr = qfl->qf_ptr;
2701 int qfidx = qfl->qf_index;
2702
2703 if (dir != 0) // next/prev valid entry
2704 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2705 else if (errornr != 0) // go to specified number
2706 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2707
2708 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002709 return qf_ptr;
2710}
2711
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002712/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002713 * Find a window displaying a Vim help file.
2714 */
2715 static win_T *
2716qf_find_help_win(void)
2717{
2718 win_T *wp;
2719
2720 FOR_ALL_WINDOWS(wp)
2721 if (bt_help(wp->w_buffer))
2722 return wp;
2723
2724 return NULL;
2725}
2726
2727/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002728 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2729 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002730 */
2731 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002732jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002733{
2734 win_T *wp;
2735 int flags;
2736
Bram Moolenaarb2443732018-11-11 22:50:27 +01002737 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002738 wp = NULL;
2739 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002740 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002741 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2742 win_enter(wp, TRUE);
2743 else
2744 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002745 // Split off help window; put it at far top if no position
2746 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002747 flags = WSP_HELP;
2748 if (cmdmod.split == 0 && curwin->w_width != Columns
2749 && curwin->w_width < 80)
2750 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002751 // If the user asks to open a new window, then copy the location list.
2752 // Otherwise, don't copy the location list.
2753 if (IS_LL_STACK(qi) && !newwin)
2754 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002755
2756 if (win_split(0, flags) == FAIL)
2757 return FAIL;
2758
2759 *opened_window = TRUE;
2760
2761 if (curwin->w_height < p_hh)
2762 win_setheight((int)p_hh);
2763
Bram Moolenaarb2443732018-11-11 22:50:27 +01002764 // When using location list, the new window should use the supplied
2765 // location list. If the user asks to open a new window, then the new
2766 // window will get a copy of the location list.
2767 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002768 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002769 curwin->w_llist = qi;
2770 qi->qf_refcount++;
2771 }
2772 }
2773
2774 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002775 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002776
2777 return OK;
2778}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002779
2780/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002781 * Find a non-quickfix window in the current tabpage using the given location
2782 * list stack.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002783 * Returns NULL if a matching window is not found.
2784 */
2785 static win_T *
2786qf_find_win_with_loclist(qf_info_T *ll)
2787{
2788 win_T *wp;
2789
2790 FOR_ALL_WINDOWS(wp)
2791 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2792 return wp;
2793
2794 return NULL;
2795}
2796
2797/*
2798 * Find a window containing a normal buffer
2799 */
2800 static win_T *
2801qf_find_win_with_normal_buf(void)
2802{
2803 win_T *wp;
2804
2805 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002806 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002807 return wp;
2808
2809 return NULL;
2810}
2811
2812/*
2813 * Go to a window in any tabpage containing the specified file. Returns TRUE
2814 * if successfully jumped to the window. Otherwise returns FALSE.
2815 */
2816 static int
2817qf_goto_tabwin_with_file(int fnum)
2818{
2819 tabpage_T *tp;
2820 win_T *wp;
2821
2822 FOR_ALL_TAB_WINDOWS(tp, wp)
2823 if (wp->w_buffer->b_fnum == fnum)
2824 {
2825 goto_tabpage_win(tp, wp);
2826 return TRUE;
2827 }
2828
2829 return FALSE;
2830}
2831
2832/*
2833 * Create a new window to show a file above the quickfix window. Called when
2834 * only the quickfix window is present.
2835 */
2836 static int
2837qf_open_new_file_win(qf_info_T *ll_ref)
2838{
2839 int flags;
2840
2841 flags = WSP_ABOVE;
2842 if (ll_ref != NULL)
2843 flags |= WSP_NEWLOC;
2844 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002845 return FAIL; // not enough room for window
2846 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002847 swb_flags = 0;
2848 RESET_BINDING(curwin);
2849 if (ll_ref != NULL)
2850 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002851 // The new window should use the location list from the
2852 // location list window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002853 curwin->w_llist = ll_ref;
2854 ll_ref->qf_refcount++;
2855 }
2856 return OK;
2857}
2858
2859/*
2860 * Go to a window that shows the right buffer. If the window is not found, go
2861 * to the window just above the location list window. This is used for opening
2862 * a file from a location window and not from a quickfix window. If some usable
2863 * window is previously found, then it is supplied in 'use_win'.
2864 */
2865 static void
2866qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2867{
2868 win_T *win = use_win;
2869
2870 if (win == NULL)
2871 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002872 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002873 FOR_ALL_WINDOWS(win)
2874 if (win->w_buffer->b_fnum == qf_fnum)
2875 break;
2876 if (win == NULL)
2877 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002878 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002879 win = curwin;
2880 do
2881 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002882 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002883 break;
2884 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002885 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002886 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002887 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002888 } while (win != curwin);
2889 }
2890 }
2891 win_goto(win);
2892
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002893 // If the location list for the window is not set, then set it
2894 // to the location list from the location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002895 if (win->w_llist == NULL)
2896 {
2897 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002898 if (ll_ref != NULL)
2899 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002900 }
2901}
2902
2903/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002904 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2905 * not found, then go to the window just above the quickfix window. This is
2906 * used for opening a file from a quickfix window and not from a location
2907 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002908 */
2909 static void
2910qf_goto_win_with_qfl_file(int qf_fnum)
2911{
2912 win_T *win;
2913 win_T *altwin;
2914
2915 win = curwin;
2916 altwin = NULL;
2917 for (;;)
2918 {
2919 if (win->w_buffer->b_fnum == qf_fnum)
2920 break;
2921 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002922 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002923 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002924 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002925
2926 if (IS_QF_WINDOW(win))
2927 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002928 // Didn't find it, go to the window before the quickfix
2929 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002930 if (altwin != NULL)
2931 win = altwin;
2932 else if (curwin->w_prev != NULL)
2933 win = curwin->w_prev;
2934 else
2935 win = curwin->w_next;
2936 break;
2937 }
2938
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002939 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02002940 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002941 altwin = win;
2942 }
2943
2944 win_goto(win);
2945}
2946
2947/*
2948 * Find a suitable window for opening a file (qf_fnum) from the
2949 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01002950 * window, jump to it. Otherwise open a new window to display the file. If
2951 * 'newwin' is TRUE, then always open a new window. This is called from either
2952 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002953 */
2954 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002955qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002956{
2957 win_T *usable_win_ptr = NULL;
2958 int usable_win;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002959 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002960 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002961
2962 usable_win = 0;
2963
Bram Moolenaarb2443732018-11-11 22:50:27 +01002964 // If opening a new window, then don't use the location list referred by
2965 // the current window. Otherwise two windows will refer to the same
2966 // location list.
2967 if (!newwin)
2968 ll_ref = curwin->w_llist_ref;
2969
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002970 if (ll_ref != NULL)
2971 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002972 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002973 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2974 if (usable_win_ptr != NULL)
2975 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002976 }
2977
2978 if (!usable_win)
2979 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002980 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002981 win = qf_find_win_with_normal_buf();
2982 if (win != NULL)
2983 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002984 }
2985
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002986 // If no usable window is found and 'switchbuf' contains "usetab"
2987 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002988 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002989 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002990
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002991 // If there is only one window and it is the quickfix window, create a
2992 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01002993 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002994 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002995 if (qf_open_new_file_win(ll_ref) != OK)
2996 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002997 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002998 }
2999 else
3000 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003001 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003002 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003003 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003004 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003005 }
3006
3007 return OK;
3008}
3009
3010/*
3011 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003012 * Returns OK if successfully edited the file, FAIL on failing to open the
3013 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3014 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003015 */
3016 static int
3017qf_jump_edit_buffer(
3018 qf_info_T *qi,
3019 qfline_T *qf_ptr,
3020 int forceit,
3021 win_T *oldwin,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003022 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003023{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003024 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003025 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003026 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003027 int old_qf_curlist = qi->qf_curlist;
3028 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003029
3030 if (qf_ptr->qf_type == 1)
3031 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003032 // Open help file (do_ecmd() will set b_help flag, readfile() will
3033 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003034 if (!can_abandon(curbuf, forceit))
3035 {
3036 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003037 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003038 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003039
3040 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3041 ECMD_HIDE + ECMD_SET_HELP,
3042 oldwin == curwin ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003043 }
3044 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003045 retval = buflist_getfile(qf_ptr->qf_fnum,
3046 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003047
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003048 // If a location list, check whether the associated window is still
3049 // present.
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003050 if (qfl_type == QFLT_LOCATION && !win_valid_any_tab(oldwin))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003051 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003052 emsg(_("E924: Current window was closed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003053 *opened_window = FALSE;
3054 return NOTDONE;
3055 }
3056
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003057 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003058 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003059 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003060 return NOTDONE;
3061 }
3062
3063 if (old_qf_curlist != qi->qf_curlist
3064 || !is_qf_entry_present(qfl, qf_ptr))
3065 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003066 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003067 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003068 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003069 emsg(_(e_loc_list_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003070 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003071 }
3072
3073 return retval;
3074}
3075
3076/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003077 * Go to the error line in the current file using either line/column number or
3078 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003079 */
3080 static void
3081qf_jump_goto_line(
3082 linenr_T qf_lnum,
3083 int qf_col,
3084 char_u qf_viscol,
3085 char_u *qf_pattern)
3086{
3087 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003088
3089 if (qf_pattern == NULL)
3090 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003091 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003092 i = qf_lnum;
3093 if (i > 0)
3094 {
3095 if (i > curbuf->b_ml.ml_line_count)
3096 i = curbuf->b_ml.ml_line_count;
3097 curwin->w_cursor.lnum = i;
3098 }
3099 if (qf_col > 0)
3100 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003101 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003102 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003103 coladvance(qf_col - 1);
3104 else
3105 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003106 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003107 check_cursor();
3108 }
3109 else
3110 beginline(BL_WHITE | BL_FIX);
3111 }
3112 else
3113 {
3114 pos_T save_cursor;
3115
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003116 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003117 save_cursor = curwin->w_cursor;
3118 curwin->w_cursor.lnum = 0;
3119 if (!do_search(NULL, '/', qf_pattern, (long)1,
3120 SEARCH_KEEP, NULL, NULL))
3121 curwin->w_cursor = save_cursor;
3122 }
3123}
3124
3125/*
3126 * Display quickfix list index and size message
3127 */
3128 static void
3129qf_jump_print_msg(
3130 qf_info_T *qi,
3131 int qf_index,
3132 qfline_T *qf_ptr,
3133 buf_T *old_curbuf,
3134 linenr_T old_lnum)
3135{
3136 linenr_T i;
3137 int len;
3138
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003139 // Update the screen before showing the message, unless the screen
3140 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003141 if (!msg_scrolled)
3142 update_topline_redraw();
3143 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3144 qi->qf_lists[qi->qf_curlist].qf_count,
3145 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3146 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003147 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003148 len = (int)STRLEN(IObuff);
3149 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3150
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003151 // Output the message. Overwrite to avoid scrolling when the 'O'
3152 // flag is present in 'shortmess'; But when not jumping, print the
3153 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003154 i = msg_scroll;
3155 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3156 msg_scroll = TRUE;
3157 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3158 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003159 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003160 msg_scroll = i;
3161}
3162
3163/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003164 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003165 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3166 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003167 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3168 * able to jump/open a window. Returns NOTDONE if a file is not associated
3169 * with the entry.
3170 */
3171 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003172qf_jump_open_window(
3173 qf_info_T *qi,
3174 qfline_T *qf_ptr,
3175 int newwin,
3176 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003177{
3178 // For ":helpgrep" find a help window or open one.
3179 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003180 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003181 return FAIL;
3182
3183 // If currently in the quickfix window, find another window to show the
3184 // file in.
3185 if (bt_quickfix(curbuf) && !*opened_window)
3186 {
3187 // If there is no file specified, we don't know where to go.
3188 // But do advance, otherwise ":cn" gets stuck.
3189 if (qf_ptr->qf_fnum == 0)
3190 return NOTDONE;
3191
Bram Moolenaarb2443732018-11-11 22:50:27 +01003192 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3193 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003194 return FAIL;
3195 }
3196
3197 return OK;
3198}
3199
3200/*
3201 * Edit a selected file from the quickfix/location list and jump to a
3202 * particular line/column, adjust the folds and display a message about the
3203 * jump.
3204 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3205 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3206 * the file.
3207 */
3208 static int
3209qf_jump_to_buffer(
3210 qf_info_T *qi,
3211 int qf_index,
3212 qfline_T *qf_ptr,
3213 int forceit,
3214 win_T *oldwin,
3215 int *opened_window,
3216 int openfold,
3217 int print_message)
3218{
3219 buf_T *old_curbuf;
3220 linenr_T old_lnum;
3221 int retval = OK;
3222
3223 // If there is a file name, read the wanted file if needed, and check
3224 // autowrite etc.
3225 old_curbuf = curbuf;
3226 old_lnum = curwin->w_cursor.lnum;
3227
3228 if (qf_ptr->qf_fnum != 0)
3229 {
3230 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3231 opened_window);
3232 if (retval != OK)
3233 return retval;
3234 }
3235
3236 // When not switched to another buffer, still need to set pc mark
3237 if (curbuf == old_curbuf)
3238 setpcmark();
3239
3240 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3241 qf_ptr->qf_pattern);
3242
3243#ifdef FEAT_FOLDING
3244 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3245 foldOpenCursor();
3246#endif
3247 if (print_message)
3248 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3249
3250 return retval;
3251}
3252
3253/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003254 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 */
3256 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003257qf_jump(qf_info_T *qi,
3258 int dir,
3259 int errornr,
3260 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003262 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3263}
3264
3265/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003266 * Jump to a quickfix line.
3267 * If dir == 0 go to entry "errornr".
3268 * If dir == FORWARD go "errornr" valid entries forward.
3269 * If dir == BACKWARD go "errornr" valid entries backward.
3270 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3271 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3272 * else if "errornr" is zero, redisplay the same line
3273 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003274 * If 'newwin' is TRUE, then open the file in a new window.
3275 */
3276 void
3277qf_jump_newwin(qf_info_T *qi,
3278 int dir,
3279 int errornr,
3280 int forceit,
3281 int newwin)
3282{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003283 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003284 qfline_T *qf_ptr;
3285 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003288 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003289 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003291 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003294 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003296 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003298 if (qi == NULL)
3299 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003300
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003301 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003303 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 return;
3305 }
3306
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003307 qfl = &qi->qf_lists[qi->qf_curlist];
3308
3309 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003311 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003313
3314 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3315 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003317 qf_ptr = old_qf_ptr;
3318 qf_index = old_qf_index;
3319 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003322 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003323 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003324 // No need to print the error message if it's visible in the error
3325 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 print_message = FALSE;
3327
Bram Moolenaarb2443732018-11-11 22:50:27 +01003328 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003329 if (retval == FAIL)
3330 goto failed;
3331 if (retval == NOTDONE)
3332 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003333
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003334 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, oldwin,
3335 &opened_window, old_KeyTyped, print_message);
3336 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003338 // Quickfix/location list is freed by an autocmd
3339 qi = NULL;
3340 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003343 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003346 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003347 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003349 // Couldn't open file, so put index back where it was. This could
3350 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 qf_ptr = old_qf_ptr;
3353 qf_index = old_qf_index;
3354 }
3355 }
3356theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003357 if (qi != NULL)
3358 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003359 qfl->qf_ptr = qf_ptr;
3360 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (p_swb != old_swb && opened_window)
3363 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003364 // Restore old 'switchbuf' value, but not when an autocommand or
3365 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003367 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003369 swb_flags = old_swb_flags;
3370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 else
3372 free_string_option(old_swb);
3373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374}
3375
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003376// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003377static int qfFileAttr;
3378static int qfSepAttr;
3379static int qfLineAttr;
3380
3381/*
3382 * Display information about a single entry from the quickfix/location list.
3383 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003384 * 'cursel' will be set to TRUE for the currently selected entry in the
3385 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003386 */
3387 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003388qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003389{
3390 char_u *fname;
3391 buf_T *buf;
3392 int filter_entry;
3393
3394 fname = NULL;
3395 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3396 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3397 (char *)qfp->qf_module);
3398 else {
3399 if (qfp->qf_fnum != 0
3400 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3401 {
3402 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003403 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003404 fname = gettail(fname);
3405 }
3406 if (fname == NULL)
3407 sprintf((char *)IObuff, "%2d", qf_idx);
3408 else
3409 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3410 qf_idx, (char *)fname);
3411 }
3412
3413 // Support for filtering entries using :filter /pat/ clist
3414 // Match against the module name, file name, search pattern and
3415 // text of the entry.
3416 filter_entry = TRUE;
3417 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3418 filter_entry &= message_filtered(qfp->qf_module);
3419 if (filter_entry && fname != NULL)
3420 filter_entry &= message_filtered(fname);
3421 if (filter_entry && qfp->qf_pattern != NULL)
3422 filter_entry &= message_filtered(qfp->qf_pattern);
3423 if (filter_entry)
3424 filter_entry &= message_filtered(qfp->qf_text);
3425 if (filter_entry)
3426 return;
3427
3428 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003429 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003430
3431 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003432 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003433 if (qfp->qf_lnum == 0)
3434 IObuff[0] = NUL;
3435 else if (qfp->qf_col == 0)
3436 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3437 else
3438 sprintf((char *)IObuff, "%ld col %d",
3439 qfp->qf_lnum, qfp->qf_col);
3440 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3441 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003442 msg_puts_attr((char *)IObuff, qfLineAttr);
3443 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003444 if (qfp->qf_pattern != NULL)
3445 {
3446 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003447 msg_puts((char *)IObuff);
3448 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003449 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003450 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003451
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003452 // Remove newlines and leading whitespace from the text. For an
3453 // unrecognized line keep the indent, the compiler may mark a word
3454 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003455 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3456 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3457 IObuff, IOSIZE);
3458 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003459 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003460}
3461
3462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003464 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 */
3466 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003467qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003469 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003470 qfline_T *qfp;
3471 int i;
3472 int idx1 = 1;
3473 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003474 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003475 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003476 int all = eap->forceit; // if not :cl!, only show
3477 // recognised errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003478 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
Bram Moolenaar39665952018-08-15 20:59:48 +02003480 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003481 {
3482 qi = GET_LOC_LIST(curwin);
3483 if (qi == NULL)
3484 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003485 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003486 return;
3487 }
3488 }
3489
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003490 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003492 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 return;
3494 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003495 if (*arg == '+')
3496 {
3497 ++arg;
3498 plus = TRUE;
3499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3501 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003502 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 return;
3504 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003505 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003506 if (plus)
3507 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003508 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003509 idx2 = i + idx1;
3510 idx1 = i;
3511 }
3512 else
3513 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003514 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003515 if (idx1 < 0)
3516 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3517 if (idx2 < 0)
3518 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003521 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003522 shorten_fnames(FALSE);
3523
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003524 // Get the attributes for the different quickfix highlight items. Note
3525 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003526 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3527 if (qfFileAttr == 0)
3528 qfFileAttr = HL_ATTR(HLF_D);
3529 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3530 if (qfSepAttr == 0)
3531 qfSepAttr = HL_ATTR(HLF_D);
3532 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3533 if (qfLineAttr == 0)
3534 qfLineAttr = HL_ATTR(HLF_N);
3535
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003536 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003538 qfp = qfl->qf_start;
3539 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
3541 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3542 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003543 if (got_int)
3544 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003545
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003546 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003548
3549 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003550 if (qfp == NULL)
3551 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003552 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 ui_breakcheck();
3554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555}
3556
3557/*
3558 * Remove newlines and leading whitespace from an error message.
3559 * Put the result in "buf[bufsize]".
3560 */
3561 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003562qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563{
3564 int i;
3565 char_u *p = text;
3566
3567 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3568 {
3569 if (*p == '\n')
3570 {
3571 buf[i] = ' ';
3572 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003573 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 break;
3575 }
3576 else
3577 buf[i] = *p++;
3578 }
3579 buf[i] = NUL;
3580}
3581
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003582/*
3583 * Display information (list number, list size and the title) about a
3584 * quickfix/location list.
3585 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003586 static void
3587qf_msg(qf_info_T *qi, int which, char *lead)
3588{
3589 char *title = (char *)qi->qf_lists[which].qf_title;
3590 int count = qi->qf_lists[which].qf_count;
3591 char_u buf[IOSIZE];
3592
3593 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3594 lead,
3595 which + 1,
3596 qi->qf_listcount,
3597 count);
3598
3599 if (title != NULL)
3600 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003601 size_t len = STRLEN(buf);
3602
3603 if (len < 34)
3604 {
3605 vim_memset(buf + len, ' ', 34 - len);
3606 buf[34] = NUL;
3607 }
3608 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003609 }
3610 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003611 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003612}
3613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614/*
3615 * ":colder [count]": Up in the quickfix stack.
3616 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003617 * ":lolder [count]": Up in the location list stack.
3618 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 */
3620 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003621qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003623 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 int count;
3625
Bram Moolenaar39665952018-08-15 20:59:48 +02003626 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003627 {
3628 qi = GET_LOC_LIST(curwin);
3629 if (qi == NULL)
3630 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003631 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003632 return;
3633 }
3634 }
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (eap->addr_count != 0)
3637 count = eap->line2;
3638 else
3639 count = 1;
3640 while (count--)
3641 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003642 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003644 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003646 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003647 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003649 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 }
3651 else
3652 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003653 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003655 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003656 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003658 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
3660 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003661 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003662 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663}
3664
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003665/*
3666 * Display the information about all the quickfix/location lists in the stack
3667 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003668 void
3669qf_history(exarg_T *eap)
3670{
3671 qf_info_T *qi = &ql_info;
3672 int i;
3673
Bram Moolenaar39665952018-08-15 20:59:48 +02003674 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003675 qi = GET_LOC_LIST(curwin);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003676 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003677 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003678 else
3679 for (i = 0; i < qi->qf_listcount; ++i)
3680 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3681}
3682
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003684 * Free all the entries in the error list "idx". Note that other information
3685 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 */
3687 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003688qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003690 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003691 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003692 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003694 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003696 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003697 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003698 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003699 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003700 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003701 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003702 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003703 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003704 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003705 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003706 // Somehow qf_count may have an incorrect value, set it to 1
3707 // to avoid crashing when it's wrong.
3708 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003709 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003710 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003711 qfl->qf_start = qfpnext;
3712 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003714
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003715 qfl->qf_index = 0;
3716 qfl->qf_start = NULL;
3717 qfl->qf_last = NULL;
3718 qfl->qf_ptr = NULL;
3719 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003720
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003721 qf_clean_dir_stack(&qfl->qf_dir_stack);
3722 qfl->qf_directory = NULL;
3723 qf_clean_dir_stack(&qfl->qf_file_stack);
3724 qfl->qf_currfile = NULL;
3725 qfl->qf_multiline = FALSE;
3726 qfl->qf_multiignore = FALSE;
3727 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728}
3729
3730/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003731 * Free error list "idx". Frees all the entries in the quickfix list,
3732 * associated context information and the title.
3733 */
3734 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003735qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003736{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003737 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003738
Bram Moolenaard23a8232018-02-10 18:45:26 +01003739 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003740 free_tv(qfl->qf_ctx);
3741 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003742 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003743 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003744}
3745
3746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 * qf_mark_adjust: adjust marks
3748 */
3749 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003750qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003751 win_T *wp,
3752 linenr_T line1,
3753 linenr_T line2,
3754 long amount,
3755 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003757 int i;
3758 qfline_T *qfp;
3759 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003760 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003761 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003762 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763
Bram Moolenaarc1542742016-07-20 21:44:37 +02003764 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003765 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003766 if (wp != NULL)
3767 {
3768 if (wp->w_llist == NULL)
3769 return;
3770 qi = wp->w_llist;
3771 }
3772
3773 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003774 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003775 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003776 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3777 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 if (qfp->qf_fnum == curbuf->b_fnum)
3779 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003780 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3782 {
3783 if (amount == MAXLNUM)
3784 qfp->qf_cleared = TRUE;
3785 else
3786 qfp->qf_lnum += amount;
3787 }
3788 else if (amount_after && qfp->qf_lnum > line2)
3789 qfp->qf_lnum += amount_after;
3790 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003791
3792 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003793 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794}
3795
3796/*
3797 * Make a nice message out of the error character and the error number:
3798 * char number message
3799 * e or E 0 " error"
3800 * w or W 0 " warning"
3801 * i or I 0 " info"
3802 * 0 0 ""
3803 * other 0 " c"
3804 * e or E n " error n"
3805 * w or W n " warning n"
3806 * i or I n " info n"
3807 * 0 n " error n"
3808 * other n " c n"
3809 * 1 x "" :helpgrep
3810 */
3811 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003812qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813{
3814 static char_u buf[20];
3815 static char_u cc[3];
3816 char_u *p;
3817
3818 if (c == 'W' || c == 'w')
3819 p = (char_u *)" warning";
3820 else if (c == 'I' || c == 'i')
3821 p = (char_u *)" info";
3822 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3823 p = (char_u *)" error";
3824 else if (c == 0 || c == 1)
3825 p = (char_u *)"";
3826 else
3827 {
3828 cc[0] = ' ';
3829 cc[1] = c;
3830 cc[2] = NUL;
3831 p = cc;
3832 }
3833
3834 if (nr <= 0)
3835 return p;
3836
3837 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3838 return buf;
3839}
3840
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003842 * When "split" is FALSE: Open the entry/result under the cursor.
3843 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3844 */
3845 void
3846qf_view_result(int split)
3847{
3848 qf_info_T *qi = &ql_info;
3849
3850 if (!bt_quickfix(curbuf))
3851 return;
3852
3853 if (IS_LL_WINDOW(curwin))
3854 qi = GET_LOC_LIST(curwin);
3855
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003856 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003857 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003858 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003859 return;
3860 }
3861
3862 if (split)
3863 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003864 // Open the selected entry in a new window
3865 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3866 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003867 return;
3868 }
3869
3870 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3871}
3872
3873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 * ":cwindow": open the quickfix window if we have errors to display,
3875 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003876 * ":lwindow": open the location list window if we have locations to display,
3877 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 */
3879 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003880ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003882 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003883 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 win_T *win;
3885
Bram Moolenaar39665952018-08-15 20:59:48 +02003886 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003887 {
3888 qi = GET_LOC_LIST(curwin);
3889 if (qi == NULL)
3890 return;
3891 }
3892
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003893 qfl = &qi->qf_lists[qi->qf_curlist];
3894
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003895 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003896 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003898 // If a quickfix window is open but we have no errors to display,
3899 // close the window. If a quickfix window is not open, then open
3900 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003901 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003902 || qfl->qf_nonevalid
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003903 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 {
3905 if (win != NULL)
3906 ex_cclose(eap);
3907 }
3908 else if (win == NULL)
3909 ex_copen(eap);
3910}
3911
3912/*
3913 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003914 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003917ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003919 win_T *win = NULL;
3920 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
Bram Moolenaar39665952018-08-15 20:59:48 +02003922 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003923 {
3924 qi = GET_LOC_LIST(curwin);
3925 if (qi == NULL)
3926 return;
3927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003929 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003930 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 if (win != NULL)
3932 win_close(win, FALSE);
3933}
3934
3935/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003936 * Set "w:quickfix_title" if "qi" has a title.
3937 */
3938 static void
3939qf_set_title_var(qf_list_T *qfl)
3940{
3941 if (qfl->qf_title != NULL)
3942 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3943}
3944
3945/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003946 * Goto a quickfix or location list window (if present).
3947 * Returns OK if the window is found, FAIL otherwise.
3948 */
3949 static int
3950qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3951{
3952 win_T *win;
3953
3954 win = qf_find_win(qi);
3955 if (win == NULL)
3956 return FAIL;
3957
3958 win_goto(win);
3959 if (resize)
3960 {
3961 if (vertsplit)
3962 {
3963 if (sz != win->w_width)
3964 win_setwidth(sz);
3965 }
3966 else if (sz != win->w_height)
3967 win_setheight(sz);
3968 }
3969
3970 return OK;
3971}
3972
3973/*
3974 * Open a new quickfix or location list window, load the quickfix buffer and
3975 * set the appropriate options for the window.
3976 * Returns FAIL if the window could not be opened.
3977 */
3978 static int
3979qf_open_new_cwindow(qf_info_T *qi, int height)
3980{
3981 buf_T *qf_buf;
3982 win_T *oldwin = curwin;
3983 tabpage_T *prevtab = curtab;
3984 int flags = 0;
3985 win_T *win;
3986
3987 qf_buf = qf_find_buf(qi);
3988
3989 // The current window becomes the previous window afterwards.
3990 win = curwin;
3991
3992 if (IS_QF_STACK(qi) && cmdmod.split == 0)
3993 // Create the new quickfix window at the very bottom, except when
3994 // :belowright or :aboveleft is used.
3995 win_goto(lastwin);
3996 // Default is to open the window below the current window
3997 if (cmdmod.split == 0)
3998 flags = WSP_BELOW;
3999 flags |= WSP_NEWLOC;
4000 if (win_split(height, flags) == FAIL)
4001 return FAIL; // not enough room for window
4002 RESET_BINDING(curwin);
4003
4004 if (IS_LL_STACK(qi))
4005 {
4006 // For the location list window, create a reference to the
4007 // location list from the window 'win'.
4008 curwin->w_llist_ref = win->w_llist;
4009 win->w_llist->qf_refcount++;
4010 }
4011
4012 if (oldwin != curwin)
4013 oldwin = NULL; // don't store info when in another window
4014 if (qf_buf != NULL)
4015 {
4016 // Use the existing quickfix buffer
4017 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4018 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4019 }
4020 else
4021 {
4022 // Create a new quickfix buffer
4023 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4024
4025 // switch off 'swapfile'
4026 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4027 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4028 OPT_LOCAL);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004029 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004030 RESET_BINDING(curwin);
4031#ifdef FEAT_DIFF
4032 curwin->w_p_diff = FALSE;
4033#endif
4034#ifdef FEAT_FOLDING
4035 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4036 OPT_LOCAL);
4037#endif
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004038 // save the number of the new buffer
4039 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004040 }
4041
4042 // Only set the height when still in the same tab page and there is no
4043 // window to the side.
4044 if (curtab == prevtab && curwin->w_width == Columns)
4045 win_setheight(height);
4046 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4047 if (win_valid(win))
4048 prevwin = win;
4049
4050 return OK;
4051}
4052
4053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004055 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 */
4057 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004058ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004060 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004061 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004063 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004064 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
Bram Moolenaar39665952018-08-15 20:59:48 +02004066 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004067 {
4068 qi = GET_LOC_LIST(curwin);
4069 if (qi == NULL)
4070 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004071 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004072 return;
4073 }
4074 }
4075
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004076 incr_quickfix_busy();
4077
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 if (eap->addr_count != 0)
4079 height = eap->line2;
4080 else
4081 height = QF_WINHEIGHT;
4082
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004083 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084#ifdef FEAT_GUI
4085 need_mouse_correct = TRUE;
4086#endif
4087
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004088 // Find an existing quickfix window, or open a new one.
4089 if (cmdmod.tab == 0)
4090 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4091 cmdmod.split & WSP_VERT);
4092 if (status == FAIL)
4093 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004094 {
4095 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004096 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004099 qfl = &qi->qf_lists[qi->qf_curlist];
4100 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004101 // Save the current index here, as updating the quickfix buffer may free
4102 // the quickfix list
4103 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004104
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004105 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004106 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004108 decr_quickfix_busy();
4109
4110 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 curwin->w_cursor.col = 0;
4112 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004113 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114}
4115
4116/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004117 * Move the cursor in the quickfix window to "lnum".
4118 */
4119 static void
4120qf_win_goto(win_T *win, linenr_T lnum)
4121{
4122 win_T *old_curwin = curwin;
4123
4124 curwin = win;
4125 curbuf = win->w_buffer;
4126 curwin->w_cursor.lnum = lnum;
4127 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004128 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004129 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004130 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004131 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004132 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004133 curwin = old_curwin;
4134 curbuf = curwin->w_buffer;
4135}
4136
4137/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004138 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004139 */
4140 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004141ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004142{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004143 qf_info_T *qi = &ql_info;
4144 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004145
Bram Moolenaar39665952018-08-15 20:59:48 +02004146 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004147 {
4148 qi = GET_LOC_LIST(curwin);
4149 if (qi == NULL)
4150 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004151 emsg(_(e_loclist));
Bram Moolenaar537ef082016-07-09 17:56:19 +02004152 return;
4153 }
4154 }
4155
4156 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004157 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4158 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4159}
4160
4161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 * Return the number of the current entry (line number in the quickfix
4163 * window).
4164 */
4165 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004166qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004168 qf_info_T *qi = &ql_info;
4169
4170 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004171 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004172 qi = wp->w_llist_ref;
4173
4174 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175}
4176
4177/*
4178 * Update the cursor position in the quickfix window to the current error.
4179 * Return TRUE if there is a quickfix window.
4180 */
4181 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004182qf_win_pos_update(
4183 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004184 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185{
4186 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004187 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004189 // Put the cursor on the current error in the quickfix window, so that
4190 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004191 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 if (win != NULL
4193 && qf_index <= win->w_buffer->b_ml.ml_line_count
4194 && old_qf_index != qf_index)
4195 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 if (qf_index > old_qf_index)
4197 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004198 win->w_redraw_top = old_qf_index;
4199 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 else
4202 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004203 win->w_redraw_top = qf_index;
4204 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004206 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
4208 return win != NULL;
4209}
4210
4211/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004212 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004213 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004214 */
4215 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004216is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004217{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004218 // A window displaying the quickfix buffer will have the w_llist_ref field
4219 // set to NULL.
4220 // A window displaying a location list buffer will have the w_llist_ref
4221 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004222 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004223 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4224 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004225 return TRUE;
4226
4227 return FALSE;
4228}
4229
4230/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004231 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004232 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004233 */
4234 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004235qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004236{
4237 win_T *win;
4238
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004239 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004240 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004241 return win;
4242 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004243}
4244
4245/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004246 * Find a quickfix buffer.
4247 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 */
4249 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004250qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004252 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004253 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004255 if (qi->qf_bufnr != INVALID_QFBUFNR)
4256 {
4257 buf_T *qfbuf;
4258 qfbuf = buflist_findnr(qi->qf_bufnr);
4259 if (qfbuf != NULL)
4260 return qfbuf;
4261 // buffer is no longer present
4262 qi->qf_bufnr = INVALID_QFBUFNR;
4263 }
4264
Bram Moolenaar9c102382006-05-03 21:26:49 +00004265 FOR_ALL_TAB_WINDOWS(tp, win)
4266 if (is_qf_win(win, qi))
4267 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004268
Bram Moolenaar9c102382006-05-03 21:26:49 +00004269 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270}
4271
4272/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004273 * Update the w:quickfix_title variable in the quickfix/location list window
4274 */
4275 static void
4276qf_update_win_titlevar(qf_info_T *qi)
4277{
4278 win_T *win;
4279 win_T *curwin_save;
4280
4281 if ((win = qf_find_win(qi)) != NULL)
4282 {
4283 curwin_save = curwin;
4284 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004285 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004286 curwin = curwin_save;
4287 }
4288}
4289
4290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 * Find the quickfix buffer. If it exists, update the contents.
4292 */
4293 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004294qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295{
4296 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004297 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004300 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004301 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 if (buf != NULL)
4303 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004304 linenr_T old_line_count = buf->b_ml.ml_line_count;
4305
4306 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004307 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004308 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
Bram Moolenaard823fa92016-08-12 16:29:27 +02004310 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004311
Bram Moolenaar864293a2016-06-02 13:40:04 +02004312 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004313 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004314
Bram Moolenaar864293a2016-06-02 13:40:04 +02004315 if (old_last == NULL)
4316 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004317 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004318
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004319 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004320 aucmd_restbuf(&aco);
4321 }
4322
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004323 // Only redraw when added lines are visible. This avoids flickering
4324 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004325 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4326 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328}
4329
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004330/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004331 * Add an error line to the quickfix buffer.
4332 */
4333 static int
4334qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4335{
4336 int len;
4337 buf_T *errbuf;
4338
4339 if (qfp->qf_module != NULL)
4340 {
4341 STRCPY(IObuff, qfp->qf_module);
4342 len = (int)STRLEN(IObuff);
4343 }
4344 else if (qfp->qf_fnum != 0
4345 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4346 && errbuf->b_fname != NULL)
4347 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004348 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004349 STRCPY(IObuff, gettail(errbuf->b_fname));
4350 else
4351 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004352 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004353 if (errbuf->b_sfname == NULL
4354 || mch_isFullName(errbuf->b_sfname))
4355 {
4356 if (*dirname == NUL)
4357 mch_dirname(dirname, MAXPATHL);
4358 shorten_buf_fname(errbuf, dirname, FALSE);
4359 }
4360 STRCPY(IObuff, errbuf->b_fname);
4361 }
4362 len = (int)STRLEN(IObuff);
4363 }
4364 else
4365 len = 0;
4366 IObuff[len++] = '|';
4367
4368 if (qfp->qf_lnum > 0)
4369 {
4370 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4371 len += (int)STRLEN(IObuff + len);
4372
4373 if (qfp->qf_col > 0)
4374 {
4375 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4376 len += (int)STRLEN(IObuff + len);
4377 }
4378
4379 sprintf((char *)IObuff + len, "%s",
4380 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4381 len += (int)STRLEN(IObuff + len);
4382 }
4383 else if (qfp->qf_pattern != NULL)
4384 {
4385 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4386 len += (int)STRLEN(IObuff + len);
4387 }
4388 IObuff[len++] = '|';
4389 IObuff[len++] = ' ';
4390
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004391 // Remove newlines and leading whitespace from the text.
4392 // For an unrecognized line keep the indent, the compiler may
4393 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004394 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4395 IObuff + len, IOSIZE - len);
4396
4397 if (ml_append_buf(buf, lnum, IObuff,
4398 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4399 return FAIL;
4400
4401 return OK;
4402}
4403
4404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 * Fill current buffer with quickfix errors, replacing any previous contents.
4406 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004407 * If "old_last" is not NULL append the items after this one.
4408 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4409 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 */
4411 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004412qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004414 linenr_T lnum;
4415 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004416 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
Bram Moolenaar864293a2016-06-02 13:40:04 +02004418 if (old_last == NULL)
4419 {
4420 if (buf != curbuf)
4421 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004422 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004423 return;
4424 }
4425
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004426 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004427 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4428 (void)ml_delete((linenr_T)1, FALSE);
4429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004431 // Check if there is anything to display
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004432 if (!qf_stack_empty(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004434 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4435 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004436
4437 *dirname = NUL;
4438
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004439 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004440 if (old_last == NULL)
4441 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004442 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004443 lnum = 0;
4444 }
4445 else
4446 {
4447 qfp = old_last->qf_next;
4448 lnum = buf->b_ml.ml_line_count;
4449 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004450 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004452 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004454
Bram Moolenaar864293a2016-06-02 13:40:04 +02004455 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004457 if (qfp == NULL)
4458 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004460
4461 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004462 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004463 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004466 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 check_lnums(TRUE);
4468
Bram Moolenaar864293a2016-06-02 13:40:04 +02004469 if (old_last == NULL)
4470 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004471 // Set the 'filetype' to "qf" each time after filling the buffer.
4472 // This resembles reading a file into a buffer, it's more logical when
4473 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004474 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004475 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4476 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004478 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004479 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004481 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004483 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004484 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004485
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004486 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004487 redraw_curbuf_later(NOT_VALID);
4488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004490 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 KeyTyped = old_KeyTyped;
4492}
4493
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004494/*
4495 * For every change made to the quickfix list, update the changed tick.
4496 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004497 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004498qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004499{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004500 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004501}
4502
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004504 * Return the quickfix/location list number with the given identifier.
4505 * Returns -1 if list is not found.
4506 */
4507 static int
4508qf_id2nr(qf_info_T *qi, int_u qfid)
4509{
4510 int qf_idx;
4511
4512 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4513 if (qi->qf_lists[qf_idx].qf_id == qfid)
4514 return qf_idx;
4515 return INVALID_QFIDX;
4516}
4517
4518/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004519 * If the current list is not "save_qfid" and we can find the list with that ID
4520 * then make it the current list.
4521 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004522 * Returns OK if successfully restored the list. Returns FAIL if the list with
4523 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004524 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004525 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004526qf_restore_list(qf_info_T *qi, int_u save_qfid)
4527{
4528 int curlist;
4529
4530 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4531 {
4532 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004533 if (curlist < 0)
4534 // list is not present
4535 return FAIL;
4536 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004537 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004538 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004539}
4540
4541/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004542 * Jump to the first entry if there is one.
4543 */
4544 static void
4545qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4546{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004547 if (qf_restore_list(qi, save_qfid) == FAIL)
4548 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004549
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004550 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004551 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004552 qf_jump(qi, 0, 0, forceit);
4553}
4554
4555/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004556 * Return TRUE when using ":vimgrep" for ":grep".
4557 */
4558 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004559grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004560{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004561 return ((cmdidx == CMD_grep
4562 || cmdidx == CMD_lgrep
4563 || cmdidx == CMD_grepadd
4564 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004565 && STRCMP("internal",
4566 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4567}
4568
4569/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004570 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004572 static char_u *
4573make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004575 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004576 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004577 case CMD_make: return (char_u *)"make";
4578 case CMD_lmake: return (char_u *)"lmake";
4579 case CMD_grep: return (char_u *)"grep";
4580 case CMD_lgrep: return (char_u *)"lgrep";
4581 case CMD_grepadd: return (char_u *)"grepadd";
4582 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4583 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585}
4586
4587/*
4588 * Return the name for the errorfile, in allocated memory.
4589 * Find a new unique name when 'makeef' contains "##".
4590 * Returns NULL for error.
4591 */
4592 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004593get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594{
4595 char_u *p;
4596 char_u *name;
4597 static int start = -1;
4598 static int off = 0;
4599#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004600 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601#endif
4602
4603 if (*p_mef == NUL)
4604 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004605 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004607 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 return name;
4609 }
4610
4611 for (p = p_mef; *p; ++p)
4612 if (p[0] == '#' && p[1] == '#')
4613 break;
4614
4615 if (*p == NUL)
4616 return vim_strsave(p_mef);
4617
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004618 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 for (;;)
4620 {
4621 if (start == -1)
4622 start = mch_get_pid();
4623 else
4624 off += 19;
4625
4626 name = alloc((unsigned)STRLEN(p_mef) + 30);
4627 if (name == NULL)
4628 break;
4629 STRCPY(name, p_mef);
4630 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4631 STRCAT(name, p + 2);
4632 if (mch_getperm(name) < 0
4633#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004634 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 && mch_lstat((char *)name, &sb) < 0
4636#endif
4637 )
4638 break;
4639 vim_free(name);
4640 }
4641 return name;
4642}
4643
4644/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004645 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4646 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4647 */
4648 static char_u *
4649make_get_fullcmd(char_u *makecmd, char_u *fname)
4650{
4651 char_u *cmd;
4652 unsigned len;
4653
4654 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4655 if (*p_sp != NUL)
4656 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4657 cmd = alloc(len);
4658 if (cmd == NULL)
4659 return NULL;
4660 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4661 (char *)p_shq);
4662
4663 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4664 if (*p_sp != NUL)
4665 append_redir(cmd, len, p_sp, fname);
4666
4667 // Display the fully formed command. Output a newline if there's something
4668 // else than the :make command that was typed (in which case the cursor is
4669 // in column 0).
4670 if (msg_col == 0)
4671 msg_didout = FALSE;
4672 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004673 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004674 msg_outtrans(cmd); // show what we are doing
4675
4676 return cmd;
4677}
4678
4679/*
4680 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4681 */
4682 void
4683ex_make(exarg_T *eap)
4684{
4685 char_u *fname;
4686 char_u *cmd;
4687 char_u *enc = NULL;
4688 win_T *wp = NULL;
4689 qf_info_T *qi = &ql_info;
4690 int res;
4691 char_u *au_name = NULL;
4692 int_u save_qfid;
4693
4694 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4695 if (grep_internal(eap->cmdidx))
4696 {
4697 ex_vimgrep(eap);
4698 return;
4699 }
4700
4701 au_name = make_get_auname(eap->cmdidx);
4702 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4703 curbuf->b_fname, TRUE, curbuf))
4704 {
4705#ifdef FEAT_EVAL
4706 if (aborting())
4707 return;
4708#endif
4709 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004710 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004711
4712 if (is_loclist_cmd(eap->cmdidx))
4713 wp = curwin;
4714
4715 autowrite_all();
4716 fname = get_mef_name();
4717 if (fname == NULL)
4718 return;
4719 mch_remove(fname); // in case it's not unique
4720
4721 cmd = make_get_fullcmd(eap->arg, fname);
4722 if (cmd == NULL)
4723 return;
4724
4725 // let the shell know if we are redirecting output or not
4726 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4727
4728#ifdef AMIGA
4729 out_flush();
4730 // read window status report and redraw before message
4731 (void)char_avail();
4732#endif
4733
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004734 incr_quickfix_busy();
4735
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004736 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4737 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4738 (eap->cmdidx != CMD_grepadd
4739 && eap->cmdidx != CMD_lgrepadd),
4740 qf_cmdtitle(*eap->cmdlinep), enc);
4741 if (wp != NULL)
4742 {
4743 qi = GET_LOC_LIST(wp);
4744 if (qi == NULL)
4745 goto cleanup;
4746 }
4747 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004748 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004749
4750 // Remember the current quickfix list identifier, so that we can
4751 // check for autocommands changing the current quickfix list.
4752 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4753 if (au_name != NULL)
4754 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4755 curbuf->b_fname, TRUE, curbuf);
4756 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4757 // display the first error
4758 qf_jump_first(qi, save_qfid, FALSE);
4759
4760cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004761 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004762 mch_remove(fname);
4763 vim_free(fname);
4764 vim_free(cmd);
4765}
4766
4767/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004768 * Returns the number of valid entries in the current quickfix/location list.
4769 */
4770 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004771qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004772{
4773 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004774 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004775 qfline_T *qfp;
4776 int i, sz = 0;
4777 int prev_fnum = 0;
4778
Bram Moolenaar39665952018-08-15 20:59:48 +02004779 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004780 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004781 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004782 qi = GET_LOC_LIST(curwin);
4783 if (qi == NULL)
4784 return 0;
4785 }
4786
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004787 qfl = &qi->qf_lists[qi->qf_curlist];
4788 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004789 ++i, qfp = qfp->qf_next)
4790 {
4791 if (qfp->qf_valid)
4792 {
4793 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004794 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004795 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4796 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004797 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004798 sz++;
4799 prev_fnum = qfp->qf_fnum;
4800 }
4801 }
4802 }
4803
4804 return sz;
4805}
4806
4807/*
4808 * Returns the current index of the quickfix/location list.
4809 * Returns 0 if there is an error.
4810 */
4811 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004812qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004813{
4814 qf_info_T *qi = &ql_info;
4815
Bram Moolenaar39665952018-08-15 20:59:48 +02004816 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004817 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004818 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004819 qi = GET_LOC_LIST(curwin);
4820 if (qi == NULL)
4821 return 0;
4822 }
4823
4824 return qi->qf_lists[qi->qf_curlist].qf_index;
4825}
4826
4827/*
4828 * Returns the current index in the quickfix/location list (counting only valid
4829 * entries). If no valid entries are in the list, then returns 1.
4830 */
4831 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004832qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004833{
4834 qf_info_T *qi = &ql_info;
4835 qf_list_T *qfl;
4836 qfline_T *qfp;
4837 int i, eidx = 0;
4838 int prev_fnum = 0;
4839
Bram Moolenaar39665952018-08-15 20:59:48 +02004840 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004841 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004842 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004843 qi = GET_LOC_LIST(curwin);
4844 if (qi == NULL)
4845 return 1;
4846 }
4847
4848 qfl = &qi->qf_lists[qi->qf_curlist];
4849 qfp = qfl->qf_start;
4850
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004851 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004852 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4853 return 1;
4854
4855 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4856 {
4857 if (qfp->qf_valid)
4858 {
4859 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4860 {
4861 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4862 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004863 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004864 eidx++;
4865 prev_fnum = qfp->qf_fnum;
4866 }
4867 }
4868 else
4869 eidx++;
4870 }
4871 }
4872
4873 return eidx ? eidx : 1;
4874}
4875
4876/*
4877 * Get the 'n'th valid error entry in the quickfix or location list.
4878 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4879 * For :cdo and :ldo returns the 'n'th valid error entry.
4880 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4881 */
4882 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004883qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004884{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004885 qfline_T *qfp = qfl->qf_start;
4886 int i, eidx;
4887 int prev_fnum = 0;
4888
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004889 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004890 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4891 return 1;
4892
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004893 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004894 i++, qfp = qfp->qf_next)
4895 {
4896 if (qfp->qf_valid)
4897 {
4898 if (fdo)
4899 {
4900 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4901 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004902 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004903 eidx++;
4904 prev_fnum = qfp->qf_fnum;
4905 }
4906 }
4907 else
4908 eidx++;
4909 }
4910
4911 if (eidx == n)
4912 break;
4913 }
4914
4915 if (i <= qfl->qf_count)
4916 return i;
4917 else
4918 return 1;
4919}
4920
4921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004923 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004924 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 */
4926 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004927ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004929 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004930 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004931
Bram Moolenaar39665952018-08-15 20:59:48 +02004932 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004933 {
4934 qi = GET_LOC_LIST(curwin);
4935 if (qi == NULL)
4936 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004937 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004938 return;
4939 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004940 }
4941
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004942 if (eap->addr_count > 0)
4943 errornr = (int)eap->line2;
4944 else
4945 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004946 switch (eap->cmdidx)
4947 {
4948 case CMD_cc: case CMD_ll:
4949 errornr = 0;
4950 break;
4951 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4952 case CMD_lfirst:
4953 errornr = 1;
4954 break;
4955 default:
4956 errornr = 32767;
4957 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004958 }
4959
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004960 // For cdo and ldo commands, jump to the nth valid error.
4961 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02004962 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4963 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004964 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004965 eap->addr_count > 0 ? (int)eap->line1 : 1,
4966 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4967
4968 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969}
4970
4971/*
4972 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004973 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004974 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 */
4976 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004977ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004979 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004980 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004981 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004982
Bram Moolenaar39665952018-08-15 20:59:48 +02004983 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004984 {
4985 qi = GET_LOC_LIST(curwin);
4986 if (qi == NULL)
4987 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004988 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004989 return;
4990 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004991 }
4992
Bram Moolenaar55b69262017-08-13 13:42:01 +02004993 if (eap->addr_count > 0
4994 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4995 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004996 errornr = (int)eap->line2;
4997 else
4998 errornr = 1;
4999
Bram Moolenaar39665952018-08-15 20:59:48 +02005000 // Depending on the command jump to either next or previous entry/file.
5001 switch (eap->cmdidx)
5002 {
5003 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5004 dir = FORWARD;
5005 break;
5006 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5007 case CMD_lNext:
5008 dir = BACKWARD;
5009 break;
5010 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5011 dir = FORWARD_FILE;
5012 break;
5013 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5014 dir = BACKWARD_FILE;
5015 break;
5016 default:
5017 dir = FORWARD;
5018 break;
5019 }
5020
5021 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022}
5023
5024/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005025 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005026 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 */
5028 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005029ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005031 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005032 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005033 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005034 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005035 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005036 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005037
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005038 switch (eap->cmdidx)
5039 {
5040 case CMD_cfile: au_name = (char_u *)"cfile"; break;
5041 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
5042 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
5043 case CMD_lfile: au_name = (char_u *)"lfile"; break;
5044 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
5045 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
5046 default: break;
5047 }
5048 if (au_name != NULL)
5049 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005050 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005051#ifdef FEAT_BROWSE
5052 if (cmdmod.browse)
5053 {
5054 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005055 NULL, NULL,
5056 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005057 if (browse_file == NULL)
5058 return;
5059 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5060 vim_free(browse_file);
5061 }
5062 else
5063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005065 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005066
Bram Moolenaar39665952018-08-15 20:59:48 +02005067 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005068 wp = curwin;
5069
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005070 incr_quickfix_busy();
5071
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005072 // This function is used by the :cfile, :cgetfile and :caddfile
5073 // commands.
5074 // :cfile always creates a new quickfix list and jumps to the
5075 // first error.
5076 // :cgetfile creates a new quickfix list but doesn't jump to the
5077 // first error.
5078 // :caddfile adds to an existing quickfix list. If there is no
5079 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005080 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005081 && eap->cmdidx != CMD_laddfile),
5082 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005083 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005084 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005085 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005086 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005087 {
5088 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005089 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005090 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005091 }
5092 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005093 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005094 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005095 if (au_name != NULL)
5096 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005097
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005098 // Jump to the first error for a new list and if autocmds didn't
5099 // free the list.
5100 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5101 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005102 // display the first error
5103 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005104
5105 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005106}
5107
5108/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005109 * Return the vimgrep autocmd name.
5110 */
5111 static char_u *
5112vgr_get_auname(cmdidx_T cmdidx)
5113{
5114 switch (cmdidx)
5115 {
5116 case CMD_vimgrep: return (char_u *)"vimgrep";
5117 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5118 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5119 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5120 case CMD_grep: return (char_u *)"grep";
5121 case CMD_lgrep: return (char_u *)"lgrep";
5122 case CMD_grepadd: return (char_u *)"grepadd";
5123 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5124 default: return NULL;
5125 }
5126}
5127
5128/*
5129 * Initialize the regmatch used by vimgrep for pattern "s".
5130 */
5131 static void
5132vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5133{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005134 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005135 regmatch->regprog = NULL;
5136
5137 if (s == NULL || *s == NUL)
5138 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005139 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005140 if (last_search_pat() == NULL)
5141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005142 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005143 return;
5144 }
5145 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5146 }
5147 else
5148 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5149
5150 regmatch->rmm_ic = p_ic;
5151 regmatch->rmm_maxcol = 0;
5152}
5153
5154/*
5155 * Display a file name when vimgrep is running.
5156 */
5157 static void
5158vgr_display_fname(char_u *fname)
5159{
5160 char_u *p;
5161
5162 msg_start();
5163 p = msg_strtrunc(fname, TRUE);
5164 if (p == NULL)
5165 msg_outtrans(fname);
5166 else
5167 {
5168 msg_outtrans(p);
5169 vim_free(p);
5170 }
5171 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005172 msg_didout = FALSE; // overwrite this message
5173 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005174 msg_col = 0;
5175 out_flush();
5176}
5177
5178/*
5179 * Load a dummy buffer to search for a pattern using vimgrep.
5180 */
5181 static buf_T *
5182vgr_load_dummy_buf(
5183 char_u *fname,
5184 char_u *dirname_start,
5185 char_u *dirname_now)
5186{
5187 int save_mls;
5188#if defined(FEAT_SYN_HL)
5189 char_u *save_ei = NULL;
5190#endif
5191 buf_T *buf;
5192
5193#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005194 // Don't do Filetype autocommands to avoid loading syntax and
5195 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005196 save_ei = au_event_disable(",Filetype");
5197#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005198 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005199 save_mls = p_mls;
5200 p_mls = 0;
5201
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005202 // Load file into a buffer, so that 'fileencoding' is detected,
5203 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005204 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5205
5206 p_mls = save_mls;
5207#if defined(FEAT_SYN_HL)
5208 au_event_restore(save_ei);
5209#endif
5210
5211 return buf;
5212}
5213
5214/*
5215 * Check whether a quickfix/location list valid. Autocmds may remove or change
5216 * a quickfix list when vimgrep is running. If the list is not found, create a
5217 * new list.
5218 */
5219 static int
5220vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005221 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005222 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005223 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005224 char_u *title)
5225{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005226 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005227 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005228 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005229 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005230 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005231 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005232 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005233 return FALSE;
5234 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005235 else
5236 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005237 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005238 qf_new_list(qi, title);
5239 return TRUE;
5240 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005241 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005242
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005243 if (qf_restore_list(qi, qfid) == FAIL)
5244 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005245
5246 return TRUE;
5247}
5248
5249/*
5250 * Search for a pattern in all the lines in a buffer and add the matching lines
5251 * to a quickfix list.
5252 */
5253 static int
5254vgr_match_buflines(
5255 qf_info_T *qi,
5256 char_u *fname,
5257 buf_T *buf,
5258 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005259 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005260 int duplicate_name,
5261 int flags)
5262{
5263 int found_match = FALSE;
5264 long lnum;
5265 colnr_T col;
5266
Bram Moolenaar1c299432018-10-28 14:36:09 +01005267 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005268 {
5269 col = 0;
5270 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5271 col, NULL, NULL) > 0)
5272 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005273 // Pass the buffer number so that it gets used even for a
5274 // dummy buffer, unless duplicate_name is set, then the
5275 // buffer will be wiped out below.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005276 if (qf_add_entry(qi,
5277 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005278 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005279 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005280 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005281 duplicate_name ? 0 : buf->b_fnum,
5282 ml_get_buf(buf,
5283 regmatch->startpos[0].lnum + lnum, FALSE),
5284 regmatch->startpos[0].lnum + lnum,
5285 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005286 FALSE, // vis_col
5287 NULL, // search pattern
5288 0, // nr
5289 0, // type
5290 TRUE // valid
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005291 ) == FAIL)
5292 {
5293 got_int = TRUE;
5294 break;
5295 }
5296 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005297 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005298 break;
5299 if ((flags & VGR_GLOBAL) == 0
5300 || regmatch->endpos[0].lnum > 0)
5301 break;
5302 col = regmatch->endpos[0].col
5303 + (col == regmatch->endpos[0].col);
5304 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5305 break;
5306 }
5307 line_breakcheck();
5308 if (got_int)
5309 break;
5310 }
5311
5312 return found_match;
5313}
5314
5315/*
5316 * Jump to the first match and update the directory.
5317 */
5318 static void
5319vgr_jump_to_match(
5320 qf_info_T *qi,
5321 int forceit,
5322 int *redraw_for_dummy,
5323 buf_T *first_match_buf,
5324 char_u *target_dir)
5325{
5326 buf_T *buf;
5327
5328 buf = curbuf;
5329 qf_jump(qi, 0, 0, forceit);
5330 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005331 // If we jumped to another buffer redrawing will already be
5332 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005333 *redraw_for_dummy = FALSE;
5334
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005335 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005336 if (curbuf == first_match_buf && target_dir != NULL)
5337 {
5338 exarg_T ea;
5339
5340 ea.arg = target_dir;
5341 ea.cmdidx = CMD_lcd;
5342 ex_cd(&ea);
5343 }
5344}
5345
5346/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005347 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005348 * ":vimgrepadd {pattern} file(s)"
5349 * ":lvimgrep {pattern} file(s)"
5350 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005351 */
5352 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005353ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005354{
Bram Moolenaar81695252004-12-29 20:58:21 +00005355 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005356 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005357 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005358 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005359 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005360 char_u *s;
5361 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005362 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005363 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005364 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005365 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005366 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005367 buf_T *buf;
5368 int duplicate_name = FALSE;
5369 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005370 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005371 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005372 buf_T *first_match_buf = NULL;
5373 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005374 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005375 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005376 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005377 char_u *dirname_start = NULL;
5378 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005379 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005380 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005381
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005382 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005383 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5384 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005385 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005386#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005387 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005388 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005389#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005390 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005391
Bram Moolenaar39665952018-08-15 20:59:48 +02005392 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005393 {
5394 qi = ll_get_or_alloc_list(curwin);
5395 if (qi == NULL)
5396 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005397 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005398 }
5399
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005400 if (eap->addr_count > 0)
5401 tomatch = eap->line2;
5402 else
5403 tomatch = MAXLNUM;
5404
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005405 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005406 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005407 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005408 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005409 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005410 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005411 emsg(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005412 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005413 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005414
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005415 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005416 if (regmatch.regprog == NULL)
5417 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005418
5419 p = skipwhite(p);
5420 if (*p == NUL)
5421 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005422 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005423 goto theend;
5424 }
5425
Bram Moolenaar55b69262017-08-13 13:42:01 +02005426 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005427 && eap->cmdidx != CMD_vimgrepadd
5428 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005429 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005430 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005431 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005432
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005433 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005434 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005435 goto theend;
5436 if (fcount == 0)
5437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005438 emsg(_(e_nomatch));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005439 goto theend;
5440 }
5441
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005442 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5443 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005444 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005445 {
5446 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005447 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005448 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005449
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005450 // Remember the current directory, because a BufRead autocommand that does
5451 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005452 mch_dirname(dirname_start, MAXPATHL);
5453
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005454 incr_quickfix_busy();
5455
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005456 // Remember the current quickfix list identifier, so that we can check for
5457 // autocommands changing the current quickfix list.
Bram Moolenaar3c097222017-12-21 20:54:49 +01005458 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005459
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005460 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005461 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005462 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005463 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005464 if (time(NULL) > seconds)
5465 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005466 // Display the file name every second or so, show the user we are
5467 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005468 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005469 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005470 }
5471
Bram Moolenaar81695252004-12-29 20:58:21 +00005472 buf = buflist_findname_exp(fnames[fi]);
5473 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5474 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005475 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005476 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005477 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005478 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005479
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005480 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005481 }
5482 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005483 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005484 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005485
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005486 // Check whether the quickfix list is still valid. When loading a
5487 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005488 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005489 {
5490 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005491 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005492 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005493 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005494 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005495
Bram Moolenaar81695252004-12-29 20:58:21 +00005496 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005497 {
5498 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005499 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005500 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005501 else
5502 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005503 // Try for a match in all lines of the buffer.
5504 // For ":1vimgrep" look for first match only.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005505 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005506 &tomatch, duplicate_name, flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005507
Bram Moolenaar81695252004-12-29 20:58:21 +00005508 if (using_dummy)
5509 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005510 if (found_match && first_match_buf == NULL)
5511 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005512 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005513 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005514 // Never keep a dummy buffer if there is another buffer
5515 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005516 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005517 buf = NULL;
5518 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005519 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005520 || buf->b_p_bh[0] == 'u' // "unload"
5521 || buf->b_p_bh[0] == 'w' // "wipe"
5522 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005523 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005524 // When no match was found we don't need to remember the
5525 // buffer, wipe it out. If there was a match and it
5526 // wasn't the first one or we won't jump there: only
5527 // unload the buffer.
5528 // Ignore 'hidden' here, because it may lead to having too
5529 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005530 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005531 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005532 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005533 buf = NULL;
5534 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005535 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005536 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005537 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005538 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005539 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005540 buf = NULL;
5541 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005542 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005543
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005544 if (buf != NULL)
5545 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005546 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005547 buf->b_flags &= ~BF_DUMMY;
5548
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005549 // If the buffer is still loaded we need to use the
5550 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005551 if (buf == first_match_buf
5552 && target_dir == NULL
5553 && STRCMP(dirname_start, dirname_now) != 0)
5554 target_dir = vim_strsave(dirname_now);
5555
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005556 // The buffer is still loaded, the Filetype autocommands
5557 // need to be done now, in that buffer. And the modelines
5558 // need to be done (again). But not the window-local
5559 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005560 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005561#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005562 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5563 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005564#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005565 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005566 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005567 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005568 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005569 }
5570 }
5571
5572 FreeWild(fcount, fnames);
5573
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005574 qfl = &qi->qf_lists[qi->qf_curlist];
5575 qfl->qf_nonevalid = FALSE;
5576 qfl->qf_ptr = qfl->qf_start;
5577 qfl->qf_index = 1;
5578 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005579
Bram Moolenaar864293a2016-06-02 13:40:04 +02005580 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005581
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005582 if (au_name != NULL)
5583 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5584 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005585 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5586 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005587 if (!qflist_valid(wp, save_qfid)
5588 || qf_restore_list(qi, save_qfid) == FAIL)
5589 {
5590 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005591 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005592 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005593
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005594 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005595 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005596 {
5597 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005598 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5599 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005600 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005601 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005602 semsg(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005603
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005604 decr_quickfix_busy();
5605
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005606 // If we loaded a dummy buffer into the current window, the autocommands
5607 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005608 if (redraw_for_dummy)
5609 {
5610#ifdef FEAT_FOLDING
5611 foldUpdateAll(curwin);
5612#else
5613 redraw_later(NOT_VALID);
5614#endif
5615 }
5616
Bram Moolenaar86b68352004-12-27 21:59:20 +00005617theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005618 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005619 vim_free(dirname_now);
5620 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005621 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005622 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005623}
5624
5625/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005626 * Restore current working directory to "dirname_start" if they differ, taking
5627 * into account whether it is set locally or globally.
5628 */
5629 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005630restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005631{
5632 char_u *dirname_now = alloc(MAXPATHL);
5633
5634 if (NULL != dirname_now)
5635 {
5636 mch_dirname(dirname_now, MAXPATHL);
5637 if (STRCMP(dirname_start, dirname_now) != 0)
5638 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005639 // If the directory has changed, change it back by building up an
5640 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005641 exarg_T ea;
5642
5643 ea.arg = dirname_start;
5644 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5645 ex_cd(&ea);
5646 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005647 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005648 }
5649}
5650
5651/*
5652 * Load file "fname" into a dummy buffer and return the buffer pointer,
5653 * placing the directory resulting from the buffer load into the
5654 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5655 * prior to calling this function. Restores directory to "dirname_start" prior
5656 * to returning, if autocmds or the 'autochdir' option have changed it.
5657 *
5658 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5659 * or wipe_dummy_buffer() later!
5660 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005661 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005662 */
5663 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005664load_dummy_buffer(
5665 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005666 char_u *dirname_start, // in: old directory
5667 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005668{
5669 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005670 bufref_T newbufref;
5671 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005672 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005673 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005674 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005675
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005676 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005677 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5678 if (newbuf == NULL)
5679 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005680 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005681
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005682 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005683 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5684
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005685 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005686 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005687 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005688 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005689 ++newbuf->b_locked;
5690
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005691 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005692 aucmd_prepbuf(&aco, newbuf);
5693
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005694 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005695 (void)setfname(curbuf, fname, NULL, FALSE);
5696
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005697 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005698 check_need_swap(TRUE);
5699
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005700 // Remove the "dummy" flag, otherwise autocommands may not
5701 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005702 curbuf->b_flags &= ~BF_DUMMY;
5703
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005704 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005705 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005706 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005707 NULL, READ_NEW | READ_DUMMY);
5708 --newbuf->b_locked;
5709 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005710 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005711 && !(curbuf->b_flags & BF_NEW))
5712 {
5713 failed = FALSE;
5714 if (curbuf != newbuf)
5715 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005716 // Bloody autocommands changed the buffer! Can happen when
5717 // using netrw and editing a remote file. Use the current
5718 // buffer instead, delete the dummy one after restoring the
5719 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005720 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005721 newbuf = curbuf;
5722 }
5723 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005724
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005725 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005726 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005727 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5728 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005729
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005730 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5731 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005732 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005733 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005734
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005735 // When autocommands/'autochdir' option changed directory: go back.
5736 // Let the caller know what the resulting dir was first, in case it is
5737 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005738 mch_dirname(resulting_dir, MAXPATHL);
5739 restore_start_dir(dirname_start);
5740
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005741 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005742 return NULL;
5743 if (failed)
5744 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005745 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005746 return NULL;
5747 }
5748 return newbuf;
5749}
5750
5751/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005752 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5753 * directory to "dirname_start" prior to returning, if autocmds or the
5754 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005755 */
5756 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005757wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005758{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005759 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005760 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005761#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005762 cleanup_T cs;
5763
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005764 // Reset the error/interrupt/exception state here so that aborting()
5765 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5766 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005767 enter_cleanup(&cs);
5768#endif
5769
Bram Moolenaar81695252004-12-29 20:58:21 +00005770 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005771
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005772#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005773 // Restore the error/interrupt/exception state if not discarded by a
5774 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005775 leave_cleanup(&cs);
5776#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005777 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005778 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005779 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005780}
5781
5782/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005783 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5784 * directory to "dirname_start" prior to returning, if autocmds or the
5785 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005786 */
5787 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005788unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005789{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005790 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005791 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005792 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005793
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005794 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005795 restore_start_dir(dirname_start);
5796 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005797}
5798
Bram Moolenaar05159a02005-02-26 23:04:13 +00005799#if defined(FEAT_EVAL) || defined(PROTO)
5800/*
5801 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005802 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005803 */
5804 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005805get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005806{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005807 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005808 dict_T *dict;
5809 char_u buf[2];
5810 qfline_T *qfp;
5811 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005812 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005813
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005814 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005815 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005816 qi = &ql_info;
5817 if (wp != NULL)
5818 {
5819 qi = GET_LOC_LIST(wp);
5820 if (qi == NULL)
5821 return FAIL;
5822 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005823 }
5824
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005825 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005826 qf_idx = qi->qf_curlist;
5827
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005828 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005829 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005830
Bram Moolenaard823fa92016-08-12 16:29:27 +02005831 qfp = qi->qf_lists[qf_idx].qf_start;
5832 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005833 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005834 // Handle entries with a non-existing buffer number.
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005835 bufnum = qfp->qf_fnum;
5836 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5837 bufnum = 0;
5838
Bram Moolenaar05159a02005-02-26 23:04:13 +00005839 if ((dict = dict_alloc()) == NULL)
5840 return FAIL;
5841 if (list_append_dict(list, dict) == FAIL)
5842 return FAIL;
5843
5844 buf[0] = qfp->qf_type;
5845 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005846 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5847 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5848 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5849 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5850 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5851 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5852 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5853 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5854 || dict_add_string(dict, "type", buf) == FAIL
5855 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005856 return FAIL;
5857
5858 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005859 if (qfp == NULL)
5860 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005861 }
5862 return OK;
5863}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005864
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005865// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005866enum {
5867 QF_GETLIST_NONE = 0x0,
5868 QF_GETLIST_TITLE = 0x1,
5869 QF_GETLIST_ITEMS = 0x2,
5870 QF_GETLIST_NR = 0x4,
5871 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005872 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005873 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005874 QF_GETLIST_IDX = 0x40,
5875 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005876 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005877 QF_GETLIST_FILEWINID = 0x200,
5878 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005879};
5880
5881/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005882 * Parse text from 'di' and return the quickfix list items.
5883 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005884 */
5885 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005886qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005887{
5888 int status = FAIL;
5889 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005890 char_u *errorformat = p_efm;
5891 dictitem_T *efm_di;
5892 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005893
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005894 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005895 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005896 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005897 // If errorformat is supplied then use it, otherwise use the 'efm'
5898 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005899 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5900 {
5901 if (efm_di->di_tv.v_type != VAR_STRING ||
5902 efm_di->di_tv.vval.v_string == NULL)
5903 return FAIL;
5904 errorformat = efm_di->di_tv.vval.v_string;
5905 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005906
Bram Moolenaar36538222017-09-02 19:51:44 +02005907 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005908 if (l == NULL)
5909 return FAIL;
5910
Bram Moolenaar2d67d302018-11-16 18:46:02 +01005911 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005912 if (qi != NULL)
5913 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005914 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005915 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5916 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005917 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005918 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005919 }
5920 free(qi);
5921 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005922 dict_add_list(retdict, "items", l);
5923 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005924 }
5925
5926 return status;
5927}
5928
5929/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005930 * Return the quickfix/location list window identifier in the current tabpage.
5931 */
5932 static int
5933qf_winid(qf_info_T *qi)
5934{
5935 win_T *win;
5936
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005937 // The quickfix window can be opened even if the quickfix list is not set
5938 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005939 if (qi == NULL)
5940 return 0;
5941 win = qf_find_win(qi);
5942 if (win != NULL)
5943 return win->w_id;
5944 return 0;
5945}
5946
5947/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005948 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005949 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005950 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005951qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005952{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005953 int flags = QF_GETLIST_NONE;
5954
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005955 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005956 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005957 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005958 if (!loclist)
5959 // File window ID is applicable only to location list windows
5960 flags &= ~ QF_GETLIST_FILEWINID;
5961 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005962
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005963 if (dict_find(what, (char_u *)"title", -1) != NULL)
5964 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005965
Bram Moolenaara6d48492017-12-12 22:45:31 +01005966 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5967 flags |= QF_GETLIST_NR;
5968
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005969 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5970 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005971
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005972 if (dict_find(what, (char_u *)"context", -1) != NULL)
5973 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005974
Bram Moolenaara6d48492017-12-12 22:45:31 +01005975 if (dict_find(what, (char_u *)"id", -1) != NULL)
5976 flags |= QF_GETLIST_ID;
5977
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005978 if (dict_find(what, (char_u *)"items", -1) != NULL)
5979 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005980
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005981 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5982 flags |= QF_GETLIST_IDX;
5983
5984 if (dict_find(what, (char_u *)"size", -1) != NULL)
5985 flags |= QF_GETLIST_SIZE;
5986
Bram Moolenaarb254af32017-12-18 19:48:58 +01005987 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5988 flags |= QF_GETLIST_TICK;
5989
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005990 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5991 flags |= QF_GETLIST_FILEWINID;
5992
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005993 return flags;
5994}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005995
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005996/*
5997 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5998 * If 'nr' and 'id' are not present in 'what' then return the current
5999 * quickfix list index.
6000 * If 'nr' is zero then return the current quickfix list index.
6001 * If 'nr' is '$' then return the last quickfix list index.
6002 * If 'id' is present then return the index of the quickfix list with that id.
6003 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6004 * Return -1, if quickfix list is not present or if the stack is empty.
6005 */
6006 static int
6007qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6008{
6009 int qf_idx;
6010 dictitem_T *di;
6011
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006012 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006013 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6014 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006015 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006016 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006017 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006018 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006019 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006020 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006021 qf_idx = di->di_tv.vval.v_number - 1;
6022 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006023 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006024 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006025 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006026 else if (di->di_tv.v_type == VAR_STRING
6027 && di->di_tv.vval.v_string != NULL
6028 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006029 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006030 qf_idx = qi->qf_listcount - 1;
6031 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006032 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006033 }
6034
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006035 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6036 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006037 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006038 if (di->di_tv.v_type == VAR_NUMBER)
6039 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006040 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006041 if (di->di_tv.vval.v_number != 0)
6042 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6043 }
6044 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006045 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006046 }
6047
6048 return qf_idx;
6049}
6050
6051/*
6052 * Return default values for quickfix list properties in retdict.
6053 */
6054 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006055qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006056{
6057 int status = OK;
6058
6059 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006060 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006061 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6062 {
6063 list_T *l = list_alloc();
6064 if (l != NULL)
6065 status = dict_add_list(retdict, "items", l);
6066 else
6067 status = FAIL;
6068 }
6069 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006070 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006071 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006072 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006073 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006074 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006075 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006076 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006077 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006078 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006079 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006080 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006081 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006082 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006083 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006084 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006085
6086 return status;
6087}
6088
6089/*
6090 * Return the quickfix list title as 'title' in retdict
6091 */
6092 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006093qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006094{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006095 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006096}
6097
6098/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006099 * Returns the identifier of the window used to display files from a location
6100 * list. If there is no associated window, then returns 0. Useful only when
6101 * called from a location list window.
6102 */
6103 static int
6104qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6105{
6106 int winid = 0;
6107
6108 if (wp != NULL && IS_LL_WINDOW(wp))
6109 {
6110 win_T *ll_wp = qf_find_win_with_loclist(qi);
6111 if (ll_wp != NULL)
6112 winid = ll_wp->w_id;
6113 }
6114
6115 return dict_add_number(retdict, "filewinid", winid);
6116}
6117
6118/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006119 * Return the quickfix list items/entries as 'items' in retdict
6120 */
6121 static int
6122qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6123{
6124 int status = OK;
6125 list_T *l = list_alloc();
6126 if (l != NULL)
6127 {
6128 (void)get_errorlist(qi, NULL, qf_idx, l);
6129 dict_add_list(retdict, "items", l);
6130 }
6131 else
6132 status = FAIL;
6133
6134 return status;
6135}
6136
6137/*
6138 * Return the quickfix list context (if any) as 'context' in retdict.
6139 */
6140 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006141qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006142{
6143 int status;
6144 dictitem_T *di;
6145
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006146 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006147 {
6148 di = dictitem_alloc((char_u *)"context");
6149 if (di != NULL)
6150 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006151 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006152 status = dict_add(retdict, di);
6153 if (status == FAIL)
6154 dictitem_free(di);
6155 }
6156 else
6157 status = FAIL;
6158 }
6159 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006160 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006161
6162 return status;
6163}
6164
6165/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006166 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006167 */
6168 static int
6169qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6170{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006171 int curidx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006172 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006173 // For empty lists, current index is set to 0
6174 curidx = 0;
6175 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006176}
6177
6178/*
6179 * Return quickfix/location list details (title) as a
6180 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6181 * then current list is used. Otherwise the specified list is used.
6182 */
6183 int
6184qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6185{
6186 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006187 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006188 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006189 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006190 dictitem_T *di;
6191 int flags = QF_GETLIST_NONE;
6192
6193 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6194 return qf_get_list_from_lines(what, di, retdict);
6195
6196 if (wp != NULL)
6197 qi = GET_LOC_LIST(wp);
6198
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006199 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006200
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006201 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006202 qf_idx = qf_getprop_qfidx(qi, what);
6203
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006204 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006205 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006206 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006207
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006208 qfl = &qi->qf_lists[qf_idx];
6209
Bram Moolenaard823fa92016-08-12 16:29:27 +02006210 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006211 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006212 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006213 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006214 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006215 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006216 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006217 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006218 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006219 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006220 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006221 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006222 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006223 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006224 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006225 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006226 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006227 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006228 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6229 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006230
Bram Moolenaard823fa92016-08-12 16:29:27 +02006231 return status;
6232}
6233
6234/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006235 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006236 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6237 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006238 */
6239 static int
6240qf_add_entry_from_dict(
6241 qf_info_T *qi,
6242 int qf_idx,
6243 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006244 int first_entry,
6245 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006246{
6247 static int did_bufnr_emsg;
6248 char_u *filename, *module, *pattern, *text, *type;
6249 int bufnum, valid, status, col, vcol, nr;
6250 long lnum;
6251
6252 if (first_entry)
6253 did_bufnr_emsg = FALSE;
6254
Bram Moolenaar8f667172018-12-14 15:38:31 +01006255 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6256 module = dict_get_string(d, (char_u *)"module", TRUE);
6257 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6258 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6259 col = (int)dict_get_number(d, (char_u *)"col");
6260 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6261 nr = (int)dict_get_number(d, (char_u *)"nr");
6262 type = dict_get_string(d, (char_u *)"type", TRUE);
6263 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6264 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006265 if (text == NULL)
6266 text = vim_strsave((char_u *)"");
6267
6268 valid = TRUE;
6269 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6270 valid = FALSE;
6271
6272 // Mark entries with non-existing buffer number as not valid. Give the
6273 // error message only once.
6274 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6275 {
6276 if (!did_bufnr_emsg)
6277 {
6278 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006279 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006280 }
6281 valid = FALSE;
6282 bufnum = 0;
6283 }
6284
6285 // If the 'valid' field is present it overrules the detected value.
6286 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006287 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006288
6289 status = qf_add_entry(qi,
6290 qf_idx,
6291 NULL, // dir
6292 filename,
6293 module,
6294 bufnum,
6295 text,
6296 lnum,
6297 col,
6298 vcol, // vis_col
6299 pattern, // search pattern
6300 nr,
6301 type == NULL ? NUL : *type,
6302 valid);
6303
6304 vim_free(filename);
6305 vim_free(module);
6306 vim_free(pattern);
6307 vim_free(text);
6308 vim_free(type);
6309
Bram Moolenaar9752c722018-12-22 16:49:34 +01006310 if (valid)
6311 *valid_entry = TRUE;
6312
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006313 return status;
6314}
6315
6316/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006317 * Add list of entries to quickfix/location list. Each list entry is
6318 * a dictionary with item information.
6319 */
6320 static int
6321qf_add_entries(
6322 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006323 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006324 list_T *list,
6325 char_u *title,
6326 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006327{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006328 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006329 listitem_T *li;
6330 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006331 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006332 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006333 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006334
Bram Moolenaara3921f42017-06-04 15:30:34 +02006335 if (action == ' ' || qf_idx == qi->qf_listcount)
6336 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006337 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006338 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006339 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006340 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006341 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006342 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006343 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006344 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006345 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006346 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006347 qf_free_items(qfl);
6348 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006349 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006350
6351 for (li = list->lv_first; li != NULL; li = li->li_next)
6352 {
6353 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006354 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006355
6356 d = li->li_tv.vval.v_dict;
6357 if (d == NULL)
6358 continue;
6359
Bram Moolenaar9752c722018-12-22 16:49:34 +01006360 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first,
6361 &valid_entry);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006362 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006363 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006364 }
6365
Bram Moolenaar9752c722018-12-22 16:49:34 +01006366 // Check if any valid error entries are added to the list.
6367 if (valid_entry)
6368 qfl->qf_nonevalid = FALSE;
6369 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006370 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006371 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006372
6373 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006374 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006375 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006376
6377 // Update the current error index if not appending to the list or if the
6378 // list was empty before and it is not empty now.
6379 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qi, qf_idx))
6380 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006381
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006382 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006383 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006384
6385 return retval;
6386}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006387
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006388/*
6389 * Get the quickfix list index from 'nr' or 'id'
6390 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006391 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006392qf_setprop_get_qfidx(
6393 qf_info_T *qi,
6394 dict_T *what,
6395 int action,
6396 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006397{
6398 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006399 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006400
Bram Moolenaard823fa92016-08-12 16:29:27 +02006401 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6402 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006403 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006404 if (di->di_tv.v_type == VAR_NUMBER)
6405 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006406 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006407 if (di->di_tv.vval.v_number != 0)
6408 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006409
Bram Moolenaar55b69262017-08-13 13:42:01 +02006410 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6411 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006412 // When creating a new list, accept qf_idx pointing to the next
6413 // non-available list and add the new list at the end of the
6414 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006415 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006416 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006417 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006418 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006419 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006420 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006421 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006422 }
6423 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006424 && di->di_tv.vval.v_string != NULL
6425 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006426 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006427 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006428 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006429 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006430 qf_idx = 0;
6431 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006432 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006433 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006434 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006435 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006436 }
6437
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006438 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006439 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006440 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006441 if (di->di_tv.v_type != VAR_NUMBER)
6442 return INVALID_QFIDX;
6443
6444 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006445 }
6446
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006447 return qf_idx;
6448}
6449
6450/*
6451 * Set the quickfix list title.
6452 */
6453 static int
6454qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6455{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006456 qf_list_T *qfl = &qi->qf_lists[qf_idx];
6457
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006458 if (di->di_tv.v_type != VAR_STRING)
6459 return FAIL;
6460
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006461 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01006462 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006463 if (qf_idx == qi->qf_curlist)
6464 qf_update_win_titlevar(qi);
6465
6466 return OK;
6467}
6468
6469/*
6470 * Set quickfix list items/entries.
6471 */
6472 static int
6473qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6474{
6475 int retval = FAIL;
6476 char_u *title_save;
6477
6478 if (di->di_tv.v_type != VAR_LIST)
6479 return FAIL;
6480
6481 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6482 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6483 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006484 vim_free(title_save);
6485
6486 return retval;
6487}
6488
6489/*
6490 * Set quickfix list items/entries from a list of lines.
6491 */
6492 static int
6493qf_setprop_items_from_lines(
6494 qf_info_T *qi,
6495 int qf_idx,
6496 dict_T *what,
6497 dictitem_T *di,
6498 int action)
6499{
6500 char_u *errorformat = p_efm;
6501 dictitem_T *efm_di;
6502 int retval = FAIL;
6503
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006504 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006505 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6506 {
6507 if (efm_di->di_tv.v_type != VAR_STRING ||
6508 efm_di->di_tv.vval.v_string == NULL)
6509 return FAIL;
6510 errorformat = efm_di->di_tv.vval.v_string;
6511 }
6512
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006513 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006514 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6515 return FAIL;
6516
6517 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006518 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006519 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6520 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6521 retval = OK;
6522
6523 return retval;
6524}
6525
6526/*
6527 * Set quickfix list context.
6528 */
6529 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006530qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006531{
6532 typval_T *ctx;
6533
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006534 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006535 ctx = alloc_tv();
6536 if (ctx != NULL)
6537 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006538 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006539
6540 return OK;
6541}
6542
6543/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006544 * Set the current index in the specified quickfix list
6545 */
6546 static int
6547qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
6548{
6549 int denote = FALSE;
6550 int newidx;
6551 int old_qfidx;
6552 qfline_T *qf_ptr;
6553
6554 // If the specified index is '$', then use the last entry
6555 if (di->di_tv.v_type == VAR_STRING
6556 && di->di_tv.vval.v_string != NULL
6557 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
6558 newidx = qfl->qf_count;
6559 else
6560 {
6561 // Otherwise use the specified index
6562 newidx = tv_get_number_chk(&di->di_tv, &denote);
6563 if (denote)
6564 return FAIL;
6565 }
6566
6567 if (newidx < 1) // sanity check
6568 return FAIL;
6569 if (newidx > qfl->qf_count)
6570 newidx = qfl->qf_count;
6571
6572 old_qfidx = qfl->qf_index;
6573 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
6574 if (qf_ptr == NULL)
6575 return FAIL;
6576 qfl->qf_ptr = qf_ptr;
6577 qfl->qf_index = newidx;
6578
6579 // If the current list is modified and it is displayed in the quickfix
6580 // window, then Update it.
6581 if (qi->qf_lists[qi->qf_curlist].qf_id == qfl->qf_id)
6582 qf_win_pos_update(qi, old_qfidx);
6583
6584 return OK;
6585}
6586
6587/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006588 * Set quickfix/location list properties (title, items, context).
6589 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006590 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006591 */
6592 static int
6593qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6594{
6595 dictitem_T *di;
6596 int retval = FAIL;
6597 int qf_idx;
6598 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006599 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006600
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006601 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006602 newlist = TRUE;
6603
6604 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006605 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006606 return FAIL;
6607
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006608 if (newlist)
6609 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006610 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006611 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006612 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006613 }
6614
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006615 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaard823fa92016-08-12 16:29:27 +02006616 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006617 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006618 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006619 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006620 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006621 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006622 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006623 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006624 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6625 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006626
Bram Moolenaarb254af32017-12-18 19:48:58 +01006627 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006628 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006629
Bram Moolenaard823fa92016-08-12 16:29:27 +02006630 return retval;
6631}
6632
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006633/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006634 * Free the entire quickfix/location list stack.
6635 * If the quickfix/location list window is open, then clear it.
6636 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006637 static void
6638qf_free_stack(win_T *wp, qf_info_T *qi)
6639{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006640 win_T *qfwin = qf_find_win(qi);
6641 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006642
6643 if (qfwin != NULL)
6644 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006645 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006646 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006647 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006648 qf_update_buffer(qi, NULL);
6649 }
6650
6651 if (wp != NULL && IS_LL_WINDOW(wp))
6652 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006653 // If in the location list window, then use the non-location list
6654 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006655 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006656 if (llwin != NULL)
6657 wp = llwin;
6658 }
6659
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006660 qf_free_all(wp);
6661 if (wp == NULL)
6662 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006663 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006664 qi->qf_curlist = 0;
6665 qi->qf_listcount = 0;
6666 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006667 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006668 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006669 // If the location list window is open, then create a new empty
6670 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006671 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006672 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006673
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006674 // first free the list reference in the location list window
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006675 ll_free_all(&qfwin->w_llist_ref);
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006676
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006677 qfwin->w_llist_ref = new_ll;
6678 if (wp != qfwin)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006679 {
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006680 wp->w_llist = new_ll;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006681 new_ll->qf_refcount++;
6682 }
6683 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006684}
6685
Bram Moolenaard823fa92016-08-12 16:29:27 +02006686/*
6687 * Populate the quickfix list with the items supplied in the list
6688 * of dictionaries. "title" will be copied to w:quickfix_title.
6689 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6690 */
6691 int
6692set_errorlist(
6693 win_T *wp,
6694 list_T *list,
6695 int action,
6696 char_u *title,
6697 dict_T *what)
6698{
6699 qf_info_T *qi = &ql_info;
6700 int retval = OK;
6701
6702 if (wp != NULL)
6703 {
6704 qi = ll_get_or_alloc_list(wp);
6705 if (qi == NULL)
6706 return FAIL;
6707 }
6708
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006709 if (action == 'f')
6710 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006711 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006712 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006713 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006714 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006715
6716 incr_quickfix_busy();
6717
6718 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006719 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006720 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006721 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006722 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006723 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006724 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006725 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006726
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006727 decr_quickfix_busy();
6728
Bram Moolenaard823fa92016-08-12 16:29:27 +02006729 return retval;
6730}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006731
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006732/*
6733 * Mark the context as in use for all the lists in a quickfix stack.
6734 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006735 static int
6736mark_quickfix_ctx(qf_info_T *qi, int copyID)
6737{
6738 int i;
6739 int abort = FALSE;
6740 typval_T *ctx;
6741
6742 for (i = 0; i < LISTCOUNT && !abort; ++i)
6743 {
6744 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006745 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6746 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006747 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6748 }
6749
6750 return abort;
6751}
6752
6753/*
6754 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006755 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006756 */
6757 int
6758set_ref_in_quickfix(int copyID)
6759{
6760 int abort = FALSE;
6761 tabpage_T *tp;
6762 win_T *win;
6763
6764 abort = mark_quickfix_ctx(&ql_info, copyID);
6765 if (abort)
6766 return abort;
6767
6768 FOR_ALL_TAB_WINDOWS(tp, win)
6769 {
6770 if (win->w_llist != NULL)
6771 {
6772 abort = mark_quickfix_ctx(win->w_llist, copyID);
6773 if (abort)
6774 return abort;
6775 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006776 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6777 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006778 // In a location list window and none of the other windows is
6779 // referring to this location list. Mark the location list
6780 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006781 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6782 if (abort)
6783 return abort;
6784 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006785 }
6786
6787 return abort;
6788}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006789#endif
6790
Bram Moolenaar81695252004-12-29 20:58:21 +00006791/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006792 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006793 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006794 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006795 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006796 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006797 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006798 */
6799 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006800ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006801{
6802 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006803 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006804 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006805 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006806 int_u save_qfid;
6807 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006808
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006809 switch (eap->cmdidx)
6810 {
6811 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6812 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6813 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6814 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6815 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6816 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6817 default: break;
6818 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006819 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6820 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006821 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006822#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006823 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006824 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006825#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006826 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006827
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006828 // Must come after autocommands.
Bram Moolenaar39665952018-08-15 20:59:48 +02006829 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006830 {
6831 qi = ll_get_or_alloc_list(curwin);
6832 if (qi == NULL)
6833 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006834 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006835 }
6836
Bram Moolenaar86b68352004-12-27 21:59:20 +00006837 if (*eap->arg == NUL)
6838 buf = curbuf;
6839 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6840 buf = buflist_findnr(atoi((char *)eap->arg));
6841 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006842 emsg(_(e_invarg));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006843 else if (buf->b_ml.ml_mfp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006844 emsg(_("E681: Buffer is not loaded"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006845 else
6846 {
6847 if (eap->addr_count == 0)
6848 {
6849 eap->line1 = 1;
6850 eap->line2 = buf->b_ml.ml_line_count;
6851 }
6852 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6853 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006854 emsg(_(e_invrange));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006855 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006856 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006857 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006858
6859 if (buf->b_sfname)
6860 {
6861 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6862 (char *)qf_title, (char *)buf->b_sfname);
6863 qf_title = IObuff;
6864 }
6865
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006866 incr_quickfix_busy();
6867
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006868 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006869 (eap->cmdidx != CMD_caddbuffer
6870 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006871 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006872 qf_title, NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006873 if (qf_stack_empty(qi))
6874 {
6875 decr_quickfix_busy();
6876 return;
6877 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006878 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006879 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006880
6881 // Remember the current quickfix list identifier, so that we can
6882 // check for autocommands changing the current quickfix list.
6883 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006884 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006885 {
6886 buf_T *curbuf_old = curbuf;
6887
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006888 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6889 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006890 if (curbuf != curbuf_old)
6891 // Autocommands changed buffer, don't jump now, "qi" may
6892 // be invalid.
6893 res = 0;
6894 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006895 // Jump to the first error for a new list and if autocmds didn't
6896 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006897 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006898 eap->cmdidx == CMD_lbuffer)
6899 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006900 // display the first error
6901 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006902
6903 decr_quickfix_busy();
Bram Moolenaar754b5602006-02-09 23:53:20 +00006904 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006905 }
6906}
6907
Bram Moolenaar1e015462005-09-25 22:16:38 +00006908#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006909/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006910 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6911 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006912 */
6913 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006914ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006915{
6916 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006917 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006918 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006919 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006920 int_u save_qfid;
6921 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006922
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006923 switch (eap->cmdidx)
6924 {
6925 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6926 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6927 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6928 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6929 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6930 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6931 default: break;
6932 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006933 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6934 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006935 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006936#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006937 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006938 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006939#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006940 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006941
Bram Moolenaar39665952018-08-15 20:59:48 +02006942 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006943 {
6944 qi = ll_get_or_alloc_list(curwin);
6945 if (qi == NULL)
6946 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006947 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006948 }
6949
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006950 // Evaluate the expression. When the result is a string or a list we can
6951 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006952 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006953 if (tv != NULL)
6954 {
6955 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6956 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6957 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006958 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006959 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006960 (eap->cmdidx != CMD_caddexpr
6961 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006962 (linenr_T)0, (linenr_T)0,
6963 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006964 if (qf_stack_empty(qi))
6965 {
6966 decr_quickfix_busy();
6967 goto cleanup;
6968 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006969 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006970 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006971
6972 // Remember the current quickfix list identifier, so that we can
6973 // check for autocommands changing the current quickfix list.
6974 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006975 if (au_name != NULL)
6976 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6977 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006978
6979 // Jump to the first error for a new list and if autocmds didn't
6980 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006981 if (res > 0 && (eap->cmdidx == CMD_cexpr
6982 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006983 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006984 // display the first error
6985 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006986 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006987 }
6988 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006989 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006990cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00006991 free_tv(tv);
6992 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006993}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006994#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006995
6996/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006997 * Get the location list for ":lhelpgrep"
6998 */
6999 static qf_info_T *
7000hgr_get_ll(int *new_ll)
7001{
7002 win_T *wp;
7003 qf_info_T *qi;
7004
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007005 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007006 if (bt_help(curwin->w_buffer))
7007 wp = curwin;
7008 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007009 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007010 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007011
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007012 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007013 qi = NULL;
7014 else
7015 qi = wp->w_llist;
7016
7017 if (qi == NULL)
7018 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007019 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007020 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007021 return NULL;
7022 *new_ll = TRUE;
7023 }
7024
7025 return qi;
7026}
7027
7028/*
7029 * Search for a pattern in a help file.
7030 */
7031 static void
7032hgr_search_file(
7033 qf_info_T *qi,
7034 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007035 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007036 regmatch_T *p_regmatch)
7037{
7038 FILE *fd;
7039 long lnum;
7040
7041 fd = mch_fopen((char *)fname, "r");
7042 if (fd == NULL)
7043 return;
7044
7045 lnum = 1;
7046 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7047 {
7048 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007049
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007050 // Convert a line if 'encoding' is not utf-8 and
7051 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007052 if (p_vc->vc_type != CONV_NONE
7053 && has_non_ascii(IObuff))
7054 {
7055 line = string_convert(p_vc, IObuff, NULL);
7056 if (line == NULL)
7057 line = IObuff;
7058 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007059
7060 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7061 {
7062 int l = (int)STRLEN(line);
7063
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007064 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007065 while (l > 0 && line[l - 1] <= ' ')
7066 line[--l] = NUL;
7067
7068 if (qf_add_entry(qi,
7069 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007070 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007071 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007072 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007073 0,
7074 line,
7075 lnum,
7076 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007077 + 1, // col
7078 FALSE, // vis_col
7079 NULL, // search pattern
7080 0, // nr
7081 1, // type
7082 TRUE // valid
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007083 ) == FAIL)
7084 {
7085 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007086 if (line != IObuff)
7087 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007088 break;
7089 }
7090 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007091 if (line != IObuff)
7092 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007093 ++lnum;
7094 line_breakcheck();
7095 }
7096 fclose(fd);
7097}
7098
7099/*
7100 * Search for a pattern in all the help files in the doc directory under
7101 * the given directory.
7102 */
7103 static void
7104hgr_search_files_in_dir(
7105 qf_info_T *qi,
7106 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007107 regmatch_T *p_regmatch,
7108 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007109#ifdef FEAT_MULTI_LANG
7110 , char_u *lang
7111#endif
7112 )
7113{
7114 int fcount;
7115 char_u **fnames;
7116 int fi;
7117
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007118 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007119 add_pathsep(dirname);
7120 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7121 if (gen_expand_wildcards(1, &dirname, &fcount,
7122 &fnames, EW_FILE|EW_SILENT) == OK
7123 && fcount > 0)
7124 {
7125 for (fi = 0; fi < fcount && !got_int; ++fi)
7126 {
7127#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007128 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007129 if (lang != NULL
7130 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007131 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007132 && !(STRNICMP(lang, "en", 2) == 0
7133 && STRNICMP("txt", fnames[fi]
7134 + STRLEN(fnames[fi]) - 3, 3) == 0))
7135 continue;
7136#endif
7137
Bram Moolenaara12a1612019-01-24 16:39:02 +01007138 hgr_search_file(qi, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007139 }
7140 FreeWild(fcount, fnames);
7141 }
7142}
7143
7144/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007145 * Search for a pattern in all the help files in the 'runtimepath'
7146 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007147 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007148 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007149 */
7150 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007151hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007152{
7153 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007154
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007155 vimconv_T vc;
7156
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007157 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7158 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007159 vc.vc_type = CONV_NONE;
7160 if (!enc_utf8)
7161 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007162
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007163 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007164 p = p_rtp;
7165 while (*p != NUL && !got_int)
7166 {
7167 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7168
Bram Moolenaara12a1612019-01-24 16:39:02 +01007169 hgr_search_files_in_dir(qi, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007170#ifdef FEAT_MULTI_LANG
7171 , lang
7172#endif
7173 );
7174 }
7175
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007176 if (vc.vc_type != CONV_NONE)
7177 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007178}
7179
7180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 * ":helpgrep {pattern}"
7182 */
7183 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007184ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185{
7186 regmatch_T regmatch;
7187 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007188 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007189 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007190 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007191 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
Bram Moolenaar73633f82012-01-20 13:39:07 +01007193 switch (eap->cmdidx)
7194 {
7195 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7196 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7197 default: break;
7198 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007199 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7200 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007201 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007202#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007203 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007204 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007205#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007206 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007207
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007208 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01007209 save_cpo = p_cpo;
7210 p_cpo = empty_option;
7211
Bram Moolenaar39665952018-08-15 20:59:48 +02007212 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007213 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007214 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007215 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007216 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007217 }
7218
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007219 incr_quickfix_busy();
7220
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007221#ifdef FEAT_MULTI_LANG
7222 // Check for a specified language
7223 lang = check_help_lang(eap->arg);
7224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7226 regmatch.rm_ic = FALSE;
7227 if (regmatch.regprog != NULL)
7228 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007229 qf_list_T *qfl;
7230
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007231 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007232 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007234 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007235
Bram Moolenaar473de612013-06-08 18:19:48 +02007236 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007238 qfl = &qi->qf_lists[qi->qf_curlist];
7239 qfl->qf_nonevalid = FALSE;
7240 qfl->qf_ptr = qfl->qf_start;
7241 qfl->qf_index = 1;
7242 qf_list_changed(qfl);
7243 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
7245
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007246 if (p_cpo == empty_option)
7247 p_cpo = save_cpo;
7248 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007249 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007250 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251
Bram Moolenaar73633f82012-01-20 13:39:07 +01007252 if (au_name != NULL)
7253 {
7254 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7255 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007256 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007257 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007258 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007259 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007260 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007261 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007262 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007263
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007264 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007265 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007266 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007267 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007268 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007269
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007270 decr_quickfix_busy();
7271
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007272 if (eap->cmdidx == CMD_lhelpgrep)
7273 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007274 // If the help window is not opened or if it already points to the
7275 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007276 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007277 {
7278 if (new_qi)
7279 ll_free_all(&qi);
7280 }
7281 else if (curwin->w_llist == NULL)
7282 curwin->w_llist = qi;
7283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284}
7285
7286#endif /* FEAT_QUICKFIX */