blob: bc09053e1e91c6bb230b595a2ad94172a764ac1f [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 Moolenaar2c7292d2017-03-05 17:43:31 +0100807#ifdef FEAT_MBYTE
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200808 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200809 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
810 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100811 char_u *line;
812
813 line = string_convert(&state->vc, state->linebuf, &state->linelen);
814 if (line != NULL)
815 {
816 if (state->linelen < IOSIZE)
817 {
818 STRCPY(state->linebuf, line);
819 vim_free(line);
820 }
821 else
822 {
823 vim_free(state->growbuf);
824 state->linebuf = state->growbuf = line;
825 state->growbufsiz = state->linelen < LINE_MAXLEN
826 ? state->linelen : LINE_MAXLEN;
827 }
828 }
829 }
830#endif
831
Bram Moolenaare0d37972016-07-15 22:36:01 +0200832 return QF_OK;
833}
834
835/*
836 * Get the next string from a file/buffer/list/string.
837 */
838 static int
839qf_get_nextline(qfstate_T *state)
840{
841 int status = QF_FAIL;
842
843 if (state->fd == NULL)
844 {
845 if (state->tv != NULL)
846 {
847 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200848 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200849 status = qf_get_next_str_line(state);
850 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200851 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200852 status = qf_get_next_list_line(state);
853 }
854 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200855 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200856 status = qf_get_next_buf_line(state);
857 }
858 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200859 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200860 status = qf_get_next_file_line(state);
861
862 if (status != QF_OK)
863 return status;
864
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200865 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200866 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200867 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200868 state->linebuf[state->linelen - 1] = NUL;
869#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200870 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
871 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200872#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200873 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200874
875#ifdef FEAT_MBYTE
876 remove_bom(state->linebuf);
877#endif
878
879 return QF_OK;
880}
881
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200882typedef struct {
883 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200884 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200885 char_u *errmsg;
886 int errmsglen;
887 long lnum;
888 int col;
889 char_u use_viscol;
890 char_u *pattern;
891 int enr;
892 int type;
893 int valid;
894} qffields_T;
895
896/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200897 * Parse the match for filename ('%f') pattern in regmatch.
898 * Return the matched value in "fields->namebuf".
899 */
900 static int
901qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
902{
903 int c;
904
905 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
906 return QF_FAIL;
907
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200908 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200909 c = *rmp->endp[midx];
910 *rmp->endp[midx] = NUL;
911 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
912 *rmp->endp[midx] = c;
913
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200914 // For separate filename patterns (%O, %P and %Q), the specified file
915 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200916 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
917 && mch_getperm(fields->namebuf) == -1)
918 return QF_FAIL;
919
920 return QF_OK;
921}
922
923/*
924 * Parse the match for error number ('%n') pattern in regmatch.
925 * Return the matched value in "fields->enr".
926 */
927 static int
928qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
929{
930 if (rmp->startp[midx] == NULL)
931 return QF_FAIL;
932 fields->enr = (int)atol((char *)rmp->startp[midx]);
933 return QF_OK;
934}
935
936/*
937 * Parse the match for line number (%l') pattern in regmatch.
938 * Return the matched value in "fields->lnum".
939 */
940 static int
941qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
942{
943 if (rmp->startp[midx] == NULL)
944 return QF_FAIL;
945 fields->lnum = atol((char *)rmp->startp[midx]);
946 return QF_OK;
947}
948
949/*
950 * Parse the match for column number ('%c') pattern in regmatch.
951 * Return the matched value in "fields->col".
952 */
953 static int
954qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
955{
956 if (rmp->startp[midx] == NULL)
957 return QF_FAIL;
958 fields->col = (int)atol((char *)rmp->startp[midx]);
959 return QF_OK;
960}
961
962/*
963 * Parse the match for error type ('%t') pattern in regmatch.
964 * Return the matched value in "fields->type".
965 */
966 static int
967qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
968{
969 if (rmp->startp[midx] == NULL)
970 return QF_FAIL;
971 fields->type = *rmp->startp[midx];
972 return QF_OK;
973}
974
975/*
976 * Parse the match for '%+' format pattern. The whole matching line is included
977 * in the error string. Return the matched line in "fields->errmsg".
978 */
979 static int
980qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
981{
982 char_u *p;
983
984 if (linelen >= fields->errmsglen)
985 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200986 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200987 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
988 return QF_NOMEM;
989 fields->errmsg = p;
990 fields->errmsglen = linelen + 1;
991 }
992 vim_strncpy(fields->errmsg, linebuf, linelen);
993 return QF_OK;
994}
995
996/*
997 * Parse the match for error message ('%m') pattern in regmatch.
998 * Return the matched value in "fields->errmsg".
999 */
1000 static int
1001qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1002{
1003 char_u *p;
1004 int len;
1005
1006 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1007 return QF_FAIL;
1008 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1009 if (len >= fields->errmsglen)
1010 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001011 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001012 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1013 return QF_NOMEM;
1014 fields->errmsg = p;
1015 fields->errmsglen = len + 1;
1016 }
1017 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1018 return QF_OK;
1019}
1020
1021/*
1022 * Parse the match for rest of a single-line file message ('%r') pattern.
1023 * Return the matched value in "tail".
1024 */
1025 static int
1026qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1027{
1028 if (rmp->startp[midx] == NULL)
1029 return QF_FAIL;
1030 *tail = rmp->startp[midx];
1031 return QF_OK;
1032}
1033
1034/*
1035 * Parse the match for the pointer line ('%p') pattern in regmatch.
1036 * Return the matched value in "fields->col".
1037 */
1038 static int
1039qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1040{
1041 char_u *match_ptr;
1042
1043 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1044 return QF_FAIL;
1045 fields->col = 0;
1046 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1047 ++match_ptr)
1048 {
1049 ++fields->col;
1050 if (*match_ptr == TAB)
1051 {
1052 fields->col += 7;
1053 fields->col -= fields->col % 8;
1054 }
1055 }
1056 ++fields->col;
1057 fields->use_viscol = TRUE;
1058 return QF_OK;
1059}
1060
1061/*
1062 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1063 * Return the matched value in "fields->col".
1064 */
1065 static int
1066qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1067{
1068 if (rmp->startp[midx] == NULL)
1069 return QF_FAIL;
1070 fields->col = (int)atol((char *)rmp->startp[midx]);
1071 fields->use_viscol = TRUE;
1072 return QF_OK;
1073}
1074
1075/*
1076 * Parse the match for the search text ('%s') pattern in regmatch.
1077 * Return the matched value in "fields->pattern".
1078 */
1079 static int
1080qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1081{
1082 int len;
1083
1084 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1085 return QF_FAIL;
1086 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1087 if (len > CMDBUFFSIZE - 5)
1088 len = CMDBUFFSIZE - 5;
1089 STRCPY(fields->pattern, "^\\V");
1090 STRNCAT(fields->pattern, rmp->startp[midx], len);
1091 fields->pattern[len + 3] = '\\';
1092 fields->pattern[len + 4] = '$';
1093 fields->pattern[len + 5] = NUL;
1094 return QF_OK;
1095}
1096
1097/*
1098 * Parse the match for the module ('%o') pattern in regmatch.
1099 * Return the matched value in "fields->module".
1100 */
1101 static int
1102qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1103{
1104 int len;
1105
1106 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1107 return QF_FAIL;
1108 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1109 if (len > CMDBUFFSIZE)
1110 len = CMDBUFFSIZE;
1111 STRNCAT(fields->module, rmp->startp[midx], len);
1112 return QF_OK;
1113}
1114
1115/*
1116 * 'errorformat' format pattern parser functions.
1117 * The '%f' and '%r' formats are parsed differently from other formats.
1118 * See qf_parse_match() for details.
1119 */
1120static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1121{
1122 NULL,
1123 qf_parse_fmt_n,
1124 qf_parse_fmt_l,
1125 qf_parse_fmt_c,
1126 qf_parse_fmt_t,
1127 qf_parse_fmt_m,
1128 NULL,
1129 qf_parse_fmt_p,
1130 qf_parse_fmt_v,
1131 qf_parse_fmt_s,
1132 qf_parse_fmt_o
1133};
1134
1135/*
1136 * Parse the error format pattern matches in "regmatch" and set the values in
1137 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1138 * match. Returns QF_OK if all the matches are successfully parsed. On
1139 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001140 */
1141 static int
1142qf_parse_match(
1143 char_u *linebuf,
1144 int linelen,
1145 efm_T *fmt_ptr,
1146 regmatch_T *regmatch,
1147 qffields_T *fields,
1148 int qf_multiline,
1149 int qf_multiscan,
1150 char_u **tail)
1151{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001152 int idx = fmt_ptr->prefix;
1153 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001154 int midx;
1155 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001156
1157 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1158 return QF_FAIL;
1159 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1160 fields->type = idx;
1161 else
1162 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001163
1164 // Extract error message data from matched line.
1165 // We check for an actual submatch, because "\[" and "\]" in
1166 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001167 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001168 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001169 status = QF_OK;
1170 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001171 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001172 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1173 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001174 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001175 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001176 status = qf_parse_fmt_plus(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001177 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001178 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001179 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001180 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001181 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001182 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001183 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001184
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001185 if (status != QF_OK)
1186 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001187 }
1188
1189 return QF_OK;
1190}
1191
1192/*
1193 * Parse an error line in 'linebuf' using a single error format string in
1194 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1195 * Returns QF_OK if the efm format matches completely and the fields are
1196 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1197 */
1198 static int
1199qf_parse_get_fields(
1200 char_u *linebuf,
1201 int linelen,
1202 efm_T *fmt_ptr,
1203 qffields_T *fields,
1204 int qf_multiline,
1205 int qf_multiscan,
1206 char_u **tail)
1207{
1208 regmatch_T regmatch;
1209 int status = QF_FAIL;
1210 int r;
1211
1212 if (qf_multiscan &&
1213 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1214 return QF_FAIL;
1215
1216 fields->namebuf[0] = NUL;
1217 fields->module[0] = NUL;
1218 fields->pattern[0] = NUL;
1219 if (!qf_multiscan)
1220 fields->errmsg[0] = NUL;
1221 fields->lnum = 0;
1222 fields->col = 0;
1223 fields->use_viscol = FALSE;
1224 fields->enr = -1;
1225 fields->type = 0;
1226 *tail = NULL;
1227
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001228 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001229 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001230 regmatch.regprog = fmt_ptr->prog;
1231 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1232 fmt_ptr->prog = regmatch.regprog;
1233 if (r)
1234 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1235 fields, qf_multiline, qf_multiscan, tail);
1236
1237 return status;
1238}
1239
1240/*
1241 * Parse directory error format prefixes (%D and %X).
1242 * Push and pop directories from the directory stack when scanning directory
1243 * names.
1244 */
1245 static int
1246qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1247{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001248 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001249 {
1250 if (*fields->namebuf == NUL)
1251 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001252 emsg(_("E379: Missing or empty directory name"));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001253 return QF_FAIL;
1254 }
1255 qfl->qf_directory =
1256 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1257 if (qfl->qf_directory == NULL)
1258 return QF_FAIL;
1259 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001260 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001261 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1262
1263 return QF_OK;
1264}
1265
1266/*
1267 * Parse global file name error format prefixes (%O, %P and %Q).
1268 */
1269 static int
1270qf_parse_file_pfx(
1271 int idx,
1272 qffields_T *fields,
1273 qf_list_T *qfl,
1274 char_u *tail)
1275{
1276 fields->valid = FALSE;
1277 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1278 {
1279 if (*fields->namebuf && idx == 'P')
1280 qfl->qf_currfile =
1281 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1282 else if (idx == 'Q')
1283 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1284 *fields->namebuf = NUL;
1285 if (tail && *tail)
1286 {
1287 STRMOVE(IObuff, skipwhite(tail));
1288 qfl->qf_multiscan = TRUE;
1289 return QF_MULTISCAN;
1290 }
1291 }
1292
1293 return QF_OK;
1294}
1295
1296/*
1297 * Parse a non-error line (a line which doesn't match any of the error
1298 * format in 'efm').
1299 */
1300 static int
1301qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1302{
1303 char_u *p;
1304
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001305 fields->namebuf[0] = NUL; // no match found, remove file name
1306 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001307 fields->valid = FALSE;
1308 if (linelen >= fields->errmsglen)
1309 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001310 // linelen + null terminator
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001311 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1312 return QF_NOMEM;
1313 fields->errmsg = p;
1314 fields->errmsglen = linelen + 1;
1315 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001316 // copy whole line to error message
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001317 vim_strncpy(fields->errmsg, linebuf, linelen);
1318
1319 return QF_OK;
1320}
1321
1322/*
1323 * Parse multi-line error format prefixes (%C and %Z)
1324 */
1325 static int
1326qf_parse_multiline_pfx(
1327 qf_info_T *qi,
1328 int qf_idx,
1329 int idx,
1330 qf_list_T *qfl,
1331 qffields_T *fields)
1332{
1333 char_u *ptr;
1334 int len;
1335
1336 if (!qfl->qf_multiignore)
1337 {
1338 qfline_T *qfprev = qfl->qf_last;
1339
1340 if (qfprev == NULL)
1341 return QF_FAIL;
1342 if (*fields->errmsg && !qfl->qf_multiignore)
1343 {
1344 len = (int)STRLEN(qfprev->qf_text);
1345 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1346 == NULL)
1347 return QF_FAIL;
1348 STRCPY(ptr, qfprev->qf_text);
1349 vim_free(qfprev->qf_text);
1350 qfprev->qf_text = ptr;
1351 *(ptr += len) = '\n';
1352 STRCPY(++ptr, fields->errmsg);
1353 }
1354 if (qfprev->qf_nr == -1)
1355 qfprev->qf_nr = fields->enr;
1356 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001357 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001358 qfprev->qf_type = fields->type;
1359
1360 if (!qfprev->qf_lnum)
1361 qfprev->qf_lnum = fields->lnum;
1362 if (!qfprev->qf_col)
1363 qfprev->qf_col = fields->col;
1364 qfprev->qf_viscol = fields->use_viscol;
1365 if (!qfprev->qf_fnum)
1366 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1367 qfl->qf_directory,
1368 *fields->namebuf || qfl->qf_directory != NULL
1369 ? fields->namebuf
1370 : qfl->qf_currfile != NULL && fields->valid
1371 ? qfl->qf_currfile : 0);
1372 }
1373 if (idx == 'Z')
1374 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1375 line_breakcheck();
1376
1377 return QF_IGNORE_LINE;
1378}
1379
1380/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001381 * Parse a line and get the quickfix fields.
1382 * Return the QF_ status.
1383 */
1384 static int
1385qf_parse_line(
1386 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001387 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001388 char_u *linebuf,
1389 int linelen,
1390 efm_T *fmt_first,
1391 qffields_T *fields)
1392{
1393 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001394 int idx = 0;
1395 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001396 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001397 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001398
Bram Moolenaare333e792018-04-08 13:27:39 +02001399restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001400 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001401 if (fmt_start == NULL)
1402 fmt_ptr = fmt_first;
1403 else
1404 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001405 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001406 fmt_ptr = fmt_start;
1407 fmt_start = NULL;
1408 }
1409
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001410 // Try to match each part of 'errorformat' until we find a complete
1411 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001412 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001413 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1414 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001415 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001416 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1417 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1418 if (status == QF_NOMEM)
1419 return status;
1420 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001421 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001422 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001423 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001424
1425 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1426 {
1427 if (fmt_ptr != NULL)
1428 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001429 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001430 status = qf_parse_dir_pfx(idx, fields, qfl);
1431 if (status != QF_OK)
1432 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001433 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001434
1435 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1436 if (status != QF_OK)
1437 return status;
1438
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001439 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001440 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001441 }
1442 else if (fmt_ptr != NULL)
1443 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001444 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001445 if (fmt_ptr->conthere)
1446 fmt_start = fmt_ptr;
1447
1448 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1449 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001450 qfl->qf_multiline = TRUE; // start of a multi-line message
1451 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001452 }
1453 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001454 { // continuation of multi-line msg
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001455 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1456 if (status != QF_OK)
1457 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001458 }
1459 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001460 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001461 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1462 if (status == QF_MULTISCAN)
1463 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001464 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001465 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001466 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001467 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001468 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001469 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001470 return QF_IGNORE_LINE;
1471 }
1472 }
1473
1474 return QF_OK;
1475}
1476
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001477/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001478 * Returns TRUE if the specified quickfix/location stack is empty
1479 */
1480 static int
1481qf_stack_empty(qf_info_T *qi)
1482{
1483 return qi == NULL || qi->qf_listcount <= 0;
1484}
1485
1486/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001487 * Returns TRUE if the specified quickfix/location list is empty.
1488 */
1489 static int
1490qf_list_empty(qf_info_T *qi, int qf_idx)
1491{
1492 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1493 return TRUE;
1494 return qi->qf_lists[qf_idx].qf_count <= 0;
1495}
1496
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001497/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001498 * Allocate the fields used for parsing lines and populating a quickfix list.
1499 */
1500 static int
1501qf_alloc_fields(qffields_T *pfields)
1502{
1503 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1504 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1505 pfields->errmsglen = CMDBUFFSIZE + 1;
1506 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1507 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1508 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1509 || pfields->pattern == NULL || pfields->module == NULL)
1510 return FAIL;
1511
1512 return OK;
1513}
1514
1515/*
1516 * Free the fields used for parsing lines and populating a quickfix list.
1517 */
1518 static void
1519qf_free_fields(qffields_T *pfields)
1520{
1521 vim_free(pfields->namebuf);
1522 vim_free(pfields->module);
1523 vim_free(pfields->errmsg);
1524 vim_free(pfields->pattern);
1525}
1526
1527/*
1528 * Setup the state information used for parsing lines and populating a
1529 * quickfix list.
1530 */
1531 static int
1532qf_setup_state(
1533 qfstate_T *pstate,
1534 char_u *enc,
1535 char_u *efile,
1536 typval_T *tv,
1537 buf_T *buf,
1538 linenr_T lnumfirst,
1539 linenr_T lnumlast)
1540{
1541#ifdef FEAT_MBYTE
1542 pstate->vc.vc_type = CONV_NONE;
1543 if (enc != NULL && *enc != NUL)
1544 convert_setup(&pstate->vc, enc, p_enc);
1545#endif
1546
1547 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001549 semsg(_(e_openerrf), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001550 return FAIL;
1551 }
1552
1553 if (tv != NULL)
1554 {
1555 if (tv->v_type == VAR_STRING)
1556 pstate->p_str = tv->vval.v_string;
1557 else if (tv->v_type == VAR_LIST)
1558 pstate->p_li = tv->vval.v_list->lv_first;
1559 pstate->tv = tv;
1560 }
1561 pstate->buf = buf;
1562 pstate->buflnum = lnumfirst;
1563 pstate->lnumlast = lnumlast;
1564
1565 return OK;
1566}
1567
1568/*
1569 * Cleanup the state information used for parsing lines and populating a
1570 * quickfix list.
1571 */
1572 static void
1573qf_cleanup_state(qfstate_T *pstate)
1574{
1575 if (pstate->fd != NULL)
1576 fclose(pstate->fd);
1577
1578 vim_free(pstate->growbuf);
1579#ifdef FEAT_MBYTE
1580 if (pstate->vc.vc_type != CONV_NONE)
1581 convert_setup(&pstate->vc, NULL, NULL);
1582#endif
1583}
1584
1585/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001586 * Read the errorfile "efile" into memory, line by line, building the error
1587 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001588 * Alternative: when "efile" is NULL read errors from buffer "buf".
1589 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001590 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001591 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1592 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001593 * Return -1 for error, number of errors for success.
1594 */
1595 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001596qf_init_ext(
1597 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001598 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001599 char_u *efile,
1600 buf_T *buf,
1601 typval_T *tv,
1602 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001603 int newlist, // TRUE: start a new error list
1604 linenr_T lnumfirst, // first line number to use
1605 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001606 char_u *qf_title,
1607 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001608{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001609 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001610 qfstate_T state;
1611 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001612 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001613 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001614 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001616 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001617 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001618 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001620 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001621 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001622
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001623 vim_memset(&state, 0, sizeof(state));
1624 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001625 if ((qf_alloc_fields(&fields) == FAIL) ||
1626 (qf_setup_state(&state, enc, efile, tv, buf,
1627 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 goto qf_init_end;
1629
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001630 if (newlist || qf_idx == qi->qf_listcount)
1631 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001632 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001633 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001634 qf_idx = qi->qf_curlist;
1635 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001636 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001637 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001638 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001639 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001640 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001641 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001644 qfl = &qi->qf_lists[qf_idx];
1645
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001646 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001647 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001648 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 else
1650 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001652 // If the errorformat didn't change between calls, then reuse the
1653 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001654 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1655 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001656 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001657 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001658 free_efm_list(&fmt_first);
1659
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001660 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001661 fmt_first = parse_efm_option(efm);
1662 if (fmt_first != NULL)
1663 last_efm = vim_strsave(efm);
1664 }
1665
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001666 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001669 // got_int is reset here, because it was probably set when killing the
1670 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 got_int = FALSE;
1672
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001673 // Read the lines in the error file one by one.
1674 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001675 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001677 // Get the next line from a file/buffer/list/string
Bram Moolenaare0d37972016-07-15 22:36:01 +02001678 status = qf_get_nextline(&state);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001679 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001680 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001681 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001682 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001684 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1685 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001686 if (status == QF_FAIL)
1687 goto error2;
1688 if (status == QF_NOMEM)
1689 goto qf_init_end;
1690 if (status == QF_IGNORE_LINE)
1691 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001693 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001694 qf_idx,
1695 qfl->qf_directory,
1696 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001697 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001698 : ((qfl->qf_currfile != NULL && fields.valid)
1699 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001700 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001701 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001702 fields.errmsg,
1703 fields.lnum,
1704 fields.col,
1705 fields.use_viscol,
1706 fields.pattern,
1707 fields.enr,
1708 fields.type,
1709 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 goto error2;
1711 line_breakcheck();
1712 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001713 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001715 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001717 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001718 qfl->qf_ptr = qfl->qf_start;
1719 qfl->qf_index = 1;
1720 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 }
1722 else
1723 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001724 qfl->qf_nonevalid = FALSE;
1725 if (qfl->qf_ptr == NULL)
1726 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001728 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001729 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001730 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001732 emsg(_(e_readerrf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001734 if (!adding)
1735 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001736 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001737 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001738 qi->qf_listcount--;
1739 if (qi->qf_curlist > 0)
1740 --qi->qf_curlist;
1741 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001742qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001743 if (qf_idx == qi->qf_curlist)
1744 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001745 qf_cleanup_state(&state);
1746 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
1748 return retval;
1749}
1750
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001751/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001752 * Read the errorfile "efile" into memory, line by line, building the error
1753 * list. Set the error list's title to qf_title.
1754 * Return -1 for error, number of errors for success.
1755 */
1756 int
1757qf_init(win_T *wp,
1758 char_u *efile,
1759 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001760 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001761 char_u *qf_title,
1762 char_u *enc)
1763{
1764 qf_info_T *qi = &ql_info;
1765
1766 if (wp != NULL)
1767 {
1768 qi = ll_get_or_alloc_list(wp);
1769 if (qi == NULL)
1770 return FAIL;
1771 }
1772
1773 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1774 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1775}
1776
1777/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001778 * Set the title of the specified quickfix list. Frees the previous title.
1779 * Prepends ':' to the title.
1780 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001781 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001782qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001783{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001784 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001785
Bram Moolenaarfb604092014-07-23 15:55:00 +02001786 if (title != NULL)
1787 {
1788 char_u *p = alloc((int)STRLEN(title) + 2);
1789
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001790 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001791 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001792 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001793 }
1794}
1795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001797 * The title of a quickfix/location list is set, by default, to the command
1798 * that created the quickfix list with the ":" prefix.
1799 * Create a quickfix list title string by prepending ":" to a user command.
1800 * Returns a pointer to a static buffer with the title.
1801 */
1802 static char_u *
1803qf_cmdtitle(char_u *cmd)
1804{
1805 static char_u qftitle_str[IOSIZE];
1806
1807 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1808 return qftitle_str;
1809}
1810
1811/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001812 * Prepare for adding a new quickfix list. If the current list is in the
1813 * middle of the stack, then all the following lists are freed and then
1814 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 */
1816 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001817qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
1819 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001820 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001822 // If the current entry is not the last entry, delete entries beyond
1823 // the current entry. This makes it possible to browse in a tree-like
1824 // way with ":grep'.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001825 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001826 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001828 // When the stack is full, remove to oldest entry
1829 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001830 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001832 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001834 qi->qf_lists[i - 1] = qi->qf_lists[i];
1835 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
1837 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001838 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001839 qfl = &qi->qf_lists[qi->qf_curlist];
1840 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1841 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001842 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001843 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844}
1845
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001846/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001847 * Queue location list stack delete request.
1848 */
1849 static void
1850locstack_queue_delreq(qf_info_T *qi)
1851{
1852 qf_delq_T *q;
1853
1854 q = (qf_delq_T *)alloc((unsigned)sizeof(qf_delq_T));
1855 if (q != NULL)
1856 {
1857 q->qi = qi;
1858 q->next = qf_delq_head;
1859 qf_delq_head = q;
1860 }
1861}
1862
1863/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001864 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001865 */
1866 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001867ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001868{
1869 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001870 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001871
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001872 qi = *pqi;
1873 if (qi == NULL)
1874 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001875 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001876
1877 qi->qf_refcount--;
1878 if (qi->qf_refcount < 1)
1879 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001880 // No references to this location list.
1881 // If the location list is still in use, then queue the delete request
1882 // to be processed later.
1883 if (quickfix_busy > 0)
1884 locstack_queue_delreq(qi);
1885 else
1886 {
1887 for (i = 0; i < qi->qf_listcount; ++i)
1888 qf_free(&qi->qf_lists[i]);
1889 vim_free(qi);
1890 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001891 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001892}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001893
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001894/*
1895 * Free all the quickfix/location lists in the stack.
1896 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001897 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001898qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001899{
1900 int i;
1901 qf_info_T *qi = &ql_info;
1902
1903 if (wp != NULL)
1904 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001905 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001906 ll_free_all(&wp->w_llist);
1907 ll_free_all(&wp->w_llist_ref);
1908 }
1909 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001910 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001911 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001912 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001913}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001914
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001916 * Delay freeing of location list stacks when the quickfix code is running.
1917 * Used to avoid problems with autocmds freeing location list stacks when the
1918 * quickfix code is still referencing the stack.
1919 * Must always call decr_quickfix_busy() exactly once after this.
1920 */
1921 static void
1922incr_quickfix_busy(void)
1923{
1924 quickfix_busy++;
1925}
1926
1927/*
1928 * Safe to free location list stacks. Process any delayed delete requests.
1929 */
1930 static void
1931decr_quickfix_busy(void)
1932{
1933 if (--quickfix_busy == 0)
1934 {
1935 // No longer referencing the location lists. Process all the pending
1936 // delete requests.
1937 while (qf_delq_head != NULL)
1938 {
1939 qf_delq_T *q = qf_delq_head;
1940
1941 qf_delq_head = q->next;
1942 ll_free_all(&q->qi);
1943 vim_free(q);
1944 }
1945 }
1946#ifdef ABORT_ON_INTERNAL_ERROR
1947 if (quickfix_busy < 0)
1948 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001949 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001950 abort();
1951 }
1952#endif
1953}
1954
1955#if defined(EXITFREE) || defined(PROTO)
1956 void
1957check_quickfix_busy(void)
1958{
1959 if (quickfix_busy != 0)
1960 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001961 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001962# ifdef ABORT_ON_INTERNAL_ERROR
1963 abort();
1964# endif
1965 }
1966}
1967#endif
1968
1969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 * Add an entry to the end of the list of errors.
1971 * Returns OK or FAIL.
1972 */
1973 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001974qf_add_entry(
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001975 qf_info_T *qi, // quickfix list
1976 int qf_idx, // list index
1977 char_u *dir, // optional directory name
1978 char_u *fname, // file name or NULL
1979 char_u *module, // module name or NULL
1980 int bufnum, // buffer number or zero
1981 char_u *mesg, // message
1982 long lnum, // line number
1983 int col, // column
1984 int vis_col, // using visual column
1985 char_u *pattern, // search pattern
1986 int nr, // error number
1987 int type, // type character
1988 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001990 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001991 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001992 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001994 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001996 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001997 {
1998 buf_T *buf = buflist_findnr(bufnum);
1999
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002000 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002001 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002002 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002003 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002004 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002005 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002006 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2008 {
2009 vim_free(qfp);
2010 return FAIL;
2011 }
2012 qfp->qf_lnum = lnum;
2013 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002014 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002015 if (pattern == NULL || *pattern == NUL)
2016 qfp->qf_pattern = NULL;
2017 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2018 {
2019 vim_free(qfp->qf_text);
2020 vim_free(qfp);
2021 return FAIL;
2022 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002023 if (module == NULL || *module == NUL)
2024 qfp->qf_module = NULL;
2025 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2026 {
2027 vim_free(qfp->qf_text);
2028 vim_free(qfp->qf_pattern);
2029 vim_free(qfp);
2030 return FAIL;
2031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002033 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 type = 0;
2035 qfp->qf_type = type;
2036 qfp->qf_valid = valid;
2037
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002038 lastp = &qfl->qf_last;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002039 if (qf_list_empty(qi, qf_idx)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002041 qfl->qf_start = qfp;
2042 qfl->qf_ptr = qfp;
2043 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002044 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 }
2046 else
2047 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002048 qfp->qf_prev = *lastp;
2049 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002051 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002053 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002054 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002055 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002057 qfl->qf_index = qfl->qf_count;
2058 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 }
2060
2061 return OK;
2062}
2063
2064/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002065 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002066 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002067 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002068qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002069{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002070 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002071
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002072 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002073 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002074 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002075 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002076 qi->qfl_type = qfltype;
2077 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002078 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002079}
2080
2081/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002082 * Return the location list stack for window 'wp'.
2083 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002084 */
2085 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002086ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002087{
2088 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002089 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002090 return wp->w_llist_ref;
2091
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002092 // For a non-location list window, w_llist_ref should not point to a
2093 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002094 ll_free_all(&wp->w_llist_ref);
2095
2096 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002097 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002098 return wp->w_llist;
2099}
2100
2101/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002102 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2103 */
2104 static int
2105copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2106{
2107 int i;
2108 qfline_T *from_qfp;
2109 qfline_T *prevp;
2110
2111 // copy all the location entries in this list
2112 for (i = 0, from_qfp = from_qfl->qf_start;
2113 i < from_qfl->qf_count && from_qfp != NULL;
2114 ++i, from_qfp = from_qfp->qf_next)
2115 {
2116 if (qf_add_entry(to_qi,
2117 to_qi->qf_curlist,
2118 NULL,
2119 NULL,
2120 from_qfp->qf_module,
2121 0,
2122 from_qfp->qf_text,
2123 from_qfp->qf_lnum,
2124 from_qfp->qf_col,
2125 from_qfp->qf_viscol,
2126 from_qfp->qf_pattern,
2127 from_qfp->qf_nr,
2128 0,
2129 from_qfp->qf_valid) == FAIL)
2130 return FAIL;
2131
2132 // qf_add_entry() will not set the qf_num field, as the
2133 // directory and file names are not supplied. So the qf_fnum
2134 // field is copied here.
2135 prevp = to_qfl->qf_last;
2136 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2137 prevp->qf_type = from_qfp->qf_type; // error type
2138 if (from_qfl->qf_ptr == from_qfp)
2139 to_qfl->qf_ptr = prevp; // current location
2140 }
2141
2142 return OK;
2143}
2144
2145/*
2146 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2147 */
2148 static int
2149copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2150{
2151 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002152 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002153 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2154 to_qfl->qf_count = 0;
2155 to_qfl->qf_index = 0;
2156 to_qfl->qf_start = NULL;
2157 to_qfl->qf_last = NULL;
2158 to_qfl->qf_ptr = NULL;
2159 if (from_qfl->qf_title != NULL)
2160 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2161 else
2162 to_qfl->qf_title = NULL;
2163 if (from_qfl->qf_ctx != NULL)
2164 {
2165 to_qfl->qf_ctx = alloc_tv();
2166 if (to_qfl->qf_ctx != NULL)
2167 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2168 }
2169 else
2170 to_qfl->qf_ctx = NULL;
2171
2172 if (from_qfl->qf_count)
2173 if (copy_loclist_entries(from_qfl, to_qfl, to_qi) == FAIL)
2174 return FAIL;
2175
2176 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2177
2178 // Assign a new ID for the location list
2179 to_qfl->qf_id = ++last_qf_id;
2180 to_qfl->qf_changedtick = 0L;
2181
2182 // When no valid entries are present in the list, qf_ptr points to
2183 // the first item in the list
2184 if (to_qfl->qf_nonevalid)
2185 {
2186 to_qfl->qf_ptr = to_qfl->qf_start;
2187 to_qfl->qf_index = 1;
2188 }
2189
2190 return OK;
2191}
2192
2193/*
2194 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002195 */
2196 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002197copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002198{
2199 qf_info_T *qi;
2200 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002201
Bram Moolenaar09037502018-09-25 22:08:14 +02002202 // When copying from a location list window, copy the referenced
2203 // location list. For other windows, copy the location list for
2204 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002205 if (IS_LL_WINDOW(from))
2206 qi = from->w_llist_ref;
2207 else
2208 qi = from->w_llist;
2209
Bram Moolenaar09037502018-09-25 22:08:14 +02002210 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002211 return;
2212
Bram Moolenaar09037502018-09-25 22:08:14 +02002213 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002214 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002215 return;
2216
2217 to->w_llist->qf_listcount = qi->qf_listcount;
2218
Bram Moolenaar09037502018-09-25 22:08:14 +02002219 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002220 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002221 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002222 to->w_llist->qf_curlist = idx;
2223
Bram Moolenaar09037502018-09-25 22:08:14 +02002224 if (copy_loclist(&qi->qf_lists[idx],
2225 &to->w_llist->qf_lists[idx], to->w_llist) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002226 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002227 qf_free_all(to);
2228 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002229 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002230 }
2231
Bram Moolenaar09037502018-09-25 22:08:14 +02002232 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002233}
2234
2235/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002236 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002237 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 */
2239 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002240qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002242 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar82404332016-07-10 17:00:38 +02002243 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002244 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002245 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002246
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002247 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
Bram Moolenaare60acc12011-05-10 16:41:25 +02002250#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002251 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002252#endif
2253#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002254 if (directory != NULL)
2255 slash_adjust(directory);
2256 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002257#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002258 if (directory != NULL && !vim_isAbsName(fname)
2259 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2260 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002261 // Here we check if the file really exists.
2262 // This should normally be true, but if make works without
2263 // "leaving directory"-messages we might have missed a
2264 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002265 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002268 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002269 if (directory)
2270 ptr = concat_fnames(directory, fname, TRUE);
2271 else
2272 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002274 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002275 bufname = ptr;
2276 }
2277 else
2278 bufname = fname;
2279
2280 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002281 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002282 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002283 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002284 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002286 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002287 {
2288 vim_free(qf_last_bufname);
2289 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2290 if (bufname == ptr)
2291 qf_last_bufname = bufname;
2292 else
2293 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002294 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002295 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002296 if (buf == NULL)
2297 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002298
Bram Moolenaarc1542742016-07-20 21:44:37 +02002299 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002300 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002301 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302}
2303
2304/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002305 * Push dirbuf onto the directory stack and return pointer to actual dir or
2306 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 */
2308 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002309qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
2311 struct dir_stack_T *ds_new;
2312 struct dir_stack_T *ds_ptr;
2313
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002314 // allocate new stack element and hook it in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2316 if (ds_new == NULL)
2317 return NULL;
2318
2319 ds_new->next = *stackptr;
2320 *stackptr = ds_new;
2321
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002322 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 if (vim_isAbsName(dirbuf)
2324 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002325 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 (*stackptr)->dirname = vim_strsave(dirbuf);
2327 else
2328 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002329 // Okay we don't have an absolute path.
2330 // dirbuf must be a subdir of one of the directories on the stack.
2331 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 ds_new = (*stackptr)->next;
2333 (*stackptr)->dirname = NULL;
2334 while (ds_new)
2335 {
2336 vim_free((*stackptr)->dirname);
2337 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2338 TRUE);
2339 if (mch_isdir((*stackptr)->dirname) == TRUE)
2340 break;
2341
2342 ds_new = ds_new->next;
2343 }
2344
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002345 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 while ((*stackptr)->next != ds_new)
2347 {
2348 ds_ptr = (*stackptr)->next;
2349 (*stackptr)->next = (*stackptr)->next->next;
2350 vim_free(ds_ptr->dirname);
2351 vim_free(ds_ptr);
2352 }
2353
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002354 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 if (ds_new == NULL)
2356 {
2357 vim_free((*stackptr)->dirname);
2358 (*stackptr)->dirname = vim_strsave(dirbuf);
2359 }
2360 }
2361
2362 if ((*stackptr)->dirname != NULL)
2363 return (*stackptr)->dirname;
2364 else
2365 {
2366 ds_ptr = *stackptr;
2367 *stackptr = (*stackptr)->next;
2368 vim_free(ds_ptr);
2369 return NULL;
2370 }
2371}
2372
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373/*
2374 * pop dirbuf from the directory stack and return previous directory or NULL if
2375 * stack is empty
2376 */
2377 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002378qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379{
2380 struct dir_stack_T *ds_ptr;
2381
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002382 // TODO: Should we check if dirbuf is the directory on top of the stack?
2383 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002385 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 if (*stackptr != NULL)
2387 {
2388 ds_ptr = *stackptr;
2389 *stackptr = (*stackptr)->next;
2390 vim_free(ds_ptr->dirname);
2391 vim_free(ds_ptr);
2392 }
2393
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002394 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 return *stackptr ? (*stackptr)->dirname : NULL;
2396}
2397
2398/*
2399 * clean up directory stack
2400 */
2401 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002402qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403{
2404 struct dir_stack_T *ds_ptr;
2405
2406 while ((ds_ptr = *stackptr) != NULL)
2407 {
2408 *stackptr = (*stackptr)->next;
2409 vim_free(ds_ptr->dirname);
2410 vim_free(ds_ptr);
2411 }
2412}
2413
2414/*
2415 * Check in which directory of the directory stack the given file can be
2416 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002417 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 * Cleans up intermediate directory entries.
2419 *
2420 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002421 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 * ./
2423 * ./aa
2424 * ./aa/bb
2425 * ./bb
2426 * ./bb/x.c
2427 * and make says:
2428 * making all in aa
2429 * making all in bb
2430 * x.c:9: Error
2431 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2432 * qf_guess_filepath will return NULL.
2433 */
2434 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002435qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436{
2437 struct dir_stack_T *ds_ptr;
2438 struct dir_stack_T *ds_tmp;
2439 char_u *fullname;
2440
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002441 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002442 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 return NULL;
2444
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002445 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 fullname = NULL;
2447 while (ds_ptr)
2448 {
2449 vim_free(fullname);
2450 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2451
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002452 // If concat_fnames failed, just go on. The worst thing that can happen
2453 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2455 break;
2456
2457 ds_ptr = ds_ptr->next;
2458 }
2459
2460 vim_free(fullname);
2461
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002462 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002463 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002465 ds_tmp = qfl->qf_dir_stack->next;
2466 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 vim_free(ds_tmp->dirname);
2468 vim_free(ds_tmp);
2469 }
2470
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002471 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472}
2473
2474/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002475 * Returns TRUE if a quickfix/location list with the given identifier exists.
2476 */
2477 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002478qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002479{
2480 qf_info_T *qi = &ql_info;
2481 int i;
2482
2483 if (wp != NULL)
2484 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002485 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002486 if (qi == NULL)
2487 return FALSE;
2488 }
2489
2490 for (i = 0; i < qi->qf_listcount; ++i)
2491 if (qi->qf_lists[i].qf_id == qf_id)
2492 return TRUE;
2493
2494 return FALSE;
2495}
2496
2497/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002498 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002499 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002500 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002501 * Similar to location list.
2502 */
2503 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002504is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002505{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002506 qfline_T *qfp;
2507 int i;
2508
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002509 // Search for the entry in the current list
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002510 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2511 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002512 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002513 break;
2514
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002515 if (i == qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002516 return FALSE;
2517
2518 return TRUE;
2519}
2520
2521/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002522 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002523 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002524 */
2525 static qfline_T *
2526get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002527 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002528 qfline_T *qf_ptr,
2529 int *qf_index,
2530 int dir)
2531{
2532 int idx;
2533 int old_qf_fnum;
2534
2535 idx = *qf_index;
2536 old_qf_fnum = qf_ptr->qf_fnum;
2537
2538 do
2539 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002540 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002541 return NULL;
2542 ++idx;
2543 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002544 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002545 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2546
2547 *qf_index = idx;
2548 return qf_ptr;
2549}
2550
2551/*
2552 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002553 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002554 */
2555 static qfline_T *
2556get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002557 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002558 qfline_T *qf_ptr,
2559 int *qf_index,
2560 int dir)
2561{
2562 int idx;
2563 int old_qf_fnum;
2564
2565 idx = *qf_index;
2566 old_qf_fnum = qf_ptr->qf_fnum;
2567
2568 do
2569 {
2570 if (idx == 1 || qf_ptr->qf_prev == NULL)
2571 return NULL;
2572 --idx;
2573 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002574 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002575 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2576
2577 *qf_index = idx;
2578 return qf_ptr;
2579}
2580
2581/*
2582 * Get the n'th (errornr) previous/next valid entry from the current entry in
2583 * the quickfix list.
2584 * dir == FORWARD or FORWARD_FILE: next valid entry
2585 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2586 */
2587 static qfline_T *
2588get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002589 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002590 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002591 int dir,
2592 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002593{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002594 qfline_T *qf_ptr = qfl->qf_ptr;
2595 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002596 qfline_T *prev_qf_ptr;
2597 int prev_index;
2598 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2599 char_u *err = e_no_more_items;
2600
2601 while (errornr--)
2602 {
2603 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002604 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002605
2606 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002607 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002608 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002609 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002610 if (qf_ptr == NULL)
2611 {
2612 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002613 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002614 if (err != NULL)
2615 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002616 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002617 return NULL;
2618 }
2619 break;
2620 }
2621
2622 err = NULL;
2623 }
2624
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002625 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002626 return qf_ptr;
2627}
2628
2629/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002630 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2631 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002632 */
2633 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002634get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002635{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002636 qfline_T *qf_ptr = qfl->qf_ptr;
2637 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002638
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002639 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002640 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2641 {
2642 --qf_idx;
2643 qf_ptr = qf_ptr->qf_prev;
2644 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002645 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002646 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2647 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002648 {
2649 ++qf_idx;
2650 qf_ptr = qf_ptr->qf_next;
2651 }
2652
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002653 *new_qfidx = qf_idx;
2654 return qf_ptr;
2655}
2656
2657/*
2658 * Get a entry specied by 'errornr' and 'dir' from the current
2659 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2660 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2661 * Returns a pointer to the entry and the index of the new entry is stored in
2662 * 'new_qfidx'.
2663 */
2664 static qfline_T *
2665qf_get_entry(
2666 qf_list_T *qfl,
2667 int errornr,
2668 int dir,
2669 int *new_qfidx)
2670{
2671 qfline_T *qf_ptr = qfl->qf_ptr;
2672 int qfidx = qfl->qf_index;
2673
2674 if (dir != 0) // next/prev valid entry
2675 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2676 else if (errornr != 0) // go to specified number
2677 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2678
2679 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002680 return qf_ptr;
2681}
2682
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002683/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002684 * Find a window displaying a Vim help file.
2685 */
2686 static win_T *
2687qf_find_help_win(void)
2688{
2689 win_T *wp;
2690
2691 FOR_ALL_WINDOWS(wp)
2692 if (bt_help(wp->w_buffer))
2693 return wp;
2694
2695 return NULL;
2696}
2697
2698/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002699 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2700 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002701 */
2702 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002703jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002704{
2705 win_T *wp;
2706 int flags;
2707
Bram Moolenaarb2443732018-11-11 22:50:27 +01002708 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002709 wp = NULL;
2710 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002711 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002712 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2713 win_enter(wp, TRUE);
2714 else
2715 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002716 // Split off help window; put it at far top if no position
2717 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002718 flags = WSP_HELP;
2719 if (cmdmod.split == 0 && curwin->w_width != Columns
2720 && curwin->w_width < 80)
2721 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002722 // If the user asks to open a new window, then copy the location list.
2723 // Otherwise, don't copy the location list.
2724 if (IS_LL_STACK(qi) && !newwin)
2725 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002726
2727 if (win_split(0, flags) == FAIL)
2728 return FAIL;
2729
2730 *opened_window = TRUE;
2731
2732 if (curwin->w_height < p_hh)
2733 win_setheight((int)p_hh);
2734
Bram Moolenaarb2443732018-11-11 22:50:27 +01002735 // When using location list, the new window should use the supplied
2736 // location list. If the user asks to open a new window, then the new
2737 // window will get a copy of the location list.
2738 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002739 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002740 curwin->w_llist = qi;
2741 qi->qf_refcount++;
2742 }
2743 }
2744
2745 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002746 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002747
2748 return OK;
2749}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002750
2751/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002752 * Find a non-quickfix window using the given location list.
2753 * Returns NULL if a matching window is not found.
2754 */
2755 static win_T *
2756qf_find_win_with_loclist(qf_info_T *ll)
2757{
2758 win_T *wp;
2759
2760 FOR_ALL_WINDOWS(wp)
2761 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2762 return wp;
2763
2764 return NULL;
2765}
2766
2767/*
2768 * Find a window containing a normal buffer
2769 */
2770 static win_T *
2771qf_find_win_with_normal_buf(void)
2772{
2773 win_T *wp;
2774
2775 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002776 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002777 return wp;
2778
2779 return NULL;
2780}
2781
2782/*
2783 * Go to a window in any tabpage containing the specified file. Returns TRUE
2784 * if successfully jumped to the window. Otherwise returns FALSE.
2785 */
2786 static int
2787qf_goto_tabwin_with_file(int fnum)
2788{
2789 tabpage_T *tp;
2790 win_T *wp;
2791
2792 FOR_ALL_TAB_WINDOWS(tp, wp)
2793 if (wp->w_buffer->b_fnum == fnum)
2794 {
2795 goto_tabpage_win(tp, wp);
2796 return TRUE;
2797 }
2798
2799 return FALSE;
2800}
2801
2802/*
2803 * Create a new window to show a file above the quickfix window. Called when
2804 * only the quickfix window is present.
2805 */
2806 static int
2807qf_open_new_file_win(qf_info_T *ll_ref)
2808{
2809 int flags;
2810
2811 flags = WSP_ABOVE;
2812 if (ll_ref != NULL)
2813 flags |= WSP_NEWLOC;
2814 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002815 return FAIL; // not enough room for window
2816 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002817 swb_flags = 0;
2818 RESET_BINDING(curwin);
2819 if (ll_ref != NULL)
2820 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002821 // The new window should use the location list from the
2822 // location list window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002823 curwin->w_llist = ll_ref;
2824 ll_ref->qf_refcount++;
2825 }
2826 return OK;
2827}
2828
2829/*
2830 * Go to a window that shows the right buffer. If the window is not found, go
2831 * to the window just above the location list window. This is used for opening
2832 * a file from a location window and not from a quickfix window. If some usable
2833 * window is previously found, then it is supplied in 'use_win'.
2834 */
2835 static void
2836qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2837{
2838 win_T *win = use_win;
2839
2840 if (win == NULL)
2841 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002842 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002843 FOR_ALL_WINDOWS(win)
2844 if (win->w_buffer->b_fnum == qf_fnum)
2845 break;
2846 if (win == NULL)
2847 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002848 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002849 win = curwin;
2850 do
2851 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002852 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002853 break;
2854 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002855 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002856 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002857 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002858 } while (win != curwin);
2859 }
2860 }
2861 win_goto(win);
2862
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002863 // If the location list for the window is not set, then set it
2864 // to the location list from the location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002865 if (win->w_llist == NULL)
2866 {
2867 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002868 if (ll_ref != NULL)
2869 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002870 }
2871}
2872
2873/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002874 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2875 * not found, then go to the window just above the quickfix window. This is
2876 * used for opening a file from a quickfix window and not from a location
2877 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002878 */
2879 static void
2880qf_goto_win_with_qfl_file(int qf_fnum)
2881{
2882 win_T *win;
2883 win_T *altwin;
2884
2885 win = curwin;
2886 altwin = NULL;
2887 for (;;)
2888 {
2889 if (win->w_buffer->b_fnum == qf_fnum)
2890 break;
2891 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002892 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002893 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002894 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002895
2896 if (IS_QF_WINDOW(win))
2897 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002898 // Didn't find it, go to the window before the quickfix
2899 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002900 if (altwin != NULL)
2901 win = altwin;
2902 else if (curwin->w_prev != NULL)
2903 win = curwin->w_prev;
2904 else
2905 win = curwin->w_next;
2906 break;
2907 }
2908
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002909 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02002910 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002911 altwin = win;
2912 }
2913
2914 win_goto(win);
2915}
2916
2917/*
2918 * Find a suitable window for opening a file (qf_fnum) from the
2919 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01002920 * window, jump to it. Otherwise open a new window to display the file. If
2921 * 'newwin' is TRUE, then always open a new window. This is called from either
2922 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002923 */
2924 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002925qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002926{
2927 win_T *usable_win_ptr = NULL;
2928 int usable_win;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002929 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002930 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002931
2932 usable_win = 0;
2933
Bram Moolenaarb2443732018-11-11 22:50:27 +01002934 // If opening a new window, then don't use the location list referred by
2935 // the current window. Otherwise two windows will refer to the same
2936 // location list.
2937 if (!newwin)
2938 ll_ref = curwin->w_llist_ref;
2939
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002940 if (ll_ref != NULL)
2941 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002942 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002943 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2944 if (usable_win_ptr != NULL)
2945 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002946 }
2947
2948 if (!usable_win)
2949 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002950 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002951 win = qf_find_win_with_normal_buf();
2952 if (win != NULL)
2953 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002954 }
2955
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002956 // If no usable window is found and 'switchbuf' contains "usetab"
2957 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002958 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002959 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002960
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002961 // If there is only one window and it is the quickfix window, create a
2962 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01002963 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002964 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002965 if (qf_open_new_file_win(ll_ref) != OK)
2966 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002967 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002968 }
2969 else
2970 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002971 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002972 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002973 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002974 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002975 }
2976
2977 return OK;
2978}
2979
2980/*
2981 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002982 * Returns OK if successfully edited the file, FAIL on failing to open the
2983 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
2984 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002985 */
2986 static int
2987qf_jump_edit_buffer(
2988 qf_info_T *qi,
2989 qfline_T *qf_ptr,
2990 int forceit,
2991 win_T *oldwin,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002992 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002993{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002994 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002995 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002996 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02002997 int old_qf_curlist = qi->qf_curlist;
2998 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002999
3000 if (qf_ptr->qf_type == 1)
3001 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003002 // Open help file (do_ecmd() will set b_help flag, readfile() will
3003 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003004 if (!can_abandon(curbuf, forceit))
3005 {
3006 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003007 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003008 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003009
3010 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3011 ECMD_HIDE + ECMD_SET_HELP,
3012 oldwin == curwin ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003013 }
3014 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003015 retval = buflist_getfile(qf_ptr->qf_fnum,
3016 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003017
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003018 // If a location list, check whether the associated window is still
3019 // present.
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003020 if (qfl_type == QFLT_LOCATION && !win_valid_any_tab(oldwin))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003022 emsg(_("E924: Current window was closed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003023 *opened_window = FALSE;
3024 return NOTDONE;
3025 }
3026
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003027 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003028 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003029 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003030 return NOTDONE;
3031 }
3032
3033 if (old_qf_curlist != qi->qf_curlist
3034 || !is_qf_entry_present(qfl, qf_ptr))
3035 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003036 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003037 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003038 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003039 emsg(_(e_loc_list_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003040 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003041 }
3042
3043 return retval;
3044}
3045
3046/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003047 * Go to the error line in the current file using either line/column number or
3048 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003049 */
3050 static void
3051qf_jump_goto_line(
3052 linenr_T qf_lnum,
3053 int qf_col,
3054 char_u qf_viscol,
3055 char_u *qf_pattern)
3056{
3057 linenr_T i;
3058 char_u *line;
3059 colnr_T screen_col;
3060 colnr_T char_col;
3061
3062 if (qf_pattern == NULL)
3063 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003064 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003065 i = qf_lnum;
3066 if (i > 0)
3067 {
3068 if (i > curbuf->b_ml.ml_line_count)
3069 i = curbuf->b_ml.ml_line_count;
3070 curwin->w_cursor.lnum = i;
3071 }
3072 if (qf_col > 0)
3073 {
3074 curwin->w_cursor.col = qf_col - 1;
3075#ifdef FEAT_VIRTUALEDIT
3076 curwin->w_cursor.coladd = 0;
3077#endif
3078 if (qf_viscol == TRUE)
3079 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003080 // Check each character from the beginning of the error
3081 // line up to the error column. For each tab character
3082 // found, reduce the error column value by the length of
3083 // a tab character.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003084 line = ml_get_curline();
3085 screen_col = 0;
3086 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3087 {
3088 if (*line == NUL)
3089 break;
3090 if (*line++ == '\t')
3091 {
3092 curwin->w_cursor.col -= 7 - (screen_col % 8);
3093 screen_col += 8 - (screen_col % 8);
3094 }
3095 else
3096 ++screen_col;
3097 }
3098 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003099 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003100 check_cursor();
3101 }
3102 else
3103 beginline(BL_WHITE | BL_FIX);
3104 }
3105 else
3106 {
3107 pos_T save_cursor;
3108
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003109 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003110 save_cursor = curwin->w_cursor;
3111 curwin->w_cursor.lnum = 0;
3112 if (!do_search(NULL, '/', qf_pattern, (long)1,
3113 SEARCH_KEEP, NULL, NULL))
3114 curwin->w_cursor = save_cursor;
3115 }
3116}
3117
3118/*
3119 * Display quickfix list index and size message
3120 */
3121 static void
3122qf_jump_print_msg(
3123 qf_info_T *qi,
3124 int qf_index,
3125 qfline_T *qf_ptr,
3126 buf_T *old_curbuf,
3127 linenr_T old_lnum)
3128{
3129 linenr_T i;
3130 int len;
3131
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003132 // Update the screen before showing the message, unless the screen
3133 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003134 if (!msg_scrolled)
3135 update_topline_redraw();
3136 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3137 qi->qf_lists[qi->qf_curlist].qf_count,
3138 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3139 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003140 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003141 len = (int)STRLEN(IObuff);
3142 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3143
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003144 // Output the message. Overwrite to avoid scrolling when the 'O'
3145 // flag is present in 'shortmess'; But when not jumping, print the
3146 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003147 i = msg_scroll;
3148 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3149 msg_scroll = TRUE;
3150 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3151 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003152 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003153 msg_scroll = i;
3154}
3155
3156/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003157 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003158 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3159 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003160 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3161 * able to jump/open a window. Returns NOTDONE if a file is not associated
3162 * with the entry.
3163 */
3164 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003165qf_jump_open_window(
3166 qf_info_T *qi,
3167 qfline_T *qf_ptr,
3168 int newwin,
3169 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003170{
3171 // For ":helpgrep" find a help window or open one.
3172 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003173 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003174 return FAIL;
3175
3176 // If currently in the quickfix window, find another window to show the
3177 // file in.
3178 if (bt_quickfix(curbuf) && !*opened_window)
3179 {
3180 // If there is no file specified, we don't know where to go.
3181 // But do advance, otherwise ":cn" gets stuck.
3182 if (qf_ptr->qf_fnum == 0)
3183 return NOTDONE;
3184
Bram Moolenaarb2443732018-11-11 22:50:27 +01003185 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3186 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003187 return FAIL;
3188 }
3189
3190 return OK;
3191}
3192
3193/*
3194 * Edit a selected file from the quickfix/location list and jump to a
3195 * particular line/column, adjust the folds and display a message about the
3196 * jump.
3197 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3198 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3199 * the file.
3200 */
3201 static int
3202qf_jump_to_buffer(
3203 qf_info_T *qi,
3204 int qf_index,
3205 qfline_T *qf_ptr,
3206 int forceit,
3207 win_T *oldwin,
3208 int *opened_window,
3209 int openfold,
3210 int print_message)
3211{
3212 buf_T *old_curbuf;
3213 linenr_T old_lnum;
3214 int retval = OK;
3215
3216 // If there is a file name, read the wanted file if needed, and check
3217 // autowrite etc.
3218 old_curbuf = curbuf;
3219 old_lnum = curwin->w_cursor.lnum;
3220
3221 if (qf_ptr->qf_fnum != 0)
3222 {
3223 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3224 opened_window);
3225 if (retval != OK)
3226 return retval;
3227 }
3228
3229 // When not switched to another buffer, still need to set pc mark
3230 if (curbuf == old_curbuf)
3231 setpcmark();
3232
3233 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3234 qf_ptr->qf_pattern);
3235
3236#ifdef FEAT_FOLDING
3237 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3238 foldOpenCursor();
3239#endif
3240 if (print_message)
3241 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3242
3243 return retval;
3244}
3245
3246/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003247 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 */
3249 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003250qf_jump(qf_info_T *qi,
3251 int dir,
3252 int errornr,
3253 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003255 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3256}
3257
3258/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003259 * Jump to a quickfix line.
3260 * If dir == 0 go to entry "errornr".
3261 * If dir == FORWARD go "errornr" valid entries forward.
3262 * If dir == BACKWARD go "errornr" valid entries backward.
3263 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3264 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3265 * else if "errornr" is zero, redisplay the same line
3266 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003267 * If 'newwin' is TRUE, then open the file in a new window.
3268 */
3269 void
3270qf_jump_newwin(qf_info_T *qi,
3271 int dir,
3272 int errornr,
3273 int forceit,
3274 int newwin)
3275{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003276 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003277 qfline_T *qf_ptr;
3278 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003281 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003282 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003284 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003287 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003289 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003291 if (qi == NULL)
3292 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003293
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003294 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003296 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 return;
3298 }
3299
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003300 qfl = &qi->qf_lists[qi->qf_curlist];
3301
3302 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003304 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003306
3307 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3308 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003310 qf_ptr = old_qf_ptr;
3311 qf_index = old_qf_index;
3312 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003315 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003316 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003317 // No need to print the error message if it's visible in the error
3318 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 print_message = FALSE;
3320
Bram Moolenaarb2443732018-11-11 22:50:27 +01003321 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003322 if (retval == FAIL)
3323 goto failed;
3324 if (retval == NOTDONE)
3325 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003326
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003327 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, oldwin,
3328 &opened_window, old_KeyTyped, print_message);
3329 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003331 // Quickfix/location list is freed by an autocmd
3332 qi = NULL;
3333 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003336 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003339 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003340 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003342 // Couldn't open file, so put index back where it was. This could
3343 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 qf_ptr = old_qf_ptr;
3346 qf_index = old_qf_index;
3347 }
3348 }
3349theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003350 if (qi != NULL)
3351 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003352 qfl->qf_ptr = qf_ptr;
3353 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (p_swb != old_swb && opened_window)
3356 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003357 // Restore old 'switchbuf' value, but not when an autocommand or
3358 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003362 swb_flags = old_swb_flags;
3363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 else
3365 free_string_option(old_swb);
3366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367}
3368
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003369// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003370static int qfFileAttr;
3371static int qfSepAttr;
3372static int qfLineAttr;
3373
3374/*
3375 * Display information about a single entry from the quickfix/location list.
3376 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003377 * 'cursel' will be set to TRUE for the currently selected entry in the
3378 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003379 */
3380 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003381qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003382{
3383 char_u *fname;
3384 buf_T *buf;
3385 int filter_entry;
3386
3387 fname = NULL;
3388 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3389 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3390 (char *)qfp->qf_module);
3391 else {
3392 if (qfp->qf_fnum != 0
3393 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3394 {
3395 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003396 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003397 fname = gettail(fname);
3398 }
3399 if (fname == NULL)
3400 sprintf((char *)IObuff, "%2d", qf_idx);
3401 else
3402 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3403 qf_idx, (char *)fname);
3404 }
3405
3406 // Support for filtering entries using :filter /pat/ clist
3407 // Match against the module name, file name, search pattern and
3408 // text of the entry.
3409 filter_entry = TRUE;
3410 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3411 filter_entry &= message_filtered(qfp->qf_module);
3412 if (filter_entry && fname != NULL)
3413 filter_entry &= message_filtered(fname);
3414 if (filter_entry && qfp->qf_pattern != NULL)
3415 filter_entry &= message_filtered(qfp->qf_pattern);
3416 if (filter_entry)
3417 filter_entry &= message_filtered(qfp->qf_text);
3418 if (filter_entry)
3419 return;
3420
3421 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003422 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003423
3424 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003425 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003426 if (qfp->qf_lnum == 0)
3427 IObuff[0] = NUL;
3428 else if (qfp->qf_col == 0)
3429 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3430 else
3431 sprintf((char *)IObuff, "%ld col %d",
3432 qfp->qf_lnum, qfp->qf_col);
3433 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3434 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003435 msg_puts_attr((char *)IObuff, qfLineAttr);
3436 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003437 if (qfp->qf_pattern != NULL)
3438 {
3439 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003440 msg_puts((char *)IObuff);
3441 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003442 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003443 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003444
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003445 // Remove newlines and leading whitespace from the text. For an
3446 // unrecognized line keep the indent, the compiler may mark a word
3447 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003448 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3449 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3450 IObuff, IOSIZE);
3451 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003452 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003453}
3454
3455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003457 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 */
3459 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003460qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003462 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003463 qfline_T *qfp;
3464 int i;
3465 int idx1 = 1;
3466 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003467 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003468 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003469 int all = eap->forceit; // if not :cl!, only show
3470 // recognised errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003471 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
Bram Moolenaar39665952018-08-15 20:59:48 +02003473 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003474 {
3475 qi = GET_LOC_LIST(curwin);
3476 if (qi == NULL)
3477 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003478 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003479 return;
3480 }
3481 }
3482
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003483 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003485 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 return;
3487 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003488 if (*arg == '+')
3489 {
3490 ++arg;
3491 plus = TRUE;
3492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003495 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 return;
3497 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003498 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003499 if (plus)
3500 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003501 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003502 idx2 = i + idx1;
3503 idx1 = i;
3504 }
3505 else
3506 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003507 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003508 if (idx1 < 0)
3509 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3510 if (idx2 < 0)
3511 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003514 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003515 shorten_fnames(FALSE);
3516
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003517 // Get the attributes for the different quickfix highlight items. Note
3518 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003519 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3520 if (qfFileAttr == 0)
3521 qfFileAttr = HL_ATTR(HLF_D);
3522 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3523 if (qfSepAttr == 0)
3524 qfSepAttr = HL_ATTR(HLF_D);
3525 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3526 if (qfLineAttr == 0)
3527 qfLineAttr = HL_ATTR(HLF_N);
3528
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003529 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003531 qfp = qfl->qf_start;
3532 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 {
3534 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3535 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003536 if (got_int)
3537 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003538
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003539 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003541
3542 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003543 if (qfp == NULL)
3544 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003545 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 ui_breakcheck();
3547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548}
3549
3550/*
3551 * Remove newlines and leading whitespace from an error message.
3552 * Put the result in "buf[bufsize]".
3553 */
3554 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003555qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556{
3557 int i;
3558 char_u *p = text;
3559
3560 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3561 {
3562 if (*p == '\n')
3563 {
3564 buf[i] = ' ';
3565 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003566 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 break;
3568 }
3569 else
3570 buf[i] = *p++;
3571 }
3572 buf[i] = NUL;
3573}
3574
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003575/*
3576 * Display information (list number, list size and the title) about a
3577 * quickfix/location list.
3578 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003579 static void
3580qf_msg(qf_info_T *qi, int which, char *lead)
3581{
3582 char *title = (char *)qi->qf_lists[which].qf_title;
3583 int count = qi->qf_lists[which].qf_count;
3584 char_u buf[IOSIZE];
3585
3586 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3587 lead,
3588 which + 1,
3589 qi->qf_listcount,
3590 count);
3591
3592 if (title != NULL)
3593 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003594 size_t len = STRLEN(buf);
3595
3596 if (len < 34)
3597 {
3598 vim_memset(buf + len, ' ', 34 - len);
3599 buf[34] = NUL;
3600 }
3601 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003602 }
3603 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003604 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003605}
3606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607/*
3608 * ":colder [count]": Up in the quickfix stack.
3609 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003610 * ":lolder [count]": Up in the location list stack.
3611 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 */
3613 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003614qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003616 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 int count;
3618
Bram Moolenaar39665952018-08-15 20:59:48 +02003619 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003620 {
3621 qi = GET_LOC_LIST(curwin);
3622 if (qi == NULL)
3623 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003624 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003625 return;
3626 }
3627 }
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (eap->addr_count != 0)
3630 count = eap->line2;
3631 else
3632 count = 1;
3633 while (count--)
3634 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003635 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003637 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003639 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003640 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003642 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644 else
3645 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003646 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003648 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003649 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003651 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 }
3653 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003654 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003655 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656}
3657
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003658/*
3659 * Display the information about all the quickfix/location lists in the stack
3660 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003661 void
3662qf_history(exarg_T *eap)
3663{
3664 qf_info_T *qi = &ql_info;
3665 int i;
3666
Bram Moolenaar39665952018-08-15 20:59:48 +02003667 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003668 qi = GET_LOC_LIST(curwin);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003669 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003670 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003671 else
3672 for (i = 0; i < qi->qf_listcount; ++i)
3673 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3674}
3675
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003677 * Free all the entries in the error list "idx". Note that other information
3678 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 */
3680 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003681qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003683 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003684 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003685 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003687 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003689 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003690 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003691 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003692 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003693 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003694 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003695 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003696 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003697 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003698 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003699 // Somehow qf_count may have an incorrect value, set it to 1
3700 // to avoid crashing when it's wrong.
3701 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003702 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003703 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003704 qfl->qf_start = qfpnext;
3705 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003707
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003708 qfl->qf_index = 0;
3709 qfl->qf_start = NULL;
3710 qfl->qf_last = NULL;
3711 qfl->qf_ptr = NULL;
3712 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003713
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003714 qf_clean_dir_stack(&qfl->qf_dir_stack);
3715 qfl->qf_directory = NULL;
3716 qf_clean_dir_stack(&qfl->qf_file_stack);
3717 qfl->qf_currfile = NULL;
3718 qfl->qf_multiline = FALSE;
3719 qfl->qf_multiignore = FALSE;
3720 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721}
3722
3723/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003724 * Free error list "idx". Frees all the entries in the quickfix list,
3725 * associated context information and the title.
3726 */
3727 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003728qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003729{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003730 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003731
Bram Moolenaard23a8232018-02-10 18:45:26 +01003732 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003733 free_tv(qfl->qf_ctx);
3734 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003735 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003736 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003737}
3738
3739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 * qf_mark_adjust: adjust marks
3741 */
3742 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003743qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003744 win_T *wp,
3745 linenr_T line1,
3746 linenr_T line2,
3747 long amount,
3748 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003750 int i;
3751 qfline_T *qfp;
3752 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003753 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003754 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003755 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756
Bram Moolenaarc1542742016-07-20 21:44:37 +02003757 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003758 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003759 if (wp != NULL)
3760 {
3761 if (wp->w_llist == NULL)
3762 return;
3763 qi = wp->w_llist;
3764 }
3765
3766 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003767 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003768 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003769 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3770 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 if (qfp->qf_fnum == curbuf->b_fnum)
3772 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003773 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3775 {
3776 if (amount == MAXLNUM)
3777 qfp->qf_cleared = TRUE;
3778 else
3779 qfp->qf_lnum += amount;
3780 }
3781 else if (amount_after && qfp->qf_lnum > line2)
3782 qfp->qf_lnum += amount_after;
3783 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003784
3785 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003786 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787}
3788
3789/*
3790 * Make a nice message out of the error character and the error number:
3791 * char number message
3792 * e or E 0 " error"
3793 * w or W 0 " warning"
3794 * i or I 0 " info"
3795 * 0 0 ""
3796 * other 0 " c"
3797 * e or E n " error n"
3798 * w or W n " warning n"
3799 * i or I n " info n"
3800 * 0 n " error n"
3801 * other n " c n"
3802 * 1 x "" :helpgrep
3803 */
3804 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003805qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
3807 static char_u buf[20];
3808 static char_u cc[3];
3809 char_u *p;
3810
3811 if (c == 'W' || c == 'w')
3812 p = (char_u *)" warning";
3813 else if (c == 'I' || c == 'i')
3814 p = (char_u *)" info";
3815 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3816 p = (char_u *)" error";
3817 else if (c == 0 || c == 1)
3818 p = (char_u *)"";
3819 else
3820 {
3821 cc[0] = ' ';
3822 cc[1] = c;
3823 cc[2] = NUL;
3824 p = cc;
3825 }
3826
3827 if (nr <= 0)
3828 return p;
3829
3830 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3831 return buf;
3832}
3833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003835 * When "split" is FALSE: Open the entry/result under the cursor.
3836 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3837 */
3838 void
3839qf_view_result(int split)
3840{
3841 qf_info_T *qi = &ql_info;
3842
3843 if (!bt_quickfix(curbuf))
3844 return;
3845
3846 if (IS_LL_WINDOW(curwin))
3847 qi = GET_LOC_LIST(curwin);
3848
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003849 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003850 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003851 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003852 return;
3853 }
3854
3855 if (split)
3856 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003857 // Open the selected entry in a new window
3858 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3859 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003860 return;
3861 }
3862
3863 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3864}
3865
3866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 * ":cwindow": open the quickfix window if we have errors to display,
3868 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003869 * ":lwindow": open the location list window if we have locations to display,
3870 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 */
3872 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003873ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003875 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003876 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 win_T *win;
3878
Bram Moolenaar39665952018-08-15 20:59:48 +02003879 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003880 {
3881 qi = GET_LOC_LIST(curwin);
3882 if (qi == NULL)
3883 return;
3884 }
3885
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003886 qfl = &qi->qf_lists[qi->qf_curlist];
3887
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003888 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003889 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003891 // If a quickfix window is open but we have no errors to display,
3892 // close the window. If a quickfix window is not open, then open
3893 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003894 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003895 || qfl->qf_nonevalid
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003896 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 {
3898 if (win != NULL)
3899 ex_cclose(eap);
3900 }
3901 else if (win == NULL)
3902 ex_copen(eap);
3903}
3904
3905/*
3906 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003907 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003910ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003912 win_T *win = NULL;
3913 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914
Bram Moolenaar39665952018-08-15 20:59:48 +02003915 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003916 {
3917 qi = GET_LOC_LIST(curwin);
3918 if (qi == NULL)
3919 return;
3920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003922 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003923 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if (win != NULL)
3925 win_close(win, FALSE);
3926}
3927
3928/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003929 * Set "w:quickfix_title" if "qi" has a title.
3930 */
3931 static void
3932qf_set_title_var(qf_list_T *qfl)
3933{
3934 if (qfl->qf_title != NULL)
3935 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3936}
3937
3938/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003939 * Goto a quickfix or location list window (if present).
3940 * Returns OK if the window is found, FAIL otherwise.
3941 */
3942 static int
3943qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3944{
3945 win_T *win;
3946
3947 win = qf_find_win(qi);
3948 if (win == NULL)
3949 return FAIL;
3950
3951 win_goto(win);
3952 if (resize)
3953 {
3954 if (vertsplit)
3955 {
3956 if (sz != win->w_width)
3957 win_setwidth(sz);
3958 }
3959 else if (sz != win->w_height)
3960 win_setheight(sz);
3961 }
3962
3963 return OK;
3964}
3965
3966/*
3967 * Open a new quickfix or location list window, load the quickfix buffer and
3968 * set the appropriate options for the window.
3969 * Returns FAIL if the window could not be opened.
3970 */
3971 static int
3972qf_open_new_cwindow(qf_info_T *qi, int height)
3973{
3974 buf_T *qf_buf;
3975 win_T *oldwin = curwin;
3976 tabpage_T *prevtab = curtab;
3977 int flags = 0;
3978 win_T *win;
3979
3980 qf_buf = qf_find_buf(qi);
3981
3982 // The current window becomes the previous window afterwards.
3983 win = curwin;
3984
3985 if (IS_QF_STACK(qi) && cmdmod.split == 0)
3986 // Create the new quickfix window at the very bottom, except when
3987 // :belowright or :aboveleft is used.
3988 win_goto(lastwin);
3989 // Default is to open the window below the current window
3990 if (cmdmod.split == 0)
3991 flags = WSP_BELOW;
3992 flags |= WSP_NEWLOC;
3993 if (win_split(height, flags) == FAIL)
3994 return FAIL; // not enough room for window
3995 RESET_BINDING(curwin);
3996
3997 if (IS_LL_STACK(qi))
3998 {
3999 // For the location list window, create a reference to the
4000 // location list from the window 'win'.
4001 curwin->w_llist_ref = win->w_llist;
4002 win->w_llist->qf_refcount++;
4003 }
4004
4005 if (oldwin != curwin)
4006 oldwin = NULL; // don't store info when in another window
4007 if (qf_buf != NULL)
4008 {
4009 // Use the existing quickfix buffer
4010 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4011 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4012 }
4013 else
4014 {
4015 // Create a new quickfix buffer
4016 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4017
4018 // switch off 'swapfile'
4019 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4020 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4021 OPT_LOCAL);
4022 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
4023 RESET_BINDING(curwin);
4024#ifdef FEAT_DIFF
4025 curwin->w_p_diff = FALSE;
4026#endif
4027#ifdef FEAT_FOLDING
4028 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4029 OPT_LOCAL);
4030#endif
4031 }
4032
4033 // Only set the height when still in the same tab page and there is no
4034 // window to the side.
4035 if (curtab == prevtab && curwin->w_width == Columns)
4036 win_setheight(height);
4037 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4038 if (win_valid(win))
4039 prevwin = win;
4040
4041 return OK;
4042}
4043
4044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004046 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 */
4048 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004049ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004051 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004052 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004054 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004055 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
Bram Moolenaar39665952018-08-15 20:59:48 +02004057 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004058 {
4059 qi = GET_LOC_LIST(curwin);
4060 if (qi == NULL)
4061 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004062 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004063 return;
4064 }
4065 }
4066
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004067 incr_quickfix_busy();
4068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 if (eap->addr_count != 0)
4070 height = eap->line2;
4071 else
4072 height = QF_WINHEIGHT;
4073
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004074 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075#ifdef FEAT_GUI
4076 need_mouse_correct = TRUE;
4077#endif
4078
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004079 // Find an existing quickfix window, or open a new one.
4080 if (cmdmod.tab == 0)
4081 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4082 cmdmod.split & WSP_VERT);
4083 if (status == FAIL)
4084 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004085 {
4086 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004087 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004090 qfl = &qi->qf_lists[qi->qf_curlist];
4091 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004092 // Save the current index here, as updating the quickfix buffer may free
4093 // the quickfix list
4094 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004095
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004096 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004097 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004099 decr_quickfix_busy();
4100
4101 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 curwin->w_cursor.col = 0;
4103 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004104 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105}
4106
4107/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004108 * Move the cursor in the quickfix window to "lnum".
4109 */
4110 static void
4111qf_win_goto(win_T *win, linenr_T lnum)
4112{
4113 win_T *old_curwin = curwin;
4114
4115 curwin = win;
4116 curbuf = win->w_buffer;
4117 curwin->w_cursor.lnum = lnum;
4118 curwin->w_cursor.col = 0;
4119#ifdef FEAT_VIRTUALEDIT
4120 curwin->w_cursor.coladd = 0;
4121#endif
4122 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004123 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004124 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004125 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004126 curwin = old_curwin;
4127 curbuf = curwin->w_buffer;
4128}
4129
4130/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004131 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004132 */
4133 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004134ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004135{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004136 qf_info_T *qi = &ql_info;
4137 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004138
Bram Moolenaar39665952018-08-15 20:59:48 +02004139 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004140 {
4141 qi = GET_LOC_LIST(curwin);
4142 if (qi == NULL)
4143 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004144 emsg(_(e_loclist));
Bram Moolenaar537ef082016-07-09 17:56:19 +02004145 return;
4146 }
4147 }
4148
4149 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004150 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4151 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4152}
4153
4154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 * Return the number of the current entry (line number in the quickfix
4156 * window).
4157 */
4158 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004159qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004161 qf_info_T *qi = &ql_info;
4162
4163 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004164 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004165 qi = wp->w_llist_ref;
4166
4167 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168}
4169
4170/*
4171 * Update the cursor position in the quickfix window to the current error.
4172 * Return TRUE if there is a quickfix window.
4173 */
4174 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004175qf_win_pos_update(
4176 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004177 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
4179 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004180 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004182 // Put the cursor on the current error in the quickfix window, so that
4183 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004184 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 if (win != NULL
4186 && qf_index <= win->w_buffer->b_ml.ml_line_count
4187 && old_qf_index != qf_index)
4188 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 if (qf_index > old_qf_index)
4190 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004191 win->w_redraw_top = old_qf_index;
4192 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
4194 else
4195 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004196 win->w_redraw_top = qf_index;
4197 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004199 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 return win != NULL;
4202}
4203
4204/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004205 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004206 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004207 */
4208 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004209is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004210{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004211 // A window displaying the quickfix buffer will have the w_llist_ref field
4212 // set to NULL.
4213 // A window displaying a location list buffer will have the w_llist_ref
4214 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004215 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004216 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4217 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004218 return TRUE;
4219
4220 return FALSE;
4221}
4222
4223/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004224 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004225 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004226 */
4227 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004228qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004229{
4230 win_T *win;
4231
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004232 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004233 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004234 return win;
4235 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004236}
4237
4238/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004239 * Find a quickfix buffer.
4240 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 */
4242 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004243qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004245 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004246 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
Bram Moolenaar9c102382006-05-03 21:26:49 +00004248 FOR_ALL_TAB_WINDOWS(tp, win)
4249 if (is_qf_win(win, qi))
4250 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004251
Bram Moolenaar9c102382006-05-03 21:26:49 +00004252 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253}
4254
4255/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004256 * Update the w:quickfix_title variable in the quickfix/location list window
4257 */
4258 static void
4259qf_update_win_titlevar(qf_info_T *qi)
4260{
4261 win_T *win;
4262 win_T *curwin_save;
4263
4264 if ((win = qf_find_win(qi)) != NULL)
4265 {
4266 curwin_save = curwin;
4267 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004268 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004269 curwin = curwin_save;
4270 }
4271}
4272
4273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 * Find the quickfix buffer. If it exists, update the contents.
4275 */
4276 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004277qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278{
4279 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004280 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004283 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004284 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 if (buf != NULL)
4286 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004287 linenr_T old_line_count = buf->b_ml.ml_line_count;
4288
4289 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004290 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004291 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
Bram Moolenaard823fa92016-08-12 16:29:27 +02004293 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004294
Bram Moolenaar864293a2016-06-02 13:40:04 +02004295 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004296 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004297
Bram Moolenaar864293a2016-06-02 13:40:04 +02004298 if (old_last == NULL)
4299 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004300 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004301
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004302 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004303 aucmd_restbuf(&aco);
4304 }
4305
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004306 // Only redraw when added lines are visible. This avoids flickering
4307 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004308 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4309 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
4311}
4312
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004313/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004314 * Add an error line to the quickfix buffer.
4315 */
4316 static int
4317qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4318{
4319 int len;
4320 buf_T *errbuf;
4321
4322 if (qfp->qf_module != NULL)
4323 {
4324 STRCPY(IObuff, qfp->qf_module);
4325 len = (int)STRLEN(IObuff);
4326 }
4327 else if (qfp->qf_fnum != 0
4328 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4329 && errbuf->b_fname != NULL)
4330 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004331 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004332 STRCPY(IObuff, gettail(errbuf->b_fname));
4333 else
4334 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004335 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004336 if (errbuf->b_sfname == NULL
4337 || mch_isFullName(errbuf->b_sfname))
4338 {
4339 if (*dirname == NUL)
4340 mch_dirname(dirname, MAXPATHL);
4341 shorten_buf_fname(errbuf, dirname, FALSE);
4342 }
4343 STRCPY(IObuff, errbuf->b_fname);
4344 }
4345 len = (int)STRLEN(IObuff);
4346 }
4347 else
4348 len = 0;
4349 IObuff[len++] = '|';
4350
4351 if (qfp->qf_lnum > 0)
4352 {
4353 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4354 len += (int)STRLEN(IObuff + len);
4355
4356 if (qfp->qf_col > 0)
4357 {
4358 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4359 len += (int)STRLEN(IObuff + len);
4360 }
4361
4362 sprintf((char *)IObuff + len, "%s",
4363 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4364 len += (int)STRLEN(IObuff + len);
4365 }
4366 else if (qfp->qf_pattern != NULL)
4367 {
4368 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4369 len += (int)STRLEN(IObuff + len);
4370 }
4371 IObuff[len++] = '|';
4372 IObuff[len++] = ' ';
4373
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004374 // Remove newlines and leading whitespace from the text.
4375 // For an unrecognized line keep the indent, the compiler may
4376 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004377 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4378 IObuff + len, IOSIZE - len);
4379
4380 if (ml_append_buf(buf, lnum, IObuff,
4381 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4382 return FAIL;
4383
4384 return OK;
4385}
4386
4387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 * Fill current buffer with quickfix errors, replacing any previous contents.
4389 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004390 * If "old_last" is not NULL append the items after this one.
4391 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4392 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 */
4394 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004395qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004397 linenr_T lnum;
4398 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004399 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
Bram Moolenaar864293a2016-06-02 13:40:04 +02004401 if (old_last == NULL)
4402 {
4403 if (buf != curbuf)
4404 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004405 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004406 return;
4407 }
4408
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004409 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004410 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4411 (void)ml_delete((linenr_T)1, FALSE);
4412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004414 // Check if there is anything to display
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004415 if (!qf_stack_empty(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004417 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4418 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004419
4420 *dirname = NUL;
4421
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004422 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004423 if (old_last == NULL)
4424 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004425 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004426 lnum = 0;
4427 }
4428 else
4429 {
4430 qfp = old_last->qf_next;
4431 lnum = buf->b_ml.ml_line_count;
4432 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004433 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004435 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004437
Bram Moolenaar864293a2016-06-02 13:40:04 +02004438 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004440 if (qfp == NULL)
4441 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004443
4444 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004445 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004446 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004449 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 check_lnums(TRUE);
4451
Bram Moolenaar864293a2016-06-02 13:40:04 +02004452 if (old_last == NULL)
4453 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004454 // Set the 'filetype' to "qf" each time after filling the buffer.
4455 // This resembles reading a file into a buffer, it's more logical when
4456 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004457 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004458 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4459 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004461 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004462 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004464 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004466 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004467 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004468
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004469 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004470 redraw_curbuf_later(NOT_VALID);
4471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004473 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 KeyTyped = old_KeyTyped;
4475}
4476
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004477/*
4478 * For every change made to the quickfix list, update the changed tick.
4479 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004480 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004481qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004482{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004483 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004484}
4485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004487 * Return the quickfix/location list number with the given identifier.
4488 * Returns -1 if list is not found.
4489 */
4490 static int
4491qf_id2nr(qf_info_T *qi, int_u qfid)
4492{
4493 int qf_idx;
4494
4495 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4496 if (qi->qf_lists[qf_idx].qf_id == qfid)
4497 return qf_idx;
4498 return INVALID_QFIDX;
4499}
4500
4501/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004502 * If the current list is not "save_qfid" and we can find the list with that ID
4503 * then make it the current list.
4504 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004505 * Returns OK if successfully restored the list. Returns FAIL if the list with
4506 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004507 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004508 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004509qf_restore_list(qf_info_T *qi, int_u save_qfid)
4510{
4511 int curlist;
4512
4513 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4514 {
4515 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004516 if (curlist < 0)
4517 // list is not present
4518 return FAIL;
4519 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004520 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004521 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004522}
4523
4524/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004525 * Jump to the first entry if there is one.
4526 */
4527 static void
4528qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4529{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004530 if (qf_restore_list(qi, save_qfid) == FAIL)
4531 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004532
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004533 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004534 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004535 qf_jump(qi, 0, 0, forceit);
4536}
4537
4538/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004539 * Return TRUE when using ":vimgrep" for ":grep".
4540 */
4541 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004542grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004543{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004544 return ((cmdidx == CMD_grep
4545 || cmdidx == CMD_lgrep
4546 || cmdidx == CMD_grepadd
4547 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004548 && STRCMP("internal",
4549 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4550}
4551
4552/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004553 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004555 static char_u *
4556make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004558 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004559 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004560 case CMD_make: return (char_u *)"make";
4561 case CMD_lmake: return (char_u *)"lmake";
4562 case CMD_grep: return (char_u *)"grep";
4563 case CMD_lgrep: return (char_u *)"lgrep";
4564 case CMD_grepadd: return (char_u *)"grepadd";
4565 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4566 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568}
4569
4570/*
4571 * Return the name for the errorfile, in allocated memory.
4572 * Find a new unique name when 'makeef' contains "##".
4573 * Returns NULL for error.
4574 */
4575 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004576get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577{
4578 char_u *p;
4579 char_u *name;
4580 static int start = -1;
4581 static int off = 0;
4582#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004583 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584#endif
4585
4586 if (*p_mef == NUL)
4587 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004588 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004590 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 return name;
4592 }
4593
4594 for (p = p_mef; *p; ++p)
4595 if (p[0] == '#' && p[1] == '#')
4596 break;
4597
4598 if (*p == NUL)
4599 return vim_strsave(p_mef);
4600
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004601 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 for (;;)
4603 {
4604 if (start == -1)
4605 start = mch_get_pid();
4606 else
4607 off += 19;
4608
4609 name = alloc((unsigned)STRLEN(p_mef) + 30);
4610 if (name == NULL)
4611 break;
4612 STRCPY(name, p_mef);
4613 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4614 STRCAT(name, p + 2);
4615 if (mch_getperm(name) < 0
4616#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004617 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 && mch_lstat((char *)name, &sb) < 0
4619#endif
4620 )
4621 break;
4622 vim_free(name);
4623 }
4624 return name;
4625}
4626
4627/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004628 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4629 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4630 */
4631 static char_u *
4632make_get_fullcmd(char_u *makecmd, char_u *fname)
4633{
4634 char_u *cmd;
4635 unsigned len;
4636
4637 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4638 if (*p_sp != NUL)
4639 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4640 cmd = alloc(len);
4641 if (cmd == NULL)
4642 return NULL;
4643 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4644 (char *)p_shq);
4645
4646 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4647 if (*p_sp != NUL)
4648 append_redir(cmd, len, p_sp, fname);
4649
4650 // Display the fully formed command. Output a newline if there's something
4651 // else than the :make command that was typed (in which case the cursor is
4652 // in column 0).
4653 if (msg_col == 0)
4654 msg_didout = FALSE;
4655 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004656 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004657 msg_outtrans(cmd); // show what we are doing
4658
4659 return cmd;
4660}
4661
4662/*
4663 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4664 */
4665 void
4666ex_make(exarg_T *eap)
4667{
4668 char_u *fname;
4669 char_u *cmd;
4670 char_u *enc = NULL;
4671 win_T *wp = NULL;
4672 qf_info_T *qi = &ql_info;
4673 int res;
4674 char_u *au_name = NULL;
4675 int_u save_qfid;
4676
4677 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4678 if (grep_internal(eap->cmdidx))
4679 {
4680 ex_vimgrep(eap);
4681 return;
4682 }
4683
4684 au_name = make_get_auname(eap->cmdidx);
4685 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4686 curbuf->b_fname, TRUE, curbuf))
4687 {
4688#ifdef FEAT_EVAL
4689 if (aborting())
4690 return;
4691#endif
4692 }
4693#ifdef FEAT_MBYTE
4694 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4695#endif
4696
4697 if (is_loclist_cmd(eap->cmdidx))
4698 wp = curwin;
4699
4700 autowrite_all();
4701 fname = get_mef_name();
4702 if (fname == NULL)
4703 return;
4704 mch_remove(fname); // in case it's not unique
4705
4706 cmd = make_get_fullcmd(eap->arg, fname);
4707 if (cmd == NULL)
4708 return;
4709
4710 // let the shell know if we are redirecting output or not
4711 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4712
4713#ifdef AMIGA
4714 out_flush();
4715 // read window status report and redraw before message
4716 (void)char_avail();
4717#endif
4718
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004719 incr_quickfix_busy();
4720
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004721 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4722 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4723 (eap->cmdidx != CMD_grepadd
4724 && eap->cmdidx != CMD_lgrepadd),
4725 qf_cmdtitle(*eap->cmdlinep), enc);
4726 if (wp != NULL)
4727 {
4728 qi = GET_LOC_LIST(wp);
4729 if (qi == NULL)
4730 goto cleanup;
4731 }
4732 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004733 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004734
4735 // Remember the current quickfix list identifier, so that we can
4736 // check for autocommands changing the current quickfix list.
4737 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4738 if (au_name != NULL)
4739 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4740 curbuf->b_fname, TRUE, curbuf);
4741 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4742 // display the first error
4743 qf_jump_first(qi, save_qfid, FALSE);
4744
4745cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004746 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004747 mch_remove(fname);
4748 vim_free(fname);
4749 vim_free(cmd);
4750}
4751
4752/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004753 * Returns the number of valid entries in the current quickfix/location list.
4754 */
4755 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004756qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004757{
4758 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004759 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004760 qfline_T *qfp;
4761 int i, sz = 0;
4762 int prev_fnum = 0;
4763
Bram Moolenaar39665952018-08-15 20:59:48 +02004764 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004765 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004766 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004767 qi = GET_LOC_LIST(curwin);
4768 if (qi == NULL)
4769 return 0;
4770 }
4771
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004772 qfl = &qi->qf_lists[qi->qf_curlist];
4773 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004774 ++i, qfp = qfp->qf_next)
4775 {
4776 if (qfp->qf_valid)
4777 {
4778 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004779 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004780 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4781 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004782 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004783 sz++;
4784 prev_fnum = qfp->qf_fnum;
4785 }
4786 }
4787 }
4788
4789 return sz;
4790}
4791
4792/*
4793 * Returns the current index of the quickfix/location list.
4794 * Returns 0 if there is an error.
4795 */
4796 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004797qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004798{
4799 qf_info_T *qi = &ql_info;
4800
Bram Moolenaar39665952018-08-15 20:59:48 +02004801 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004802 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004803 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004804 qi = GET_LOC_LIST(curwin);
4805 if (qi == NULL)
4806 return 0;
4807 }
4808
4809 return qi->qf_lists[qi->qf_curlist].qf_index;
4810}
4811
4812/*
4813 * Returns the current index in the quickfix/location list (counting only valid
4814 * entries). If no valid entries are in the list, then returns 1.
4815 */
4816 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004817qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004818{
4819 qf_info_T *qi = &ql_info;
4820 qf_list_T *qfl;
4821 qfline_T *qfp;
4822 int i, eidx = 0;
4823 int prev_fnum = 0;
4824
Bram Moolenaar39665952018-08-15 20:59:48 +02004825 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004826 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004827 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004828 qi = GET_LOC_LIST(curwin);
4829 if (qi == NULL)
4830 return 1;
4831 }
4832
4833 qfl = &qi->qf_lists[qi->qf_curlist];
4834 qfp = qfl->qf_start;
4835
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004836 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004837 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4838 return 1;
4839
4840 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4841 {
4842 if (qfp->qf_valid)
4843 {
4844 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4845 {
4846 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4847 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004848 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004849 eidx++;
4850 prev_fnum = qfp->qf_fnum;
4851 }
4852 }
4853 else
4854 eidx++;
4855 }
4856 }
4857
4858 return eidx ? eidx : 1;
4859}
4860
4861/*
4862 * Get the 'n'th valid error entry in the quickfix or location list.
4863 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4864 * For :cdo and :ldo returns the 'n'th valid error entry.
4865 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4866 */
4867 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004868qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004869{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004870 qfline_T *qfp = qfl->qf_start;
4871 int i, eidx;
4872 int prev_fnum = 0;
4873
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004874 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004875 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4876 return 1;
4877
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004878 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004879 i++, qfp = qfp->qf_next)
4880 {
4881 if (qfp->qf_valid)
4882 {
4883 if (fdo)
4884 {
4885 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4886 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004887 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004888 eidx++;
4889 prev_fnum = qfp->qf_fnum;
4890 }
4891 }
4892 else
4893 eidx++;
4894 }
4895
4896 if (eidx == n)
4897 break;
4898 }
4899
4900 if (i <= qfl->qf_count)
4901 return i;
4902 else
4903 return 1;
4904}
4905
4906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004908 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004909 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 */
4911 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004912ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004914 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004915 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004916
Bram Moolenaar39665952018-08-15 20:59:48 +02004917 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004918 {
4919 qi = GET_LOC_LIST(curwin);
4920 if (qi == NULL)
4921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004922 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004923 return;
4924 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004925 }
4926
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004927 if (eap->addr_count > 0)
4928 errornr = (int)eap->line2;
4929 else
4930 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004931 switch (eap->cmdidx)
4932 {
4933 case CMD_cc: case CMD_ll:
4934 errornr = 0;
4935 break;
4936 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4937 case CMD_lfirst:
4938 errornr = 1;
4939 break;
4940 default:
4941 errornr = 32767;
4942 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004943 }
4944
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004945 // For cdo and ldo commands, jump to the nth valid error.
4946 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02004947 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4948 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004949 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004950 eap->addr_count > 0 ? (int)eap->line1 : 1,
4951 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4952
4953 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954}
4955
4956/*
4957 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004958 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004959 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 */
4961 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004962ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004964 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004965 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004966 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004967
Bram Moolenaar39665952018-08-15 20:59:48 +02004968 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004969 {
4970 qi = GET_LOC_LIST(curwin);
4971 if (qi == NULL)
4972 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004973 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004974 return;
4975 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004976 }
4977
Bram Moolenaar55b69262017-08-13 13:42:01 +02004978 if (eap->addr_count > 0
4979 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4980 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004981 errornr = (int)eap->line2;
4982 else
4983 errornr = 1;
4984
Bram Moolenaar39665952018-08-15 20:59:48 +02004985 // Depending on the command jump to either next or previous entry/file.
4986 switch (eap->cmdidx)
4987 {
4988 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4989 dir = FORWARD;
4990 break;
4991 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4992 case CMD_lNext:
4993 dir = BACKWARD;
4994 break;
4995 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4996 dir = FORWARD_FILE;
4997 break;
4998 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4999 dir = BACKWARD_FILE;
5000 break;
5001 default:
5002 dir = FORWARD;
5003 break;
5004 }
5005
5006 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007}
5008
5009/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005010 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005011 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 */
5013 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005014ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005016 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005017 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005018 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005019 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005020 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005021 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005022
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005023 switch (eap->cmdidx)
5024 {
5025 case CMD_cfile: au_name = (char_u *)"cfile"; break;
5026 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
5027 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
5028 case CMD_lfile: au_name = (char_u *)"lfile"; break;
5029 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
5030 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
5031 default: break;
5032 }
5033 if (au_name != NULL)
5034 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005035#ifdef FEAT_MBYTE
5036 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
5037#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02005038#ifdef FEAT_BROWSE
5039 if (cmdmod.browse)
5040 {
5041 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005042 NULL, NULL,
5043 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005044 if (browse_file == NULL)
5045 return;
5046 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5047 vim_free(browse_file);
5048 }
5049 else
5050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005052 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005053
Bram Moolenaar39665952018-08-15 20:59:48 +02005054 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005055 wp = curwin;
5056
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005057 incr_quickfix_busy();
5058
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005059 // This function is used by the :cfile, :cgetfile and :caddfile
5060 // commands.
5061 // :cfile always creates a new quickfix list and jumps to the
5062 // first error.
5063 // :cgetfile creates a new quickfix list but doesn't jump to the
5064 // first error.
5065 // :caddfile adds to an existing quickfix list. If there is no
5066 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005067 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005068 && eap->cmdidx != CMD_laddfile),
5069 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005070 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005071 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005072 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005073 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005074 {
5075 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005076 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005077 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005078 }
5079 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005080 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005081 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005082 if (au_name != NULL)
5083 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005084
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005085 // Jump to the first error for a new list and if autocmds didn't
5086 // free the list.
5087 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5088 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005089 // display the first error
5090 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005091
5092 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005093}
5094
5095/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005096 * Return the vimgrep autocmd name.
5097 */
5098 static char_u *
5099vgr_get_auname(cmdidx_T cmdidx)
5100{
5101 switch (cmdidx)
5102 {
5103 case CMD_vimgrep: return (char_u *)"vimgrep";
5104 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5105 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5106 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5107 case CMD_grep: return (char_u *)"grep";
5108 case CMD_lgrep: return (char_u *)"lgrep";
5109 case CMD_grepadd: return (char_u *)"grepadd";
5110 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5111 default: return NULL;
5112 }
5113}
5114
5115/*
5116 * Initialize the regmatch used by vimgrep for pattern "s".
5117 */
5118 static void
5119vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5120{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005121 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005122 regmatch->regprog = NULL;
5123
5124 if (s == NULL || *s == NUL)
5125 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005126 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005127 if (last_search_pat() == NULL)
5128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005129 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005130 return;
5131 }
5132 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5133 }
5134 else
5135 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5136
5137 regmatch->rmm_ic = p_ic;
5138 regmatch->rmm_maxcol = 0;
5139}
5140
5141/*
5142 * Display a file name when vimgrep is running.
5143 */
5144 static void
5145vgr_display_fname(char_u *fname)
5146{
5147 char_u *p;
5148
5149 msg_start();
5150 p = msg_strtrunc(fname, TRUE);
5151 if (p == NULL)
5152 msg_outtrans(fname);
5153 else
5154 {
5155 msg_outtrans(p);
5156 vim_free(p);
5157 }
5158 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005159 msg_didout = FALSE; // overwrite this message
5160 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005161 msg_col = 0;
5162 out_flush();
5163}
5164
5165/*
5166 * Load a dummy buffer to search for a pattern using vimgrep.
5167 */
5168 static buf_T *
5169vgr_load_dummy_buf(
5170 char_u *fname,
5171 char_u *dirname_start,
5172 char_u *dirname_now)
5173{
5174 int save_mls;
5175#if defined(FEAT_SYN_HL)
5176 char_u *save_ei = NULL;
5177#endif
5178 buf_T *buf;
5179
5180#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005181 // Don't do Filetype autocommands to avoid loading syntax and
5182 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005183 save_ei = au_event_disable(",Filetype");
5184#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005185 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005186 save_mls = p_mls;
5187 p_mls = 0;
5188
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005189 // Load file into a buffer, so that 'fileencoding' is detected,
5190 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005191 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5192
5193 p_mls = save_mls;
5194#if defined(FEAT_SYN_HL)
5195 au_event_restore(save_ei);
5196#endif
5197
5198 return buf;
5199}
5200
5201/*
5202 * Check whether a quickfix/location list valid. Autocmds may remove or change
5203 * a quickfix list when vimgrep is running. If the list is not found, create a
5204 * new list.
5205 */
5206 static int
5207vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005208 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005209 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005210 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005211 char_u *title)
5212{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005213 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005214 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005215 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005216 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005217 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005218 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005219 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005220 return FALSE;
5221 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005222 else
5223 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005224 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005225 qf_new_list(qi, title);
5226 return TRUE;
5227 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005228 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005229
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005230 if (qf_restore_list(qi, qfid) == FAIL)
5231 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005232
5233 return TRUE;
5234}
5235
5236/*
5237 * Search for a pattern in all the lines in a buffer and add the matching lines
5238 * to a quickfix list.
5239 */
5240 static int
5241vgr_match_buflines(
5242 qf_info_T *qi,
5243 char_u *fname,
5244 buf_T *buf,
5245 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005246 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005247 int duplicate_name,
5248 int flags)
5249{
5250 int found_match = FALSE;
5251 long lnum;
5252 colnr_T col;
5253
Bram Moolenaar1c299432018-10-28 14:36:09 +01005254 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005255 {
5256 col = 0;
5257 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5258 col, NULL, NULL) > 0)
5259 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005260 // Pass the buffer number so that it gets used even for a
5261 // dummy buffer, unless duplicate_name is set, then the
5262 // buffer will be wiped out below.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005263 if (qf_add_entry(qi,
5264 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005265 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005266 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005267 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005268 duplicate_name ? 0 : buf->b_fnum,
5269 ml_get_buf(buf,
5270 regmatch->startpos[0].lnum + lnum, FALSE),
5271 regmatch->startpos[0].lnum + lnum,
5272 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005273 FALSE, // vis_col
5274 NULL, // search pattern
5275 0, // nr
5276 0, // type
5277 TRUE // valid
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005278 ) == FAIL)
5279 {
5280 got_int = TRUE;
5281 break;
5282 }
5283 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005284 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005285 break;
5286 if ((flags & VGR_GLOBAL) == 0
5287 || regmatch->endpos[0].lnum > 0)
5288 break;
5289 col = regmatch->endpos[0].col
5290 + (col == regmatch->endpos[0].col);
5291 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5292 break;
5293 }
5294 line_breakcheck();
5295 if (got_int)
5296 break;
5297 }
5298
5299 return found_match;
5300}
5301
5302/*
5303 * Jump to the first match and update the directory.
5304 */
5305 static void
5306vgr_jump_to_match(
5307 qf_info_T *qi,
5308 int forceit,
5309 int *redraw_for_dummy,
5310 buf_T *first_match_buf,
5311 char_u *target_dir)
5312{
5313 buf_T *buf;
5314
5315 buf = curbuf;
5316 qf_jump(qi, 0, 0, forceit);
5317 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005318 // If we jumped to another buffer redrawing will already be
5319 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005320 *redraw_for_dummy = FALSE;
5321
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005322 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005323 if (curbuf == first_match_buf && target_dir != NULL)
5324 {
5325 exarg_T ea;
5326
5327 ea.arg = target_dir;
5328 ea.cmdidx = CMD_lcd;
5329 ex_cd(&ea);
5330 }
5331}
5332
5333/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005334 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005335 * ":vimgrepadd {pattern} file(s)"
5336 * ":lvimgrep {pattern} file(s)"
5337 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005338 */
5339 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005340ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005341{
Bram Moolenaar81695252004-12-29 20:58:21 +00005342 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005343 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005344 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005345 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005346 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005347 char_u *s;
5348 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005349 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005350 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005351 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005352 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005353 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005354 buf_T *buf;
5355 int duplicate_name = FALSE;
5356 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005357 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005358 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005359 buf_T *first_match_buf = NULL;
5360 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005361 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005362 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005363 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005364 char_u *dirname_start = NULL;
5365 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005366 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005367 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005368
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005369 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005370 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5371 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005372 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005373#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005374 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005375 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005376#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005377 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005378
Bram Moolenaar39665952018-08-15 20:59:48 +02005379 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005380 {
5381 qi = ll_get_or_alloc_list(curwin);
5382 if (qi == NULL)
5383 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005384 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005385 }
5386
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005387 if (eap->addr_count > 0)
5388 tomatch = eap->line2;
5389 else
5390 tomatch = MAXLNUM;
5391
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005392 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005393 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005394 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005395 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005396 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005397 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005398 emsg(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005399 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005400 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005401
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005402 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005403 if (regmatch.regprog == NULL)
5404 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005405
5406 p = skipwhite(p);
5407 if (*p == NUL)
5408 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005409 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005410 goto theend;
5411 }
5412
Bram Moolenaar55b69262017-08-13 13:42:01 +02005413 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005414 && eap->cmdidx != CMD_vimgrepadd
5415 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005416 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005417 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005418 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005419
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005420 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005421 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005422 goto theend;
5423 if (fcount == 0)
5424 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005425 emsg(_(e_nomatch));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005426 goto theend;
5427 }
5428
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005429 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5430 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005431 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005432 {
5433 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005434 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005435 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005436
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005437 // Remember the current directory, because a BufRead autocommand that does
5438 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005439 mch_dirname(dirname_start, MAXPATHL);
5440
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005441 incr_quickfix_busy();
5442
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005443 // Remember the current quickfix list identifier, so that we can check for
5444 // autocommands changing the current quickfix list.
Bram Moolenaar3c097222017-12-21 20:54:49 +01005445 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005446
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005447 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005448 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005449 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005450 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005451 if (time(NULL) > seconds)
5452 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005453 // Display the file name every second or so, show the user we are
5454 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005455 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005456 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005457 }
5458
Bram Moolenaar81695252004-12-29 20:58:21 +00005459 buf = buflist_findname_exp(fnames[fi]);
5460 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5461 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005462 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005463 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005464 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005465 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005466
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005467 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005468 }
5469 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005470 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005471 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005472
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005473 // Check whether the quickfix list is still valid. When loading a
5474 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005475 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005476 {
5477 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005478 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005479 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005480 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005481 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005482
Bram Moolenaar81695252004-12-29 20:58:21 +00005483 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005484 {
5485 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005486 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005487 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005488 else
5489 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005490 // Try for a match in all lines of the buffer.
5491 // For ":1vimgrep" look for first match only.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005492 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005493 &tomatch, duplicate_name, flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005494
Bram Moolenaar81695252004-12-29 20:58:21 +00005495 if (using_dummy)
5496 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005497 if (found_match && first_match_buf == NULL)
5498 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005499 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005500 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005501 // Never keep a dummy buffer if there is another buffer
5502 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005503 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005504 buf = NULL;
5505 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005506 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005507 || buf->b_p_bh[0] == 'u' // "unload"
5508 || buf->b_p_bh[0] == 'w' // "wipe"
5509 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005510 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005511 // When no match was found we don't need to remember the
5512 // buffer, wipe it out. If there was a match and it
5513 // wasn't the first one or we won't jump there: only
5514 // unload the buffer.
5515 // Ignore 'hidden' here, because it may lead to having too
5516 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005517 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005518 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005519 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005520 buf = NULL;
5521 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005522 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005523 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005524 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005525 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005526 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005527 buf = NULL;
5528 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005529 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005530
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005531 if (buf != NULL)
5532 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005533 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005534 buf->b_flags &= ~BF_DUMMY;
5535
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005536 // If the buffer is still loaded we need to use the
5537 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005538 if (buf == first_match_buf
5539 && target_dir == NULL
5540 && STRCMP(dirname_start, dirname_now) != 0)
5541 target_dir = vim_strsave(dirname_now);
5542
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005543 // The buffer is still loaded, the Filetype autocommands
5544 // need to be done now, in that buffer. And the modelines
5545 // need to be done (again). But not the window-local
5546 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005547 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005548#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005549 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5550 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005551#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005552 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005553 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005554 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005555 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005556 }
5557 }
5558
5559 FreeWild(fcount, fnames);
5560
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005561 qfl = &qi->qf_lists[qi->qf_curlist];
5562 qfl->qf_nonevalid = FALSE;
5563 qfl->qf_ptr = qfl->qf_start;
5564 qfl->qf_index = 1;
5565 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005566
Bram Moolenaar864293a2016-06-02 13:40:04 +02005567 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005568
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005569 if (au_name != NULL)
5570 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5571 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005572 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5573 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005574 if (!qflist_valid(wp, save_qfid)
5575 || qf_restore_list(qi, save_qfid) == FAIL)
5576 {
5577 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005578 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005579 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005580
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005581 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005582 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005583 {
5584 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005585 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5586 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005587 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005588 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005589 semsg(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005590
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005591 decr_quickfix_busy();
5592
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005593 // If we loaded a dummy buffer into the current window, the autocommands
5594 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005595 if (redraw_for_dummy)
5596 {
5597#ifdef FEAT_FOLDING
5598 foldUpdateAll(curwin);
5599#else
5600 redraw_later(NOT_VALID);
5601#endif
5602 }
5603
Bram Moolenaar86b68352004-12-27 21:59:20 +00005604theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005605 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005606 vim_free(dirname_now);
5607 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005608 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005609 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005610}
5611
5612/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005613 * Restore current working directory to "dirname_start" if they differ, taking
5614 * into account whether it is set locally or globally.
5615 */
5616 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005617restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005618{
5619 char_u *dirname_now = alloc(MAXPATHL);
5620
5621 if (NULL != dirname_now)
5622 {
5623 mch_dirname(dirname_now, MAXPATHL);
5624 if (STRCMP(dirname_start, dirname_now) != 0)
5625 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005626 // If the directory has changed, change it back by building up an
5627 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005628 exarg_T ea;
5629
5630 ea.arg = dirname_start;
5631 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5632 ex_cd(&ea);
5633 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005634 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005635 }
5636}
5637
5638/*
5639 * Load file "fname" into a dummy buffer and return the buffer pointer,
5640 * placing the directory resulting from the buffer load into the
5641 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5642 * prior to calling this function. Restores directory to "dirname_start" prior
5643 * to returning, if autocmds or the 'autochdir' option have changed it.
5644 *
5645 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5646 * or wipe_dummy_buffer() later!
5647 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005648 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005649 */
5650 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005651load_dummy_buffer(
5652 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005653 char_u *dirname_start, // in: old directory
5654 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005655{
5656 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005657 bufref_T newbufref;
5658 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005659 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005660 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005661 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005662
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005663 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005664 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5665 if (newbuf == NULL)
5666 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005667 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005668
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005669 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005670 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5671
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005672 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005673 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005674 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005675 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005676 ++newbuf->b_locked;
5677
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005678 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005679 aucmd_prepbuf(&aco, newbuf);
5680
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005681 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005682 (void)setfname(curbuf, fname, NULL, FALSE);
5683
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005684 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005685 check_need_swap(TRUE);
5686
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005687 // Remove the "dummy" flag, otherwise autocommands may not
5688 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005689 curbuf->b_flags &= ~BF_DUMMY;
5690
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005691 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005692 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005693 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005694 NULL, READ_NEW | READ_DUMMY);
5695 --newbuf->b_locked;
5696 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005697 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005698 && !(curbuf->b_flags & BF_NEW))
5699 {
5700 failed = FALSE;
5701 if (curbuf != newbuf)
5702 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005703 // Bloody autocommands changed the buffer! Can happen when
5704 // using netrw and editing a remote file. Use the current
5705 // buffer instead, delete the dummy one after restoring the
5706 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005707 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005708 newbuf = curbuf;
5709 }
5710 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005711
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005712 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005713 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005714 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5715 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005716
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005717 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5718 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005719 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005720 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005722 // When autocommands/'autochdir' option changed directory: go back.
5723 // Let the caller know what the resulting dir was first, in case it is
5724 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005725 mch_dirname(resulting_dir, MAXPATHL);
5726 restore_start_dir(dirname_start);
5727
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005728 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005729 return NULL;
5730 if (failed)
5731 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005732 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005733 return NULL;
5734 }
5735 return newbuf;
5736}
5737
5738/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005739 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5740 * directory to "dirname_start" prior to returning, if autocmds or the
5741 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005742 */
5743 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005744wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005745{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005746 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005747 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005748#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005749 cleanup_T cs;
5750
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005751 // Reset the error/interrupt/exception state here so that aborting()
5752 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5753 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005754 enter_cleanup(&cs);
5755#endif
5756
Bram Moolenaar81695252004-12-29 20:58:21 +00005757 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005758
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005759#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005760 // Restore the error/interrupt/exception state if not discarded by a
5761 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005762 leave_cleanup(&cs);
5763#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005764 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005765 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005766 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005767}
5768
5769/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005770 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5771 * directory to "dirname_start" prior to returning, if autocmds or the
5772 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005773 */
5774 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005775unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005776{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005777 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005778 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005779 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005780
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005781 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005782 restore_start_dir(dirname_start);
5783 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005784}
5785
Bram Moolenaar05159a02005-02-26 23:04:13 +00005786#if defined(FEAT_EVAL) || defined(PROTO)
5787/*
5788 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005789 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005790 */
5791 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005792get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005793{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005794 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005795 dict_T *dict;
5796 char_u buf[2];
5797 qfline_T *qfp;
5798 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005799 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005800
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005801 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005802 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005803 qi = &ql_info;
5804 if (wp != NULL)
5805 {
5806 qi = GET_LOC_LIST(wp);
5807 if (qi == NULL)
5808 return FAIL;
5809 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005810 }
5811
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005812 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005813 qf_idx = qi->qf_curlist;
5814
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005815 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005816 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005817
Bram Moolenaard823fa92016-08-12 16:29:27 +02005818 qfp = qi->qf_lists[qf_idx].qf_start;
5819 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005820 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005821 // Handle entries with a non-existing buffer number.
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005822 bufnum = qfp->qf_fnum;
5823 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5824 bufnum = 0;
5825
Bram Moolenaar05159a02005-02-26 23:04:13 +00005826 if ((dict = dict_alloc()) == NULL)
5827 return FAIL;
5828 if (list_append_dict(list, dict) == FAIL)
5829 return FAIL;
5830
5831 buf[0] = qfp->qf_type;
5832 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005833 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5834 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5835 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5836 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5837 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5838 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5839 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5840 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5841 || dict_add_string(dict, "type", buf) == FAIL
5842 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005843 return FAIL;
5844
5845 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005846 if (qfp == NULL)
5847 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005848 }
5849 return OK;
5850}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005851
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005852// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005853enum {
5854 QF_GETLIST_NONE = 0x0,
5855 QF_GETLIST_TITLE = 0x1,
5856 QF_GETLIST_ITEMS = 0x2,
5857 QF_GETLIST_NR = 0x4,
5858 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005859 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005860 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005861 QF_GETLIST_IDX = 0x40,
5862 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005863 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005864 QF_GETLIST_FILEWINID = 0x200,
5865 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005866};
5867
5868/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005869 * Parse text from 'di' and return the quickfix list items.
5870 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005871 */
5872 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005873qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005874{
5875 int status = FAIL;
5876 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005877 char_u *errorformat = p_efm;
5878 dictitem_T *efm_di;
5879 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005880
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005881 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005882 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005883 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005884 // If errorformat is supplied then use it, otherwise use the 'efm'
5885 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005886 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5887 {
5888 if (efm_di->di_tv.v_type != VAR_STRING ||
5889 efm_di->di_tv.vval.v_string == NULL)
5890 return FAIL;
5891 errorformat = efm_di->di_tv.vval.v_string;
5892 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005893
Bram Moolenaar36538222017-09-02 19:51:44 +02005894 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005895 if (l == NULL)
5896 return FAIL;
5897
Bram Moolenaar2d67d302018-11-16 18:46:02 +01005898 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005899 if (qi != NULL)
5900 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005901 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005902 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5903 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005904 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005905 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005906 }
5907 free(qi);
5908 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005909 dict_add_list(retdict, "items", l);
5910 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005911 }
5912
5913 return status;
5914}
5915
5916/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005917 * Return the quickfix/location list window identifier in the current tabpage.
5918 */
5919 static int
5920qf_winid(qf_info_T *qi)
5921{
5922 win_T *win;
5923
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005924 // The quickfix window can be opened even if the quickfix list is not set
5925 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005926 if (qi == NULL)
5927 return 0;
5928 win = qf_find_win(qi);
5929 if (win != NULL)
5930 return win->w_id;
5931 return 0;
5932}
5933
5934/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005935 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005936 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005937 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005938qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005939{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005940 int flags = QF_GETLIST_NONE;
5941
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005942 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005943 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005944 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005945 if (!loclist)
5946 // File window ID is applicable only to location list windows
5947 flags &= ~ QF_GETLIST_FILEWINID;
5948 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005949
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005950 if (dict_find(what, (char_u *)"title", -1) != NULL)
5951 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005952
Bram Moolenaara6d48492017-12-12 22:45:31 +01005953 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5954 flags |= QF_GETLIST_NR;
5955
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005956 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5957 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005958
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005959 if (dict_find(what, (char_u *)"context", -1) != NULL)
5960 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005961
Bram Moolenaara6d48492017-12-12 22:45:31 +01005962 if (dict_find(what, (char_u *)"id", -1) != NULL)
5963 flags |= QF_GETLIST_ID;
5964
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005965 if (dict_find(what, (char_u *)"items", -1) != NULL)
5966 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005967
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005968 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5969 flags |= QF_GETLIST_IDX;
5970
5971 if (dict_find(what, (char_u *)"size", -1) != NULL)
5972 flags |= QF_GETLIST_SIZE;
5973
Bram Moolenaarb254af32017-12-18 19:48:58 +01005974 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5975 flags |= QF_GETLIST_TICK;
5976
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005977 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5978 flags |= QF_GETLIST_FILEWINID;
5979
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005980 return flags;
5981}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005982
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005983/*
5984 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5985 * If 'nr' and 'id' are not present in 'what' then return the current
5986 * quickfix list index.
5987 * If 'nr' is zero then return the current quickfix list index.
5988 * If 'nr' is '$' then return the last quickfix list index.
5989 * If 'id' is present then return the index of the quickfix list with that id.
5990 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5991 * Return -1, if quickfix list is not present or if the stack is empty.
5992 */
5993 static int
5994qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5995{
5996 int qf_idx;
5997 dictitem_T *di;
5998
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005999 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006000 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6001 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006002 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006003 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006004 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006005 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006006 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006007 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006008 qf_idx = di->di_tv.vval.v_number - 1;
6009 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006010 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006011 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006012 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006013 else if (di->di_tv.v_type == VAR_STRING
6014 && di->di_tv.vval.v_string != NULL
6015 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006016 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006017 qf_idx = qi->qf_listcount - 1;
6018 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006019 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006020 }
6021
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006022 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6023 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006024 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006025 if (di->di_tv.v_type == VAR_NUMBER)
6026 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006027 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006028 if (di->di_tv.vval.v_number != 0)
6029 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6030 }
6031 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006032 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006033 }
6034
6035 return qf_idx;
6036}
6037
6038/*
6039 * Return default values for quickfix list properties in retdict.
6040 */
6041 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006042qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006043{
6044 int status = OK;
6045
6046 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006047 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006048 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6049 {
6050 list_T *l = list_alloc();
6051 if (l != NULL)
6052 status = dict_add_list(retdict, "items", l);
6053 else
6054 status = FAIL;
6055 }
6056 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006057 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006058 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006059 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006060 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006061 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006062 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006063 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006064 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006065 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006066 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006067 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006068 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006069 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006070 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006071 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006072
6073 return status;
6074}
6075
6076/*
6077 * Return the quickfix list title as 'title' in retdict
6078 */
6079 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006080qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006081{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006082 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006083}
6084
6085/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006086 * Returns the identifier of the window used to display files from a location
6087 * list. If there is no associated window, then returns 0. Useful only when
6088 * called from a location list window.
6089 */
6090 static int
6091qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6092{
6093 int winid = 0;
6094
6095 if (wp != NULL && IS_LL_WINDOW(wp))
6096 {
6097 win_T *ll_wp = qf_find_win_with_loclist(qi);
6098 if (ll_wp != NULL)
6099 winid = ll_wp->w_id;
6100 }
6101
6102 return dict_add_number(retdict, "filewinid", winid);
6103}
6104
6105/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006106 * Return the quickfix list items/entries as 'items' in retdict
6107 */
6108 static int
6109qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6110{
6111 int status = OK;
6112 list_T *l = list_alloc();
6113 if (l != NULL)
6114 {
6115 (void)get_errorlist(qi, NULL, qf_idx, l);
6116 dict_add_list(retdict, "items", l);
6117 }
6118 else
6119 status = FAIL;
6120
6121 return status;
6122}
6123
6124/*
6125 * Return the quickfix list context (if any) as 'context' in retdict.
6126 */
6127 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006128qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006129{
6130 int status;
6131 dictitem_T *di;
6132
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006133 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006134 {
6135 di = dictitem_alloc((char_u *)"context");
6136 if (di != NULL)
6137 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006138 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006139 status = dict_add(retdict, di);
6140 if (status == FAIL)
6141 dictitem_free(di);
6142 }
6143 else
6144 status = FAIL;
6145 }
6146 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006147 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006148
6149 return status;
6150}
6151
6152/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006153 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006154 */
6155 static int
6156qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6157{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006158 int curidx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006159 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006160 // For empty lists, current index is set to 0
6161 curidx = 0;
6162 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006163}
6164
6165/*
6166 * Return quickfix/location list details (title) as a
6167 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6168 * then current list is used. Otherwise the specified list is used.
6169 */
6170 int
6171qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6172{
6173 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006174 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006175 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006176 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006177 dictitem_T *di;
6178 int flags = QF_GETLIST_NONE;
6179
6180 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6181 return qf_get_list_from_lines(what, di, retdict);
6182
6183 if (wp != NULL)
6184 qi = GET_LOC_LIST(wp);
6185
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006186 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006187
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006188 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006189 qf_idx = qf_getprop_qfidx(qi, what);
6190
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006191 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006192 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006193 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006194
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006195 qfl = &qi->qf_lists[qf_idx];
6196
Bram Moolenaard823fa92016-08-12 16:29:27 +02006197 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006198 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006199 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006200 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006201 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006202 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006203 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006204 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006205 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006206 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006207 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006208 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006209 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006210 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006211 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006212 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006213 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006214 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006215 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6216 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006217
Bram Moolenaard823fa92016-08-12 16:29:27 +02006218 return status;
6219}
6220
6221/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006222 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006223 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6224 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006225 */
6226 static int
6227qf_add_entry_from_dict(
6228 qf_info_T *qi,
6229 int qf_idx,
6230 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006231 int first_entry,
6232 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006233{
6234 static int did_bufnr_emsg;
6235 char_u *filename, *module, *pattern, *text, *type;
6236 int bufnum, valid, status, col, vcol, nr;
6237 long lnum;
6238
6239 if (first_entry)
6240 did_bufnr_emsg = FALSE;
6241
Bram Moolenaar8f667172018-12-14 15:38:31 +01006242 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6243 module = dict_get_string(d, (char_u *)"module", TRUE);
6244 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6245 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6246 col = (int)dict_get_number(d, (char_u *)"col");
6247 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6248 nr = (int)dict_get_number(d, (char_u *)"nr");
6249 type = dict_get_string(d, (char_u *)"type", TRUE);
6250 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6251 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006252 if (text == NULL)
6253 text = vim_strsave((char_u *)"");
6254
6255 valid = TRUE;
6256 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6257 valid = FALSE;
6258
6259 // Mark entries with non-existing buffer number as not valid. Give the
6260 // error message only once.
6261 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6262 {
6263 if (!did_bufnr_emsg)
6264 {
6265 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006266 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006267 }
6268 valid = FALSE;
6269 bufnum = 0;
6270 }
6271
6272 // If the 'valid' field is present it overrules the detected value.
6273 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006274 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006275
6276 status = qf_add_entry(qi,
6277 qf_idx,
6278 NULL, // dir
6279 filename,
6280 module,
6281 bufnum,
6282 text,
6283 lnum,
6284 col,
6285 vcol, // vis_col
6286 pattern, // search pattern
6287 nr,
6288 type == NULL ? NUL : *type,
6289 valid);
6290
6291 vim_free(filename);
6292 vim_free(module);
6293 vim_free(pattern);
6294 vim_free(text);
6295 vim_free(type);
6296
Bram Moolenaar9752c722018-12-22 16:49:34 +01006297 if (valid)
6298 *valid_entry = TRUE;
6299
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006300 return status;
6301}
6302
6303/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006304 * Add list of entries to quickfix/location list. Each list entry is
6305 * a dictionary with item information.
6306 */
6307 static int
6308qf_add_entries(
6309 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006310 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006311 list_T *list,
6312 char_u *title,
6313 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006314{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006315 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006316 listitem_T *li;
6317 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006318 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006319 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006320 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006321
Bram Moolenaara3921f42017-06-04 15:30:34 +02006322 if (action == ' ' || qf_idx == qi->qf_listcount)
6323 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006324 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006325 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006326 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006327 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006328 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006329 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006330 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006331 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006332 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006333 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006334 qf_free_items(qfl);
6335 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006336 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006337
6338 for (li = list->lv_first; li != NULL; li = li->li_next)
6339 {
6340 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006341 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006342
6343 d = li->li_tv.vval.v_dict;
6344 if (d == NULL)
6345 continue;
6346
Bram Moolenaar9752c722018-12-22 16:49:34 +01006347 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first,
6348 &valid_entry);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006349 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006350 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006351 }
6352
Bram Moolenaar9752c722018-12-22 16:49:34 +01006353 // Check if any valid error entries are added to the list.
6354 if (valid_entry)
6355 qfl->qf_nonevalid = FALSE;
6356 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006357 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006358 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006359
6360 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006361 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006362 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006363
6364 // Update the current error index if not appending to the list or if the
6365 // list was empty before and it is not empty now.
6366 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qi, qf_idx))
6367 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006368
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006369 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006370 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006371
6372 return retval;
6373}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006374
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006375/*
6376 * Get the quickfix list index from 'nr' or 'id'
6377 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006378 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006379qf_setprop_get_qfidx(
6380 qf_info_T *qi,
6381 dict_T *what,
6382 int action,
6383 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006384{
6385 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006386 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006387
Bram Moolenaard823fa92016-08-12 16:29:27 +02006388 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6389 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006390 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006391 if (di->di_tv.v_type == VAR_NUMBER)
6392 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006393 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006394 if (di->di_tv.vval.v_number != 0)
6395 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006396
Bram Moolenaar55b69262017-08-13 13:42:01 +02006397 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6398 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006399 // When creating a new list, accept qf_idx pointing to the next
6400 // non-available list and add the new list at the end of the
6401 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006402 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006403 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006404 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006405 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006406 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006407 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006408 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006409 }
6410 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006411 && di->di_tv.vval.v_string != NULL
6412 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006413 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006414 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006415 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006416 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006417 qf_idx = 0;
6418 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006419 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006420 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006421 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006422 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006423 }
6424
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006425 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006426 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006427 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006428 if (di->di_tv.v_type != VAR_NUMBER)
6429 return INVALID_QFIDX;
6430
6431 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006432 }
6433
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006434 return qf_idx;
6435}
6436
6437/*
6438 * Set the quickfix list title.
6439 */
6440 static int
6441qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6442{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006443 qf_list_T *qfl = &qi->qf_lists[qf_idx];
6444
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006445 if (di->di_tv.v_type != VAR_STRING)
6446 return FAIL;
6447
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006448 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01006449 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006450 if (qf_idx == qi->qf_curlist)
6451 qf_update_win_titlevar(qi);
6452
6453 return OK;
6454}
6455
6456/*
6457 * Set quickfix list items/entries.
6458 */
6459 static int
6460qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6461{
6462 int retval = FAIL;
6463 char_u *title_save;
6464
6465 if (di->di_tv.v_type != VAR_LIST)
6466 return FAIL;
6467
6468 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6469 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6470 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006471 vim_free(title_save);
6472
6473 return retval;
6474}
6475
6476/*
6477 * Set quickfix list items/entries from a list of lines.
6478 */
6479 static int
6480qf_setprop_items_from_lines(
6481 qf_info_T *qi,
6482 int qf_idx,
6483 dict_T *what,
6484 dictitem_T *di,
6485 int action)
6486{
6487 char_u *errorformat = p_efm;
6488 dictitem_T *efm_di;
6489 int retval = FAIL;
6490
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006491 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006492 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6493 {
6494 if (efm_di->di_tv.v_type != VAR_STRING ||
6495 efm_di->di_tv.vval.v_string == NULL)
6496 return FAIL;
6497 errorformat = efm_di->di_tv.vval.v_string;
6498 }
6499
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006500 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006501 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6502 return FAIL;
6503
6504 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006505 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006506 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6507 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6508 retval = OK;
6509
6510 return retval;
6511}
6512
6513/*
6514 * Set quickfix list context.
6515 */
6516 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006517qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006518{
6519 typval_T *ctx;
6520
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006521 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006522 ctx = alloc_tv();
6523 if (ctx != NULL)
6524 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006525 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006526
6527 return OK;
6528}
6529
6530/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006531 * Set the current index in the specified quickfix list
6532 */
6533 static int
6534qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
6535{
6536 int denote = FALSE;
6537 int newidx;
6538 int old_qfidx;
6539 qfline_T *qf_ptr;
6540
6541 // If the specified index is '$', then use the last entry
6542 if (di->di_tv.v_type == VAR_STRING
6543 && di->di_tv.vval.v_string != NULL
6544 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
6545 newidx = qfl->qf_count;
6546 else
6547 {
6548 // Otherwise use the specified index
6549 newidx = tv_get_number_chk(&di->di_tv, &denote);
6550 if (denote)
6551 return FAIL;
6552 }
6553
6554 if (newidx < 1) // sanity check
6555 return FAIL;
6556 if (newidx > qfl->qf_count)
6557 newidx = qfl->qf_count;
6558
6559 old_qfidx = qfl->qf_index;
6560 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
6561 if (qf_ptr == NULL)
6562 return FAIL;
6563 qfl->qf_ptr = qf_ptr;
6564 qfl->qf_index = newidx;
6565
6566 // If the current list is modified and it is displayed in the quickfix
6567 // window, then Update it.
6568 if (qi->qf_lists[qi->qf_curlist].qf_id == qfl->qf_id)
6569 qf_win_pos_update(qi, old_qfidx);
6570
6571 return OK;
6572}
6573
6574/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006575 * Set quickfix/location list properties (title, items, context).
6576 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006577 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006578 */
6579 static int
6580qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6581{
6582 dictitem_T *di;
6583 int retval = FAIL;
6584 int qf_idx;
6585 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006586 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006587
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006588 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006589 newlist = TRUE;
6590
6591 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006592 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006593 return FAIL;
6594
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006595 if (newlist)
6596 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006597 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006598 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006599 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006600 }
6601
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006602 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaard823fa92016-08-12 16:29:27 +02006603 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006604 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006605 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006606 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006607 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006608 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006609 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006610 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006611 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6612 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006613
Bram Moolenaarb254af32017-12-18 19:48:58 +01006614 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006615 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006616
Bram Moolenaard823fa92016-08-12 16:29:27 +02006617 return retval;
6618}
6619
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006620/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006621 * Find the non-location list window with the specified location list stack in
6622 * the current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006623 */
6624 static win_T *
6625find_win_with_ll(qf_info_T *qi)
6626{
6627 win_T *wp = NULL;
6628
6629 FOR_ALL_WINDOWS(wp)
6630 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6631 return wp;
6632
6633 return NULL;
6634}
6635
6636/*
6637 * Free the entire quickfix/location list stack.
6638 * If the quickfix/location list window is open, then clear it.
6639 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006640 static void
6641qf_free_stack(win_T *wp, qf_info_T *qi)
6642{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006643 win_T *qfwin = qf_find_win(qi);
6644 win_T *llwin = NULL;
6645 win_T *orig_wp = wp;
6646
6647 if (qfwin != NULL)
6648 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006649 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006650 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006651 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006652 qf_update_buffer(qi, NULL);
6653 }
6654
6655 if (wp != NULL && IS_LL_WINDOW(wp))
6656 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006657 // If in the location list window, then use the non-location list
6658 // window with this location list (if present)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006659 llwin = find_win_with_ll(qi);
6660 if (llwin != NULL)
6661 wp = llwin;
6662 }
6663
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006664 qf_free_all(wp);
6665 if (wp == NULL)
6666 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006667 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006668 qi->qf_curlist = 0;
6669 qi->qf_listcount = 0;
6670 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006671 else if (IS_LL_WINDOW(orig_wp))
6672 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006673 // If the location list window is open, then create a new empty
6674 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006675 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006676
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006677 // first free the list reference in the location list window
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006678 ll_free_all(&orig_wp->w_llist_ref);
6679
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006680 orig_wp->w_llist_ref = new_ll;
6681 if (llwin != NULL)
6682 {
6683 llwin->w_llist = new_ll;
6684 new_ll->qf_refcount++;
6685 }
6686 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006687}
6688
Bram Moolenaard823fa92016-08-12 16:29:27 +02006689/*
6690 * Populate the quickfix list with the items supplied in the list
6691 * of dictionaries. "title" will be copied to w:quickfix_title.
6692 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6693 */
6694 int
6695set_errorlist(
6696 win_T *wp,
6697 list_T *list,
6698 int action,
6699 char_u *title,
6700 dict_T *what)
6701{
6702 qf_info_T *qi = &ql_info;
6703 int retval = OK;
6704
6705 if (wp != NULL)
6706 {
6707 qi = ll_get_or_alloc_list(wp);
6708 if (qi == NULL)
6709 return FAIL;
6710 }
6711
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006712 if (action == 'f')
6713 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006714 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006715 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006716 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006717 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006718
6719 incr_quickfix_busy();
6720
6721 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006722 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006723 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006724 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006725 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006726 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006727 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006728 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006729
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006730 decr_quickfix_busy();
6731
Bram Moolenaard823fa92016-08-12 16:29:27 +02006732 return retval;
6733}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006734
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006735/*
6736 * Mark the context as in use for all the lists in a quickfix stack.
6737 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006738 static int
6739mark_quickfix_ctx(qf_info_T *qi, int copyID)
6740{
6741 int i;
6742 int abort = FALSE;
6743 typval_T *ctx;
6744
6745 for (i = 0; i < LISTCOUNT && !abort; ++i)
6746 {
6747 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006748 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6749 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006750 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6751 }
6752
6753 return abort;
6754}
6755
6756/*
6757 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006758 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006759 */
6760 int
6761set_ref_in_quickfix(int copyID)
6762{
6763 int abort = FALSE;
6764 tabpage_T *tp;
6765 win_T *win;
6766
6767 abort = mark_quickfix_ctx(&ql_info, copyID);
6768 if (abort)
6769 return abort;
6770
6771 FOR_ALL_TAB_WINDOWS(tp, win)
6772 {
6773 if (win->w_llist != NULL)
6774 {
6775 abort = mark_quickfix_ctx(win->w_llist, copyID);
6776 if (abort)
6777 return abort;
6778 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006779 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6780 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006781 // In a location list window and none of the other windows is
6782 // referring to this location list. Mark the location list
6783 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006784 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6785 if (abort)
6786 return abort;
6787 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006788 }
6789
6790 return abort;
6791}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006792#endif
6793
Bram Moolenaar81695252004-12-29 20:58:21 +00006794/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006795 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006796 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006797 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006798 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006799 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006800 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006801 */
6802 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006803ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006804{
6805 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006806 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006807 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006808 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006809 int_u save_qfid;
6810 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006811
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006812 switch (eap->cmdidx)
6813 {
6814 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6815 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6816 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6817 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6818 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6819 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6820 default: break;
6821 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006822 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6823 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006824 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006825#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006826 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006827 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006828#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006829 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006830
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006831 // Must come after autocommands.
Bram Moolenaar39665952018-08-15 20:59:48 +02006832 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006833 {
6834 qi = ll_get_or_alloc_list(curwin);
6835 if (qi == NULL)
6836 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006837 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006838 }
6839
Bram Moolenaar86b68352004-12-27 21:59:20 +00006840 if (*eap->arg == NUL)
6841 buf = curbuf;
6842 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6843 buf = buflist_findnr(atoi((char *)eap->arg));
6844 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006845 emsg(_(e_invarg));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006846 else if (buf->b_ml.ml_mfp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006847 emsg(_("E681: Buffer is not loaded"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006848 else
6849 {
6850 if (eap->addr_count == 0)
6851 {
6852 eap->line1 = 1;
6853 eap->line2 = buf->b_ml.ml_line_count;
6854 }
6855 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6856 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006857 emsg(_(e_invrange));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006858 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006859 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006860 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006861
6862 if (buf->b_sfname)
6863 {
6864 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6865 (char *)qf_title, (char *)buf->b_sfname);
6866 qf_title = IObuff;
6867 }
6868
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006869 incr_quickfix_busy();
6870
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006871 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006872 (eap->cmdidx != CMD_caddbuffer
6873 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006874 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006875 qf_title, NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006876 if (qf_stack_empty(qi))
6877 {
6878 decr_quickfix_busy();
6879 return;
6880 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006881 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006882 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006883
6884 // Remember the current quickfix list identifier, so that we can
6885 // check for autocommands changing the current quickfix list.
6886 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006887 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006888 {
6889 buf_T *curbuf_old = curbuf;
6890
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006891 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6892 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006893 if (curbuf != curbuf_old)
6894 // Autocommands changed buffer, don't jump now, "qi" may
6895 // be invalid.
6896 res = 0;
6897 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006898 // Jump to the first error for a new list and if autocmds didn't
6899 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006900 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006901 eap->cmdidx == CMD_lbuffer)
6902 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006903 // display the first error
6904 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006905
6906 decr_quickfix_busy();
Bram Moolenaar754b5602006-02-09 23:53:20 +00006907 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006908 }
6909}
6910
Bram Moolenaar1e015462005-09-25 22:16:38 +00006911#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006912/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006913 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6914 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006915 */
6916 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006917ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006918{
6919 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006920 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006921 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006922 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006923 int_u save_qfid;
6924 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006925
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006926 switch (eap->cmdidx)
6927 {
6928 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6929 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6930 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6931 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6932 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6933 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6934 default: break;
6935 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006936 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6937 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006938 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006939#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006940 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006941 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006942#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006943 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006944
Bram Moolenaar39665952018-08-15 20:59:48 +02006945 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006946 {
6947 qi = ll_get_or_alloc_list(curwin);
6948 if (qi == NULL)
6949 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006950 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006951 }
6952
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006953 // Evaluate the expression. When the result is a string or a list we can
6954 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006955 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006956 if (tv != NULL)
6957 {
6958 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6959 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6960 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006961 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006962 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006963 (eap->cmdidx != CMD_caddexpr
6964 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006965 (linenr_T)0, (linenr_T)0,
6966 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006967 if (qf_stack_empty(qi))
6968 {
6969 decr_quickfix_busy();
6970 goto cleanup;
6971 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006972 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006973 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006974
6975 // Remember the current quickfix list identifier, so that we can
6976 // check for autocommands changing the current quickfix list.
6977 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006978 if (au_name != NULL)
6979 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6980 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006981
6982 // Jump to the first error for a new list and if autocmds didn't
6983 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006984 if (res > 0 && (eap->cmdidx == CMD_cexpr
6985 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006986 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006987 // display the first error
6988 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006989 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006990 }
6991 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006992 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006993cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00006994 free_tv(tv);
6995 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006996}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006997#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006998
6999/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007000 * Get the location list for ":lhelpgrep"
7001 */
7002 static qf_info_T *
7003hgr_get_ll(int *new_ll)
7004{
7005 win_T *wp;
7006 qf_info_T *qi;
7007
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007008 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007009 if (bt_help(curwin->w_buffer))
7010 wp = curwin;
7011 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007012 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007013 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007014
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007015 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007016 qi = NULL;
7017 else
7018 qi = wp->w_llist;
7019
7020 if (qi == NULL)
7021 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007022 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007023 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007024 return NULL;
7025 *new_ll = TRUE;
7026 }
7027
7028 return qi;
7029}
7030
7031/*
7032 * Search for a pattern in a help file.
7033 */
7034 static void
7035hgr_search_file(
7036 qf_info_T *qi,
7037 char_u *fname,
7038#ifdef FEAT_MBYTE
7039 vimconv_T *p_vc,
7040#endif
7041 regmatch_T *p_regmatch)
7042{
7043 FILE *fd;
7044 long lnum;
7045
7046 fd = mch_fopen((char *)fname, "r");
7047 if (fd == NULL)
7048 return;
7049
7050 lnum = 1;
7051 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7052 {
7053 char_u *line = IObuff;
7054#ifdef FEAT_MBYTE
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007055 // Convert a line if 'encoding' is not utf-8 and
7056 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007057 if (p_vc->vc_type != CONV_NONE
7058 && has_non_ascii(IObuff))
7059 {
7060 line = string_convert(p_vc, IObuff, NULL);
7061 if (line == NULL)
7062 line = IObuff;
7063 }
7064#endif
7065
7066 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7067 {
7068 int l = (int)STRLEN(line);
7069
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007070 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007071 while (l > 0 && line[l - 1] <= ' ')
7072 line[--l] = NUL;
7073
7074 if (qf_add_entry(qi,
7075 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007076 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007077 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007078 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007079 0,
7080 line,
7081 lnum,
7082 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007083 + 1, // col
7084 FALSE, // vis_col
7085 NULL, // search pattern
7086 0, // nr
7087 1, // type
7088 TRUE // valid
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007089 ) == FAIL)
7090 {
7091 got_int = TRUE;
7092#ifdef FEAT_MBYTE
7093 if (line != IObuff)
7094 vim_free(line);
7095#endif
7096 break;
7097 }
7098 }
7099#ifdef FEAT_MBYTE
7100 if (line != IObuff)
7101 vim_free(line);
7102#endif
7103 ++lnum;
7104 line_breakcheck();
7105 }
7106 fclose(fd);
7107}
7108
7109/*
7110 * Search for a pattern in all the help files in the doc directory under
7111 * the given directory.
7112 */
7113 static void
7114hgr_search_files_in_dir(
7115 qf_info_T *qi,
7116 char_u *dirname,
7117 regmatch_T *p_regmatch
7118#ifdef FEAT_MBYTE
7119 , vimconv_T *p_vc
7120#endif
7121#ifdef FEAT_MULTI_LANG
7122 , char_u *lang
7123#endif
7124 )
7125{
7126 int fcount;
7127 char_u **fnames;
7128 int fi;
7129
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007130 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007131 add_pathsep(dirname);
7132 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7133 if (gen_expand_wildcards(1, &dirname, &fcount,
7134 &fnames, EW_FILE|EW_SILENT) == OK
7135 && fcount > 0)
7136 {
7137 for (fi = 0; fi < fcount && !got_int; ++fi)
7138 {
7139#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007140 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007141 if (lang != NULL
7142 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007143 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007144 && !(STRNICMP(lang, "en", 2) == 0
7145 && STRNICMP("txt", fnames[fi]
7146 + STRLEN(fnames[fi]) - 3, 3) == 0))
7147 continue;
7148#endif
7149
7150 hgr_search_file(qi, fnames[fi],
7151#ifdef FEAT_MBYTE
7152 p_vc,
7153#endif
7154 p_regmatch);
7155 }
7156 FreeWild(fcount, fnames);
7157 }
7158}
7159
7160/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007161 * Search for a pattern in all the help files in the 'runtimepath'
7162 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007163 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007164 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007165 */
7166 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007167hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007168{
7169 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007170
7171#ifdef FEAT_MBYTE
7172 vimconv_T vc;
7173
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007174 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7175 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007176 vc.vc_type = CONV_NONE;
7177 if (!enc_utf8)
7178 convert_setup(&vc, (char_u *)"utf-8", p_enc);
7179#endif
7180
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007181 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007182 p = p_rtp;
7183 while (*p != NUL && !got_int)
7184 {
7185 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7186
7187 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
7188#ifdef FEAT_MBYTE
7189 , &vc
7190#endif
7191#ifdef FEAT_MULTI_LANG
7192 , lang
7193#endif
7194 );
7195 }
7196
7197#ifdef FEAT_MBYTE
7198 if (vc.vc_type != CONV_NONE)
7199 convert_setup(&vc, NULL, NULL);
7200#endif
7201}
7202
7203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 * ":helpgrep {pattern}"
7205 */
7206 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007207ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208{
7209 regmatch_T regmatch;
7210 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007211 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007212 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007213 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007214 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
Bram Moolenaar73633f82012-01-20 13:39:07 +01007216 switch (eap->cmdidx)
7217 {
7218 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7219 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7220 default: break;
7221 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007222 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7223 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007224 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007225#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007226 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007227 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007228#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007229 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007230
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007231 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01007232 save_cpo = p_cpo;
7233 p_cpo = empty_option;
7234
Bram Moolenaar39665952018-08-15 20:59:48 +02007235 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007236 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007237 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007238 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007239 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007240 }
7241
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007242 incr_quickfix_busy();
7243
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007244#ifdef FEAT_MULTI_LANG
7245 // Check for a specified language
7246 lang = check_help_lang(eap->arg);
7247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7249 regmatch.rm_ic = FALSE;
7250 if (regmatch.regprog != NULL)
7251 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007252 qf_list_T *qfl;
7253
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007254 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007255 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007257 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007258
Bram Moolenaar473de612013-06-08 18:19:48 +02007259 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007261 qfl = &qi->qf_lists[qi->qf_curlist];
7262 qfl->qf_nonevalid = FALSE;
7263 qfl->qf_ptr = qfl->qf_start;
7264 qfl->qf_index = 1;
7265 qf_list_changed(qfl);
7266 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 }
7268
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007269 if (p_cpo == empty_option)
7270 p_cpo = save_cpo;
7271 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007272 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007273 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
Bram Moolenaar73633f82012-01-20 13:39:07 +01007275 if (au_name != NULL)
7276 {
7277 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7278 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007279 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007280 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007281 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007282 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007283 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007284 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007285 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007286
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007287 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007288 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007289 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007290 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007291 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007292
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007293 decr_quickfix_busy();
7294
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007295 if (eap->cmdidx == CMD_lhelpgrep)
7296 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007297 // If the help window is not opened or if it already points to the
7298 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007299 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007300 {
7301 if (new_qi)
7302 ll_free_all(&qi);
7303 }
7304 else if (curwin->w_llist == NULL)
7305 curwin->w_llist = qi;
7306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307}
7308
7309#endif /* FEAT_QUICKFIX */