blob: 3bfa027d6209742d0bb06d76687d0100af975124 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020030 qfline_T *qf_next; // pointer to next error in the list
31 qfline_T *qf_prev; // pointer to previous error in the list
32 linenr_T qf_lnum; // line number where the error occurred
33 int qf_fnum; // file number for the line
34 int qf_col; // column where the error occurred
35 int qf_nr; // error number
36 char_u *qf_module; // module name for this error
37 char_u *qf_pattern; // search pattern for the error
38 char_u *qf_text; // description of the error
39 char_u qf_viscol; // set to TRUE if qf_col is screen column
40 char_u qf_cleared; // set to TRUE if line has been deleted
41 char_u qf_type; // type of the error (mostly 'E'); 1 for
42 // :helpgrep
43 char_u qf_valid; // valid error message detected
Bram Moolenaar071d4272004-06-13 20:20:40 +000044};
45
46/*
47 * There is a stack of error lists.
48 */
49#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020050#define INVALID_QFIDX (-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020052/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010053 * Quickfix list type.
54 */
55typedef enum
56{
57 QFLT_QUICKFIX, // Quickfix list - global list
58 QFLT_LOCATION, // Location list - per window list
59 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
60} qfltype_T;
61
62/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020063 * Quickfix/Location list definition
64 * Contains a list of entries (qfline_T). qf_start points to the first entry
65 * and qf_last points to the last entry. qf_count contains the list size.
66 *
67 * Usually the list contains one or more entries. But an empty list can be
68 * created using setqflist()/setloclist() with a title and/or user context
69 * information and entries can be added later using setqflist()/setloclist().
70 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000071typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000072{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020073 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010074 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020075 qfline_T *qf_start; // pointer to the first error
76 qfline_T *qf_last; // pointer to the last error
77 qfline_T *qf_ptr; // pointer to the current error
78 int qf_count; // number of errors (0 means empty list)
79 int qf_index; // current index in the error list
80 int qf_nonevalid; // TRUE if not a single valid entry found
81 char_u *qf_title; // title derived from the command that created
82 // the error list or set by setqflist
83 typval_T *qf_ctx; // context set by setqflist/setloclist
Bram Moolenaara7df8c72017-07-19 13:23:06 +020084
85 struct dir_stack_T *qf_dir_stack;
86 char_u *qf_directory;
87 struct dir_stack_T *qf_file_stack;
88 char_u *qf_currfile;
89 int qf_multiline;
90 int qf_multiignore;
91 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010092 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000093} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020095/*
96 * Quickfix/Location list stack definition
97 * Contains a list of quickfix/location lists (qf_list_T)
98 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000099struct qf_info_S
100{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200101 // Count of references to this list. Used only for location lists.
102 // When a location list window reference this list, qf_refcount
103 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
104 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000105 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200106 int qf_listcount; // current number of lists
107 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000108 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100109 qfltype_T qfl_type; // type of list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110};
111
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200112static qf_info_T ql_info; // global quickfix list
113static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200115#define FMT_PATTERNS 11 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
117/*
118 * Structure used to hold the info of one part of 'errorformat'
119 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000120typedef struct efm_S efm_T;
121struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200123 regprog_T *prog; // pre-formatted part of 'errorformat'
124 efm_T *next; // pointer to next (NULL if last)
125 char_u addr[FMT_PATTERNS]; // indices of used % patterns
126 char_u prefix; // prefix of this format line:
127 // 'D' enter directory
128 // 'X' leave directory
129 // 'A' start of multi-line message
130 // 'E' error message
131 // 'W' warning message
132 // 'I' informational message
133 // 'C' continuation line
134 // 'Z' end of multi-line message
135 // 'G' general, unspecific message
136 // 'P' push file (partial) message
137 // 'Q' pop/quit file (partial) message
138 // 'O' overread (partial) message
139 char_u flags; // additional flags given in prefix
140 // '-' do not include this line
141 // '+' include whole line in message
142 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143};
144
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200145// List of location lists to be deleted.
146// Used to delay the deletion of locations lists by autocmds.
147typedef struct qf_delq_S
148{
149 struct qf_delq_S *next;
150 qf_info_T *qi;
151} qf_delq_T;
152static qf_delq_T *qf_delq_head = NULL;
153
154// Counter to prevent autocmds from freeing up location lists when they are
155// still being used.
156static int quickfix_busy = 0;
157
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200158static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100159
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaard76ce852018-05-01 15:02:04 +0200161static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200162static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200164static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200165static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100166static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200167static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100169static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static win_T *qf_find_win(qf_info_T *qi);
171static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200172static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200173static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
175static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
176static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
177static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200179// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000180#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200181// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000182#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200183
184// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100185#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
186#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
187#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
188#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200189
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000190/*
191 * Return location list for window 'wp'
192 * For location list window, return the referenced location list
193 */
194#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200197 * Looking up a buffer can be slow if there are many. Remember the last one
198 * to make this a lot faster if there are multiple matches in the same file.
199 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200200static char_u *qf_last_bufname = NULL;
201static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200202
Bram Moolenaar3c097222017-12-21 20:54:49 +0100203static char *e_loc_list_changed =
204 N_("E926: Current location list was changed");
205
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200206/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200207 * Maximum number of bytes allowed per line while reading a errorfile.
208 */
209#define LINE_MAXLEN 4096
210
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200211static struct fmtpattern
212{
213 char_u convchar;
214 char *pattern;
215} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200216 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200217 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200218 {'n', "\\d\\+"},
219 {'l', "\\d\\+"},
220 {'c', "\\d\\+"},
221 {'t', "."},
222 {'m', ".\\+"},
223 {'r', ".*"},
224 {'p', "[- .]*"},
225 {'v', "\\d\\+"},
226 {'s', ".\\+"},
227 {'o', ".\\+"}
228 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200229
230/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200231 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200232 * See fmt_pat definition above for the list of supported patterns. The
233 * pattern specifier is supplied in "efmpat". The converted pattern is stored
234 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200235 */
236 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200237efmpat_to_regpat(
238 char_u *efmpat,
239 char_u *regpat,
240 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200241 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100242 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200243{
244 char_u *srcptr;
245
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200246 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200247 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200248 // Each errorformat pattern can occur only once
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100249 semsg(_("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200250 return NULL;
251 }
252 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200253 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200254 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200255 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200256 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100257 semsg(_("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200258 return NULL;
259 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200260 efminfo->addr[idx] = (char_u)++round;
261 *regpat++ = '\\';
262 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200263#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200264 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200265 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200266 // Also match "c:" in the file name, even when
267 // checking for a colon next: "%f:".
268 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200269 STRCPY(regpat, "\\%(\\a:\\)\\=");
270 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200271 }
272#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200273 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200274 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200275 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200276 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200277 // A file name may contain spaces, but this isn't
278 // in "\f". For "%f:%l:%m" there may be a ":" in
279 // the file name. Use ".\{-1,}x" instead (x is
280 // the next character), the requirement that :999:
281 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200282 STRCPY(regpat, ".\\{-1,}");
283 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200284 }
285 else
286 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200287 // File name followed by '\\' or '%': include as
288 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200289 STRCPY(regpat, "\\f\\+");
290 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200291 }
292 }
293 else
294 {
295 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200296 while ((*regpat = *srcptr++) != NUL)
297 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200298 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200299 *regpat++ = '\\';
300 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200301
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200302 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200303}
304
305/*
306 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200307 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200308 */
309 static char_u *
310scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200311 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200312 char_u *efm,
313 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100314 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200315{
316 char_u *efmp = *pefmp;
317
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200318 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200319 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200320 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200321 {
322 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200323 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200324 if (efmp < efm + len)
325 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200326 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200327 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200328 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200329 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200330 if (efmp == efm + len)
331 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100332 emsg(_("E374: Missing ] in format string"));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200333 return NULL;
334 }
335 }
336 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200337 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200338 *regpat++ = *++efmp;
339 *regpat++ = '\\';
340 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200341 }
342 else
343 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200344 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100345 semsg(_("E375: Unsupported %%%c in format string"), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200346 return NULL;
347 }
348
349 *pefmp = efmp;
350
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200351 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200352}
353
354/*
355 * Analyze/parse an errorformat prefix.
356 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200357 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100358efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200359{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200360 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200361 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200362 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200363 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200364 else
365 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100366 semsg(_("E376: Invalid %%%c in format string prefix"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200367 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200368 }
369
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200370 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200371}
372
373/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200374 * Converts a 'errorformat' string part in 'efm' to a regular expression
375 * pattern. The resulting regex pattern is returned in "regpat". Additional
376 * information about the 'erroformat' pattern is returned in "fmt_ptr".
377 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200378 */
379 static int
380efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200381 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200382 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200383 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100384 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200385{
386 char_u *ptr;
387 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200388 int round;
389 int idx = 0;
390
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200391 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200392 ptr = regpat;
393 *ptr++ = '^';
394 round = 0;
395 for (efmp = efm; efmp < efm + len; ++efmp)
396 {
397 if (*efmp == '%')
398 {
399 ++efmp;
400 for (idx = 0; idx < FMT_PATTERNS; ++idx)
401 if (fmt_pat[idx].convchar == *efmp)
402 break;
403 if (idx < FMT_PATTERNS)
404 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100405 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200406 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200407 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200408 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200409 }
410 else if (*efmp == '*')
411 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200412 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100413 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200414 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200415 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200416 }
417 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200418 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200419 else if (*efmp == '#')
420 *ptr++ = '*';
421 else if (*efmp == '>')
422 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200423 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200424 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200425 // prefix is allowed only at the beginning of the errorformat
426 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100427 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200428 if (efmp == NULL)
429 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200430 }
431 else
432 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100433 semsg(_("E377: Invalid %%%c in format string"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200434 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200435 }
436 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200437 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200438 {
439 if (*efmp == '\\' && efmp + 1 < efm + len)
440 ++efmp;
441 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200442 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200443 if (*efmp)
444 *ptr++ = *efmp;
445 }
446 }
447 *ptr++ = '$';
448 *ptr = NUL;
449
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200450 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200451}
452
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200453/*
454 * Free the 'errorformat' information list
455 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200456 static void
457free_efm_list(efm_T **efm_first)
458{
459 efm_T *efm_ptr;
460
461 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
462 {
463 *efm_first = efm_ptr->next;
464 vim_regfree(efm_ptr->prog);
465 vim_free(efm_ptr);
466 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100467 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200468}
469
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200470/*
471 * Compute the size of the buffer used to convert a 'errorformat' pattern into
472 * a regular expression pattern.
473 */
474 static int
475efm_regpat_bufsz(char_u *efm)
476{
477 int sz;
478 int i;
479
480 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
481 for (i = FMT_PATTERNS; i > 0; )
482 sz += (int)STRLEN(fmt_pat[--i].pattern);
483#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200484 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200485#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200486 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200487#endif
488
489 return sz;
490}
491
492/*
493 * Return the length of a 'errorformat' option part (separated by ",").
494 */
495 static int
496efm_option_part_len(char_u *efm)
497{
498 int len;
499
500 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
501 if (efm[len] == '\\' && efm[len + 1] != NUL)
502 ++len;
503
504 return len;
505}
506
507/*
508 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
509 * are parsed and converted to regular expressions. Returns information about
510 * the parsed 'errorformat' option.
511 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200512 static efm_T *
513parse_efm_option(char_u *efm)
514{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200515 efm_T *fmt_ptr = NULL;
516 efm_T *fmt_first = NULL;
517 efm_T *fmt_last = NULL;
518 char_u *fmtstr = NULL;
519 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200520 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200521
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200522 // Each part of the format string is copied and modified from errorformat
523 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200524
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200525 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200526 sz = efm_regpat_bufsz(efm);
527 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200528 goto parse_efm_error;
529
530 while (efm[0] != NUL)
531 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200532 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200533 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
534 if (fmt_ptr == NULL)
535 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200536 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200537 fmt_first = fmt_ptr;
538 else
539 fmt_last->next = fmt_ptr;
540 fmt_last = fmt_ptr;
541
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200542 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200543 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200544
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100545 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200546 goto parse_efm_error;
547 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
548 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200549 // Advance to next part
550 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200551 }
552
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200553 if (fmt_first == NULL) // nothing found
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100554 emsg(_("E378: 'errorformat' contains no pattern"));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200555
556 goto parse_efm_end;
557
558parse_efm_error:
559 free_efm_list(&fmt_first);
560
561parse_efm_end:
562 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200563
564 return fmt_first;
565}
566
Bram Moolenaare0d37972016-07-15 22:36:01 +0200567enum {
568 QF_FAIL = 0,
569 QF_OK = 1,
570 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200571 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200572 QF_IGNORE_LINE = 4,
573 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200574};
575
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200576/*
577 * State information used to parse lines and add entries to a quickfix/location
578 * list.
579 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200580typedef struct {
581 char_u *linebuf;
582 int linelen;
583 char_u *growbuf;
584 int growbufsiz;
585 FILE *fd;
586 typval_T *tv;
587 char_u *p_str;
588 listitem_T *p_li;
589 buf_T *buf;
590 linenr_T buflnum;
591 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100592 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200593} qfstate_T;
594
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200595/*
596 * Allocate more memory for the line buffer used for parsing lines.
597 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200598 static char_u *
599qf_grow_linebuf(qfstate_T *state, int newsz)
600{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200601 char_u *p;
602
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200603 // If the line exceeds LINE_MAXLEN exclude the last
604 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200605 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
606 if (state->growbuf == NULL)
607 {
608 state->growbuf = alloc(state->linelen + 1);
609 if (state->growbuf == NULL)
610 return NULL;
611 state->growbufsiz = state->linelen;
612 }
613 else if (state->linelen > state->growbufsiz)
614 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200615 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200616 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200617 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200618 state->growbufsiz = state->linelen;
619 }
620 return state->growbuf;
621}
622
623/*
624 * Get the next string (separated by newline) from state->p_str.
625 */
626 static int
627qf_get_next_str_line(qfstate_T *state)
628{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200629 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200630 char_u *p_str = state->p_str;
631 char_u *p;
632 int len;
633
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200634 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200635 return QF_END_OF_INPUT;
636
637 p = vim_strchr(p_str, '\n');
638 if (p != NULL)
639 len = (int)(p - p_str) + 1;
640 else
641 len = (int)STRLEN(p_str);
642
643 if (len > IOSIZE - 2)
644 {
645 state->linebuf = qf_grow_linebuf(state, len);
646 if (state->linebuf == NULL)
647 return QF_NOMEM;
648 }
649 else
650 {
651 state->linebuf = IObuff;
652 state->linelen = len;
653 }
654 vim_strncpy(state->linebuf, p_str, state->linelen);
655
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200656 // Increment using len in order to discard the rest of the
657 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200658 p_str += len;
659 state->p_str = p_str;
660
661 return QF_OK;
662}
663
664/*
665 * Get the next string from state->p_Li.
666 */
667 static int
668qf_get_next_list_line(qfstate_T *state)
669{
670 listitem_T *p_li = state->p_li;
671 int len;
672
673 while (p_li != NULL
674 && (p_li->li_tv.v_type != VAR_STRING
675 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200676 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200677
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200678 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200679 {
680 state->p_li = NULL;
681 return QF_END_OF_INPUT;
682 }
683
684 len = (int)STRLEN(p_li->li_tv.vval.v_string);
685 if (len > IOSIZE - 2)
686 {
687 state->linebuf = qf_grow_linebuf(state, len);
688 if (state->linebuf == NULL)
689 return QF_NOMEM;
690 }
691 else
692 {
693 state->linebuf = IObuff;
694 state->linelen = len;
695 }
696
697 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
698
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200699 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200700 return QF_OK;
701}
702
703/*
704 * Get the next string from state->buf.
705 */
706 static int
707qf_get_next_buf_line(qfstate_T *state)
708{
709 char_u *p_buf = NULL;
710 int len;
711
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200712 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200713 if (state->buflnum > state->lnumlast)
714 return QF_END_OF_INPUT;
715
716 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
717 state->buflnum += 1;
718
719 len = (int)STRLEN(p_buf);
720 if (len > IOSIZE - 2)
721 {
722 state->linebuf = qf_grow_linebuf(state, len);
723 if (state->linebuf == NULL)
724 return QF_NOMEM;
725 }
726 else
727 {
728 state->linebuf = IObuff;
729 state->linelen = len;
730 }
731 vim_strncpy(state->linebuf, p_buf, state->linelen);
732
733 return QF_OK;
734}
735
736/*
737 * Get the next string from file state->fd.
738 */
739 static int
740qf_get_next_file_line(qfstate_T *state)
741{
742 int discard;
743 int growbuflen;
744
745 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
746 return QF_END_OF_INPUT;
747
748 discard = FALSE;
749 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200750 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200751 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200752 // The current line exceeds IObuff, continue reading using
753 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200754 if (state->growbuf == NULL)
755 {
756 state->growbufsiz = 2 * (IOSIZE - 1);
757 state->growbuf = alloc(state->growbufsiz);
758 if (state->growbuf == NULL)
759 return QF_NOMEM;
760 }
761
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200762 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200763 memcpy(state->growbuf, IObuff, IOSIZE - 1);
764 growbuflen = state->linelen;
765
766 for (;;)
767 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200768 char_u *p;
769
Bram Moolenaare0d37972016-07-15 22:36:01 +0200770 if (fgets((char *)state->growbuf + growbuflen,
771 state->growbufsiz - growbuflen, state->fd) == NULL)
772 break;
773 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
774 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200775 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200776 break;
777 if (state->growbufsiz == LINE_MAXLEN)
778 {
779 discard = TRUE;
780 break;
781 }
782
783 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
784 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200785 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200786 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200787 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200788 }
789
790 while (discard)
791 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200792 // The current line is longer than LINE_MAXLEN, continue
793 // reading but discard everything until EOL or EOF is
794 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200795 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
796 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200797 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200798 break;
799 }
800
801 state->linebuf = state->growbuf;
802 state->linelen = growbuflen;
803 }
804 else
805 state->linebuf = IObuff;
806
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200807 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200808 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
809 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100810 char_u *line;
811
812 line = string_convert(&state->vc, state->linebuf, &state->linelen);
813 if (line != NULL)
814 {
815 if (state->linelen < IOSIZE)
816 {
817 STRCPY(state->linebuf, line);
818 vim_free(line);
819 }
820 else
821 {
822 vim_free(state->growbuf);
823 state->linebuf = state->growbuf = line;
824 state->growbufsiz = state->linelen < LINE_MAXLEN
825 ? state->linelen : LINE_MAXLEN;
826 }
827 }
828 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100829
Bram Moolenaare0d37972016-07-15 22:36:01 +0200830 return QF_OK;
831}
832
833/*
834 * Get the next string from a file/buffer/list/string.
835 */
836 static int
837qf_get_nextline(qfstate_T *state)
838{
839 int status = QF_FAIL;
840
841 if (state->fd == NULL)
842 {
843 if (state->tv != NULL)
844 {
845 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200846 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200847 status = qf_get_next_str_line(state);
848 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200849 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200850 status = qf_get_next_list_line(state);
851 }
852 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200853 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200854 status = qf_get_next_buf_line(state);
855 }
856 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200857 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200858 status = qf_get_next_file_line(state);
859
860 if (status != QF_OK)
861 return status;
862
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200863 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200864 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200865 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200866 state->linebuf[state->linelen - 1] = NUL;
867#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200868 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
869 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200870#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200871 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200872
Bram Moolenaare0d37972016-07-15 22:36:01 +0200873 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200874
875 return QF_OK;
876}
877
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200878typedef struct {
879 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200880 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200881 char_u *errmsg;
882 int errmsglen;
883 long lnum;
884 int col;
885 char_u use_viscol;
886 char_u *pattern;
887 int enr;
888 int type;
889 int valid;
890} qffields_T;
891
892/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200893 * Parse the match for filename ('%f') pattern in regmatch.
894 * Return the matched value in "fields->namebuf".
895 */
896 static int
897qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
898{
899 int c;
900
901 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
902 return QF_FAIL;
903
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200904 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200905 c = *rmp->endp[midx];
906 *rmp->endp[midx] = NUL;
907 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
908 *rmp->endp[midx] = c;
909
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200910 // For separate filename patterns (%O, %P and %Q), the specified file
911 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200912 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
913 && mch_getperm(fields->namebuf) == -1)
914 return QF_FAIL;
915
916 return QF_OK;
917}
918
919/*
920 * Parse the match for error number ('%n') pattern in regmatch.
921 * Return the matched value in "fields->enr".
922 */
923 static int
924qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
925{
926 if (rmp->startp[midx] == NULL)
927 return QF_FAIL;
928 fields->enr = (int)atol((char *)rmp->startp[midx]);
929 return QF_OK;
930}
931
932/*
933 * Parse the match for line number (%l') pattern in regmatch.
934 * Return the matched value in "fields->lnum".
935 */
936 static int
937qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
938{
939 if (rmp->startp[midx] == NULL)
940 return QF_FAIL;
941 fields->lnum = atol((char *)rmp->startp[midx]);
942 return QF_OK;
943}
944
945/*
946 * Parse the match for column number ('%c') pattern in regmatch.
947 * Return the matched value in "fields->col".
948 */
949 static int
950qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
951{
952 if (rmp->startp[midx] == NULL)
953 return QF_FAIL;
954 fields->col = (int)atol((char *)rmp->startp[midx]);
955 return QF_OK;
956}
957
958/*
959 * Parse the match for error type ('%t') pattern in regmatch.
960 * Return the matched value in "fields->type".
961 */
962 static int
963qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
964{
965 if (rmp->startp[midx] == NULL)
966 return QF_FAIL;
967 fields->type = *rmp->startp[midx];
968 return QF_OK;
969}
970
971/*
972 * Parse the match for '%+' format pattern. The whole matching line is included
973 * in the error string. Return the matched line in "fields->errmsg".
974 */
975 static int
976qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
977{
978 char_u *p;
979
980 if (linelen >= fields->errmsglen)
981 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200982 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200983 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
984 return QF_NOMEM;
985 fields->errmsg = p;
986 fields->errmsglen = linelen + 1;
987 }
988 vim_strncpy(fields->errmsg, linebuf, linelen);
989 return QF_OK;
990}
991
992/*
993 * Parse the match for error message ('%m') pattern in regmatch.
994 * Return the matched value in "fields->errmsg".
995 */
996 static int
997qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
998{
999 char_u *p;
1000 int len;
1001
1002 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1003 return QF_FAIL;
1004 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1005 if (len >= fields->errmsglen)
1006 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001007 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001008 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1009 return QF_NOMEM;
1010 fields->errmsg = p;
1011 fields->errmsglen = len + 1;
1012 }
1013 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1014 return QF_OK;
1015}
1016
1017/*
1018 * Parse the match for rest of a single-line file message ('%r') pattern.
1019 * Return the matched value in "tail".
1020 */
1021 static int
1022qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1023{
1024 if (rmp->startp[midx] == NULL)
1025 return QF_FAIL;
1026 *tail = rmp->startp[midx];
1027 return QF_OK;
1028}
1029
1030/*
1031 * Parse the match for the pointer line ('%p') pattern in regmatch.
1032 * Return the matched value in "fields->col".
1033 */
1034 static int
1035qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1036{
1037 char_u *match_ptr;
1038
1039 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1040 return QF_FAIL;
1041 fields->col = 0;
1042 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1043 ++match_ptr)
1044 {
1045 ++fields->col;
1046 if (*match_ptr == TAB)
1047 {
1048 fields->col += 7;
1049 fields->col -= fields->col % 8;
1050 }
1051 }
1052 ++fields->col;
1053 fields->use_viscol = TRUE;
1054 return QF_OK;
1055}
1056
1057/*
1058 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1059 * Return the matched value in "fields->col".
1060 */
1061 static int
1062qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1063{
1064 if (rmp->startp[midx] == NULL)
1065 return QF_FAIL;
1066 fields->col = (int)atol((char *)rmp->startp[midx]);
1067 fields->use_viscol = TRUE;
1068 return QF_OK;
1069}
1070
1071/*
1072 * Parse the match for the search text ('%s') pattern in regmatch.
1073 * Return the matched value in "fields->pattern".
1074 */
1075 static int
1076qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1077{
1078 int len;
1079
1080 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1081 return QF_FAIL;
1082 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1083 if (len > CMDBUFFSIZE - 5)
1084 len = CMDBUFFSIZE - 5;
1085 STRCPY(fields->pattern, "^\\V");
1086 STRNCAT(fields->pattern, rmp->startp[midx], len);
1087 fields->pattern[len + 3] = '\\';
1088 fields->pattern[len + 4] = '$';
1089 fields->pattern[len + 5] = NUL;
1090 return QF_OK;
1091}
1092
1093/*
1094 * Parse the match for the module ('%o') pattern in regmatch.
1095 * Return the matched value in "fields->module".
1096 */
1097 static int
1098qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1099{
1100 int len;
1101
1102 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1103 return QF_FAIL;
1104 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1105 if (len > CMDBUFFSIZE)
1106 len = CMDBUFFSIZE;
1107 STRNCAT(fields->module, rmp->startp[midx], len);
1108 return QF_OK;
1109}
1110
1111/*
1112 * 'errorformat' format pattern parser functions.
1113 * The '%f' and '%r' formats are parsed differently from other formats.
1114 * See qf_parse_match() for details.
1115 */
1116static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1117{
1118 NULL,
1119 qf_parse_fmt_n,
1120 qf_parse_fmt_l,
1121 qf_parse_fmt_c,
1122 qf_parse_fmt_t,
1123 qf_parse_fmt_m,
1124 NULL,
1125 qf_parse_fmt_p,
1126 qf_parse_fmt_v,
1127 qf_parse_fmt_s,
1128 qf_parse_fmt_o
1129};
1130
1131/*
1132 * Parse the error format pattern matches in "regmatch" and set the values in
1133 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1134 * match. Returns QF_OK if all the matches are successfully parsed. On
1135 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001136 */
1137 static int
1138qf_parse_match(
1139 char_u *linebuf,
1140 int linelen,
1141 efm_T *fmt_ptr,
1142 regmatch_T *regmatch,
1143 qffields_T *fields,
1144 int qf_multiline,
1145 int qf_multiscan,
1146 char_u **tail)
1147{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001148 int idx = fmt_ptr->prefix;
1149 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001150 int midx;
1151 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001152
1153 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1154 return QF_FAIL;
1155 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1156 fields->type = idx;
1157 else
1158 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001159
1160 // Extract error message data from matched line.
1161 // We check for an actual submatch, because "\[" and "\]" in
1162 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001163 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001164 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001165 status = QF_OK;
1166 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001167 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001168 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1169 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001170 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001171 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001172 status = qf_parse_fmt_plus(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001173 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001174 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001175 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001176 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001177 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001178 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001179 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001180
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001181 if (status != QF_OK)
1182 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001183 }
1184
1185 return QF_OK;
1186}
1187
1188/*
1189 * Parse an error line in 'linebuf' using a single error format string in
1190 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1191 * Returns QF_OK if the efm format matches completely and the fields are
1192 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1193 */
1194 static int
1195qf_parse_get_fields(
1196 char_u *linebuf,
1197 int linelen,
1198 efm_T *fmt_ptr,
1199 qffields_T *fields,
1200 int qf_multiline,
1201 int qf_multiscan,
1202 char_u **tail)
1203{
1204 regmatch_T regmatch;
1205 int status = QF_FAIL;
1206 int r;
1207
1208 if (qf_multiscan &&
1209 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1210 return QF_FAIL;
1211
1212 fields->namebuf[0] = NUL;
1213 fields->module[0] = NUL;
1214 fields->pattern[0] = NUL;
1215 if (!qf_multiscan)
1216 fields->errmsg[0] = NUL;
1217 fields->lnum = 0;
1218 fields->col = 0;
1219 fields->use_viscol = FALSE;
1220 fields->enr = -1;
1221 fields->type = 0;
1222 *tail = NULL;
1223
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001224 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001225 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001226 regmatch.regprog = fmt_ptr->prog;
1227 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1228 fmt_ptr->prog = regmatch.regprog;
1229 if (r)
1230 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1231 fields, qf_multiline, qf_multiscan, tail);
1232
1233 return status;
1234}
1235
1236/*
1237 * Parse directory error format prefixes (%D and %X).
1238 * Push and pop directories from the directory stack when scanning directory
1239 * names.
1240 */
1241 static int
1242qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1243{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001244 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001245 {
1246 if (*fields->namebuf == NUL)
1247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001248 emsg(_("E379: Missing or empty directory name"));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001249 return QF_FAIL;
1250 }
1251 qfl->qf_directory =
1252 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1253 if (qfl->qf_directory == NULL)
1254 return QF_FAIL;
1255 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001256 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001257 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1258
1259 return QF_OK;
1260}
1261
1262/*
1263 * Parse global file name error format prefixes (%O, %P and %Q).
1264 */
1265 static int
1266qf_parse_file_pfx(
1267 int idx,
1268 qffields_T *fields,
1269 qf_list_T *qfl,
1270 char_u *tail)
1271{
1272 fields->valid = FALSE;
1273 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1274 {
1275 if (*fields->namebuf && idx == 'P')
1276 qfl->qf_currfile =
1277 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1278 else if (idx == 'Q')
1279 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1280 *fields->namebuf = NUL;
1281 if (tail && *tail)
1282 {
1283 STRMOVE(IObuff, skipwhite(tail));
1284 qfl->qf_multiscan = TRUE;
1285 return QF_MULTISCAN;
1286 }
1287 }
1288
1289 return QF_OK;
1290}
1291
1292/*
1293 * Parse a non-error line (a line which doesn't match any of the error
1294 * format in 'efm').
1295 */
1296 static int
1297qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1298{
1299 char_u *p;
1300
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001301 fields->namebuf[0] = NUL; // no match found, remove file name
1302 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001303 fields->valid = FALSE;
1304 if (linelen >= fields->errmsglen)
1305 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001306 // linelen + null terminator
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001307 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1308 return QF_NOMEM;
1309 fields->errmsg = p;
1310 fields->errmsglen = linelen + 1;
1311 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001312 // copy whole line to error message
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001313 vim_strncpy(fields->errmsg, linebuf, linelen);
1314
1315 return QF_OK;
1316}
1317
1318/*
1319 * Parse multi-line error format prefixes (%C and %Z)
1320 */
1321 static int
1322qf_parse_multiline_pfx(
1323 qf_info_T *qi,
1324 int qf_idx,
1325 int idx,
1326 qf_list_T *qfl,
1327 qffields_T *fields)
1328{
1329 char_u *ptr;
1330 int len;
1331
1332 if (!qfl->qf_multiignore)
1333 {
1334 qfline_T *qfprev = qfl->qf_last;
1335
1336 if (qfprev == NULL)
1337 return QF_FAIL;
1338 if (*fields->errmsg && !qfl->qf_multiignore)
1339 {
1340 len = (int)STRLEN(qfprev->qf_text);
1341 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1342 == NULL)
1343 return QF_FAIL;
1344 STRCPY(ptr, qfprev->qf_text);
1345 vim_free(qfprev->qf_text);
1346 qfprev->qf_text = ptr;
1347 *(ptr += len) = '\n';
1348 STRCPY(++ptr, fields->errmsg);
1349 }
1350 if (qfprev->qf_nr == -1)
1351 qfprev->qf_nr = fields->enr;
1352 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001353 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001354 qfprev->qf_type = fields->type;
1355
1356 if (!qfprev->qf_lnum)
1357 qfprev->qf_lnum = fields->lnum;
1358 if (!qfprev->qf_col)
1359 qfprev->qf_col = fields->col;
1360 qfprev->qf_viscol = fields->use_viscol;
1361 if (!qfprev->qf_fnum)
1362 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1363 qfl->qf_directory,
1364 *fields->namebuf || qfl->qf_directory != NULL
1365 ? fields->namebuf
1366 : qfl->qf_currfile != NULL && fields->valid
1367 ? qfl->qf_currfile : 0);
1368 }
1369 if (idx == 'Z')
1370 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1371 line_breakcheck();
1372
1373 return QF_IGNORE_LINE;
1374}
1375
1376/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001377 * Parse a line and get the quickfix fields.
1378 * Return the QF_ status.
1379 */
1380 static int
1381qf_parse_line(
1382 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001383 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001384 char_u *linebuf,
1385 int linelen,
1386 efm_T *fmt_first,
1387 qffields_T *fields)
1388{
1389 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001390 int idx = 0;
1391 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001392 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001393 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001394
Bram Moolenaare333e792018-04-08 13:27:39 +02001395restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001396 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001397 if (fmt_start == NULL)
1398 fmt_ptr = fmt_first;
1399 else
1400 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001401 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001402 fmt_ptr = fmt_start;
1403 fmt_start = NULL;
1404 }
1405
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001406 // Try to match each part of 'errorformat' until we find a complete
1407 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001408 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001409 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1410 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001411 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001412 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1413 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1414 if (status == QF_NOMEM)
1415 return status;
1416 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001417 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001418 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001419 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001420
1421 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1422 {
1423 if (fmt_ptr != NULL)
1424 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001425 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001426 status = qf_parse_dir_pfx(idx, fields, qfl);
1427 if (status != QF_OK)
1428 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001429 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001430
1431 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1432 if (status != QF_OK)
1433 return status;
1434
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001435 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001436 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001437 }
1438 else if (fmt_ptr != NULL)
1439 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001440 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001441 if (fmt_ptr->conthere)
1442 fmt_start = fmt_ptr;
1443
1444 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1445 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001446 qfl->qf_multiline = TRUE; // start of a multi-line message
1447 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001448 }
1449 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001450 { // continuation of multi-line msg
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001451 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1452 if (status != QF_OK)
1453 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001454 }
1455 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001456 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001457 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1458 if (status == QF_MULTISCAN)
1459 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001460 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001461 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001462 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001463 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001464 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001465 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001466 return QF_IGNORE_LINE;
1467 }
1468 }
1469
1470 return QF_OK;
1471}
1472
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001473/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001474 * Returns TRUE if the specified quickfix/location stack is empty
1475 */
1476 static int
1477qf_stack_empty(qf_info_T *qi)
1478{
1479 return qi == NULL || qi->qf_listcount <= 0;
1480}
1481
1482/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001483 * Returns TRUE if the specified quickfix/location list is empty.
1484 */
1485 static int
1486qf_list_empty(qf_info_T *qi, int qf_idx)
1487{
1488 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1489 return TRUE;
1490 return qi->qf_lists[qf_idx].qf_count <= 0;
1491}
1492
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001493/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001494 * Allocate the fields used for parsing lines and populating a quickfix list.
1495 */
1496 static int
1497qf_alloc_fields(qffields_T *pfields)
1498{
1499 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1500 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1501 pfields->errmsglen = CMDBUFFSIZE + 1;
1502 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1503 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1504 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1505 || pfields->pattern == NULL || pfields->module == NULL)
1506 return FAIL;
1507
1508 return OK;
1509}
1510
1511/*
1512 * Free the fields used for parsing lines and populating a quickfix list.
1513 */
1514 static void
1515qf_free_fields(qffields_T *pfields)
1516{
1517 vim_free(pfields->namebuf);
1518 vim_free(pfields->module);
1519 vim_free(pfields->errmsg);
1520 vim_free(pfields->pattern);
1521}
1522
1523/*
1524 * Setup the state information used for parsing lines and populating a
1525 * quickfix list.
1526 */
1527 static int
1528qf_setup_state(
1529 qfstate_T *pstate,
1530 char_u *enc,
1531 char_u *efile,
1532 typval_T *tv,
1533 buf_T *buf,
1534 linenr_T lnumfirst,
1535 linenr_T lnumlast)
1536{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001537 pstate->vc.vc_type = CONV_NONE;
1538 if (enc != NULL && *enc != NUL)
1539 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001540
1541 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001543 semsg(_(e_openerrf), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001544 return FAIL;
1545 }
1546
1547 if (tv != NULL)
1548 {
1549 if (tv->v_type == VAR_STRING)
1550 pstate->p_str = tv->vval.v_string;
1551 else if (tv->v_type == VAR_LIST)
1552 pstate->p_li = tv->vval.v_list->lv_first;
1553 pstate->tv = tv;
1554 }
1555 pstate->buf = buf;
1556 pstate->buflnum = lnumfirst;
1557 pstate->lnumlast = lnumlast;
1558
1559 return OK;
1560}
1561
1562/*
1563 * Cleanup the state information used for parsing lines and populating a
1564 * quickfix list.
1565 */
1566 static void
1567qf_cleanup_state(qfstate_T *pstate)
1568{
1569 if (pstate->fd != NULL)
1570 fclose(pstate->fd);
1571
1572 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001573 if (pstate->vc.vc_type != CONV_NONE)
1574 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001575}
1576
1577/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001578 * Read the errorfile "efile" into memory, line by line, building the error
1579 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001580 * Alternative: when "efile" is NULL read errors from buffer "buf".
1581 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001582 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001583 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1584 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001585 * Return -1 for error, number of errors for success.
1586 */
1587 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001588qf_init_ext(
1589 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001590 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001591 char_u *efile,
1592 buf_T *buf,
1593 typval_T *tv,
1594 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001595 int newlist, // TRUE: start a new error list
1596 linenr_T lnumfirst, // first line number to use
1597 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001598 char_u *qf_title,
1599 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001600{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001601 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001602 qfstate_T state;
1603 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001604 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001605 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001606 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001608 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001609 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001610 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001612 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001613 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001614
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001615 vim_memset(&state, 0, sizeof(state));
1616 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001617 if ((qf_alloc_fields(&fields) == FAIL) ||
1618 (qf_setup_state(&state, enc, efile, tv, buf,
1619 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 goto qf_init_end;
1621
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001622 if (newlist || qf_idx == qi->qf_listcount)
1623 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001624 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001625 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001626 qf_idx = qi->qf_curlist;
1627 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001628 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001629 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001630 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001631 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001632 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001633 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001636 qfl = &qi->qf_lists[qf_idx];
1637
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001638 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001639 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001640 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 else
1642 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001644 // If the errorformat didn't change between calls, then reuse the
1645 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001646 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1647 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001648 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001649 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001650 free_efm_list(&fmt_first);
1651
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001652 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001653 fmt_first = parse_efm_option(efm);
1654 if (fmt_first != NULL)
1655 last_efm = vim_strsave(efm);
1656 }
1657
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001658 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001661 // got_int is reset here, because it was probably set when killing the
1662 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 got_int = FALSE;
1664
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001665 // Read the lines in the error file one by one.
1666 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001667 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001669 // Get the next line from a file/buffer/list/string
Bram Moolenaare0d37972016-07-15 22:36:01 +02001670 status = qf_get_nextline(&state);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001671 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001672 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001673 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001674 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001676 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1677 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001678 if (status == QF_FAIL)
1679 goto error2;
1680 if (status == QF_NOMEM)
1681 goto qf_init_end;
1682 if (status == QF_IGNORE_LINE)
1683 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001685 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001686 qf_idx,
1687 qfl->qf_directory,
1688 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001689 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001690 : ((qfl->qf_currfile != NULL && fields.valid)
1691 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001692 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001693 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001694 fields.errmsg,
1695 fields.lnum,
1696 fields.col,
1697 fields.use_viscol,
1698 fields.pattern,
1699 fields.enr,
1700 fields.type,
1701 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 goto error2;
1703 line_breakcheck();
1704 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001705 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001707 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001709 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001710 qfl->qf_ptr = qfl->qf_start;
1711 qfl->qf_index = 1;
1712 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
1714 else
1715 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001716 qfl->qf_nonevalid = FALSE;
1717 if (qfl->qf_ptr == NULL)
1718 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001720 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001721 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001722 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001724 emsg(_(e_readerrf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001726 if (!adding)
1727 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001728 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001729 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001730 qi->qf_listcount--;
1731 if (qi->qf_curlist > 0)
1732 --qi->qf_curlist;
1733 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001734qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001735 if (qf_idx == qi->qf_curlist)
1736 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001737 qf_cleanup_state(&state);
1738 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740 return retval;
1741}
1742
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001743/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001744 * Read the errorfile "efile" into memory, line by line, building the error
1745 * list. Set the error list's title to qf_title.
1746 * Return -1 for error, number of errors for success.
1747 */
1748 int
1749qf_init(win_T *wp,
1750 char_u *efile,
1751 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001752 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001753 char_u *qf_title,
1754 char_u *enc)
1755{
1756 qf_info_T *qi = &ql_info;
1757
1758 if (wp != NULL)
1759 {
1760 qi = ll_get_or_alloc_list(wp);
1761 if (qi == NULL)
1762 return FAIL;
1763 }
1764
1765 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1766 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1767}
1768
1769/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001770 * Set the title of the specified quickfix list. Frees the previous title.
1771 * Prepends ':' to the title.
1772 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001773 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001774qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001775{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001776 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001777
Bram Moolenaarfb604092014-07-23 15:55:00 +02001778 if (title != NULL)
1779 {
1780 char_u *p = alloc((int)STRLEN(title) + 2);
1781
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001782 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001783 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001784 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001785 }
1786}
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001789 * The title of a quickfix/location list is set, by default, to the command
1790 * that created the quickfix list with the ":" prefix.
1791 * Create a quickfix list title string by prepending ":" to a user command.
1792 * Returns a pointer to a static buffer with the title.
1793 */
1794 static char_u *
1795qf_cmdtitle(char_u *cmd)
1796{
1797 static char_u qftitle_str[IOSIZE];
1798
1799 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1800 return qftitle_str;
1801}
1802
1803/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001804 * Prepare for adding a new quickfix list. If the current list is in the
1805 * middle of the stack, then all the following lists are freed and then
1806 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 */
1808 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001809qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
1811 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001812 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001814 // If the current entry is not the last entry, delete entries beyond
1815 // the current entry. This makes it possible to browse in a tree-like
1816 // way with ":grep'.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001817 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001818 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001820 // When the stack is full, remove to oldest entry
1821 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001822 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001824 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001826 qi->qf_lists[i - 1] = qi->qf_lists[i];
1827 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
1829 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001830 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001831 qfl = &qi->qf_lists[qi->qf_curlist];
1832 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1833 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001834 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001835 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836}
1837
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001838/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001839 * Queue location list stack delete request.
1840 */
1841 static void
1842locstack_queue_delreq(qf_info_T *qi)
1843{
1844 qf_delq_T *q;
1845
1846 q = (qf_delq_T *)alloc((unsigned)sizeof(qf_delq_T));
1847 if (q != NULL)
1848 {
1849 q->qi = qi;
1850 q->next = qf_delq_head;
1851 qf_delq_head = q;
1852 }
1853}
1854
1855/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001856 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001857 */
1858 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001859ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001860{
1861 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001862 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001863
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001864 qi = *pqi;
1865 if (qi == NULL)
1866 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001867 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001868
1869 qi->qf_refcount--;
1870 if (qi->qf_refcount < 1)
1871 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001872 // No references to this location list.
1873 // If the location list is still in use, then queue the delete request
1874 // to be processed later.
1875 if (quickfix_busy > 0)
1876 locstack_queue_delreq(qi);
1877 else
1878 {
1879 for (i = 0; i < qi->qf_listcount; ++i)
1880 qf_free(&qi->qf_lists[i]);
1881 vim_free(qi);
1882 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001883 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001884}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001885
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001886/*
1887 * Free all the quickfix/location lists in the stack.
1888 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001889 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001890qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001891{
1892 int i;
1893 qf_info_T *qi = &ql_info;
1894
1895 if (wp != NULL)
1896 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001897 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001898 ll_free_all(&wp->w_llist);
1899 ll_free_all(&wp->w_llist_ref);
1900 }
1901 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001902 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001903 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001904 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001905}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001908 * Delay freeing of location list stacks when the quickfix code is running.
1909 * Used to avoid problems with autocmds freeing location list stacks when the
1910 * quickfix code is still referencing the stack.
1911 * Must always call decr_quickfix_busy() exactly once after this.
1912 */
1913 static void
1914incr_quickfix_busy(void)
1915{
1916 quickfix_busy++;
1917}
1918
1919/*
1920 * Safe to free location list stacks. Process any delayed delete requests.
1921 */
1922 static void
1923decr_quickfix_busy(void)
1924{
1925 if (--quickfix_busy == 0)
1926 {
1927 // No longer referencing the location lists. Process all the pending
1928 // delete requests.
1929 while (qf_delq_head != NULL)
1930 {
1931 qf_delq_T *q = qf_delq_head;
1932
1933 qf_delq_head = q->next;
1934 ll_free_all(&q->qi);
1935 vim_free(q);
1936 }
1937 }
1938#ifdef ABORT_ON_INTERNAL_ERROR
1939 if (quickfix_busy < 0)
1940 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001941 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001942 abort();
1943 }
1944#endif
1945}
1946
1947#if defined(EXITFREE) || defined(PROTO)
1948 void
1949check_quickfix_busy(void)
1950{
1951 if (quickfix_busy != 0)
1952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001953 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001954# ifdef ABORT_ON_INTERNAL_ERROR
1955 abort();
1956# endif
1957 }
1958}
1959#endif
1960
1961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 * Add an entry to the end of the list of errors.
1963 * Returns OK or FAIL.
1964 */
1965 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001966qf_add_entry(
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001967 qf_info_T *qi, // quickfix list
1968 int qf_idx, // list index
1969 char_u *dir, // optional directory name
1970 char_u *fname, // file name or NULL
1971 char_u *module, // module name or NULL
1972 int bufnum, // buffer number or zero
1973 char_u *mesg, // message
1974 long lnum, // line number
1975 int col, // column
1976 int vis_col, // using visual column
1977 char_u *pattern, // search pattern
1978 int nr, // error number
1979 int type, // type character
1980 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001982 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001983 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001984 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001986 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001988 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001989 {
1990 buf_T *buf = buflist_findnr(bufnum);
1991
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001992 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001993 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001994 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001995 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001996 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001997 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001998 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2000 {
2001 vim_free(qfp);
2002 return FAIL;
2003 }
2004 qfp->qf_lnum = lnum;
2005 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002006 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002007 if (pattern == NULL || *pattern == NUL)
2008 qfp->qf_pattern = NULL;
2009 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2010 {
2011 vim_free(qfp->qf_text);
2012 vim_free(qfp);
2013 return FAIL;
2014 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002015 if (module == NULL || *module == NUL)
2016 qfp->qf_module = NULL;
2017 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2018 {
2019 vim_free(qfp->qf_text);
2020 vim_free(qfp->qf_pattern);
2021 vim_free(qfp);
2022 return FAIL;
2023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002025 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 type = 0;
2027 qfp->qf_type = type;
2028 qfp->qf_valid = valid;
2029
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002030 lastp = &qfl->qf_last;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002031 if (qf_list_empty(qi, qf_idx)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002033 qfl->qf_start = qfp;
2034 qfl->qf_ptr = qfp;
2035 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002036 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038 else
2039 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002040 qfp->qf_prev = *lastp;
2041 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002043 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002045 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002046 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002047 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002049 qfl->qf_index = qfl->qf_count;
2050 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
2052
2053 return OK;
2054}
2055
2056/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002057 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002058 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002059 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002060qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002061{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002062 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002063
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002064 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002065 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002066 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002067 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002068 qi->qfl_type = qfltype;
2069 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002070 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002071}
2072
2073/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002074 * Return the location list stack for window 'wp'.
2075 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002076 */
2077 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002078ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002079{
2080 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002081 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002082 return wp->w_llist_ref;
2083
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002084 // For a non-location list window, w_llist_ref should not point to a
2085 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002086 ll_free_all(&wp->w_llist_ref);
2087
2088 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002089 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002090 return wp->w_llist;
2091}
2092
2093/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002094 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2095 */
2096 static int
2097copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2098{
2099 int i;
2100 qfline_T *from_qfp;
2101 qfline_T *prevp;
2102
2103 // copy all the location entries in this list
2104 for (i = 0, from_qfp = from_qfl->qf_start;
2105 i < from_qfl->qf_count && from_qfp != NULL;
2106 ++i, from_qfp = from_qfp->qf_next)
2107 {
2108 if (qf_add_entry(to_qi,
2109 to_qi->qf_curlist,
2110 NULL,
2111 NULL,
2112 from_qfp->qf_module,
2113 0,
2114 from_qfp->qf_text,
2115 from_qfp->qf_lnum,
2116 from_qfp->qf_col,
2117 from_qfp->qf_viscol,
2118 from_qfp->qf_pattern,
2119 from_qfp->qf_nr,
2120 0,
2121 from_qfp->qf_valid) == FAIL)
2122 return FAIL;
2123
2124 // qf_add_entry() will not set the qf_num field, as the
2125 // directory and file names are not supplied. So the qf_fnum
2126 // field is copied here.
2127 prevp = to_qfl->qf_last;
2128 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2129 prevp->qf_type = from_qfp->qf_type; // error type
2130 if (from_qfl->qf_ptr == from_qfp)
2131 to_qfl->qf_ptr = prevp; // current location
2132 }
2133
2134 return OK;
2135}
2136
2137/*
2138 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2139 */
2140 static int
2141copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2142{
2143 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002144 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002145 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2146 to_qfl->qf_count = 0;
2147 to_qfl->qf_index = 0;
2148 to_qfl->qf_start = NULL;
2149 to_qfl->qf_last = NULL;
2150 to_qfl->qf_ptr = NULL;
2151 if (from_qfl->qf_title != NULL)
2152 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2153 else
2154 to_qfl->qf_title = NULL;
2155 if (from_qfl->qf_ctx != NULL)
2156 {
2157 to_qfl->qf_ctx = alloc_tv();
2158 if (to_qfl->qf_ctx != NULL)
2159 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2160 }
2161 else
2162 to_qfl->qf_ctx = NULL;
2163
2164 if (from_qfl->qf_count)
2165 if (copy_loclist_entries(from_qfl, to_qfl, to_qi) == FAIL)
2166 return FAIL;
2167
2168 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2169
2170 // Assign a new ID for the location list
2171 to_qfl->qf_id = ++last_qf_id;
2172 to_qfl->qf_changedtick = 0L;
2173
2174 // When no valid entries are present in the list, qf_ptr points to
2175 // the first item in the list
2176 if (to_qfl->qf_nonevalid)
2177 {
2178 to_qfl->qf_ptr = to_qfl->qf_start;
2179 to_qfl->qf_index = 1;
2180 }
2181
2182 return OK;
2183}
2184
2185/*
2186 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002187 */
2188 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002189copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002190{
2191 qf_info_T *qi;
2192 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002193
Bram Moolenaar09037502018-09-25 22:08:14 +02002194 // When copying from a location list window, copy the referenced
2195 // location list. For other windows, copy the location list for
2196 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002197 if (IS_LL_WINDOW(from))
2198 qi = from->w_llist_ref;
2199 else
2200 qi = from->w_llist;
2201
Bram Moolenaar09037502018-09-25 22:08:14 +02002202 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002203 return;
2204
Bram Moolenaar09037502018-09-25 22:08:14 +02002205 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002206 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002207 return;
2208
2209 to->w_llist->qf_listcount = qi->qf_listcount;
2210
Bram Moolenaar09037502018-09-25 22:08:14 +02002211 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002212 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002213 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002214 to->w_llist->qf_curlist = idx;
2215
Bram Moolenaar09037502018-09-25 22:08:14 +02002216 if (copy_loclist(&qi->qf_lists[idx],
2217 &to->w_llist->qf_lists[idx], to->w_llist) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002218 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002219 qf_free_all(to);
2220 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002221 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002222 }
2223
Bram Moolenaar09037502018-09-25 22:08:14 +02002224 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002225}
2226
2227/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002228 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002229 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 */
2231 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002232qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002234 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar82404332016-07-10 17:00:38 +02002235 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002236 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002237 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002238
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002239 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241
Bram Moolenaare60acc12011-05-10 16:41:25 +02002242#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002243 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002244#endif
2245#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002246 if (directory != NULL)
2247 slash_adjust(directory);
2248 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002249#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002250 if (directory != NULL && !vim_isAbsName(fname)
2251 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2252 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002253 // Here we check if the file really exists.
2254 // This should normally be true, but if make works without
2255 // "leaving directory"-messages we might have missed a
2256 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002257 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002260 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002261 if (directory)
2262 ptr = concat_fnames(directory, fname, TRUE);
2263 else
2264 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002266 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002267 bufname = ptr;
2268 }
2269 else
2270 bufname = fname;
2271
2272 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002273 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002274 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002275 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002276 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002278 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002279 {
2280 vim_free(qf_last_bufname);
2281 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2282 if (bufname == ptr)
2283 qf_last_bufname = bufname;
2284 else
2285 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002286 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002287 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002288 if (buf == NULL)
2289 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002290
Bram Moolenaarc1542742016-07-20 21:44:37 +02002291 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002292 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002293 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294}
2295
2296/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002297 * Push dirbuf onto the directory stack and return pointer to actual dir or
2298 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 */
2300 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002301qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302{
2303 struct dir_stack_T *ds_new;
2304 struct dir_stack_T *ds_ptr;
2305
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002306 // allocate new stack element and hook it in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2308 if (ds_new == NULL)
2309 return NULL;
2310
2311 ds_new->next = *stackptr;
2312 *stackptr = ds_new;
2313
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002314 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (vim_isAbsName(dirbuf)
2316 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002317 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 (*stackptr)->dirname = vim_strsave(dirbuf);
2319 else
2320 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002321 // Okay we don't have an absolute path.
2322 // dirbuf must be a subdir of one of the directories on the stack.
2323 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 ds_new = (*stackptr)->next;
2325 (*stackptr)->dirname = NULL;
2326 while (ds_new)
2327 {
2328 vim_free((*stackptr)->dirname);
2329 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2330 TRUE);
2331 if (mch_isdir((*stackptr)->dirname) == TRUE)
2332 break;
2333
2334 ds_new = ds_new->next;
2335 }
2336
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002337 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 while ((*stackptr)->next != ds_new)
2339 {
2340 ds_ptr = (*stackptr)->next;
2341 (*stackptr)->next = (*stackptr)->next->next;
2342 vim_free(ds_ptr->dirname);
2343 vim_free(ds_ptr);
2344 }
2345
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002346 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 if (ds_new == NULL)
2348 {
2349 vim_free((*stackptr)->dirname);
2350 (*stackptr)->dirname = vim_strsave(dirbuf);
2351 }
2352 }
2353
2354 if ((*stackptr)->dirname != NULL)
2355 return (*stackptr)->dirname;
2356 else
2357 {
2358 ds_ptr = *stackptr;
2359 *stackptr = (*stackptr)->next;
2360 vim_free(ds_ptr);
2361 return NULL;
2362 }
2363}
2364
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365/*
2366 * pop dirbuf from the directory stack and return previous directory or NULL if
2367 * stack is empty
2368 */
2369 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002370qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
2372 struct dir_stack_T *ds_ptr;
2373
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002374 // TODO: Should we check if dirbuf is the directory on top of the stack?
2375 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002377 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 if (*stackptr != NULL)
2379 {
2380 ds_ptr = *stackptr;
2381 *stackptr = (*stackptr)->next;
2382 vim_free(ds_ptr->dirname);
2383 vim_free(ds_ptr);
2384 }
2385
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002386 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 return *stackptr ? (*stackptr)->dirname : NULL;
2388}
2389
2390/*
2391 * clean up directory stack
2392 */
2393 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002394qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395{
2396 struct dir_stack_T *ds_ptr;
2397
2398 while ((ds_ptr = *stackptr) != NULL)
2399 {
2400 *stackptr = (*stackptr)->next;
2401 vim_free(ds_ptr->dirname);
2402 vim_free(ds_ptr);
2403 }
2404}
2405
2406/*
2407 * Check in which directory of the directory stack the given file can be
2408 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002409 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 * Cleans up intermediate directory entries.
2411 *
2412 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002413 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 * ./
2415 * ./aa
2416 * ./aa/bb
2417 * ./bb
2418 * ./bb/x.c
2419 * and make says:
2420 * making all in aa
2421 * making all in bb
2422 * x.c:9: Error
2423 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2424 * qf_guess_filepath will return NULL.
2425 */
2426 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002427qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428{
2429 struct dir_stack_T *ds_ptr;
2430 struct dir_stack_T *ds_tmp;
2431 char_u *fullname;
2432
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002433 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002434 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 return NULL;
2436
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002437 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 fullname = NULL;
2439 while (ds_ptr)
2440 {
2441 vim_free(fullname);
2442 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2443
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002444 // If concat_fnames failed, just go on. The worst thing that can happen
2445 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2447 break;
2448
2449 ds_ptr = ds_ptr->next;
2450 }
2451
2452 vim_free(fullname);
2453
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002454 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002455 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002457 ds_tmp = qfl->qf_dir_stack->next;
2458 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 vim_free(ds_tmp->dirname);
2460 vim_free(ds_tmp);
2461 }
2462
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002463 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464}
2465
2466/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002467 * Returns TRUE if a quickfix/location list with the given identifier exists.
2468 */
2469 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002470qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002471{
2472 qf_info_T *qi = &ql_info;
2473 int i;
2474
2475 if (wp != NULL)
2476 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002477 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002478 if (qi == NULL)
2479 return FALSE;
2480 }
2481
2482 for (i = 0; i < qi->qf_listcount; ++i)
2483 if (qi->qf_lists[i].qf_id == qf_id)
2484 return TRUE;
2485
2486 return FALSE;
2487}
2488
2489/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002490 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002491 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002492 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002493 * Similar to location list.
2494 */
2495 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002496is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002497{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002498 qfline_T *qfp;
2499 int i;
2500
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002501 // Search for the entry in the current list
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002502 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2503 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002504 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002505 break;
2506
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002507 if (i == qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002508 return FALSE;
2509
2510 return TRUE;
2511}
2512
2513/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002514 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002515 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002516 */
2517 static qfline_T *
2518get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002519 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002520 qfline_T *qf_ptr,
2521 int *qf_index,
2522 int dir)
2523{
2524 int idx;
2525 int old_qf_fnum;
2526
2527 idx = *qf_index;
2528 old_qf_fnum = qf_ptr->qf_fnum;
2529
2530 do
2531 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002532 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002533 return NULL;
2534 ++idx;
2535 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002536 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002537 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2538
2539 *qf_index = idx;
2540 return qf_ptr;
2541}
2542
2543/*
2544 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002545 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002546 */
2547 static qfline_T *
2548get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002549 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002550 qfline_T *qf_ptr,
2551 int *qf_index,
2552 int dir)
2553{
2554 int idx;
2555 int old_qf_fnum;
2556
2557 idx = *qf_index;
2558 old_qf_fnum = qf_ptr->qf_fnum;
2559
2560 do
2561 {
2562 if (idx == 1 || qf_ptr->qf_prev == NULL)
2563 return NULL;
2564 --idx;
2565 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002566 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002567 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2568
2569 *qf_index = idx;
2570 return qf_ptr;
2571}
2572
2573/*
2574 * Get the n'th (errornr) previous/next valid entry from the current entry in
2575 * the quickfix list.
2576 * dir == FORWARD or FORWARD_FILE: next valid entry
2577 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2578 */
2579 static qfline_T *
2580get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002581 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002582 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002583 int dir,
2584 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002585{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002586 qfline_T *qf_ptr = qfl->qf_ptr;
2587 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002588 qfline_T *prev_qf_ptr;
2589 int prev_index;
2590 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2591 char_u *err = e_no_more_items;
2592
2593 while (errornr--)
2594 {
2595 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002596 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002597
2598 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002599 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002600 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002601 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002602 if (qf_ptr == NULL)
2603 {
2604 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002605 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002606 if (err != NULL)
2607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002608 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002609 return NULL;
2610 }
2611 break;
2612 }
2613
2614 err = NULL;
2615 }
2616
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002617 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002618 return qf_ptr;
2619}
2620
2621/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002622 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2623 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002624 */
2625 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002626get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002627{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002628 qfline_T *qf_ptr = qfl->qf_ptr;
2629 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002630
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002631 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002632 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2633 {
2634 --qf_idx;
2635 qf_ptr = qf_ptr->qf_prev;
2636 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002637 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002638 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2639 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002640 {
2641 ++qf_idx;
2642 qf_ptr = qf_ptr->qf_next;
2643 }
2644
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002645 *new_qfidx = qf_idx;
2646 return qf_ptr;
2647}
2648
2649/*
2650 * Get a entry specied by 'errornr' and 'dir' from the current
2651 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2652 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2653 * Returns a pointer to the entry and the index of the new entry is stored in
2654 * 'new_qfidx'.
2655 */
2656 static qfline_T *
2657qf_get_entry(
2658 qf_list_T *qfl,
2659 int errornr,
2660 int dir,
2661 int *new_qfidx)
2662{
2663 qfline_T *qf_ptr = qfl->qf_ptr;
2664 int qfidx = qfl->qf_index;
2665
2666 if (dir != 0) // next/prev valid entry
2667 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2668 else if (errornr != 0) // go to specified number
2669 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2670
2671 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002672 return qf_ptr;
2673}
2674
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002675/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002676 * Find a window displaying a Vim help file.
2677 */
2678 static win_T *
2679qf_find_help_win(void)
2680{
2681 win_T *wp;
2682
2683 FOR_ALL_WINDOWS(wp)
2684 if (bt_help(wp->w_buffer))
2685 return wp;
2686
2687 return NULL;
2688}
2689
2690/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002691 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2692 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002693 */
2694 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002695jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002696{
2697 win_T *wp;
2698 int flags;
2699
Bram Moolenaarb2443732018-11-11 22:50:27 +01002700 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002701 wp = NULL;
2702 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002703 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002704 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2705 win_enter(wp, TRUE);
2706 else
2707 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002708 // Split off help window; put it at far top if no position
2709 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002710 flags = WSP_HELP;
2711 if (cmdmod.split == 0 && curwin->w_width != Columns
2712 && curwin->w_width < 80)
2713 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002714 // If the user asks to open a new window, then copy the location list.
2715 // Otherwise, don't copy the location list.
2716 if (IS_LL_STACK(qi) && !newwin)
2717 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002718
2719 if (win_split(0, flags) == FAIL)
2720 return FAIL;
2721
2722 *opened_window = TRUE;
2723
2724 if (curwin->w_height < p_hh)
2725 win_setheight((int)p_hh);
2726
Bram Moolenaarb2443732018-11-11 22:50:27 +01002727 // When using location list, the new window should use the supplied
2728 // location list. If the user asks to open a new window, then the new
2729 // window will get a copy of the location list.
2730 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002731 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002732 curwin->w_llist = qi;
2733 qi->qf_refcount++;
2734 }
2735 }
2736
2737 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002738 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002739
2740 return OK;
2741}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002742
2743/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002744 * Find a non-quickfix window using the given location list.
2745 * Returns NULL if a matching window is not found.
2746 */
2747 static win_T *
2748qf_find_win_with_loclist(qf_info_T *ll)
2749{
2750 win_T *wp;
2751
2752 FOR_ALL_WINDOWS(wp)
2753 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2754 return wp;
2755
2756 return NULL;
2757}
2758
2759/*
2760 * Find a window containing a normal buffer
2761 */
2762 static win_T *
2763qf_find_win_with_normal_buf(void)
2764{
2765 win_T *wp;
2766
2767 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002768 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002769 return wp;
2770
2771 return NULL;
2772}
2773
2774/*
2775 * Go to a window in any tabpage containing the specified file. Returns TRUE
2776 * if successfully jumped to the window. Otherwise returns FALSE.
2777 */
2778 static int
2779qf_goto_tabwin_with_file(int fnum)
2780{
2781 tabpage_T *tp;
2782 win_T *wp;
2783
2784 FOR_ALL_TAB_WINDOWS(tp, wp)
2785 if (wp->w_buffer->b_fnum == fnum)
2786 {
2787 goto_tabpage_win(tp, wp);
2788 return TRUE;
2789 }
2790
2791 return FALSE;
2792}
2793
2794/*
2795 * Create a new window to show a file above the quickfix window. Called when
2796 * only the quickfix window is present.
2797 */
2798 static int
2799qf_open_new_file_win(qf_info_T *ll_ref)
2800{
2801 int flags;
2802
2803 flags = WSP_ABOVE;
2804 if (ll_ref != NULL)
2805 flags |= WSP_NEWLOC;
2806 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002807 return FAIL; // not enough room for window
2808 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002809 swb_flags = 0;
2810 RESET_BINDING(curwin);
2811 if (ll_ref != NULL)
2812 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002813 // The new window should use the location list from the
2814 // location list window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002815 curwin->w_llist = ll_ref;
2816 ll_ref->qf_refcount++;
2817 }
2818 return OK;
2819}
2820
2821/*
2822 * Go to a window that shows the right buffer. If the window is not found, go
2823 * to the window just above the location list window. This is used for opening
2824 * a file from a location window and not from a quickfix window. If some usable
2825 * window is previously found, then it is supplied in 'use_win'.
2826 */
2827 static void
2828qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2829{
2830 win_T *win = use_win;
2831
2832 if (win == NULL)
2833 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002834 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002835 FOR_ALL_WINDOWS(win)
2836 if (win->w_buffer->b_fnum == qf_fnum)
2837 break;
2838 if (win == NULL)
2839 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002840 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002841 win = curwin;
2842 do
2843 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002844 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002845 break;
2846 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002847 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002848 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002849 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002850 } while (win != curwin);
2851 }
2852 }
2853 win_goto(win);
2854
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002855 // If the location list for the window is not set, then set it
2856 // to the location list from the location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002857 if (win->w_llist == NULL)
2858 {
2859 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002860 if (ll_ref != NULL)
2861 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002862 }
2863}
2864
2865/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002866 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2867 * not found, then go to the window just above the quickfix window. This is
2868 * used for opening a file from a quickfix window and not from a location
2869 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002870 */
2871 static void
2872qf_goto_win_with_qfl_file(int qf_fnum)
2873{
2874 win_T *win;
2875 win_T *altwin;
2876
2877 win = curwin;
2878 altwin = NULL;
2879 for (;;)
2880 {
2881 if (win->w_buffer->b_fnum == qf_fnum)
2882 break;
2883 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002884 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002885 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002886 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002887
2888 if (IS_QF_WINDOW(win))
2889 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002890 // Didn't find it, go to the window before the quickfix
2891 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002892 if (altwin != NULL)
2893 win = altwin;
2894 else if (curwin->w_prev != NULL)
2895 win = curwin->w_prev;
2896 else
2897 win = curwin->w_next;
2898 break;
2899 }
2900
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002901 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02002902 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002903 altwin = win;
2904 }
2905
2906 win_goto(win);
2907}
2908
2909/*
2910 * Find a suitable window for opening a file (qf_fnum) from the
2911 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01002912 * window, jump to it. Otherwise open a new window to display the file. If
2913 * 'newwin' is TRUE, then always open a new window. This is called from either
2914 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002915 */
2916 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002917qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002918{
2919 win_T *usable_win_ptr = NULL;
2920 int usable_win;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002921 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002922 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002923
2924 usable_win = 0;
2925
Bram Moolenaarb2443732018-11-11 22:50:27 +01002926 // If opening a new window, then don't use the location list referred by
2927 // the current window. Otherwise two windows will refer to the same
2928 // location list.
2929 if (!newwin)
2930 ll_ref = curwin->w_llist_ref;
2931
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002932 if (ll_ref != NULL)
2933 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002934 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002935 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2936 if (usable_win_ptr != NULL)
2937 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002938 }
2939
2940 if (!usable_win)
2941 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002942 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002943 win = qf_find_win_with_normal_buf();
2944 if (win != NULL)
2945 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002946 }
2947
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002948 // If no usable window is found and 'switchbuf' contains "usetab"
2949 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002950 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002951 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002952
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002953 // If there is only one window and it is the quickfix window, create a
2954 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01002955 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002956 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002957 if (qf_open_new_file_win(ll_ref) != OK)
2958 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002959 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002960 }
2961 else
2962 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002963 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002964 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002965 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002966 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002967 }
2968
2969 return OK;
2970}
2971
2972/*
2973 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002974 * Returns OK if successfully edited the file, FAIL on failing to open the
2975 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
2976 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002977 */
2978 static int
2979qf_jump_edit_buffer(
2980 qf_info_T *qi,
2981 qfline_T *qf_ptr,
2982 int forceit,
2983 win_T *oldwin,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002984 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002985{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002986 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002987 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002988 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02002989 int old_qf_curlist = qi->qf_curlist;
2990 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002991
2992 if (qf_ptr->qf_type == 1)
2993 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002994 // Open help file (do_ecmd() will set b_help flag, readfile() will
2995 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002996 if (!can_abandon(curbuf, forceit))
2997 {
2998 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02002999 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003000 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003001
3002 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3003 ECMD_HIDE + ECMD_SET_HELP,
3004 oldwin == curwin ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003005 }
3006 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003007 retval = buflist_getfile(qf_ptr->qf_fnum,
3008 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003009
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003010 // If a location list, check whether the associated window is still
3011 // present.
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003012 if (qfl_type == QFLT_LOCATION && !win_valid_any_tab(oldwin))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003013 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003014 emsg(_("E924: Current window was closed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003015 *opened_window = FALSE;
3016 return NOTDONE;
3017 }
3018
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003019 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003021 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003022 return NOTDONE;
3023 }
3024
3025 if (old_qf_curlist != qi->qf_curlist
3026 || !is_qf_entry_present(qfl, qf_ptr))
3027 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003028 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003029 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003030 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003031 emsg(_(e_loc_list_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003032 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003033 }
3034
3035 return retval;
3036}
3037
3038/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003039 * Go to the error line in the current file using either line/column number or
3040 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003041 */
3042 static void
3043qf_jump_goto_line(
3044 linenr_T qf_lnum,
3045 int qf_col,
3046 char_u qf_viscol,
3047 char_u *qf_pattern)
3048{
3049 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003050
3051 if (qf_pattern == NULL)
3052 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003053 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003054 i = qf_lnum;
3055 if (i > 0)
3056 {
3057 if (i > curbuf->b_ml.ml_line_count)
3058 i = curbuf->b_ml.ml_line_count;
3059 curwin->w_cursor.lnum = i;
3060 }
3061 if (qf_col > 0)
3062 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003063 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003064 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003065 coladvance(qf_col - 1);
3066 else
3067 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003068 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003069 check_cursor();
3070 }
3071 else
3072 beginline(BL_WHITE | BL_FIX);
3073 }
3074 else
3075 {
3076 pos_T save_cursor;
3077
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003078 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003079 save_cursor = curwin->w_cursor;
3080 curwin->w_cursor.lnum = 0;
3081 if (!do_search(NULL, '/', qf_pattern, (long)1,
3082 SEARCH_KEEP, NULL, NULL))
3083 curwin->w_cursor = save_cursor;
3084 }
3085}
3086
3087/*
3088 * Display quickfix list index and size message
3089 */
3090 static void
3091qf_jump_print_msg(
3092 qf_info_T *qi,
3093 int qf_index,
3094 qfline_T *qf_ptr,
3095 buf_T *old_curbuf,
3096 linenr_T old_lnum)
3097{
3098 linenr_T i;
3099 int len;
3100
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003101 // Update the screen before showing the message, unless the screen
3102 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003103 if (!msg_scrolled)
3104 update_topline_redraw();
3105 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3106 qi->qf_lists[qi->qf_curlist].qf_count,
3107 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3108 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003109 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003110 len = (int)STRLEN(IObuff);
3111 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3112
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003113 // Output the message. Overwrite to avoid scrolling when the 'O'
3114 // flag is present in 'shortmess'; But when not jumping, print the
3115 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003116 i = msg_scroll;
3117 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3118 msg_scroll = TRUE;
3119 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3120 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003121 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003122 msg_scroll = i;
3123}
3124
3125/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003126 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003127 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3128 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003129 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3130 * able to jump/open a window. Returns NOTDONE if a file is not associated
3131 * with the entry.
3132 */
3133 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003134qf_jump_open_window(
3135 qf_info_T *qi,
3136 qfline_T *qf_ptr,
3137 int newwin,
3138 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003139{
3140 // For ":helpgrep" find a help window or open one.
3141 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003142 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003143 return FAIL;
3144
3145 // If currently in the quickfix window, find another window to show the
3146 // file in.
3147 if (bt_quickfix(curbuf) && !*opened_window)
3148 {
3149 // If there is no file specified, we don't know where to go.
3150 // But do advance, otherwise ":cn" gets stuck.
3151 if (qf_ptr->qf_fnum == 0)
3152 return NOTDONE;
3153
Bram Moolenaarb2443732018-11-11 22:50:27 +01003154 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3155 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003156 return FAIL;
3157 }
3158
3159 return OK;
3160}
3161
3162/*
3163 * Edit a selected file from the quickfix/location list and jump to a
3164 * particular line/column, adjust the folds and display a message about the
3165 * jump.
3166 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3167 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3168 * the file.
3169 */
3170 static int
3171qf_jump_to_buffer(
3172 qf_info_T *qi,
3173 int qf_index,
3174 qfline_T *qf_ptr,
3175 int forceit,
3176 win_T *oldwin,
3177 int *opened_window,
3178 int openfold,
3179 int print_message)
3180{
3181 buf_T *old_curbuf;
3182 linenr_T old_lnum;
3183 int retval = OK;
3184
3185 // If there is a file name, read the wanted file if needed, and check
3186 // autowrite etc.
3187 old_curbuf = curbuf;
3188 old_lnum = curwin->w_cursor.lnum;
3189
3190 if (qf_ptr->qf_fnum != 0)
3191 {
3192 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3193 opened_window);
3194 if (retval != OK)
3195 return retval;
3196 }
3197
3198 // When not switched to another buffer, still need to set pc mark
3199 if (curbuf == old_curbuf)
3200 setpcmark();
3201
3202 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3203 qf_ptr->qf_pattern);
3204
3205#ifdef FEAT_FOLDING
3206 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3207 foldOpenCursor();
3208#endif
3209 if (print_message)
3210 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3211
3212 return retval;
3213}
3214
3215/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003216 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 */
3218 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003219qf_jump(qf_info_T *qi,
3220 int dir,
3221 int errornr,
3222 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003224 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3225}
3226
3227/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003228 * Jump to a quickfix line.
3229 * If dir == 0 go to entry "errornr".
3230 * If dir == FORWARD go "errornr" valid entries forward.
3231 * If dir == BACKWARD go "errornr" valid entries backward.
3232 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3233 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3234 * else if "errornr" is zero, redisplay the same line
3235 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003236 * If 'newwin' is TRUE, then open the file in a new window.
3237 */
3238 void
3239qf_jump_newwin(qf_info_T *qi,
3240 int dir,
3241 int errornr,
3242 int forceit,
3243 int newwin)
3244{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003245 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003246 qfline_T *qf_ptr;
3247 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003250 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003251 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003253 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003256 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003258 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003260 if (qi == NULL)
3261 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003262
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003263 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003265 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 return;
3267 }
3268
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003269 qfl = &qi->qf_lists[qi->qf_curlist];
3270
3271 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003273 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003275
3276 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3277 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003279 qf_ptr = old_qf_ptr;
3280 qf_index = old_qf_index;
3281 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003284 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003285 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003286 // No need to print the error message if it's visible in the error
3287 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 print_message = FALSE;
3289
Bram Moolenaarb2443732018-11-11 22:50:27 +01003290 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003291 if (retval == FAIL)
3292 goto failed;
3293 if (retval == NOTDONE)
3294 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003295
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003296 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, oldwin,
3297 &opened_window, old_KeyTyped, print_message);
3298 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003300 // Quickfix/location list is freed by an autocmd
3301 qi = NULL;
3302 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003305 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003308 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003309 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003311 // Couldn't open file, so put index back where it was. This could
3312 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 qf_ptr = old_qf_ptr;
3315 qf_index = old_qf_index;
3316 }
3317 }
3318theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003319 if (qi != NULL)
3320 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003321 qfl->qf_ptr = qf_ptr;
3322 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (p_swb != old_swb && opened_window)
3325 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003326 // Restore old 'switchbuf' value, but not when an autocommand or
3327 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003331 swb_flags = old_swb_flags;
3332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 else
3334 free_string_option(old_swb);
3335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336}
3337
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003338// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003339static int qfFileAttr;
3340static int qfSepAttr;
3341static int qfLineAttr;
3342
3343/*
3344 * Display information about a single entry from the quickfix/location list.
3345 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003346 * 'cursel' will be set to TRUE for the currently selected entry in the
3347 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003348 */
3349 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003350qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003351{
3352 char_u *fname;
3353 buf_T *buf;
3354 int filter_entry;
3355
3356 fname = NULL;
3357 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3358 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3359 (char *)qfp->qf_module);
3360 else {
3361 if (qfp->qf_fnum != 0
3362 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3363 {
3364 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003365 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003366 fname = gettail(fname);
3367 }
3368 if (fname == NULL)
3369 sprintf((char *)IObuff, "%2d", qf_idx);
3370 else
3371 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3372 qf_idx, (char *)fname);
3373 }
3374
3375 // Support for filtering entries using :filter /pat/ clist
3376 // Match against the module name, file name, search pattern and
3377 // text of the entry.
3378 filter_entry = TRUE;
3379 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3380 filter_entry &= message_filtered(qfp->qf_module);
3381 if (filter_entry && fname != NULL)
3382 filter_entry &= message_filtered(fname);
3383 if (filter_entry && qfp->qf_pattern != NULL)
3384 filter_entry &= message_filtered(qfp->qf_pattern);
3385 if (filter_entry)
3386 filter_entry &= message_filtered(qfp->qf_text);
3387 if (filter_entry)
3388 return;
3389
3390 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003391 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003392
3393 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003394 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003395 if (qfp->qf_lnum == 0)
3396 IObuff[0] = NUL;
3397 else if (qfp->qf_col == 0)
3398 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3399 else
3400 sprintf((char *)IObuff, "%ld col %d",
3401 qfp->qf_lnum, qfp->qf_col);
3402 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3403 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003404 msg_puts_attr((char *)IObuff, qfLineAttr);
3405 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003406 if (qfp->qf_pattern != NULL)
3407 {
3408 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003409 msg_puts((char *)IObuff);
3410 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003411 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003412 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003413
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003414 // Remove newlines and leading whitespace from the text. For an
3415 // unrecognized line keep the indent, the compiler may mark a word
3416 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003417 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3418 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3419 IObuff, IOSIZE);
3420 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003421 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003422}
3423
3424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003426 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 */
3428 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003429qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003431 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003432 qfline_T *qfp;
3433 int i;
3434 int idx1 = 1;
3435 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003436 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003437 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003438 int all = eap->forceit; // if not :cl!, only show
3439 // recognised errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003440 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
Bram Moolenaar39665952018-08-15 20:59:48 +02003442 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003443 {
3444 qi = GET_LOC_LIST(curwin);
3445 if (qi == NULL)
3446 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003447 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003448 return;
3449 }
3450 }
3451
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003452 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003454 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 return;
3456 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003457 if (*arg == '+')
3458 {
3459 ++arg;
3460 plus = TRUE;
3461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3463 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003464 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 return;
3466 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003467 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003468 if (plus)
3469 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003470 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003471 idx2 = i + idx1;
3472 idx1 = i;
3473 }
3474 else
3475 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003476 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003477 if (idx1 < 0)
3478 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3479 if (idx2 < 0)
3480 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003483 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003484 shorten_fnames(FALSE);
3485
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003486 // Get the attributes for the different quickfix highlight items. Note
3487 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003488 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3489 if (qfFileAttr == 0)
3490 qfFileAttr = HL_ATTR(HLF_D);
3491 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3492 if (qfSepAttr == 0)
3493 qfSepAttr = HL_ATTR(HLF_D);
3494 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3495 if (qfLineAttr == 0)
3496 qfLineAttr = HL_ATTR(HLF_N);
3497
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003498 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003500 qfp = qfl->qf_start;
3501 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
3503 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3504 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003505 if (got_int)
3506 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003507
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003508 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003510
3511 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003512 if (qfp == NULL)
3513 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003514 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 ui_breakcheck();
3516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517}
3518
3519/*
3520 * Remove newlines and leading whitespace from an error message.
3521 * Put the result in "buf[bufsize]".
3522 */
3523 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003524qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525{
3526 int i;
3527 char_u *p = text;
3528
3529 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3530 {
3531 if (*p == '\n')
3532 {
3533 buf[i] = ' ';
3534 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003535 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 break;
3537 }
3538 else
3539 buf[i] = *p++;
3540 }
3541 buf[i] = NUL;
3542}
3543
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003544/*
3545 * Display information (list number, list size and the title) about a
3546 * quickfix/location list.
3547 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003548 static void
3549qf_msg(qf_info_T *qi, int which, char *lead)
3550{
3551 char *title = (char *)qi->qf_lists[which].qf_title;
3552 int count = qi->qf_lists[which].qf_count;
3553 char_u buf[IOSIZE];
3554
3555 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3556 lead,
3557 which + 1,
3558 qi->qf_listcount,
3559 count);
3560
3561 if (title != NULL)
3562 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003563 size_t len = STRLEN(buf);
3564
3565 if (len < 34)
3566 {
3567 vim_memset(buf + len, ' ', 34 - len);
3568 buf[34] = NUL;
3569 }
3570 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003571 }
3572 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003573 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003574}
3575
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576/*
3577 * ":colder [count]": Up in the quickfix stack.
3578 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003579 * ":lolder [count]": Up in the location list stack.
3580 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 */
3582 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003583qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003585 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 int count;
3587
Bram Moolenaar39665952018-08-15 20:59:48 +02003588 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003589 {
3590 qi = GET_LOC_LIST(curwin);
3591 if (qi == NULL)
3592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003593 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003594 return;
3595 }
3596 }
3597
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 if (eap->addr_count != 0)
3599 count = eap->line2;
3600 else
3601 count = 1;
3602 while (count--)
3603 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003604 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003606 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003608 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003609 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003611 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613 else
3614 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003615 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003617 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003618 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003620 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003623 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003624 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625}
3626
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003627/*
3628 * Display the information about all the quickfix/location lists in the stack
3629 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003630 void
3631qf_history(exarg_T *eap)
3632{
3633 qf_info_T *qi = &ql_info;
3634 int i;
3635
Bram Moolenaar39665952018-08-15 20:59:48 +02003636 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003637 qi = GET_LOC_LIST(curwin);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003638 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003639 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003640 else
3641 for (i = 0; i < qi->qf_listcount; ++i)
3642 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3643}
3644
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003646 * Free all the entries in the error list "idx". Note that other information
3647 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 */
3649 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003650qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003652 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003653 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003654 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003656 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003658 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003659 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003660 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003661 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003662 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003663 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003664 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003665 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003666 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003667 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003668 // Somehow qf_count may have an incorrect value, set it to 1
3669 // to avoid crashing when it's wrong.
3670 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003671 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003672 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003673 qfl->qf_start = qfpnext;
3674 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003676
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003677 qfl->qf_index = 0;
3678 qfl->qf_start = NULL;
3679 qfl->qf_last = NULL;
3680 qfl->qf_ptr = NULL;
3681 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003682
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003683 qf_clean_dir_stack(&qfl->qf_dir_stack);
3684 qfl->qf_directory = NULL;
3685 qf_clean_dir_stack(&qfl->qf_file_stack);
3686 qfl->qf_currfile = NULL;
3687 qfl->qf_multiline = FALSE;
3688 qfl->qf_multiignore = FALSE;
3689 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690}
3691
3692/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003693 * Free error list "idx". Frees all the entries in the quickfix list,
3694 * associated context information and the title.
3695 */
3696 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003697qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003698{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003699 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003700
Bram Moolenaard23a8232018-02-10 18:45:26 +01003701 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003702 free_tv(qfl->qf_ctx);
3703 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003704 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003705 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003706}
3707
3708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 * qf_mark_adjust: adjust marks
3710 */
3711 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003712qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003713 win_T *wp,
3714 linenr_T line1,
3715 linenr_T line2,
3716 long amount,
3717 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003719 int i;
3720 qfline_T *qfp;
3721 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003722 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003723 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003724 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725
Bram Moolenaarc1542742016-07-20 21:44:37 +02003726 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003727 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003728 if (wp != NULL)
3729 {
3730 if (wp->w_llist == NULL)
3731 return;
3732 qi = wp->w_llist;
3733 }
3734
3735 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003736 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003737 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003738 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3739 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (qfp->qf_fnum == curbuf->b_fnum)
3741 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003742 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3744 {
3745 if (amount == MAXLNUM)
3746 qfp->qf_cleared = TRUE;
3747 else
3748 qfp->qf_lnum += amount;
3749 }
3750 else if (amount_after && qfp->qf_lnum > line2)
3751 qfp->qf_lnum += amount_after;
3752 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003753
3754 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003755 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756}
3757
3758/*
3759 * Make a nice message out of the error character and the error number:
3760 * char number message
3761 * e or E 0 " error"
3762 * w or W 0 " warning"
3763 * i or I 0 " info"
3764 * 0 0 ""
3765 * other 0 " c"
3766 * e or E n " error n"
3767 * w or W n " warning n"
3768 * i or I n " info n"
3769 * 0 n " error n"
3770 * other n " c n"
3771 * 1 x "" :helpgrep
3772 */
3773 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003774qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775{
3776 static char_u buf[20];
3777 static char_u cc[3];
3778 char_u *p;
3779
3780 if (c == 'W' || c == 'w')
3781 p = (char_u *)" warning";
3782 else if (c == 'I' || c == 'i')
3783 p = (char_u *)" info";
3784 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3785 p = (char_u *)" error";
3786 else if (c == 0 || c == 1)
3787 p = (char_u *)"";
3788 else
3789 {
3790 cc[0] = ' ';
3791 cc[1] = c;
3792 cc[2] = NUL;
3793 p = cc;
3794 }
3795
3796 if (nr <= 0)
3797 return p;
3798
3799 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3800 return buf;
3801}
3802
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003804 * When "split" is FALSE: Open the entry/result under the cursor.
3805 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3806 */
3807 void
3808qf_view_result(int split)
3809{
3810 qf_info_T *qi = &ql_info;
3811
3812 if (!bt_quickfix(curbuf))
3813 return;
3814
3815 if (IS_LL_WINDOW(curwin))
3816 qi = GET_LOC_LIST(curwin);
3817
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003818 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003819 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003820 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003821 return;
3822 }
3823
3824 if (split)
3825 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003826 // Open the selected entry in a new window
3827 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3828 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003829 return;
3830 }
3831
3832 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3833}
3834
3835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 * ":cwindow": open the quickfix window if we have errors to display,
3837 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003838 * ":lwindow": open the location list window if we have locations to display,
3839 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 */
3841 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003842ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003844 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003845 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 win_T *win;
3847
Bram Moolenaar39665952018-08-15 20:59:48 +02003848 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003849 {
3850 qi = GET_LOC_LIST(curwin);
3851 if (qi == NULL)
3852 return;
3853 }
3854
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003855 qfl = &qi->qf_lists[qi->qf_curlist];
3856
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003857 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003858 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003860 // If a quickfix window is open but we have no errors to display,
3861 // close the window. If a quickfix window is not open, then open
3862 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003863 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003864 || qfl->qf_nonevalid
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003865 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
3867 if (win != NULL)
3868 ex_cclose(eap);
3869 }
3870 else if (win == NULL)
3871 ex_copen(eap);
3872}
3873
3874/*
3875 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003876 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003879ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003881 win_T *win = NULL;
3882 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
Bram Moolenaar39665952018-08-15 20:59:48 +02003884 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003885 {
3886 qi = GET_LOC_LIST(curwin);
3887 if (qi == NULL)
3888 return;
3889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003891 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003892 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (win != NULL)
3894 win_close(win, FALSE);
3895}
3896
3897/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003898 * Set "w:quickfix_title" if "qi" has a title.
3899 */
3900 static void
3901qf_set_title_var(qf_list_T *qfl)
3902{
3903 if (qfl->qf_title != NULL)
3904 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3905}
3906
3907/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003908 * Goto a quickfix or location list window (if present).
3909 * Returns OK if the window is found, FAIL otherwise.
3910 */
3911 static int
3912qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3913{
3914 win_T *win;
3915
3916 win = qf_find_win(qi);
3917 if (win == NULL)
3918 return FAIL;
3919
3920 win_goto(win);
3921 if (resize)
3922 {
3923 if (vertsplit)
3924 {
3925 if (sz != win->w_width)
3926 win_setwidth(sz);
3927 }
3928 else if (sz != win->w_height)
3929 win_setheight(sz);
3930 }
3931
3932 return OK;
3933}
3934
3935/*
3936 * Open a new quickfix or location list window, load the quickfix buffer and
3937 * set the appropriate options for the window.
3938 * Returns FAIL if the window could not be opened.
3939 */
3940 static int
3941qf_open_new_cwindow(qf_info_T *qi, int height)
3942{
3943 buf_T *qf_buf;
3944 win_T *oldwin = curwin;
3945 tabpage_T *prevtab = curtab;
3946 int flags = 0;
3947 win_T *win;
3948
3949 qf_buf = qf_find_buf(qi);
3950
3951 // The current window becomes the previous window afterwards.
3952 win = curwin;
3953
3954 if (IS_QF_STACK(qi) && cmdmod.split == 0)
3955 // Create the new quickfix window at the very bottom, except when
3956 // :belowright or :aboveleft is used.
3957 win_goto(lastwin);
3958 // Default is to open the window below the current window
3959 if (cmdmod.split == 0)
3960 flags = WSP_BELOW;
3961 flags |= WSP_NEWLOC;
3962 if (win_split(height, flags) == FAIL)
3963 return FAIL; // not enough room for window
3964 RESET_BINDING(curwin);
3965
3966 if (IS_LL_STACK(qi))
3967 {
3968 // For the location list window, create a reference to the
3969 // location list from the window 'win'.
3970 curwin->w_llist_ref = win->w_llist;
3971 win->w_llist->qf_refcount++;
3972 }
3973
3974 if (oldwin != curwin)
3975 oldwin = NULL; // don't store info when in another window
3976 if (qf_buf != NULL)
3977 {
3978 // Use the existing quickfix buffer
3979 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
3980 ECMD_HIDE + ECMD_OLDBUF, oldwin);
3981 }
3982 else
3983 {
3984 // Create a new quickfix buffer
3985 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
3986
3987 // switch off 'swapfile'
3988 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
3989 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
3990 OPT_LOCAL);
3991 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
3992 RESET_BINDING(curwin);
3993#ifdef FEAT_DIFF
3994 curwin->w_p_diff = FALSE;
3995#endif
3996#ifdef FEAT_FOLDING
3997 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
3998 OPT_LOCAL);
3999#endif
4000 }
4001
4002 // Only set the height when still in the same tab page and there is no
4003 // window to the side.
4004 if (curtab == prevtab && curwin->w_width == Columns)
4005 win_setheight(height);
4006 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4007 if (win_valid(win))
4008 prevwin = win;
4009
4010 return OK;
4011}
4012
4013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004015 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 */
4017 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004018ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004020 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004021 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004023 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004024 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025
Bram Moolenaar39665952018-08-15 20:59:48 +02004026 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004027 {
4028 qi = GET_LOC_LIST(curwin);
4029 if (qi == NULL)
4030 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004031 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004032 return;
4033 }
4034 }
4035
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004036 incr_quickfix_busy();
4037
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 if (eap->addr_count != 0)
4039 height = eap->line2;
4040 else
4041 height = QF_WINHEIGHT;
4042
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004043 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044#ifdef FEAT_GUI
4045 need_mouse_correct = TRUE;
4046#endif
4047
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004048 // Find an existing quickfix window, or open a new one.
4049 if (cmdmod.tab == 0)
4050 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4051 cmdmod.split & WSP_VERT);
4052 if (status == FAIL)
4053 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004054 {
4055 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004056 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004059 qfl = &qi->qf_lists[qi->qf_curlist];
4060 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004061 // Save the current index here, as updating the quickfix buffer may free
4062 // the quickfix list
4063 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004064
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004065 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004066 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004068 decr_quickfix_busy();
4069
4070 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 curwin->w_cursor.col = 0;
4072 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004073 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074}
4075
4076/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004077 * Move the cursor in the quickfix window to "lnum".
4078 */
4079 static void
4080qf_win_goto(win_T *win, linenr_T lnum)
4081{
4082 win_T *old_curwin = curwin;
4083
4084 curwin = win;
4085 curbuf = win->w_buffer;
4086 curwin->w_cursor.lnum = lnum;
4087 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004088 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004089 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004090 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004091 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004092 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004093 curwin = old_curwin;
4094 curbuf = curwin->w_buffer;
4095}
4096
4097/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004098 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004099 */
4100 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004101ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004102{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004103 qf_info_T *qi = &ql_info;
4104 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004105
Bram Moolenaar39665952018-08-15 20:59:48 +02004106 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004107 {
4108 qi = GET_LOC_LIST(curwin);
4109 if (qi == NULL)
4110 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004111 emsg(_(e_loclist));
Bram Moolenaar537ef082016-07-09 17:56:19 +02004112 return;
4113 }
4114 }
4115
4116 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004117 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4118 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4119}
4120
4121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 * Return the number of the current entry (line number in the quickfix
4123 * window).
4124 */
4125 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004126qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004128 qf_info_T *qi = &ql_info;
4129
4130 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004131 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004132 qi = wp->w_llist_ref;
4133
4134 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135}
4136
4137/*
4138 * Update the cursor position in the quickfix window to the current error.
4139 * Return TRUE if there is a quickfix window.
4140 */
4141 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004142qf_win_pos_update(
4143 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004144 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145{
4146 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004147 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004149 // Put the cursor on the current error in the quickfix window, so that
4150 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004151 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 if (win != NULL
4153 && qf_index <= win->w_buffer->b_ml.ml_line_count
4154 && old_qf_index != qf_index)
4155 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 if (qf_index > old_qf_index)
4157 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004158 win->w_redraw_top = old_qf_index;
4159 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 }
4161 else
4162 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004163 win->w_redraw_top = qf_index;
4164 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004166 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 }
4168 return win != NULL;
4169}
4170
4171/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004172 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004173 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004174 */
4175 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004176is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004177{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004178 // A window displaying the quickfix buffer will have the w_llist_ref field
4179 // set to NULL.
4180 // A window displaying a location list buffer will have the w_llist_ref
4181 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004182 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004183 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4184 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004185 return TRUE;
4186
4187 return FALSE;
4188}
4189
4190/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004191 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004192 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004193 */
4194 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004195qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004196{
4197 win_T *win;
4198
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004199 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004200 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004201 return win;
4202 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004203}
4204
4205/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004206 * Find a quickfix buffer.
4207 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 */
4209 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004210qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004212 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004213 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
Bram Moolenaar9c102382006-05-03 21:26:49 +00004215 FOR_ALL_TAB_WINDOWS(tp, win)
4216 if (is_qf_win(win, qi))
4217 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004218
Bram Moolenaar9c102382006-05-03 21:26:49 +00004219 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220}
4221
4222/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004223 * Update the w:quickfix_title variable in the quickfix/location list window
4224 */
4225 static void
4226qf_update_win_titlevar(qf_info_T *qi)
4227{
4228 win_T *win;
4229 win_T *curwin_save;
4230
4231 if ((win = qf_find_win(qi)) != NULL)
4232 {
4233 curwin_save = curwin;
4234 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004235 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004236 curwin = curwin_save;
4237 }
4238}
4239
4240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 * Find the quickfix buffer. If it exists, update the contents.
4242 */
4243 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004244qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
4246 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004247 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004250 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004251 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 if (buf != NULL)
4253 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004254 linenr_T old_line_count = buf->b_ml.ml_line_count;
4255
4256 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004257 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004258 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
Bram Moolenaard823fa92016-08-12 16:29:27 +02004260 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004261
Bram Moolenaar864293a2016-06-02 13:40:04 +02004262 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004263 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004264
Bram Moolenaar864293a2016-06-02 13:40:04 +02004265 if (old_last == NULL)
4266 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004267 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004268
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004269 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004270 aucmd_restbuf(&aco);
4271 }
4272
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004273 // Only redraw when added lines are visible. This avoids flickering
4274 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004275 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4276 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278}
4279
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004280/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004281 * Add an error line to the quickfix buffer.
4282 */
4283 static int
4284qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4285{
4286 int len;
4287 buf_T *errbuf;
4288
4289 if (qfp->qf_module != NULL)
4290 {
4291 STRCPY(IObuff, qfp->qf_module);
4292 len = (int)STRLEN(IObuff);
4293 }
4294 else if (qfp->qf_fnum != 0
4295 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4296 && errbuf->b_fname != NULL)
4297 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004298 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004299 STRCPY(IObuff, gettail(errbuf->b_fname));
4300 else
4301 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004302 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004303 if (errbuf->b_sfname == NULL
4304 || mch_isFullName(errbuf->b_sfname))
4305 {
4306 if (*dirname == NUL)
4307 mch_dirname(dirname, MAXPATHL);
4308 shorten_buf_fname(errbuf, dirname, FALSE);
4309 }
4310 STRCPY(IObuff, errbuf->b_fname);
4311 }
4312 len = (int)STRLEN(IObuff);
4313 }
4314 else
4315 len = 0;
4316 IObuff[len++] = '|';
4317
4318 if (qfp->qf_lnum > 0)
4319 {
4320 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4321 len += (int)STRLEN(IObuff + len);
4322
4323 if (qfp->qf_col > 0)
4324 {
4325 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4326 len += (int)STRLEN(IObuff + len);
4327 }
4328
4329 sprintf((char *)IObuff + len, "%s",
4330 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4331 len += (int)STRLEN(IObuff + len);
4332 }
4333 else if (qfp->qf_pattern != NULL)
4334 {
4335 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4336 len += (int)STRLEN(IObuff + len);
4337 }
4338 IObuff[len++] = '|';
4339 IObuff[len++] = ' ';
4340
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004341 // Remove newlines and leading whitespace from the text.
4342 // For an unrecognized line keep the indent, the compiler may
4343 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004344 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4345 IObuff + len, IOSIZE - len);
4346
4347 if (ml_append_buf(buf, lnum, IObuff,
4348 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4349 return FAIL;
4350
4351 return OK;
4352}
4353
4354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 * Fill current buffer with quickfix errors, replacing any previous contents.
4356 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004357 * If "old_last" is not NULL append the items after this one.
4358 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4359 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 */
4361 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004362qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004364 linenr_T lnum;
4365 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004366 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
Bram Moolenaar864293a2016-06-02 13:40:04 +02004368 if (old_last == NULL)
4369 {
4370 if (buf != curbuf)
4371 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004372 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004373 return;
4374 }
4375
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004376 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004377 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4378 (void)ml_delete((linenr_T)1, FALSE);
4379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004381 // Check if there is anything to display
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004382 if (!qf_stack_empty(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004384 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4385 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004386
4387 *dirname = NUL;
4388
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004389 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004390 if (old_last == NULL)
4391 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004392 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004393 lnum = 0;
4394 }
4395 else
4396 {
4397 qfp = old_last->qf_next;
4398 lnum = buf->b_ml.ml_line_count;
4399 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004400 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004402 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004404
Bram Moolenaar864293a2016-06-02 13:40:04 +02004405 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004407 if (qfp == NULL)
4408 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004410
4411 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004412 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004413 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004416 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 check_lnums(TRUE);
4418
Bram Moolenaar864293a2016-06-02 13:40:04 +02004419 if (old_last == NULL)
4420 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004421 // Set the 'filetype' to "qf" each time after filling the buffer.
4422 // This resembles reading a file into a buffer, it's more logical when
4423 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004424 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004425 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4426 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004428 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004429 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004431 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004433 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004434 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004435
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004436 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004437 redraw_curbuf_later(NOT_VALID);
4438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004440 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 KeyTyped = old_KeyTyped;
4442}
4443
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004444/*
4445 * For every change made to the quickfix list, update the changed tick.
4446 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004447 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004448qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004449{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004450 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004451}
4452
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004454 * Return the quickfix/location list number with the given identifier.
4455 * Returns -1 if list is not found.
4456 */
4457 static int
4458qf_id2nr(qf_info_T *qi, int_u qfid)
4459{
4460 int qf_idx;
4461
4462 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4463 if (qi->qf_lists[qf_idx].qf_id == qfid)
4464 return qf_idx;
4465 return INVALID_QFIDX;
4466}
4467
4468/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004469 * If the current list is not "save_qfid" and we can find the list with that ID
4470 * then make it the current list.
4471 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004472 * Returns OK if successfully restored the list. Returns FAIL if the list with
4473 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004474 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004475 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004476qf_restore_list(qf_info_T *qi, int_u save_qfid)
4477{
4478 int curlist;
4479
4480 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4481 {
4482 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004483 if (curlist < 0)
4484 // list is not present
4485 return FAIL;
4486 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004487 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004488 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004489}
4490
4491/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004492 * Jump to the first entry if there is one.
4493 */
4494 static void
4495qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4496{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004497 if (qf_restore_list(qi, save_qfid) == FAIL)
4498 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004499
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004500 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004501 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004502 qf_jump(qi, 0, 0, forceit);
4503}
4504
4505/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004506 * Return TRUE when using ":vimgrep" for ":grep".
4507 */
4508 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004509grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004510{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004511 return ((cmdidx == CMD_grep
4512 || cmdidx == CMD_lgrep
4513 || cmdidx == CMD_grepadd
4514 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004515 && STRCMP("internal",
4516 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4517}
4518
4519/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004520 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004522 static char_u *
4523make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004525 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004526 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004527 case CMD_make: return (char_u *)"make";
4528 case CMD_lmake: return (char_u *)"lmake";
4529 case CMD_grep: return (char_u *)"grep";
4530 case CMD_lgrep: return (char_u *)"lgrep";
4531 case CMD_grepadd: return (char_u *)"grepadd";
4532 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4533 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535}
4536
4537/*
4538 * Return the name for the errorfile, in allocated memory.
4539 * Find a new unique name when 'makeef' contains "##".
4540 * Returns NULL for error.
4541 */
4542 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004543get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544{
4545 char_u *p;
4546 char_u *name;
4547 static int start = -1;
4548 static int off = 0;
4549#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004550 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551#endif
4552
4553 if (*p_mef == NUL)
4554 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004555 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004557 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 return name;
4559 }
4560
4561 for (p = p_mef; *p; ++p)
4562 if (p[0] == '#' && p[1] == '#')
4563 break;
4564
4565 if (*p == NUL)
4566 return vim_strsave(p_mef);
4567
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004568 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 for (;;)
4570 {
4571 if (start == -1)
4572 start = mch_get_pid();
4573 else
4574 off += 19;
4575
4576 name = alloc((unsigned)STRLEN(p_mef) + 30);
4577 if (name == NULL)
4578 break;
4579 STRCPY(name, p_mef);
4580 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4581 STRCAT(name, p + 2);
4582 if (mch_getperm(name) < 0
4583#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004584 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 && mch_lstat((char *)name, &sb) < 0
4586#endif
4587 )
4588 break;
4589 vim_free(name);
4590 }
4591 return name;
4592}
4593
4594/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004595 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4596 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4597 */
4598 static char_u *
4599make_get_fullcmd(char_u *makecmd, char_u *fname)
4600{
4601 char_u *cmd;
4602 unsigned len;
4603
4604 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4605 if (*p_sp != NUL)
4606 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4607 cmd = alloc(len);
4608 if (cmd == NULL)
4609 return NULL;
4610 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4611 (char *)p_shq);
4612
4613 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4614 if (*p_sp != NUL)
4615 append_redir(cmd, len, p_sp, fname);
4616
4617 // Display the fully formed command. Output a newline if there's something
4618 // else than the :make command that was typed (in which case the cursor is
4619 // in column 0).
4620 if (msg_col == 0)
4621 msg_didout = FALSE;
4622 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004623 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004624 msg_outtrans(cmd); // show what we are doing
4625
4626 return cmd;
4627}
4628
4629/*
4630 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4631 */
4632 void
4633ex_make(exarg_T *eap)
4634{
4635 char_u *fname;
4636 char_u *cmd;
4637 char_u *enc = NULL;
4638 win_T *wp = NULL;
4639 qf_info_T *qi = &ql_info;
4640 int res;
4641 char_u *au_name = NULL;
4642 int_u save_qfid;
4643
4644 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4645 if (grep_internal(eap->cmdidx))
4646 {
4647 ex_vimgrep(eap);
4648 return;
4649 }
4650
4651 au_name = make_get_auname(eap->cmdidx);
4652 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4653 curbuf->b_fname, TRUE, curbuf))
4654 {
4655#ifdef FEAT_EVAL
4656 if (aborting())
4657 return;
4658#endif
4659 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004660 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004661
4662 if (is_loclist_cmd(eap->cmdidx))
4663 wp = curwin;
4664
4665 autowrite_all();
4666 fname = get_mef_name();
4667 if (fname == NULL)
4668 return;
4669 mch_remove(fname); // in case it's not unique
4670
4671 cmd = make_get_fullcmd(eap->arg, fname);
4672 if (cmd == NULL)
4673 return;
4674
4675 // let the shell know if we are redirecting output or not
4676 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4677
4678#ifdef AMIGA
4679 out_flush();
4680 // read window status report and redraw before message
4681 (void)char_avail();
4682#endif
4683
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004684 incr_quickfix_busy();
4685
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004686 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4687 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4688 (eap->cmdidx != CMD_grepadd
4689 && eap->cmdidx != CMD_lgrepadd),
4690 qf_cmdtitle(*eap->cmdlinep), enc);
4691 if (wp != NULL)
4692 {
4693 qi = GET_LOC_LIST(wp);
4694 if (qi == NULL)
4695 goto cleanup;
4696 }
4697 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004698 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004699
4700 // Remember the current quickfix list identifier, so that we can
4701 // check for autocommands changing the current quickfix list.
4702 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4703 if (au_name != NULL)
4704 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4705 curbuf->b_fname, TRUE, curbuf);
4706 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4707 // display the first error
4708 qf_jump_first(qi, save_qfid, FALSE);
4709
4710cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004711 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004712 mch_remove(fname);
4713 vim_free(fname);
4714 vim_free(cmd);
4715}
4716
4717/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004718 * Returns the number of valid entries in the current quickfix/location list.
4719 */
4720 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004721qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004722{
4723 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004724 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004725 qfline_T *qfp;
4726 int i, sz = 0;
4727 int prev_fnum = 0;
4728
Bram Moolenaar39665952018-08-15 20:59:48 +02004729 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004730 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004731 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004732 qi = GET_LOC_LIST(curwin);
4733 if (qi == NULL)
4734 return 0;
4735 }
4736
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004737 qfl = &qi->qf_lists[qi->qf_curlist];
4738 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004739 ++i, qfp = qfp->qf_next)
4740 {
4741 if (qfp->qf_valid)
4742 {
4743 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004744 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004745 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4746 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004747 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004748 sz++;
4749 prev_fnum = qfp->qf_fnum;
4750 }
4751 }
4752 }
4753
4754 return sz;
4755}
4756
4757/*
4758 * Returns the current index of the quickfix/location list.
4759 * Returns 0 if there is an error.
4760 */
4761 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004762qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004763{
4764 qf_info_T *qi = &ql_info;
4765
Bram Moolenaar39665952018-08-15 20:59:48 +02004766 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004767 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004768 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004769 qi = GET_LOC_LIST(curwin);
4770 if (qi == NULL)
4771 return 0;
4772 }
4773
4774 return qi->qf_lists[qi->qf_curlist].qf_index;
4775}
4776
4777/*
4778 * Returns the current index in the quickfix/location list (counting only valid
4779 * entries). If no valid entries are in the list, then returns 1.
4780 */
4781 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004782qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004783{
4784 qf_info_T *qi = &ql_info;
4785 qf_list_T *qfl;
4786 qfline_T *qfp;
4787 int i, eidx = 0;
4788 int prev_fnum = 0;
4789
Bram Moolenaar39665952018-08-15 20:59:48 +02004790 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004791 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004792 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004793 qi = GET_LOC_LIST(curwin);
4794 if (qi == NULL)
4795 return 1;
4796 }
4797
4798 qfl = &qi->qf_lists[qi->qf_curlist];
4799 qfp = qfl->qf_start;
4800
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004801 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004802 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4803 return 1;
4804
4805 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4806 {
4807 if (qfp->qf_valid)
4808 {
4809 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4810 {
4811 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4812 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004813 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004814 eidx++;
4815 prev_fnum = qfp->qf_fnum;
4816 }
4817 }
4818 else
4819 eidx++;
4820 }
4821 }
4822
4823 return eidx ? eidx : 1;
4824}
4825
4826/*
4827 * Get the 'n'th valid error entry in the quickfix or location list.
4828 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4829 * For :cdo and :ldo returns the 'n'th valid error entry.
4830 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4831 */
4832 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004833qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004834{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004835 qfline_T *qfp = qfl->qf_start;
4836 int i, eidx;
4837 int prev_fnum = 0;
4838
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004839 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004840 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4841 return 1;
4842
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004843 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004844 i++, qfp = qfp->qf_next)
4845 {
4846 if (qfp->qf_valid)
4847 {
4848 if (fdo)
4849 {
4850 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4851 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004852 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004853 eidx++;
4854 prev_fnum = qfp->qf_fnum;
4855 }
4856 }
4857 else
4858 eidx++;
4859 }
4860
4861 if (eidx == n)
4862 break;
4863 }
4864
4865 if (i <= qfl->qf_count)
4866 return i;
4867 else
4868 return 1;
4869}
4870
4871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004873 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004874 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 */
4876 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004877ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004879 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004880 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004881
Bram Moolenaar39665952018-08-15 20:59:48 +02004882 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004883 {
4884 qi = GET_LOC_LIST(curwin);
4885 if (qi == NULL)
4886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004887 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004888 return;
4889 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004890 }
4891
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004892 if (eap->addr_count > 0)
4893 errornr = (int)eap->line2;
4894 else
4895 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004896 switch (eap->cmdidx)
4897 {
4898 case CMD_cc: case CMD_ll:
4899 errornr = 0;
4900 break;
4901 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4902 case CMD_lfirst:
4903 errornr = 1;
4904 break;
4905 default:
4906 errornr = 32767;
4907 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004908 }
4909
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004910 // For cdo and ldo commands, jump to the nth valid error.
4911 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02004912 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4913 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004914 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004915 eap->addr_count > 0 ? (int)eap->line1 : 1,
4916 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4917
4918 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919}
4920
4921/*
4922 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004923 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004924 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 */
4926 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004927ex_cnext(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 Moolenaar39665952018-08-15 20:59:48 +02004931 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004932
Bram Moolenaar39665952018-08-15 20:59:48 +02004933 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004934 {
4935 qi = GET_LOC_LIST(curwin);
4936 if (qi == NULL)
4937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004938 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004939 return;
4940 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004941 }
4942
Bram Moolenaar55b69262017-08-13 13:42:01 +02004943 if (eap->addr_count > 0
4944 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4945 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004946 errornr = (int)eap->line2;
4947 else
4948 errornr = 1;
4949
Bram Moolenaar39665952018-08-15 20:59:48 +02004950 // Depending on the command jump to either next or previous entry/file.
4951 switch (eap->cmdidx)
4952 {
4953 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4954 dir = FORWARD;
4955 break;
4956 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4957 case CMD_lNext:
4958 dir = BACKWARD;
4959 break;
4960 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4961 dir = FORWARD_FILE;
4962 break;
4963 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4964 dir = BACKWARD_FILE;
4965 break;
4966 default:
4967 dir = FORWARD;
4968 break;
4969 }
4970
4971 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972}
4973
4974/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004975 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004976 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 */
4978 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004979ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01004981 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004982 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004983 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004984 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004985 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01004986 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004987
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01004988 switch (eap->cmdidx)
4989 {
4990 case CMD_cfile: au_name = (char_u *)"cfile"; break;
4991 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
4992 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
4993 case CMD_lfile: au_name = (char_u *)"lfile"; break;
4994 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
4995 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
4996 default: break;
4997 }
4998 if (au_name != NULL)
4999 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005000 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005001#ifdef FEAT_BROWSE
5002 if (cmdmod.browse)
5003 {
5004 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005005 NULL, NULL,
5006 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005007 if (browse_file == NULL)
5008 return;
5009 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5010 vim_free(browse_file);
5011 }
5012 else
5013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005015 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005016
Bram Moolenaar39665952018-08-15 20:59:48 +02005017 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005018 wp = curwin;
5019
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005020 incr_quickfix_busy();
5021
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005022 // This function is used by the :cfile, :cgetfile and :caddfile
5023 // commands.
5024 // :cfile always creates a new quickfix list and jumps to the
5025 // first error.
5026 // :cgetfile creates a new quickfix list but doesn't jump to the
5027 // first error.
5028 // :caddfile adds to an existing quickfix list. If there is no
5029 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005030 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005031 && eap->cmdidx != CMD_laddfile),
5032 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005033 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005034 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005035 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005036 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005037 {
5038 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005039 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005040 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005041 }
5042 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005043 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005044 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005045 if (au_name != NULL)
5046 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005047
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005048 // Jump to the first error for a new list and if autocmds didn't
5049 // free the list.
5050 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5051 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005052 // display the first error
5053 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005054
5055 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005056}
5057
5058/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005059 * Return the vimgrep autocmd name.
5060 */
5061 static char_u *
5062vgr_get_auname(cmdidx_T cmdidx)
5063{
5064 switch (cmdidx)
5065 {
5066 case CMD_vimgrep: return (char_u *)"vimgrep";
5067 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5068 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5069 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5070 case CMD_grep: return (char_u *)"grep";
5071 case CMD_lgrep: return (char_u *)"lgrep";
5072 case CMD_grepadd: return (char_u *)"grepadd";
5073 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5074 default: return NULL;
5075 }
5076}
5077
5078/*
5079 * Initialize the regmatch used by vimgrep for pattern "s".
5080 */
5081 static void
5082vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5083{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005084 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005085 regmatch->regprog = NULL;
5086
5087 if (s == NULL || *s == NUL)
5088 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005089 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005090 if (last_search_pat() == NULL)
5091 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005092 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005093 return;
5094 }
5095 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5096 }
5097 else
5098 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5099
5100 regmatch->rmm_ic = p_ic;
5101 regmatch->rmm_maxcol = 0;
5102}
5103
5104/*
5105 * Display a file name when vimgrep is running.
5106 */
5107 static void
5108vgr_display_fname(char_u *fname)
5109{
5110 char_u *p;
5111
5112 msg_start();
5113 p = msg_strtrunc(fname, TRUE);
5114 if (p == NULL)
5115 msg_outtrans(fname);
5116 else
5117 {
5118 msg_outtrans(p);
5119 vim_free(p);
5120 }
5121 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005122 msg_didout = FALSE; // overwrite this message
5123 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005124 msg_col = 0;
5125 out_flush();
5126}
5127
5128/*
5129 * Load a dummy buffer to search for a pattern using vimgrep.
5130 */
5131 static buf_T *
5132vgr_load_dummy_buf(
5133 char_u *fname,
5134 char_u *dirname_start,
5135 char_u *dirname_now)
5136{
5137 int save_mls;
5138#if defined(FEAT_SYN_HL)
5139 char_u *save_ei = NULL;
5140#endif
5141 buf_T *buf;
5142
5143#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005144 // Don't do Filetype autocommands to avoid loading syntax and
5145 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005146 save_ei = au_event_disable(",Filetype");
5147#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005148 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005149 save_mls = p_mls;
5150 p_mls = 0;
5151
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005152 // Load file into a buffer, so that 'fileencoding' is detected,
5153 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005154 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5155
5156 p_mls = save_mls;
5157#if defined(FEAT_SYN_HL)
5158 au_event_restore(save_ei);
5159#endif
5160
5161 return buf;
5162}
5163
5164/*
5165 * Check whether a quickfix/location list valid. Autocmds may remove or change
5166 * a quickfix list when vimgrep is running. If the list is not found, create a
5167 * new list.
5168 */
5169 static int
5170vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005171 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005172 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005173 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005174 char_u *title)
5175{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005176 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005177 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005178 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005179 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005180 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005181 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005182 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005183 return FALSE;
5184 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005185 else
5186 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005187 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005188 qf_new_list(qi, title);
5189 return TRUE;
5190 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005191 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005192
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005193 if (qf_restore_list(qi, qfid) == FAIL)
5194 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005195
5196 return TRUE;
5197}
5198
5199/*
5200 * Search for a pattern in all the lines in a buffer and add the matching lines
5201 * to a quickfix list.
5202 */
5203 static int
5204vgr_match_buflines(
5205 qf_info_T *qi,
5206 char_u *fname,
5207 buf_T *buf,
5208 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005209 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005210 int duplicate_name,
5211 int flags)
5212{
5213 int found_match = FALSE;
5214 long lnum;
5215 colnr_T col;
5216
Bram Moolenaar1c299432018-10-28 14:36:09 +01005217 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005218 {
5219 col = 0;
5220 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5221 col, NULL, NULL) > 0)
5222 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005223 // Pass the buffer number so that it gets used even for a
5224 // dummy buffer, unless duplicate_name is set, then the
5225 // buffer will be wiped out below.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005226 if (qf_add_entry(qi,
5227 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005228 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005229 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005230 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005231 duplicate_name ? 0 : buf->b_fnum,
5232 ml_get_buf(buf,
5233 regmatch->startpos[0].lnum + lnum, FALSE),
5234 regmatch->startpos[0].lnum + lnum,
5235 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005236 FALSE, // vis_col
5237 NULL, // search pattern
5238 0, // nr
5239 0, // type
5240 TRUE // valid
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005241 ) == FAIL)
5242 {
5243 got_int = TRUE;
5244 break;
5245 }
5246 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005247 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005248 break;
5249 if ((flags & VGR_GLOBAL) == 0
5250 || regmatch->endpos[0].lnum > 0)
5251 break;
5252 col = regmatch->endpos[0].col
5253 + (col == regmatch->endpos[0].col);
5254 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5255 break;
5256 }
5257 line_breakcheck();
5258 if (got_int)
5259 break;
5260 }
5261
5262 return found_match;
5263}
5264
5265/*
5266 * Jump to the first match and update the directory.
5267 */
5268 static void
5269vgr_jump_to_match(
5270 qf_info_T *qi,
5271 int forceit,
5272 int *redraw_for_dummy,
5273 buf_T *first_match_buf,
5274 char_u *target_dir)
5275{
5276 buf_T *buf;
5277
5278 buf = curbuf;
5279 qf_jump(qi, 0, 0, forceit);
5280 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005281 // If we jumped to another buffer redrawing will already be
5282 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005283 *redraw_for_dummy = FALSE;
5284
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005285 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005286 if (curbuf == first_match_buf && target_dir != NULL)
5287 {
5288 exarg_T ea;
5289
5290 ea.arg = target_dir;
5291 ea.cmdidx = CMD_lcd;
5292 ex_cd(&ea);
5293 }
5294}
5295
5296/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005297 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005298 * ":vimgrepadd {pattern} file(s)"
5299 * ":lvimgrep {pattern} file(s)"
5300 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005301 */
5302 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005303ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005304{
Bram Moolenaar81695252004-12-29 20:58:21 +00005305 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005306 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005307 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005308 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005309 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005310 char_u *s;
5311 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005312 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005313 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005314 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005315 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005316 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005317 buf_T *buf;
5318 int duplicate_name = FALSE;
5319 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005320 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005321 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005322 buf_T *first_match_buf = NULL;
5323 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005324 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005325 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005326 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005327 char_u *dirname_start = NULL;
5328 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005329 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005330 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005331
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005332 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005333 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5334 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005335 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005336#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005337 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005338 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005339#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005340 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005341
Bram Moolenaar39665952018-08-15 20:59:48 +02005342 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005343 {
5344 qi = ll_get_or_alloc_list(curwin);
5345 if (qi == NULL)
5346 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005347 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005348 }
5349
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005350 if (eap->addr_count > 0)
5351 tomatch = eap->line2;
5352 else
5353 tomatch = MAXLNUM;
5354
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005355 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005356 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005357 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005358 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005359 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005361 emsg(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005362 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005363 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005364
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005365 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005366 if (regmatch.regprog == NULL)
5367 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005368
5369 p = skipwhite(p);
5370 if (*p == NUL)
5371 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005372 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005373 goto theend;
5374 }
5375
Bram Moolenaar55b69262017-08-13 13:42:01 +02005376 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005377 && eap->cmdidx != CMD_vimgrepadd
5378 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005379 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005380 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005381 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005382
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005383 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005384 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005385 goto theend;
5386 if (fcount == 0)
5387 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005388 emsg(_(e_nomatch));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005389 goto theend;
5390 }
5391
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005392 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5393 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005394 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005395 {
5396 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005397 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005398 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005399
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005400 // Remember the current directory, because a BufRead autocommand that does
5401 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005402 mch_dirname(dirname_start, MAXPATHL);
5403
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005404 incr_quickfix_busy();
5405
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005406 // Remember the current quickfix list identifier, so that we can check for
5407 // autocommands changing the current quickfix list.
Bram Moolenaar3c097222017-12-21 20:54:49 +01005408 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005409
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005410 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005411 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005412 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005413 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005414 if (time(NULL) > seconds)
5415 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005416 // Display the file name every second or so, show the user we are
5417 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005418 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005419 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005420 }
5421
Bram Moolenaar81695252004-12-29 20:58:21 +00005422 buf = buflist_findname_exp(fnames[fi]);
5423 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5424 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005425 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005426 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005427 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005428 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005429
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005430 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005431 }
5432 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005433 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005434 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005435
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005436 // Check whether the quickfix list is still valid. When loading a
5437 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005438 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005439 {
5440 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005441 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005442 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005443 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005444 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005445
Bram Moolenaar81695252004-12-29 20:58:21 +00005446 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005447 {
5448 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005449 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005450 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005451 else
5452 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005453 // Try for a match in all lines of the buffer.
5454 // For ":1vimgrep" look for first match only.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005455 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005456 &tomatch, duplicate_name, flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005457
Bram Moolenaar81695252004-12-29 20:58:21 +00005458 if (using_dummy)
5459 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005460 if (found_match && first_match_buf == NULL)
5461 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005462 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005463 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005464 // Never keep a dummy buffer if there is another buffer
5465 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005466 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005467 buf = NULL;
5468 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005469 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005470 || buf->b_p_bh[0] == 'u' // "unload"
5471 || buf->b_p_bh[0] == 'w' // "wipe"
5472 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005473 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005474 // When no match was found we don't need to remember the
5475 // buffer, wipe it out. If there was a match and it
5476 // wasn't the first one or we won't jump there: only
5477 // unload the buffer.
5478 // Ignore 'hidden' here, because it may lead to having too
5479 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005480 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005481 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005482 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005483 buf = NULL;
5484 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005485 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005486 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005487 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005488 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005489 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005490 buf = NULL;
5491 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005492 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005493
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005494 if (buf != NULL)
5495 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005496 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005497 buf->b_flags &= ~BF_DUMMY;
5498
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005499 // If the buffer is still loaded we need to use the
5500 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005501 if (buf == first_match_buf
5502 && target_dir == NULL
5503 && STRCMP(dirname_start, dirname_now) != 0)
5504 target_dir = vim_strsave(dirname_now);
5505
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005506 // The buffer is still loaded, the Filetype autocommands
5507 // need to be done now, in that buffer. And the modelines
5508 // need to be done (again). But not the window-local
5509 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005510 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005511#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005512 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5513 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005514#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005515 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005516 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005517 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005518 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005519 }
5520 }
5521
5522 FreeWild(fcount, fnames);
5523
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005524 qfl = &qi->qf_lists[qi->qf_curlist];
5525 qfl->qf_nonevalid = FALSE;
5526 qfl->qf_ptr = qfl->qf_start;
5527 qfl->qf_index = 1;
5528 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005529
Bram Moolenaar864293a2016-06-02 13:40:04 +02005530 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005531
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005532 if (au_name != NULL)
5533 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5534 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005535 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5536 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005537 if (!qflist_valid(wp, save_qfid)
5538 || qf_restore_list(qi, save_qfid) == FAIL)
5539 {
5540 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005541 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005542 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005543
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005544 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005545 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005546 {
5547 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005548 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5549 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005550 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005551 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005552 semsg(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005553
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005554 decr_quickfix_busy();
5555
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005556 // If we loaded a dummy buffer into the current window, the autocommands
5557 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005558 if (redraw_for_dummy)
5559 {
5560#ifdef FEAT_FOLDING
5561 foldUpdateAll(curwin);
5562#else
5563 redraw_later(NOT_VALID);
5564#endif
5565 }
5566
Bram Moolenaar86b68352004-12-27 21:59:20 +00005567theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005568 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005569 vim_free(dirname_now);
5570 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005571 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005572 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005573}
5574
5575/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005576 * Restore current working directory to "dirname_start" if they differ, taking
5577 * into account whether it is set locally or globally.
5578 */
5579 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005580restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005581{
5582 char_u *dirname_now = alloc(MAXPATHL);
5583
5584 if (NULL != dirname_now)
5585 {
5586 mch_dirname(dirname_now, MAXPATHL);
5587 if (STRCMP(dirname_start, dirname_now) != 0)
5588 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005589 // If the directory has changed, change it back by building up an
5590 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005591 exarg_T ea;
5592
5593 ea.arg = dirname_start;
5594 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5595 ex_cd(&ea);
5596 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005597 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005598 }
5599}
5600
5601/*
5602 * Load file "fname" into a dummy buffer and return the buffer pointer,
5603 * placing the directory resulting from the buffer load into the
5604 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5605 * prior to calling this function. Restores directory to "dirname_start" prior
5606 * to returning, if autocmds or the 'autochdir' option have changed it.
5607 *
5608 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5609 * or wipe_dummy_buffer() later!
5610 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005611 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005612 */
5613 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005614load_dummy_buffer(
5615 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005616 char_u *dirname_start, // in: old directory
5617 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005618{
5619 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005620 bufref_T newbufref;
5621 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005622 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005623 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005624 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005625
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005626 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005627 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5628 if (newbuf == NULL)
5629 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005630 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005631
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005632 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005633 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5634
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005635 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005636 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005637 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005638 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005639 ++newbuf->b_locked;
5640
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005641 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005642 aucmd_prepbuf(&aco, newbuf);
5643
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005644 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005645 (void)setfname(curbuf, fname, NULL, FALSE);
5646
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005647 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005648 check_need_swap(TRUE);
5649
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005650 // Remove the "dummy" flag, otherwise autocommands may not
5651 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005652 curbuf->b_flags &= ~BF_DUMMY;
5653
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005654 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005655 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005656 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005657 NULL, READ_NEW | READ_DUMMY);
5658 --newbuf->b_locked;
5659 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005660 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005661 && !(curbuf->b_flags & BF_NEW))
5662 {
5663 failed = FALSE;
5664 if (curbuf != newbuf)
5665 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005666 // Bloody autocommands changed the buffer! Can happen when
5667 // using netrw and editing a remote file. Use the current
5668 // buffer instead, delete the dummy one after restoring the
5669 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005670 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005671 newbuf = curbuf;
5672 }
5673 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005674
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005675 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005676 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005677 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5678 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005679
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005680 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5681 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005682 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005683 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005684
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005685 // When autocommands/'autochdir' option changed directory: go back.
5686 // Let the caller know what the resulting dir was first, in case it is
5687 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005688 mch_dirname(resulting_dir, MAXPATHL);
5689 restore_start_dir(dirname_start);
5690
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005691 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005692 return NULL;
5693 if (failed)
5694 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005695 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005696 return NULL;
5697 }
5698 return newbuf;
5699}
5700
5701/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005702 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5703 * directory to "dirname_start" prior to returning, if autocmds or the
5704 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005705 */
5706 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005707wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005708{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005709 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005710 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005711#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005712 cleanup_T cs;
5713
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005714 // Reset the error/interrupt/exception state here so that aborting()
5715 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5716 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005717 enter_cleanup(&cs);
5718#endif
5719
Bram Moolenaar81695252004-12-29 20:58:21 +00005720 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005721
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005722#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005723 // Restore the error/interrupt/exception state if not discarded by a
5724 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005725 leave_cleanup(&cs);
5726#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005727 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005728 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005729 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005730}
5731
5732/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005733 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5734 * directory to "dirname_start" prior to returning, if autocmds or the
5735 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005736 */
5737 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005738unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005739{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005740 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005741 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005742 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005743
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005744 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005745 restore_start_dir(dirname_start);
5746 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005747}
5748
Bram Moolenaar05159a02005-02-26 23:04:13 +00005749#if defined(FEAT_EVAL) || defined(PROTO)
5750/*
5751 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005752 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005753 */
5754 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005755get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005756{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005757 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005758 dict_T *dict;
5759 char_u buf[2];
5760 qfline_T *qfp;
5761 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005762 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005763
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005764 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005765 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005766 qi = &ql_info;
5767 if (wp != NULL)
5768 {
5769 qi = GET_LOC_LIST(wp);
5770 if (qi == NULL)
5771 return FAIL;
5772 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005773 }
5774
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005775 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005776 qf_idx = qi->qf_curlist;
5777
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005778 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005779 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005780
Bram Moolenaard823fa92016-08-12 16:29:27 +02005781 qfp = qi->qf_lists[qf_idx].qf_start;
5782 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005783 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005784 // Handle entries with a non-existing buffer number.
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005785 bufnum = qfp->qf_fnum;
5786 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5787 bufnum = 0;
5788
Bram Moolenaar05159a02005-02-26 23:04:13 +00005789 if ((dict = dict_alloc()) == NULL)
5790 return FAIL;
5791 if (list_append_dict(list, dict) == FAIL)
5792 return FAIL;
5793
5794 buf[0] = qfp->qf_type;
5795 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005796 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5797 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5798 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5799 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5800 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5801 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5802 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5803 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5804 || dict_add_string(dict, "type", buf) == FAIL
5805 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005806 return FAIL;
5807
5808 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005809 if (qfp == NULL)
5810 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005811 }
5812 return OK;
5813}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005814
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005815// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005816enum {
5817 QF_GETLIST_NONE = 0x0,
5818 QF_GETLIST_TITLE = 0x1,
5819 QF_GETLIST_ITEMS = 0x2,
5820 QF_GETLIST_NR = 0x4,
5821 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005822 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005823 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005824 QF_GETLIST_IDX = 0x40,
5825 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005826 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005827 QF_GETLIST_FILEWINID = 0x200,
5828 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005829};
5830
5831/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005832 * Parse text from 'di' and return the quickfix list items.
5833 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005834 */
5835 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005836qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005837{
5838 int status = FAIL;
5839 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005840 char_u *errorformat = p_efm;
5841 dictitem_T *efm_di;
5842 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005843
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005844 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005845 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005846 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005847 // If errorformat is supplied then use it, otherwise use the 'efm'
5848 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005849 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5850 {
5851 if (efm_di->di_tv.v_type != VAR_STRING ||
5852 efm_di->di_tv.vval.v_string == NULL)
5853 return FAIL;
5854 errorformat = efm_di->di_tv.vval.v_string;
5855 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005856
Bram Moolenaar36538222017-09-02 19:51:44 +02005857 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005858 if (l == NULL)
5859 return FAIL;
5860
Bram Moolenaar2d67d302018-11-16 18:46:02 +01005861 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005862 if (qi != NULL)
5863 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005864 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005865 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5866 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005867 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005868 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005869 }
5870 free(qi);
5871 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005872 dict_add_list(retdict, "items", l);
5873 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005874 }
5875
5876 return status;
5877}
5878
5879/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005880 * Return the quickfix/location list window identifier in the current tabpage.
5881 */
5882 static int
5883qf_winid(qf_info_T *qi)
5884{
5885 win_T *win;
5886
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005887 // The quickfix window can be opened even if the quickfix list is not set
5888 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005889 if (qi == NULL)
5890 return 0;
5891 win = qf_find_win(qi);
5892 if (win != NULL)
5893 return win->w_id;
5894 return 0;
5895}
5896
5897/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005898 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005899 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005900 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005901qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005902{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005903 int flags = QF_GETLIST_NONE;
5904
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005905 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005906 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005907 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005908 if (!loclist)
5909 // File window ID is applicable only to location list windows
5910 flags &= ~ QF_GETLIST_FILEWINID;
5911 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005912
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005913 if (dict_find(what, (char_u *)"title", -1) != NULL)
5914 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005915
Bram Moolenaara6d48492017-12-12 22:45:31 +01005916 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5917 flags |= QF_GETLIST_NR;
5918
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005919 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5920 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005921
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005922 if (dict_find(what, (char_u *)"context", -1) != NULL)
5923 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005924
Bram Moolenaara6d48492017-12-12 22:45:31 +01005925 if (dict_find(what, (char_u *)"id", -1) != NULL)
5926 flags |= QF_GETLIST_ID;
5927
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005928 if (dict_find(what, (char_u *)"items", -1) != NULL)
5929 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005930
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005931 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5932 flags |= QF_GETLIST_IDX;
5933
5934 if (dict_find(what, (char_u *)"size", -1) != NULL)
5935 flags |= QF_GETLIST_SIZE;
5936
Bram Moolenaarb254af32017-12-18 19:48:58 +01005937 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5938 flags |= QF_GETLIST_TICK;
5939
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005940 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5941 flags |= QF_GETLIST_FILEWINID;
5942
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005943 return flags;
5944}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005945
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005946/*
5947 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5948 * If 'nr' and 'id' are not present in 'what' then return the current
5949 * quickfix list index.
5950 * If 'nr' is zero then return the current quickfix list index.
5951 * If 'nr' is '$' then return the last quickfix list index.
5952 * If 'id' is present then return the index of the quickfix list with that id.
5953 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5954 * Return -1, if quickfix list is not present or if the stack is empty.
5955 */
5956 static int
5957qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5958{
5959 int qf_idx;
5960 dictitem_T *di;
5961
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005962 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005963 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5964 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005965 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005966 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005967 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005968 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005969 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005970 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005971 qf_idx = di->di_tv.vval.v_number - 1;
5972 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005973 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005974 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005975 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005976 else if (di->di_tv.v_type == VAR_STRING
5977 && di->di_tv.vval.v_string != NULL
5978 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005979 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005980 qf_idx = qi->qf_listcount - 1;
5981 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005982 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005983 }
5984
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005985 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5986 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005987 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005988 if (di->di_tv.v_type == VAR_NUMBER)
5989 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005990 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005991 if (di->di_tv.vval.v_number != 0)
5992 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5993 }
5994 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005995 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005996 }
5997
5998 return qf_idx;
5999}
6000
6001/*
6002 * Return default values for quickfix list properties in retdict.
6003 */
6004 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006005qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006006{
6007 int status = OK;
6008
6009 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006010 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006011 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6012 {
6013 list_T *l = list_alloc();
6014 if (l != NULL)
6015 status = dict_add_list(retdict, "items", l);
6016 else
6017 status = FAIL;
6018 }
6019 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006020 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006021 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006022 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006023 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006024 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006025 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006026 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006027 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006028 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006029 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006030 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006031 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006032 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006033 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006034 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006035
6036 return status;
6037}
6038
6039/*
6040 * Return the quickfix list title as 'title' in retdict
6041 */
6042 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006043qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006044{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006045 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006046}
6047
6048/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006049 * Returns the identifier of the window used to display files from a location
6050 * list. If there is no associated window, then returns 0. Useful only when
6051 * called from a location list window.
6052 */
6053 static int
6054qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6055{
6056 int winid = 0;
6057
6058 if (wp != NULL && IS_LL_WINDOW(wp))
6059 {
6060 win_T *ll_wp = qf_find_win_with_loclist(qi);
6061 if (ll_wp != NULL)
6062 winid = ll_wp->w_id;
6063 }
6064
6065 return dict_add_number(retdict, "filewinid", winid);
6066}
6067
6068/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006069 * Return the quickfix list items/entries as 'items' in retdict
6070 */
6071 static int
6072qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6073{
6074 int status = OK;
6075 list_T *l = list_alloc();
6076 if (l != NULL)
6077 {
6078 (void)get_errorlist(qi, NULL, qf_idx, l);
6079 dict_add_list(retdict, "items", l);
6080 }
6081 else
6082 status = FAIL;
6083
6084 return status;
6085}
6086
6087/*
6088 * Return the quickfix list context (if any) as 'context' in retdict.
6089 */
6090 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006091qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006092{
6093 int status;
6094 dictitem_T *di;
6095
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006096 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006097 {
6098 di = dictitem_alloc((char_u *)"context");
6099 if (di != NULL)
6100 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006101 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006102 status = dict_add(retdict, di);
6103 if (status == FAIL)
6104 dictitem_free(di);
6105 }
6106 else
6107 status = FAIL;
6108 }
6109 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006110 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006111
6112 return status;
6113}
6114
6115/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006116 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006117 */
6118 static int
6119qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6120{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006121 int curidx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006122 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006123 // For empty lists, current index is set to 0
6124 curidx = 0;
6125 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006126}
6127
6128/*
6129 * Return quickfix/location list details (title) as a
6130 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6131 * then current list is used. Otherwise the specified list is used.
6132 */
6133 int
6134qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6135{
6136 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006137 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006138 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006139 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006140 dictitem_T *di;
6141 int flags = QF_GETLIST_NONE;
6142
6143 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6144 return qf_get_list_from_lines(what, di, retdict);
6145
6146 if (wp != NULL)
6147 qi = GET_LOC_LIST(wp);
6148
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006149 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006150
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006151 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006152 qf_idx = qf_getprop_qfidx(qi, what);
6153
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006154 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006155 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006156 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006157
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006158 qfl = &qi->qf_lists[qf_idx];
6159
Bram Moolenaard823fa92016-08-12 16:29:27 +02006160 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006161 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006162 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006163 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006164 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006165 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006166 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006167 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006168 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006169 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006170 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006171 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006172 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006173 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006174 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006175 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006176 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006177 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006178 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6179 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006180
Bram Moolenaard823fa92016-08-12 16:29:27 +02006181 return status;
6182}
6183
6184/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006185 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006186 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6187 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006188 */
6189 static int
6190qf_add_entry_from_dict(
6191 qf_info_T *qi,
6192 int qf_idx,
6193 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006194 int first_entry,
6195 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006196{
6197 static int did_bufnr_emsg;
6198 char_u *filename, *module, *pattern, *text, *type;
6199 int bufnum, valid, status, col, vcol, nr;
6200 long lnum;
6201
6202 if (first_entry)
6203 did_bufnr_emsg = FALSE;
6204
Bram Moolenaar8f667172018-12-14 15:38:31 +01006205 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6206 module = dict_get_string(d, (char_u *)"module", TRUE);
6207 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6208 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6209 col = (int)dict_get_number(d, (char_u *)"col");
6210 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6211 nr = (int)dict_get_number(d, (char_u *)"nr");
6212 type = dict_get_string(d, (char_u *)"type", TRUE);
6213 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6214 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006215 if (text == NULL)
6216 text = vim_strsave((char_u *)"");
6217
6218 valid = TRUE;
6219 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6220 valid = FALSE;
6221
6222 // Mark entries with non-existing buffer number as not valid. Give the
6223 // error message only once.
6224 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6225 {
6226 if (!did_bufnr_emsg)
6227 {
6228 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006229 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006230 }
6231 valid = FALSE;
6232 bufnum = 0;
6233 }
6234
6235 // If the 'valid' field is present it overrules the detected value.
6236 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006237 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006238
6239 status = qf_add_entry(qi,
6240 qf_idx,
6241 NULL, // dir
6242 filename,
6243 module,
6244 bufnum,
6245 text,
6246 lnum,
6247 col,
6248 vcol, // vis_col
6249 pattern, // search pattern
6250 nr,
6251 type == NULL ? NUL : *type,
6252 valid);
6253
6254 vim_free(filename);
6255 vim_free(module);
6256 vim_free(pattern);
6257 vim_free(text);
6258 vim_free(type);
6259
Bram Moolenaar9752c722018-12-22 16:49:34 +01006260 if (valid)
6261 *valid_entry = TRUE;
6262
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006263 return status;
6264}
6265
6266/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006267 * Add list of entries to quickfix/location list. Each list entry is
6268 * a dictionary with item information.
6269 */
6270 static int
6271qf_add_entries(
6272 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006273 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006274 list_T *list,
6275 char_u *title,
6276 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006277{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006278 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006279 listitem_T *li;
6280 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006281 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006282 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006283 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006284
Bram Moolenaara3921f42017-06-04 15:30:34 +02006285 if (action == ' ' || qf_idx == qi->qf_listcount)
6286 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006287 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006288 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006289 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006290 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006291 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006292 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006293 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006294 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006295 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006296 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006297 qf_free_items(qfl);
6298 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006299 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006300
6301 for (li = list->lv_first; li != NULL; li = li->li_next)
6302 {
6303 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006304 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006305
6306 d = li->li_tv.vval.v_dict;
6307 if (d == NULL)
6308 continue;
6309
Bram Moolenaar9752c722018-12-22 16:49:34 +01006310 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first,
6311 &valid_entry);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006312 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006313 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006314 }
6315
Bram Moolenaar9752c722018-12-22 16:49:34 +01006316 // Check if any valid error entries are added to the list.
6317 if (valid_entry)
6318 qfl->qf_nonevalid = FALSE;
6319 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006320 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006321 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006322
6323 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006324 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006325 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006326
6327 // Update the current error index if not appending to the list or if the
6328 // list was empty before and it is not empty now.
6329 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qi, qf_idx))
6330 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006331
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006332 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006333 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006334
6335 return retval;
6336}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006337
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006338/*
6339 * Get the quickfix list index from 'nr' or 'id'
6340 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006341 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006342qf_setprop_get_qfidx(
6343 qf_info_T *qi,
6344 dict_T *what,
6345 int action,
6346 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006347{
6348 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006349 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006350
Bram Moolenaard823fa92016-08-12 16:29:27 +02006351 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6352 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006353 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006354 if (di->di_tv.v_type == VAR_NUMBER)
6355 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006356 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006357 if (di->di_tv.vval.v_number != 0)
6358 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006359
Bram Moolenaar55b69262017-08-13 13:42:01 +02006360 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6361 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006362 // When creating a new list, accept qf_idx pointing to the next
6363 // non-available list and add the new list at the end of the
6364 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006365 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006366 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006367 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006368 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006369 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006370 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006371 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006372 }
6373 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006374 && di->di_tv.vval.v_string != NULL
6375 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006376 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006377 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006378 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006379 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006380 qf_idx = 0;
6381 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006382 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006383 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006384 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006385 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006386 }
6387
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006388 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006389 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006390 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006391 if (di->di_tv.v_type != VAR_NUMBER)
6392 return INVALID_QFIDX;
6393
6394 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006395 }
6396
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006397 return qf_idx;
6398}
6399
6400/*
6401 * Set the quickfix list title.
6402 */
6403 static int
6404qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6405{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006406 qf_list_T *qfl = &qi->qf_lists[qf_idx];
6407
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006408 if (di->di_tv.v_type != VAR_STRING)
6409 return FAIL;
6410
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006411 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01006412 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006413 if (qf_idx == qi->qf_curlist)
6414 qf_update_win_titlevar(qi);
6415
6416 return OK;
6417}
6418
6419/*
6420 * Set quickfix list items/entries.
6421 */
6422 static int
6423qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6424{
6425 int retval = FAIL;
6426 char_u *title_save;
6427
6428 if (di->di_tv.v_type != VAR_LIST)
6429 return FAIL;
6430
6431 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6432 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6433 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006434 vim_free(title_save);
6435
6436 return retval;
6437}
6438
6439/*
6440 * Set quickfix list items/entries from a list of lines.
6441 */
6442 static int
6443qf_setprop_items_from_lines(
6444 qf_info_T *qi,
6445 int qf_idx,
6446 dict_T *what,
6447 dictitem_T *di,
6448 int action)
6449{
6450 char_u *errorformat = p_efm;
6451 dictitem_T *efm_di;
6452 int retval = FAIL;
6453
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006454 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006455 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6456 {
6457 if (efm_di->di_tv.v_type != VAR_STRING ||
6458 efm_di->di_tv.vval.v_string == NULL)
6459 return FAIL;
6460 errorformat = efm_di->di_tv.vval.v_string;
6461 }
6462
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006463 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006464 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6465 return FAIL;
6466
6467 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006468 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006469 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6470 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6471 retval = OK;
6472
6473 return retval;
6474}
6475
6476/*
6477 * Set quickfix list context.
6478 */
6479 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006480qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006481{
6482 typval_T *ctx;
6483
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006484 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006485 ctx = alloc_tv();
6486 if (ctx != NULL)
6487 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006488 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006489
6490 return OK;
6491}
6492
6493/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006494 * Set the current index in the specified quickfix list
6495 */
6496 static int
6497qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
6498{
6499 int denote = FALSE;
6500 int newidx;
6501 int old_qfidx;
6502 qfline_T *qf_ptr;
6503
6504 // If the specified index is '$', then use the last entry
6505 if (di->di_tv.v_type == VAR_STRING
6506 && di->di_tv.vval.v_string != NULL
6507 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
6508 newidx = qfl->qf_count;
6509 else
6510 {
6511 // Otherwise use the specified index
6512 newidx = tv_get_number_chk(&di->di_tv, &denote);
6513 if (denote)
6514 return FAIL;
6515 }
6516
6517 if (newidx < 1) // sanity check
6518 return FAIL;
6519 if (newidx > qfl->qf_count)
6520 newidx = qfl->qf_count;
6521
6522 old_qfidx = qfl->qf_index;
6523 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
6524 if (qf_ptr == NULL)
6525 return FAIL;
6526 qfl->qf_ptr = qf_ptr;
6527 qfl->qf_index = newidx;
6528
6529 // If the current list is modified and it is displayed in the quickfix
6530 // window, then Update it.
6531 if (qi->qf_lists[qi->qf_curlist].qf_id == qfl->qf_id)
6532 qf_win_pos_update(qi, old_qfidx);
6533
6534 return OK;
6535}
6536
6537/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006538 * Set quickfix/location list properties (title, items, context).
6539 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006540 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006541 */
6542 static int
6543qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6544{
6545 dictitem_T *di;
6546 int retval = FAIL;
6547 int qf_idx;
6548 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006549 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006550
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006551 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006552 newlist = TRUE;
6553
6554 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006555 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006556 return FAIL;
6557
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006558 if (newlist)
6559 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006560 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006561 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006562 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006563 }
6564
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006565 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaard823fa92016-08-12 16:29:27 +02006566 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006567 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006568 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006569 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006570 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006571 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006572 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006573 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006574 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6575 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006576
Bram Moolenaarb254af32017-12-18 19:48:58 +01006577 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006578 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006579
Bram Moolenaard823fa92016-08-12 16:29:27 +02006580 return retval;
6581}
6582
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006583/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006584 * Find the non-location list window with the specified location list stack in
6585 * the current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006586 */
6587 static win_T *
6588find_win_with_ll(qf_info_T *qi)
6589{
6590 win_T *wp = NULL;
6591
6592 FOR_ALL_WINDOWS(wp)
6593 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6594 return wp;
6595
6596 return NULL;
6597}
6598
6599/*
6600 * Free the entire quickfix/location list stack.
6601 * If the quickfix/location list window is open, then clear it.
6602 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006603 static void
6604qf_free_stack(win_T *wp, qf_info_T *qi)
6605{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006606 win_T *qfwin = qf_find_win(qi);
6607 win_T *llwin = NULL;
6608 win_T *orig_wp = wp;
6609
6610 if (qfwin != NULL)
6611 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006612 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006613 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006614 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006615 qf_update_buffer(qi, NULL);
6616 }
6617
6618 if (wp != NULL && IS_LL_WINDOW(wp))
6619 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006620 // If in the location list window, then use the non-location list
6621 // window with this location list (if present)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006622 llwin = find_win_with_ll(qi);
6623 if (llwin != NULL)
6624 wp = llwin;
6625 }
6626
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006627 qf_free_all(wp);
6628 if (wp == NULL)
6629 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006630 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006631 qi->qf_curlist = 0;
6632 qi->qf_listcount = 0;
6633 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006634 else if (IS_LL_WINDOW(orig_wp))
6635 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006636 // If the location list window is open, then create a new empty
6637 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006638 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006639
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006640 // first free the list reference in the location list window
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006641 ll_free_all(&orig_wp->w_llist_ref);
6642
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006643 orig_wp->w_llist_ref = new_ll;
6644 if (llwin != NULL)
6645 {
6646 llwin->w_llist = new_ll;
6647 new_ll->qf_refcount++;
6648 }
6649 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006650}
6651
Bram Moolenaard823fa92016-08-12 16:29:27 +02006652/*
6653 * Populate the quickfix list with the items supplied in the list
6654 * of dictionaries. "title" will be copied to w:quickfix_title.
6655 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6656 */
6657 int
6658set_errorlist(
6659 win_T *wp,
6660 list_T *list,
6661 int action,
6662 char_u *title,
6663 dict_T *what)
6664{
6665 qf_info_T *qi = &ql_info;
6666 int retval = OK;
6667
6668 if (wp != NULL)
6669 {
6670 qi = ll_get_or_alloc_list(wp);
6671 if (qi == NULL)
6672 return FAIL;
6673 }
6674
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006675 if (action == 'f')
6676 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006677 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006678 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006679 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006680 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006681
6682 incr_quickfix_busy();
6683
6684 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006685 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006686 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006687 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006688 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006689 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006690 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006691 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006692
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006693 decr_quickfix_busy();
6694
Bram Moolenaard823fa92016-08-12 16:29:27 +02006695 return retval;
6696}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006697
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006698/*
6699 * Mark the context as in use for all the lists in a quickfix stack.
6700 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006701 static int
6702mark_quickfix_ctx(qf_info_T *qi, int copyID)
6703{
6704 int i;
6705 int abort = FALSE;
6706 typval_T *ctx;
6707
6708 for (i = 0; i < LISTCOUNT && !abort; ++i)
6709 {
6710 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006711 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6712 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006713 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6714 }
6715
6716 return abort;
6717}
6718
6719/*
6720 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006721 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006722 */
6723 int
6724set_ref_in_quickfix(int copyID)
6725{
6726 int abort = FALSE;
6727 tabpage_T *tp;
6728 win_T *win;
6729
6730 abort = mark_quickfix_ctx(&ql_info, copyID);
6731 if (abort)
6732 return abort;
6733
6734 FOR_ALL_TAB_WINDOWS(tp, win)
6735 {
6736 if (win->w_llist != NULL)
6737 {
6738 abort = mark_quickfix_ctx(win->w_llist, copyID);
6739 if (abort)
6740 return abort;
6741 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006742 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6743 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006744 // In a location list window and none of the other windows is
6745 // referring to this location list. Mark the location list
6746 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006747 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6748 if (abort)
6749 return abort;
6750 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006751 }
6752
6753 return abort;
6754}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006755#endif
6756
Bram Moolenaar81695252004-12-29 20:58:21 +00006757/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006758 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006759 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006760 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006761 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006762 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006763 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006764 */
6765 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006766ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006767{
6768 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006769 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006770 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006771 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006772 int_u save_qfid;
6773 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006774
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006775 switch (eap->cmdidx)
6776 {
6777 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6778 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6779 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6780 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6781 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6782 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6783 default: break;
6784 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006785 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6786 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006787 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006788#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006789 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006790 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006791#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006792 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006793
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006794 // Must come after autocommands.
Bram Moolenaar39665952018-08-15 20:59:48 +02006795 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006796 {
6797 qi = ll_get_or_alloc_list(curwin);
6798 if (qi == NULL)
6799 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006800 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006801 }
6802
Bram Moolenaar86b68352004-12-27 21:59:20 +00006803 if (*eap->arg == NUL)
6804 buf = curbuf;
6805 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6806 buf = buflist_findnr(atoi((char *)eap->arg));
6807 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006808 emsg(_(e_invarg));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006809 else if (buf->b_ml.ml_mfp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006810 emsg(_("E681: Buffer is not loaded"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006811 else
6812 {
6813 if (eap->addr_count == 0)
6814 {
6815 eap->line1 = 1;
6816 eap->line2 = buf->b_ml.ml_line_count;
6817 }
6818 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6819 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006820 emsg(_(e_invrange));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006821 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006822 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006823 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006824
6825 if (buf->b_sfname)
6826 {
6827 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6828 (char *)qf_title, (char *)buf->b_sfname);
6829 qf_title = IObuff;
6830 }
6831
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006832 incr_quickfix_busy();
6833
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006834 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006835 (eap->cmdidx != CMD_caddbuffer
6836 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006837 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006838 qf_title, NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006839 if (qf_stack_empty(qi))
6840 {
6841 decr_quickfix_busy();
6842 return;
6843 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006844 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006845 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006846
6847 // Remember the current quickfix list identifier, so that we can
6848 // check for autocommands changing the current quickfix list.
6849 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006850 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006851 {
6852 buf_T *curbuf_old = curbuf;
6853
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006854 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6855 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006856 if (curbuf != curbuf_old)
6857 // Autocommands changed buffer, don't jump now, "qi" may
6858 // be invalid.
6859 res = 0;
6860 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006861 // Jump to the first error for a new list and if autocmds didn't
6862 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006863 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006864 eap->cmdidx == CMD_lbuffer)
6865 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006866 // display the first error
6867 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006868
6869 decr_quickfix_busy();
Bram Moolenaar754b5602006-02-09 23:53:20 +00006870 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006871 }
6872}
6873
Bram Moolenaar1e015462005-09-25 22:16:38 +00006874#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006875/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006876 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6877 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006878 */
6879 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006880ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006881{
6882 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006883 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006884 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006885 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006886 int_u save_qfid;
6887 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006888
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006889 switch (eap->cmdidx)
6890 {
6891 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6892 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6893 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6894 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6895 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6896 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6897 default: break;
6898 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006899 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6900 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006901 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006902#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006903 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006904 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006905#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006906 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006907
Bram Moolenaar39665952018-08-15 20:59:48 +02006908 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006909 {
6910 qi = ll_get_or_alloc_list(curwin);
6911 if (qi == NULL)
6912 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006913 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006914 }
6915
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006916 // Evaluate the expression. When the result is a string or a list we can
6917 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006918 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006919 if (tv != NULL)
6920 {
6921 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6922 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6923 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006924 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006925 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006926 (eap->cmdidx != CMD_caddexpr
6927 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006928 (linenr_T)0, (linenr_T)0,
6929 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006930 if (qf_stack_empty(qi))
6931 {
6932 decr_quickfix_busy();
6933 goto cleanup;
6934 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006935 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006936 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006937
6938 // Remember the current quickfix list identifier, so that we can
6939 // check for autocommands changing the current quickfix list.
6940 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006941 if (au_name != NULL)
6942 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6943 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006944
6945 // Jump to the first error for a new list and if autocmds didn't
6946 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006947 if (res > 0 && (eap->cmdidx == CMD_cexpr
6948 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006949 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006950 // display the first error
6951 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006952 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006953 }
6954 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006955 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006956cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00006957 free_tv(tv);
6958 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006959}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006960#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006961
6962/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006963 * Get the location list for ":lhelpgrep"
6964 */
6965 static qf_info_T *
6966hgr_get_ll(int *new_ll)
6967{
6968 win_T *wp;
6969 qf_info_T *qi;
6970
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006971 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006972 if (bt_help(curwin->w_buffer))
6973 wp = curwin;
6974 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006975 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006976 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006977
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006978 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006979 qi = NULL;
6980 else
6981 qi = wp->w_llist;
6982
6983 if (qi == NULL)
6984 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006985 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006986 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006987 return NULL;
6988 *new_ll = TRUE;
6989 }
6990
6991 return qi;
6992}
6993
6994/*
6995 * Search for a pattern in a help file.
6996 */
6997 static void
6998hgr_search_file(
6999 qf_info_T *qi,
7000 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007001 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007002 regmatch_T *p_regmatch)
7003{
7004 FILE *fd;
7005 long lnum;
7006
7007 fd = mch_fopen((char *)fname, "r");
7008 if (fd == NULL)
7009 return;
7010
7011 lnum = 1;
7012 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7013 {
7014 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007015
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007016 // Convert a line if 'encoding' is not utf-8 and
7017 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007018 if (p_vc->vc_type != CONV_NONE
7019 && has_non_ascii(IObuff))
7020 {
7021 line = string_convert(p_vc, IObuff, NULL);
7022 if (line == NULL)
7023 line = IObuff;
7024 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007025
7026 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7027 {
7028 int l = (int)STRLEN(line);
7029
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007030 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007031 while (l > 0 && line[l - 1] <= ' ')
7032 line[--l] = NUL;
7033
7034 if (qf_add_entry(qi,
7035 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007036 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007037 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007038 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007039 0,
7040 line,
7041 lnum,
7042 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007043 + 1, // col
7044 FALSE, // vis_col
7045 NULL, // search pattern
7046 0, // nr
7047 1, // type
7048 TRUE // valid
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007049 ) == FAIL)
7050 {
7051 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007052 if (line != IObuff)
7053 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007054 break;
7055 }
7056 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007057 if (line != IObuff)
7058 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007059 ++lnum;
7060 line_breakcheck();
7061 }
7062 fclose(fd);
7063}
7064
7065/*
7066 * Search for a pattern in all the help files in the doc directory under
7067 * the given directory.
7068 */
7069 static void
7070hgr_search_files_in_dir(
7071 qf_info_T *qi,
7072 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007073 regmatch_T *p_regmatch,
7074 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007075#ifdef FEAT_MULTI_LANG
7076 , char_u *lang
7077#endif
7078 )
7079{
7080 int fcount;
7081 char_u **fnames;
7082 int fi;
7083
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007084 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007085 add_pathsep(dirname);
7086 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7087 if (gen_expand_wildcards(1, &dirname, &fcount,
7088 &fnames, EW_FILE|EW_SILENT) == OK
7089 && fcount > 0)
7090 {
7091 for (fi = 0; fi < fcount && !got_int; ++fi)
7092 {
7093#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007094 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007095 if (lang != NULL
7096 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007097 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007098 && !(STRNICMP(lang, "en", 2) == 0
7099 && STRNICMP("txt", fnames[fi]
7100 + STRLEN(fnames[fi]) - 3, 3) == 0))
7101 continue;
7102#endif
7103
Bram Moolenaara12a1612019-01-24 16:39:02 +01007104 hgr_search_file(qi, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007105 }
7106 FreeWild(fcount, fnames);
7107 }
7108}
7109
7110/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007111 * Search for a pattern in all the help files in the 'runtimepath'
7112 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007113 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007114 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007115 */
7116 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007117hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007118{
7119 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007120
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007121 vimconv_T vc;
7122
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007123 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7124 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007125 vc.vc_type = CONV_NONE;
7126 if (!enc_utf8)
7127 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007128
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007129 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007130 p = p_rtp;
7131 while (*p != NUL && !got_int)
7132 {
7133 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7134
Bram Moolenaara12a1612019-01-24 16:39:02 +01007135 hgr_search_files_in_dir(qi, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007136#ifdef FEAT_MULTI_LANG
7137 , lang
7138#endif
7139 );
7140 }
7141
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007142 if (vc.vc_type != CONV_NONE)
7143 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007144}
7145
7146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 * ":helpgrep {pattern}"
7148 */
7149 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007150ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
7152 regmatch_T regmatch;
7153 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007154 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007155 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007156 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007157 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
Bram Moolenaar73633f82012-01-20 13:39:07 +01007159 switch (eap->cmdidx)
7160 {
7161 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7162 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7163 default: break;
7164 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007165 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7166 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007167 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007168#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007169 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007170 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007171#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007172 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007173
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007174 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01007175 save_cpo = p_cpo;
7176 p_cpo = empty_option;
7177
Bram Moolenaar39665952018-08-15 20:59:48 +02007178 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007179 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007180 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007181 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007182 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007183 }
7184
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007185 incr_quickfix_busy();
7186
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007187#ifdef FEAT_MULTI_LANG
7188 // Check for a specified language
7189 lang = check_help_lang(eap->arg);
7190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7192 regmatch.rm_ic = FALSE;
7193 if (regmatch.regprog != NULL)
7194 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007195 qf_list_T *qfl;
7196
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007197 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007198 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007200 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007201
Bram Moolenaar473de612013-06-08 18:19:48 +02007202 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007204 qfl = &qi->qf_lists[qi->qf_curlist];
7205 qfl->qf_nonevalid = FALSE;
7206 qfl->qf_ptr = qfl->qf_start;
7207 qfl->qf_index = 1;
7208 qf_list_changed(qfl);
7209 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 }
7211
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007212 if (p_cpo == empty_option)
7213 p_cpo = save_cpo;
7214 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007215 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007216 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217
Bram Moolenaar73633f82012-01-20 13:39:07 +01007218 if (au_name != NULL)
7219 {
7220 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7221 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007222 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007223 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007224 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007225 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007226 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007227 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007228 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007229
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007230 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007231 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007232 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007233 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007234 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007235
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007236 decr_quickfix_busy();
7237
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007238 if (eap->cmdidx == CMD_lhelpgrep)
7239 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007240 // If the help window is not opened or if it already points to the
7241 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007242 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007243 {
7244 if (new_qi)
7245 ll_free_all(&qi);
7246 }
7247 else if (curwin->w_llist == NULL)
7248 curwin->w_llist = qi;
7249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250}
7251
7252#endif /* FEAT_QUICKFIX */