blob: e292819f2ec0d59906ab67386503b013814271fa [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * quickfix.c: functions for quickfix mode, using a file with error messages
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_QUICKFIX) || defined(PROTO)
17
18struct dir_stack_T
19{
20 struct dir_stack_T *next;
21 char_u *dirname;
22};
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024/*
Bram Moolenaar68b76a62005-03-25 21:53:48 +000025 * For each error the next struct is allocated and linked in a list.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
Bram Moolenaar68b76a62005-03-25 21:53:48 +000027typedef struct qfline_S qfline_T;
28struct qfline_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020030 qfline_T *qf_next; // pointer to next error in the list
31 qfline_T *qf_prev; // pointer to previous error in the list
32 linenr_T qf_lnum; // line number where the error occurred
33 int qf_fnum; // file number for the line
34 int qf_col; // column where the error occurred
35 int qf_nr; // error number
36 char_u *qf_module; // module name for this error
37 char_u *qf_pattern; // search pattern for the error
38 char_u *qf_text; // description of the error
39 char_u qf_viscol; // set to TRUE if qf_col is screen column
40 char_u qf_cleared; // set to TRUE if line has been deleted
41 char_u qf_type; // type of the error (mostly 'E'); 1 for
42 // :helpgrep
43 char_u qf_valid; // valid error message detected
Bram Moolenaar071d4272004-06-13 20:20:40 +000044};
45
46/*
47 * There is a stack of error lists.
48 */
49#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020050#define INVALID_QFIDX (-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020052/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010053 * Quickfix list type.
54 */
55typedef enum
56{
57 QFLT_QUICKFIX, // Quickfix list - global list
58 QFLT_LOCATION, // Location list - per window list
59 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
60} qfltype_T;
61
62/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020063 * Quickfix/Location list definition
64 * Contains a list of entries (qfline_T). qf_start points to the first entry
65 * and qf_last points to the last entry. qf_count contains the list size.
66 *
67 * Usually the list contains one or more entries. But an empty list can be
68 * created using setqflist()/setloclist() with a title and/or user context
69 * information and entries can be added later using setqflist()/setloclist().
70 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000071typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000072{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020073 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010074 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020075 qfline_T *qf_start; // pointer to the first error
76 qfline_T *qf_last; // pointer to the last error
77 qfline_T *qf_ptr; // pointer to the current error
78 int qf_count; // number of errors (0 means empty list)
79 int qf_index; // current index in the error list
80 int qf_nonevalid; // TRUE if not a single valid entry found
81 char_u *qf_title; // title derived from the command that created
82 // the error list or set by setqflist
83 typval_T *qf_ctx; // context set by setqflist/setloclist
Bram Moolenaara7df8c72017-07-19 13:23:06 +020084
85 struct dir_stack_T *qf_dir_stack;
86 char_u *qf_directory;
87 struct dir_stack_T *qf_file_stack;
88 char_u *qf_currfile;
89 int qf_multiline;
90 int qf_multiignore;
91 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010092 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000093} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020095/*
96 * Quickfix/Location list stack definition
97 * Contains a list of quickfix/location lists (qf_list_T)
98 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000099struct qf_info_S
100{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200101 // Count of references to this list. Used only for location lists.
102 // When a location list window reference this list, qf_refcount
103 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
104 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000105 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200106 int qf_listcount; // current number of lists
107 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000108 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100109 qfltype_T qfl_type; // type of list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110};
111
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200112static qf_info_T ql_info; // global quickfix list
113static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200115#define FMT_PATTERNS 11 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
117/*
118 * Structure used to hold the info of one part of 'errorformat'
119 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000120typedef struct efm_S efm_T;
121struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200123 regprog_T *prog; // pre-formatted part of 'errorformat'
124 efm_T *next; // pointer to next (NULL if last)
125 char_u addr[FMT_PATTERNS]; // indices of used % patterns
126 char_u prefix; // prefix of this format line:
127 // 'D' enter directory
128 // 'X' leave directory
129 // 'A' start of multi-line message
130 // 'E' error message
131 // 'W' warning message
132 // 'I' informational message
133 // 'C' continuation line
134 // 'Z' end of multi-line message
135 // 'G' general, unspecific message
136 // 'P' push file (partial) message
137 // 'Q' pop/quit file (partial) message
138 // 'O' overread (partial) message
139 char_u flags; // additional flags given in prefix
140 // '-' do not include this line
141 // '+' include whole line in message
142 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143};
144
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200145// List of location lists to be deleted.
146// Used to delay the deletion of locations lists by autocmds.
147typedef struct qf_delq_S
148{
149 struct qf_delq_S *next;
150 qf_info_T *qi;
151} qf_delq_T;
152static qf_delq_T *qf_delq_head = NULL;
153
154// Counter to prevent autocmds from freeing up location lists when they are
155// still being used.
156static int quickfix_busy = 0;
157
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200158static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100159
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Bram Moolenaard76ce852018-05-01 15:02:04 +0200161static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200162static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static char_u *qf_types(int, int);
Bram Moolenaara7df8c72017-07-19 13:23:06 +0200164static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200165static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100166static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200167static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100169static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static win_T *qf_find_win(qf_info_T *qi);
171static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200172static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200173static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
175static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
176static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
177static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200179// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000180#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200181// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000182#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200183
184// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100185#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
186#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
187#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
188#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200189
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000190/*
191 * Return location list for window 'wp'
192 * For location list window, return the referenced location list
193 */
194#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200197 * Looking up a buffer can be slow if there are many. Remember the last one
198 * to make this a lot faster if there are multiple matches in the same file.
199 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200200static char_u *qf_last_bufname = NULL;
201static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200202
Bram Moolenaar3c097222017-12-21 20:54:49 +0100203static char *e_loc_list_changed =
204 N_("E926: Current location list was changed");
205
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200206/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200207 * Maximum number of bytes allowed per line while reading a errorfile.
208 */
209#define LINE_MAXLEN 4096
210
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200211static struct fmtpattern
212{
213 char_u convchar;
214 char *pattern;
215} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200216 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200217 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200218 {'n', "\\d\\+"},
219 {'l', "\\d\\+"},
220 {'c', "\\d\\+"},
221 {'t', "."},
222 {'m', ".\\+"},
223 {'r', ".*"},
224 {'p', "[- .]*"},
225 {'v', "\\d\\+"},
226 {'s', ".\\+"},
227 {'o', ".\\+"}
228 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200229
230/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200231 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200232 * See fmt_pat definition above for the list of supported patterns. The
233 * pattern specifier is supplied in "efmpat". The converted pattern is stored
234 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200235 */
236 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200237efmpat_to_regpat(
238 char_u *efmpat,
239 char_u *regpat,
240 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200241 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100242 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200243{
244 char_u *srcptr;
245
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200246 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200247 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200248 // Each errorformat pattern can occur only once
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100249 semsg(_("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200250 return NULL;
251 }
252 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200253 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200254 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200255 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200256 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100257 semsg(_("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200258 return NULL;
259 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200260 efminfo->addr[idx] = (char_u)++round;
261 *regpat++ = '\\';
262 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200263#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200264 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200265 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200266 // Also match "c:" in the file name, even when
267 // checking for a colon next: "%f:".
268 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200269 STRCPY(regpat, "\\%(\\a:\\)\\=");
270 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200271 }
272#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200273 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200274 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200275 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200276 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200277 // A file name may contain spaces, but this isn't
278 // in "\f". For "%f:%l:%m" there may be a ":" in
279 // the file name. Use ".\{-1,}x" instead (x is
280 // the next character), the requirement that :999:
281 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200282 STRCPY(regpat, ".\\{-1,}");
283 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200284 }
285 else
286 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200287 // File name followed by '\\' or '%': include as
288 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200289 STRCPY(regpat, "\\f\\+");
290 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200291 }
292 }
293 else
294 {
295 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200296 while ((*regpat = *srcptr++) != NUL)
297 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200298 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200299 *regpat++ = '\\';
300 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200301
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200302 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200303}
304
305/*
306 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200307 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200308 */
309 static char_u *
310scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200311 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200312 char_u *efm,
313 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100314 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200315{
316 char_u *efmp = *pefmp;
317
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200318 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200319 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200320 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200321 {
322 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200323 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200324 if (efmp < efm + len)
325 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200326 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200327 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200328 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200329 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200330 if (efmp == efm + len)
331 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100332 emsg(_("E374: Missing ] in format string"));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200333 return NULL;
334 }
335 }
336 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200337 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200338 *regpat++ = *++efmp;
339 *regpat++ = '\\';
340 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200341 }
342 else
343 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200344 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100345 semsg(_("E375: Unsupported %%%c in format string"), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200346 return NULL;
347 }
348
349 *pefmp = efmp;
350
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200351 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200352}
353
354/*
355 * Analyze/parse an errorformat prefix.
356 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200357 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100358efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200359{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200360 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200361 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200362 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200363 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200364 else
365 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100366 semsg(_("E376: Invalid %%%c in format string prefix"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200367 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200368 }
369
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200370 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200371}
372
373/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200374 * Converts a 'errorformat' string part in 'efm' to a regular expression
375 * pattern. The resulting regex pattern is returned in "regpat". Additional
376 * information about the 'erroformat' pattern is returned in "fmt_ptr".
377 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200378 */
379 static int
380efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200381 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200382 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200383 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100384 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200385{
386 char_u *ptr;
387 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200388 int round;
389 int idx = 0;
390
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200391 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200392 ptr = regpat;
393 *ptr++ = '^';
394 round = 0;
395 for (efmp = efm; efmp < efm + len; ++efmp)
396 {
397 if (*efmp == '%')
398 {
399 ++efmp;
400 for (idx = 0; idx < FMT_PATTERNS; ++idx)
401 if (fmt_pat[idx].convchar == *efmp)
402 break;
403 if (idx < FMT_PATTERNS)
404 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100405 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200406 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200407 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200408 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200409 }
410 else if (*efmp == '*')
411 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200412 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100413 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200414 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200415 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200416 }
417 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200418 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200419 else if (*efmp == '#')
420 *ptr++ = '*';
421 else if (*efmp == '>')
422 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200423 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200424 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200425 // prefix is allowed only at the beginning of the errorformat
426 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100427 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200428 if (efmp == NULL)
429 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200430 }
431 else
432 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100433 semsg(_("E377: Invalid %%%c in format string"), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200434 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200435 }
436 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200437 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200438 {
439 if (*efmp == '\\' && efmp + 1 < efm + len)
440 ++efmp;
441 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200442 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200443 if (*efmp)
444 *ptr++ = *efmp;
445 }
446 }
447 *ptr++ = '$';
448 *ptr = NUL;
449
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200450 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200451}
452
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200453/*
454 * Free the 'errorformat' information list
455 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200456 static void
457free_efm_list(efm_T **efm_first)
458{
459 efm_T *efm_ptr;
460
461 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
462 {
463 *efm_first = efm_ptr->next;
464 vim_regfree(efm_ptr->prog);
465 vim_free(efm_ptr);
466 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100467 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200468}
469
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200470/*
471 * Compute the size of the buffer used to convert a 'errorformat' pattern into
472 * a regular expression pattern.
473 */
474 static int
475efm_regpat_bufsz(char_u *efm)
476{
477 int sz;
478 int i;
479
480 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
481 for (i = FMT_PATTERNS; i > 0; )
482 sz += (int)STRLEN(fmt_pat[--i].pattern);
483#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200484 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200485#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200486 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200487#endif
488
489 return sz;
490}
491
492/*
493 * Return the length of a 'errorformat' option part (separated by ",").
494 */
495 static int
496efm_option_part_len(char_u *efm)
497{
498 int len;
499
500 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
501 if (efm[len] == '\\' && efm[len + 1] != NUL)
502 ++len;
503
504 return len;
505}
506
507/*
508 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
509 * are parsed and converted to regular expressions. Returns information about
510 * the parsed 'errorformat' option.
511 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200512 static efm_T *
513parse_efm_option(char_u *efm)
514{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200515 efm_T *fmt_ptr = NULL;
516 efm_T *fmt_first = NULL;
517 efm_T *fmt_last = NULL;
518 char_u *fmtstr = NULL;
519 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200520 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200521
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200522 // Each part of the format string is copied and modified from errorformat
523 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200524
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200525 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200526 sz = efm_regpat_bufsz(efm);
527 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200528 goto parse_efm_error;
529
530 while (efm[0] != NUL)
531 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200532 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200533 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
534 if (fmt_ptr == NULL)
535 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200536 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200537 fmt_first = fmt_ptr;
538 else
539 fmt_last->next = fmt_ptr;
540 fmt_last = fmt_ptr;
541
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200542 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200543 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200544
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100545 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200546 goto parse_efm_error;
547 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
548 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200549 // Advance to next part
550 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200551 }
552
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200553 if (fmt_first == NULL) // nothing found
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100554 emsg(_("E378: 'errorformat' contains no pattern"));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200555
556 goto parse_efm_end;
557
558parse_efm_error:
559 free_efm_list(&fmt_first);
560
561parse_efm_end:
562 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200563
564 return fmt_first;
565}
566
Bram Moolenaare0d37972016-07-15 22:36:01 +0200567enum {
568 QF_FAIL = 0,
569 QF_OK = 1,
570 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200571 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200572 QF_IGNORE_LINE = 4,
573 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200574};
575
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200576/*
577 * State information used to parse lines and add entries to a quickfix/location
578 * list.
579 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200580typedef struct {
581 char_u *linebuf;
582 int linelen;
583 char_u *growbuf;
584 int growbufsiz;
585 FILE *fd;
586 typval_T *tv;
587 char_u *p_str;
588 listitem_T *p_li;
589 buf_T *buf;
590 linenr_T buflnum;
591 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100592 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200593} qfstate_T;
594
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200595/*
596 * Allocate more memory for the line buffer used for parsing lines.
597 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200598 static char_u *
599qf_grow_linebuf(qfstate_T *state, int newsz)
600{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200601 char_u *p;
602
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200603 // If the line exceeds LINE_MAXLEN exclude the last
604 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200605 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
606 if (state->growbuf == NULL)
607 {
608 state->growbuf = alloc(state->linelen + 1);
609 if (state->growbuf == NULL)
610 return NULL;
611 state->growbufsiz = state->linelen;
612 }
613 else if (state->linelen > state->growbufsiz)
614 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200615 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200616 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200617 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200618 state->growbufsiz = state->linelen;
619 }
620 return state->growbuf;
621}
622
623/*
624 * Get the next string (separated by newline) from state->p_str.
625 */
626 static int
627qf_get_next_str_line(qfstate_T *state)
628{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200629 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200630 char_u *p_str = state->p_str;
631 char_u *p;
632 int len;
633
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200634 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200635 return QF_END_OF_INPUT;
636
637 p = vim_strchr(p_str, '\n');
638 if (p != NULL)
639 len = (int)(p - p_str) + 1;
640 else
641 len = (int)STRLEN(p_str);
642
643 if (len > IOSIZE - 2)
644 {
645 state->linebuf = qf_grow_linebuf(state, len);
646 if (state->linebuf == NULL)
647 return QF_NOMEM;
648 }
649 else
650 {
651 state->linebuf = IObuff;
652 state->linelen = len;
653 }
654 vim_strncpy(state->linebuf, p_str, state->linelen);
655
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200656 // Increment using len in order to discard the rest of the
657 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200658 p_str += len;
659 state->p_str = p_str;
660
661 return QF_OK;
662}
663
664/*
665 * Get the next string from state->p_Li.
666 */
667 static int
668qf_get_next_list_line(qfstate_T *state)
669{
670 listitem_T *p_li = state->p_li;
671 int len;
672
673 while (p_li != NULL
674 && (p_li->li_tv.v_type != VAR_STRING
675 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200676 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200677
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200678 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200679 {
680 state->p_li = NULL;
681 return QF_END_OF_INPUT;
682 }
683
684 len = (int)STRLEN(p_li->li_tv.vval.v_string);
685 if (len > IOSIZE - 2)
686 {
687 state->linebuf = qf_grow_linebuf(state, len);
688 if (state->linebuf == NULL)
689 return QF_NOMEM;
690 }
691 else
692 {
693 state->linebuf = IObuff;
694 state->linelen = len;
695 }
696
697 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
698
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200699 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200700 return QF_OK;
701}
702
703/*
704 * Get the next string from state->buf.
705 */
706 static int
707qf_get_next_buf_line(qfstate_T *state)
708{
709 char_u *p_buf = NULL;
710 int len;
711
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200712 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200713 if (state->buflnum > state->lnumlast)
714 return QF_END_OF_INPUT;
715
716 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
717 state->buflnum += 1;
718
719 len = (int)STRLEN(p_buf);
720 if (len > IOSIZE - 2)
721 {
722 state->linebuf = qf_grow_linebuf(state, len);
723 if (state->linebuf == NULL)
724 return QF_NOMEM;
725 }
726 else
727 {
728 state->linebuf = IObuff;
729 state->linelen = len;
730 }
731 vim_strncpy(state->linebuf, p_buf, state->linelen);
732
733 return QF_OK;
734}
735
736/*
737 * Get the next string from file state->fd.
738 */
739 static int
740qf_get_next_file_line(qfstate_T *state)
741{
742 int discard;
743 int growbuflen;
744
745 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
746 return QF_END_OF_INPUT;
747
748 discard = FALSE;
749 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200750 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200751 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200752 // The current line exceeds IObuff, continue reading using
753 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200754 if (state->growbuf == NULL)
755 {
756 state->growbufsiz = 2 * (IOSIZE - 1);
757 state->growbuf = alloc(state->growbufsiz);
758 if (state->growbuf == NULL)
759 return QF_NOMEM;
760 }
761
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200762 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200763 memcpy(state->growbuf, IObuff, IOSIZE - 1);
764 growbuflen = state->linelen;
765
766 for (;;)
767 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200768 char_u *p;
769
Bram Moolenaare0d37972016-07-15 22:36:01 +0200770 if (fgets((char *)state->growbuf + growbuflen,
771 state->growbufsiz - growbuflen, state->fd) == NULL)
772 break;
773 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
774 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200775 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200776 break;
777 if (state->growbufsiz == LINE_MAXLEN)
778 {
779 discard = TRUE;
780 break;
781 }
782
783 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
784 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200785 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200786 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200787 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200788 }
789
790 while (discard)
791 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200792 // The current line is longer than LINE_MAXLEN, continue
793 // reading but discard everything until EOL or EOF is
794 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200795 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
796 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200797 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200798 break;
799 }
800
801 state->linebuf = state->growbuf;
802 state->linelen = growbuflen;
803 }
804 else
805 state->linebuf = IObuff;
806
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200807 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200808 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
809 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100810 char_u *line;
811
812 line = string_convert(&state->vc, state->linebuf, &state->linelen);
813 if (line != NULL)
814 {
815 if (state->linelen < IOSIZE)
816 {
817 STRCPY(state->linebuf, line);
818 vim_free(line);
819 }
820 else
821 {
822 vim_free(state->growbuf);
823 state->linebuf = state->growbuf = line;
824 state->growbufsiz = state->linelen < LINE_MAXLEN
825 ? state->linelen : LINE_MAXLEN;
826 }
827 }
828 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100829
Bram Moolenaare0d37972016-07-15 22:36:01 +0200830 return QF_OK;
831}
832
833/*
834 * Get the next string from a file/buffer/list/string.
835 */
836 static int
837qf_get_nextline(qfstate_T *state)
838{
839 int status = QF_FAIL;
840
841 if (state->fd == NULL)
842 {
843 if (state->tv != NULL)
844 {
845 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200846 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200847 status = qf_get_next_str_line(state);
848 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200849 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200850 status = qf_get_next_list_line(state);
851 }
852 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200853 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200854 status = qf_get_next_buf_line(state);
855 }
856 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200857 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200858 status = qf_get_next_file_line(state);
859
860 if (status != QF_OK)
861 return status;
862
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200863 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200864 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200865 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200866 state->linebuf[state->linelen - 1] = NUL;
867#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200868 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
869 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200870#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200871 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200872
Bram Moolenaare0d37972016-07-15 22:36:01 +0200873 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200874
875 return QF_OK;
876}
877
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200878typedef struct {
879 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200880 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200881 char_u *errmsg;
882 int errmsglen;
883 long lnum;
884 int col;
885 char_u use_viscol;
886 char_u *pattern;
887 int enr;
888 int type;
889 int valid;
890} qffields_T;
891
892/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200893 * Parse the match for filename ('%f') pattern in regmatch.
894 * Return the matched value in "fields->namebuf".
895 */
896 static int
897qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
898{
899 int c;
900
901 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
902 return QF_FAIL;
903
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200904 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200905 c = *rmp->endp[midx];
906 *rmp->endp[midx] = NUL;
907 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
908 *rmp->endp[midx] = c;
909
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200910 // For separate filename patterns (%O, %P and %Q), the specified file
911 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200912 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
913 && mch_getperm(fields->namebuf) == -1)
914 return QF_FAIL;
915
916 return QF_OK;
917}
918
919/*
920 * Parse the match for error number ('%n') pattern in regmatch.
921 * Return the matched value in "fields->enr".
922 */
923 static int
924qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
925{
926 if (rmp->startp[midx] == NULL)
927 return QF_FAIL;
928 fields->enr = (int)atol((char *)rmp->startp[midx]);
929 return QF_OK;
930}
931
932/*
933 * Parse the match for line number (%l') pattern in regmatch.
934 * Return the matched value in "fields->lnum".
935 */
936 static int
937qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
938{
939 if (rmp->startp[midx] == NULL)
940 return QF_FAIL;
941 fields->lnum = atol((char *)rmp->startp[midx]);
942 return QF_OK;
943}
944
945/*
946 * Parse the match for column number ('%c') pattern in regmatch.
947 * Return the matched value in "fields->col".
948 */
949 static int
950qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
951{
952 if (rmp->startp[midx] == NULL)
953 return QF_FAIL;
954 fields->col = (int)atol((char *)rmp->startp[midx]);
955 return QF_OK;
956}
957
958/*
959 * Parse the match for error type ('%t') pattern in regmatch.
960 * Return the matched value in "fields->type".
961 */
962 static int
963qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
964{
965 if (rmp->startp[midx] == NULL)
966 return QF_FAIL;
967 fields->type = *rmp->startp[midx];
968 return QF_OK;
969}
970
971/*
972 * Parse the match for '%+' format pattern. The whole matching line is included
973 * in the error string. Return the matched line in "fields->errmsg".
974 */
975 static int
976qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
977{
978 char_u *p;
979
980 if (linelen >= fields->errmsglen)
981 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200982 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200983 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
984 return QF_NOMEM;
985 fields->errmsg = p;
986 fields->errmsglen = linelen + 1;
987 }
988 vim_strncpy(fields->errmsg, linebuf, linelen);
989 return QF_OK;
990}
991
992/*
993 * Parse the match for error message ('%m') pattern in regmatch.
994 * Return the matched value in "fields->errmsg".
995 */
996 static int
997qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
998{
999 char_u *p;
1000 int len;
1001
1002 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1003 return QF_FAIL;
1004 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1005 if (len >= fields->errmsglen)
1006 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001007 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001008 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1009 return QF_NOMEM;
1010 fields->errmsg = p;
1011 fields->errmsglen = len + 1;
1012 }
1013 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1014 return QF_OK;
1015}
1016
1017/*
1018 * Parse the match for rest of a single-line file message ('%r') pattern.
1019 * Return the matched value in "tail".
1020 */
1021 static int
1022qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1023{
1024 if (rmp->startp[midx] == NULL)
1025 return QF_FAIL;
1026 *tail = rmp->startp[midx];
1027 return QF_OK;
1028}
1029
1030/*
1031 * Parse the match for the pointer line ('%p') pattern in regmatch.
1032 * Return the matched value in "fields->col".
1033 */
1034 static int
1035qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1036{
1037 char_u *match_ptr;
1038
1039 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1040 return QF_FAIL;
1041 fields->col = 0;
1042 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1043 ++match_ptr)
1044 {
1045 ++fields->col;
1046 if (*match_ptr == TAB)
1047 {
1048 fields->col += 7;
1049 fields->col -= fields->col % 8;
1050 }
1051 }
1052 ++fields->col;
1053 fields->use_viscol = TRUE;
1054 return QF_OK;
1055}
1056
1057/*
1058 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1059 * Return the matched value in "fields->col".
1060 */
1061 static int
1062qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1063{
1064 if (rmp->startp[midx] == NULL)
1065 return QF_FAIL;
1066 fields->col = (int)atol((char *)rmp->startp[midx]);
1067 fields->use_viscol = TRUE;
1068 return QF_OK;
1069}
1070
1071/*
1072 * Parse the match for the search text ('%s') pattern in regmatch.
1073 * Return the matched value in "fields->pattern".
1074 */
1075 static int
1076qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1077{
1078 int len;
1079
1080 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1081 return QF_FAIL;
1082 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1083 if (len > CMDBUFFSIZE - 5)
1084 len = CMDBUFFSIZE - 5;
1085 STRCPY(fields->pattern, "^\\V");
1086 STRNCAT(fields->pattern, rmp->startp[midx], len);
1087 fields->pattern[len + 3] = '\\';
1088 fields->pattern[len + 4] = '$';
1089 fields->pattern[len + 5] = NUL;
1090 return QF_OK;
1091}
1092
1093/*
1094 * Parse the match for the module ('%o') pattern in regmatch.
1095 * Return the matched value in "fields->module".
1096 */
1097 static int
1098qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1099{
1100 int len;
1101
1102 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1103 return QF_FAIL;
1104 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1105 if (len > CMDBUFFSIZE)
1106 len = CMDBUFFSIZE;
1107 STRNCAT(fields->module, rmp->startp[midx], len);
1108 return QF_OK;
1109}
1110
1111/*
1112 * 'errorformat' format pattern parser functions.
1113 * The '%f' and '%r' formats are parsed differently from other formats.
1114 * See qf_parse_match() for details.
1115 */
1116static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1117{
1118 NULL,
1119 qf_parse_fmt_n,
1120 qf_parse_fmt_l,
1121 qf_parse_fmt_c,
1122 qf_parse_fmt_t,
1123 qf_parse_fmt_m,
1124 NULL,
1125 qf_parse_fmt_p,
1126 qf_parse_fmt_v,
1127 qf_parse_fmt_s,
1128 qf_parse_fmt_o
1129};
1130
1131/*
1132 * Parse the error format pattern matches in "regmatch" and set the values in
1133 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1134 * match. Returns QF_OK if all the matches are successfully parsed. On
1135 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001136 */
1137 static int
1138qf_parse_match(
1139 char_u *linebuf,
1140 int linelen,
1141 efm_T *fmt_ptr,
1142 regmatch_T *regmatch,
1143 qffields_T *fields,
1144 int qf_multiline,
1145 int qf_multiscan,
1146 char_u **tail)
1147{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001148 int idx = fmt_ptr->prefix;
1149 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001150 int midx;
1151 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001152
1153 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1154 return QF_FAIL;
1155 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1156 fields->type = idx;
1157 else
1158 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001159
1160 // Extract error message data from matched line.
1161 // We check for an actual submatch, because "\[" and "\]" in
1162 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001163 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001164 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001165 status = QF_OK;
1166 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001167 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001168 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1169 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001170 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001171 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001172 status = qf_parse_fmt_plus(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001173 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001174 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001175 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001176 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001177 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001178 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001179 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001180
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001181 if (status != QF_OK)
1182 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001183 }
1184
1185 return QF_OK;
1186}
1187
1188/*
1189 * Parse an error line in 'linebuf' using a single error format string in
1190 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1191 * Returns QF_OK if the efm format matches completely and the fields are
1192 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1193 */
1194 static int
1195qf_parse_get_fields(
1196 char_u *linebuf,
1197 int linelen,
1198 efm_T *fmt_ptr,
1199 qffields_T *fields,
1200 int qf_multiline,
1201 int qf_multiscan,
1202 char_u **tail)
1203{
1204 regmatch_T regmatch;
1205 int status = QF_FAIL;
1206 int r;
1207
1208 if (qf_multiscan &&
1209 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1210 return QF_FAIL;
1211
1212 fields->namebuf[0] = NUL;
1213 fields->module[0] = NUL;
1214 fields->pattern[0] = NUL;
1215 if (!qf_multiscan)
1216 fields->errmsg[0] = NUL;
1217 fields->lnum = 0;
1218 fields->col = 0;
1219 fields->use_viscol = FALSE;
1220 fields->enr = -1;
1221 fields->type = 0;
1222 *tail = NULL;
1223
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001224 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001225 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001226 regmatch.regprog = fmt_ptr->prog;
1227 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1228 fmt_ptr->prog = regmatch.regprog;
1229 if (r)
1230 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1231 fields, qf_multiline, qf_multiscan, tail);
1232
1233 return status;
1234}
1235
1236/*
1237 * Parse directory error format prefixes (%D and %X).
1238 * Push and pop directories from the directory stack when scanning directory
1239 * names.
1240 */
1241 static int
1242qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1243{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001244 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001245 {
1246 if (*fields->namebuf == NUL)
1247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001248 emsg(_("E379: Missing or empty directory name"));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001249 return QF_FAIL;
1250 }
1251 qfl->qf_directory =
1252 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1253 if (qfl->qf_directory == NULL)
1254 return QF_FAIL;
1255 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001256 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001257 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1258
1259 return QF_OK;
1260}
1261
1262/*
1263 * Parse global file name error format prefixes (%O, %P and %Q).
1264 */
1265 static int
1266qf_parse_file_pfx(
1267 int idx,
1268 qffields_T *fields,
1269 qf_list_T *qfl,
1270 char_u *tail)
1271{
1272 fields->valid = FALSE;
1273 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1274 {
1275 if (*fields->namebuf && idx == 'P')
1276 qfl->qf_currfile =
1277 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1278 else if (idx == 'Q')
1279 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1280 *fields->namebuf = NUL;
1281 if (tail && *tail)
1282 {
1283 STRMOVE(IObuff, skipwhite(tail));
1284 qfl->qf_multiscan = TRUE;
1285 return QF_MULTISCAN;
1286 }
1287 }
1288
1289 return QF_OK;
1290}
1291
1292/*
1293 * Parse a non-error line (a line which doesn't match any of the error
1294 * format in 'efm').
1295 */
1296 static int
1297qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1298{
1299 char_u *p;
1300
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001301 fields->namebuf[0] = NUL; // no match found, remove file name
1302 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001303 fields->valid = FALSE;
1304 if (linelen >= fields->errmsglen)
1305 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001306 // linelen + null terminator
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001307 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1308 return QF_NOMEM;
1309 fields->errmsg = p;
1310 fields->errmsglen = linelen + 1;
1311 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001312 // copy whole line to error message
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001313 vim_strncpy(fields->errmsg, linebuf, linelen);
1314
1315 return QF_OK;
1316}
1317
1318/*
1319 * Parse multi-line error format prefixes (%C and %Z)
1320 */
1321 static int
1322qf_parse_multiline_pfx(
1323 qf_info_T *qi,
1324 int qf_idx,
1325 int idx,
1326 qf_list_T *qfl,
1327 qffields_T *fields)
1328{
1329 char_u *ptr;
1330 int len;
1331
1332 if (!qfl->qf_multiignore)
1333 {
1334 qfline_T *qfprev = qfl->qf_last;
1335
1336 if (qfprev == NULL)
1337 return QF_FAIL;
1338 if (*fields->errmsg && !qfl->qf_multiignore)
1339 {
1340 len = (int)STRLEN(qfprev->qf_text);
1341 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1342 == NULL)
1343 return QF_FAIL;
1344 STRCPY(ptr, qfprev->qf_text);
1345 vim_free(qfprev->qf_text);
1346 qfprev->qf_text = ptr;
1347 *(ptr += len) = '\n';
1348 STRCPY(++ptr, fields->errmsg);
1349 }
1350 if (qfprev->qf_nr == -1)
1351 qfprev->qf_nr = fields->enr;
1352 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001353 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001354 qfprev->qf_type = fields->type;
1355
1356 if (!qfprev->qf_lnum)
1357 qfprev->qf_lnum = fields->lnum;
1358 if (!qfprev->qf_col)
1359 qfprev->qf_col = fields->col;
1360 qfprev->qf_viscol = fields->use_viscol;
1361 if (!qfprev->qf_fnum)
1362 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1363 qfl->qf_directory,
1364 *fields->namebuf || qfl->qf_directory != NULL
1365 ? fields->namebuf
1366 : qfl->qf_currfile != NULL && fields->valid
1367 ? qfl->qf_currfile : 0);
1368 }
1369 if (idx == 'Z')
1370 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1371 line_breakcheck();
1372
1373 return QF_IGNORE_LINE;
1374}
1375
1376/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001377 * Parse a line and get the quickfix fields.
1378 * Return the QF_ status.
1379 */
1380 static int
1381qf_parse_line(
1382 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001383 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001384 char_u *linebuf,
1385 int linelen,
1386 efm_T *fmt_first,
1387 qffields_T *fields)
1388{
1389 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001390 int idx = 0;
1391 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001392 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001393 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001394
Bram Moolenaare333e792018-04-08 13:27:39 +02001395restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001396 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001397 if (fmt_start == NULL)
1398 fmt_ptr = fmt_first;
1399 else
1400 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001401 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001402 fmt_ptr = fmt_start;
1403 fmt_start = NULL;
1404 }
1405
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001406 // Try to match each part of 'errorformat' until we find a complete
1407 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001408 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001409 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1410 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001411 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001412 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1413 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1414 if (status == QF_NOMEM)
1415 return status;
1416 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001417 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001418 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001419 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001420
1421 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1422 {
1423 if (fmt_ptr != NULL)
1424 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001425 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001426 status = qf_parse_dir_pfx(idx, fields, qfl);
1427 if (status != QF_OK)
1428 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001429 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001430
1431 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1432 if (status != QF_OK)
1433 return status;
1434
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001435 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001436 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001437 }
1438 else if (fmt_ptr != NULL)
1439 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001440 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001441 if (fmt_ptr->conthere)
1442 fmt_start = fmt_ptr;
1443
1444 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1445 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001446 qfl->qf_multiline = TRUE; // start of a multi-line message
1447 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001448 }
1449 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001450 { // continuation of multi-line msg
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001451 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1452 if (status != QF_OK)
1453 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001454 }
1455 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001456 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001457 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1458 if (status == QF_MULTISCAN)
1459 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001460 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001461 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001462 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001463 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001464 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001465 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001466 return QF_IGNORE_LINE;
1467 }
1468 }
1469
1470 return QF_OK;
1471}
1472
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001473/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001474 * Returns TRUE if the specified quickfix/location stack is empty
1475 */
1476 static int
1477qf_stack_empty(qf_info_T *qi)
1478{
1479 return qi == NULL || qi->qf_listcount <= 0;
1480}
1481
1482/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001483 * Returns TRUE if the specified quickfix/location list is empty.
1484 */
1485 static int
1486qf_list_empty(qf_info_T *qi, int qf_idx)
1487{
1488 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1489 return TRUE;
1490 return qi->qf_lists[qf_idx].qf_count <= 0;
1491}
1492
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001493/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001494 * Allocate the fields used for parsing lines and populating a quickfix list.
1495 */
1496 static int
1497qf_alloc_fields(qffields_T *pfields)
1498{
1499 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1500 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1501 pfields->errmsglen = CMDBUFFSIZE + 1;
1502 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1503 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1504 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1505 || pfields->pattern == NULL || pfields->module == NULL)
1506 return FAIL;
1507
1508 return OK;
1509}
1510
1511/*
1512 * Free the fields used for parsing lines and populating a quickfix list.
1513 */
1514 static void
1515qf_free_fields(qffields_T *pfields)
1516{
1517 vim_free(pfields->namebuf);
1518 vim_free(pfields->module);
1519 vim_free(pfields->errmsg);
1520 vim_free(pfields->pattern);
1521}
1522
1523/*
1524 * Setup the state information used for parsing lines and populating a
1525 * quickfix list.
1526 */
1527 static int
1528qf_setup_state(
1529 qfstate_T *pstate,
1530 char_u *enc,
1531 char_u *efile,
1532 typval_T *tv,
1533 buf_T *buf,
1534 linenr_T lnumfirst,
1535 linenr_T lnumlast)
1536{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001537 pstate->vc.vc_type = CONV_NONE;
1538 if (enc != NULL && *enc != NUL)
1539 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001540
1541 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001543 semsg(_(e_openerrf), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001544 return FAIL;
1545 }
1546
1547 if (tv != NULL)
1548 {
1549 if (tv->v_type == VAR_STRING)
1550 pstate->p_str = tv->vval.v_string;
1551 else if (tv->v_type == VAR_LIST)
1552 pstate->p_li = tv->vval.v_list->lv_first;
1553 pstate->tv = tv;
1554 }
1555 pstate->buf = buf;
1556 pstate->buflnum = lnumfirst;
1557 pstate->lnumlast = lnumlast;
1558
1559 return OK;
1560}
1561
1562/*
1563 * Cleanup the state information used for parsing lines and populating a
1564 * quickfix list.
1565 */
1566 static void
1567qf_cleanup_state(qfstate_T *pstate)
1568{
1569 if (pstate->fd != NULL)
1570 fclose(pstate->fd);
1571
1572 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001573 if (pstate->vc.vc_type != CONV_NONE)
1574 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001575}
1576
1577/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001578 * Read the errorfile "efile" into memory, line by line, building the error
1579 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001580 * Alternative: when "efile" is NULL read errors from buffer "buf".
1581 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001582 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001583 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1584 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001585 * Return -1 for error, number of errors for success.
1586 */
1587 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001588qf_init_ext(
1589 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001590 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001591 char_u *efile,
1592 buf_T *buf,
1593 typval_T *tv,
1594 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001595 int newlist, // TRUE: start a new error list
1596 linenr_T lnumfirst, // first line number to use
1597 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001598 char_u *qf_title,
1599 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001600{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001601 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001602 qfstate_T state;
1603 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001604 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001605 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001606 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001608 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001609 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001610 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001612 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001613 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001614
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001615 vim_memset(&state, 0, sizeof(state));
1616 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001617 if ((qf_alloc_fields(&fields) == FAIL) ||
1618 (qf_setup_state(&state, enc, efile, tv, buf,
1619 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 goto qf_init_end;
1621
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001622 if (newlist || qf_idx == qi->qf_listcount)
1623 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001624 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001625 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001626 qf_idx = qi->qf_curlist;
1627 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001628 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001629 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001630 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001631 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001632 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001633 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001636 qfl = &qi->qf_lists[qf_idx];
1637
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001638 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001639 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001640 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 else
1642 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001644 // If the errorformat didn't change between calls, then reuse the
1645 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001646 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1647 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001648 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001649 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001650 free_efm_list(&fmt_first);
1651
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001652 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001653 fmt_first = parse_efm_option(efm);
1654 if (fmt_first != NULL)
1655 last_efm = vim_strsave(efm);
1656 }
1657
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001658 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001661 // got_int is reset here, because it was probably set when killing the
1662 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 got_int = FALSE;
1664
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001665 // Read the lines in the error file one by one.
1666 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001667 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001669 // Get the next line from a file/buffer/list/string
Bram Moolenaare0d37972016-07-15 22:36:01 +02001670 status = qf_get_nextline(&state);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001671 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001672 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001673 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001674 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001676 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1677 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001678 if (status == QF_FAIL)
1679 goto error2;
1680 if (status == QF_NOMEM)
1681 goto qf_init_end;
1682 if (status == QF_IGNORE_LINE)
1683 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001685 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001686 qf_idx,
1687 qfl->qf_directory,
1688 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001689 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001690 : ((qfl->qf_currfile != NULL && fields.valid)
1691 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001692 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001693 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001694 fields.errmsg,
1695 fields.lnum,
1696 fields.col,
1697 fields.use_viscol,
1698 fields.pattern,
1699 fields.enr,
1700 fields.type,
1701 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 goto error2;
1703 line_breakcheck();
1704 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001705 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001707 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001709 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001710 qfl->qf_ptr = qfl->qf_start;
1711 qfl->qf_index = 1;
1712 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
1714 else
1715 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001716 qfl->qf_nonevalid = FALSE;
1717 if (qfl->qf_ptr == NULL)
1718 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001720 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001721 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001722 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001724 emsg(_(e_readerrf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001726 if (!adding)
1727 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001728 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001729 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001730 qi->qf_listcount--;
1731 if (qi->qf_curlist > 0)
1732 --qi->qf_curlist;
1733 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001734qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001735 if (qf_idx == qi->qf_curlist)
1736 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001737 qf_cleanup_state(&state);
1738 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740 return retval;
1741}
1742
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001743/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001744 * Read the errorfile "efile" into memory, line by line, building the error
1745 * list. Set the error list's title to qf_title.
1746 * Return -1 for error, number of errors for success.
1747 */
1748 int
1749qf_init(win_T *wp,
1750 char_u *efile,
1751 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001752 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001753 char_u *qf_title,
1754 char_u *enc)
1755{
1756 qf_info_T *qi = &ql_info;
1757
1758 if (wp != NULL)
1759 {
1760 qi = ll_get_or_alloc_list(wp);
1761 if (qi == NULL)
1762 return FAIL;
1763 }
1764
1765 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1766 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1767}
1768
1769/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001770 * Set the title of the specified quickfix list. Frees the previous title.
1771 * Prepends ':' to the title.
1772 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001773 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001774qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001775{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001776 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001777
Bram Moolenaarfb604092014-07-23 15:55:00 +02001778 if (title != NULL)
1779 {
1780 char_u *p = alloc((int)STRLEN(title) + 2);
1781
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001782 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001783 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001784 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001785 }
1786}
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001789 * The title of a quickfix/location list is set, by default, to the command
1790 * that created the quickfix list with the ":" prefix.
1791 * Create a quickfix list title string by prepending ":" to a user command.
1792 * Returns a pointer to a static buffer with the title.
1793 */
1794 static char_u *
1795qf_cmdtitle(char_u *cmd)
1796{
1797 static char_u qftitle_str[IOSIZE];
1798
1799 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1800 return qftitle_str;
1801}
1802
1803/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001804 * Prepare for adding a new quickfix list. If the current list is in the
1805 * middle of the stack, then all the following lists are freed and then
1806 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 */
1808 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001809qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
1811 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001812 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001814 // If the current entry is not the last entry, delete entries beyond
1815 // the current entry. This makes it possible to browse in a tree-like
1816 // way with ":grep'.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001817 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001818 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001820 // When the stack is full, remove to oldest entry
1821 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001822 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001824 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001826 qi->qf_lists[i - 1] = qi->qf_lists[i];
1827 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
1829 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001830 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001831 qfl = &qi->qf_lists[qi->qf_curlist];
1832 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1833 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001834 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001835 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836}
1837
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001838/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001839 * Queue location list stack delete request.
1840 */
1841 static void
1842locstack_queue_delreq(qf_info_T *qi)
1843{
1844 qf_delq_T *q;
1845
1846 q = (qf_delq_T *)alloc((unsigned)sizeof(qf_delq_T));
1847 if (q != NULL)
1848 {
1849 q->qi = qi;
1850 q->next = qf_delq_head;
1851 qf_delq_head = q;
1852 }
1853}
1854
1855/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001856 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001857 */
1858 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001859ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001860{
1861 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001862 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001863
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001864 qi = *pqi;
1865 if (qi == NULL)
1866 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001867 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001868
1869 qi->qf_refcount--;
1870 if (qi->qf_refcount < 1)
1871 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001872 // No references to this location list.
1873 // If the location list is still in use, then queue the delete request
1874 // to be processed later.
1875 if (quickfix_busy > 0)
1876 locstack_queue_delreq(qi);
1877 else
1878 {
1879 for (i = 0; i < qi->qf_listcount; ++i)
1880 qf_free(&qi->qf_lists[i]);
1881 vim_free(qi);
1882 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001883 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001884}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001885
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001886/*
1887 * Free all the quickfix/location lists in the stack.
1888 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001889 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001890qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001891{
1892 int i;
1893 qf_info_T *qi = &ql_info;
1894
1895 if (wp != NULL)
1896 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001897 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001898 ll_free_all(&wp->w_llist);
1899 ll_free_all(&wp->w_llist_ref);
1900 }
1901 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001902 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001903 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001904 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001905}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001908 * Delay freeing of location list stacks when the quickfix code is running.
1909 * Used to avoid problems with autocmds freeing location list stacks when the
1910 * quickfix code is still referencing the stack.
1911 * Must always call decr_quickfix_busy() exactly once after this.
1912 */
1913 static void
1914incr_quickfix_busy(void)
1915{
1916 quickfix_busy++;
1917}
1918
1919/*
1920 * Safe to free location list stacks. Process any delayed delete requests.
1921 */
1922 static void
1923decr_quickfix_busy(void)
1924{
1925 if (--quickfix_busy == 0)
1926 {
1927 // No longer referencing the location lists. Process all the pending
1928 // delete requests.
1929 while (qf_delq_head != NULL)
1930 {
1931 qf_delq_T *q = qf_delq_head;
1932
1933 qf_delq_head = q->next;
1934 ll_free_all(&q->qi);
1935 vim_free(q);
1936 }
1937 }
1938#ifdef ABORT_ON_INTERNAL_ERROR
1939 if (quickfix_busy < 0)
1940 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001941 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001942 abort();
1943 }
1944#endif
1945}
1946
1947#if defined(EXITFREE) || defined(PROTO)
1948 void
1949check_quickfix_busy(void)
1950{
1951 if (quickfix_busy != 0)
1952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001953 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001954# ifdef ABORT_ON_INTERNAL_ERROR
1955 abort();
1956# endif
1957 }
1958}
1959#endif
1960
1961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 * Add an entry to the end of the list of errors.
1963 * Returns OK or FAIL.
1964 */
1965 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001966qf_add_entry(
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001967 qf_info_T *qi, // quickfix list
1968 int qf_idx, // list index
1969 char_u *dir, // optional directory name
1970 char_u *fname, // file name or NULL
1971 char_u *module, // module name or NULL
1972 int bufnum, // buffer number or zero
1973 char_u *mesg, // message
1974 long lnum, // line number
1975 int col, // column
1976 int vis_col, // using visual column
1977 char_u *pattern, // search pattern
1978 int nr, // error number
1979 int type, // type character
1980 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001982 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001983 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001984 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001986 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001988 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001989 {
1990 buf_T *buf = buflist_findnr(bufnum);
1991
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001992 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001993 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02001994 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001995 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02001996 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001997 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001998 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2000 {
2001 vim_free(qfp);
2002 return FAIL;
2003 }
2004 qfp->qf_lnum = lnum;
2005 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002006 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002007 if (pattern == NULL || *pattern == NUL)
2008 qfp->qf_pattern = NULL;
2009 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2010 {
2011 vim_free(qfp->qf_text);
2012 vim_free(qfp);
2013 return FAIL;
2014 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002015 if (module == NULL || *module == NUL)
2016 qfp->qf_module = NULL;
2017 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2018 {
2019 vim_free(qfp->qf_text);
2020 vim_free(qfp->qf_pattern);
2021 vim_free(qfp);
2022 return FAIL;
2023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002025 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 type = 0;
2027 qfp->qf_type = type;
2028 qfp->qf_valid = valid;
2029
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002030 lastp = &qfl->qf_last;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002031 if (qf_list_empty(qi, qf_idx)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002033 qfl->qf_start = qfp;
2034 qfl->qf_ptr = qfp;
2035 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002036 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038 else
2039 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002040 qfp->qf_prev = *lastp;
2041 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002043 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002045 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002046 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002047 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002049 qfl->qf_index = qfl->qf_count;
2050 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
2052
2053 return OK;
2054}
2055
2056/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002057 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002058 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002059 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002060qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002061{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002062 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002063
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002064 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002065 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002066 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002067 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002068 qi->qfl_type = qfltype;
2069 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002070 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002071}
2072
2073/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002074 * Return the location list stack for window 'wp'.
2075 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002076 */
2077 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002078ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002079{
2080 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002081 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002082 return wp->w_llist_ref;
2083
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002084 // For a non-location list window, w_llist_ref should not point to a
2085 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002086 ll_free_all(&wp->w_llist_ref);
2087
2088 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002089 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002090 return wp->w_llist;
2091}
2092
2093/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002094 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2095 */
2096 static int
2097copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2098{
2099 int i;
2100 qfline_T *from_qfp;
2101 qfline_T *prevp;
2102
2103 // copy all the location entries in this list
2104 for (i = 0, from_qfp = from_qfl->qf_start;
2105 i < from_qfl->qf_count && from_qfp != NULL;
2106 ++i, from_qfp = from_qfp->qf_next)
2107 {
2108 if (qf_add_entry(to_qi,
2109 to_qi->qf_curlist,
2110 NULL,
2111 NULL,
2112 from_qfp->qf_module,
2113 0,
2114 from_qfp->qf_text,
2115 from_qfp->qf_lnum,
2116 from_qfp->qf_col,
2117 from_qfp->qf_viscol,
2118 from_qfp->qf_pattern,
2119 from_qfp->qf_nr,
2120 0,
2121 from_qfp->qf_valid) == FAIL)
2122 return FAIL;
2123
2124 // qf_add_entry() will not set the qf_num field, as the
2125 // directory and file names are not supplied. So the qf_fnum
2126 // field is copied here.
2127 prevp = to_qfl->qf_last;
2128 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2129 prevp->qf_type = from_qfp->qf_type; // error type
2130 if (from_qfl->qf_ptr == from_qfp)
2131 to_qfl->qf_ptr = prevp; // current location
2132 }
2133
2134 return OK;
2135}
2136
2137/*
2138 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2139 */
2140 static int
2141copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2142{
2143 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002144 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002145 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2146 to_qfl->qf_count = 0;
2147 to_qfl->qf_index = 0;
2148 to_qfl->qf_start = NULL;
2149 to_qfl->qf_last = NULL;
2150 to_qfl->qf_ptr = NULL;
2151 if (from_qfl->qf_title != NULL)
2152 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2153 else
2154 to_qfl->qf_title = NULL;
2155 if (from_qfl->qf_ctx != NULL)
2156 {
2157 to_qfl->qf_ctx = alloc_tv();
2158 if (to_qfl->qf_ctx != NULL)
2159 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2160 }
2161 else
2162 to_qfl->qf_ctx = NULL;
2163
2164 if (from_qfl->qf_count)
2165 if (copy_loclist_entries(from_qfl, to_qfl, to_qi) == FAIL)
2166 return FAIL;
2167
2168 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2169
2170 // Assign a new ID for the location list
2171 to_qfl->qf_id = ++last_qf_id;
2172 to_qfl->qf_changedtick = 0L;
2173
2174 // When no valid entries are present in the list, qf_ptr points to
2175 // the first item in the list
2176 if (to_qfl->qf_nonevalid)
2177 {
2178 to_qfl->qf_ptr = to_qfl->qf_start;
2179 to_qfl->qf_index = 1;
2180 }
2181
2182 return OK;
2183}
2184
2185/*
2186 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002187 */
2188 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002189copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002190{
2191 qf_info_T *qi;
2192 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002193
Bram Moolenaar09037502018-09-25 22:08:14 +02002194 // When copying from a location list window, copy the referenced
2195 // location list. For other windows, copy the location list for
2196 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002197 if (IS_LL_WINDOW(from))
2198 qi = from->w_llist_ref;
2199 else
2200 qi = from->w_llist;
2201
Bram Moolenaar09037502018-09-25 22:08:14 +02002202 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002203 return;
2204
Bram Moolenaar09037502018-09-25 22:08:14 +02002205 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002206 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002207 return;
2208
2209 to->w_llist->qf_listcount = qi->qf_listcount;
2210
Bram Moolenaar09037502018-09-25 22:08:14 +02002211 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002212 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002213 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002214 to->w_llist->qf_curlist = idx;
2215
Bram Moolenaar09037502018-09-25 22:08:14 +02002216 if (copy_loclist(&qi->qf_lists[idx],
2217 &to->w_llist->qf_lists[idx], to->w_llist) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002218 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002219 qf_free_all(to);
2220 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002221 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002222 }
2223
Bram Moolenaar09037502018-09-25 22:08:14 +02002224 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002225}
2226
2227/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002228 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002229 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 */
2231 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002232qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002234 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar82404332016-07-10 17:00:38 +02002235 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002236 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002237 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002238
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002239 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241
Bram Moolenaare60acc12011-05-10 16:41:25 +02002242#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002243 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002244#endif
2245#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002246 if (directory != NULL)
2247 slash_adjust(directory);
2248 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002249#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002250 if (directory != NULL && !vim_isAbsName(fname)
2251 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2252 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002253 // Here we check if the file really exists.
2254 // This should normally be true, but if make works without
2255 // "leaving directory"-messages we might have missed a
2256 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002257 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002260 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002261 if (directory)
2262 ptr = concat_fnames(directory, fname, TRUE);
2263 else
2264 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002266 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002267 bufname = ptr;
2268 }
2269 else
2270 bufname = fname;
2271
2272 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002273 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002274 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002275 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002276 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002278 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002279 {
2280 vim_free(qf_last_bufname);
2281 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2282 if (bufname == ptr)
2283 qf_last_bufname = bufname;
2284 else
2285 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002286 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002287 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002288 if (buf == NULL)
2289 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002290
Bram Moolenaarc1542742016-07-20 21:44:37 +02002291 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002292 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002293 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294}
2295
2296/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002297 * Push dirbuf onto the directory stack and return pointer to actual dir or
2298 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 */
2300 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002301qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302{
2303 struct dir_stack_T *ds_new;
2304 struct dir_stack_T *ds_ptr;
2305
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002306 // allocate new stack element and hook it in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2308 if (ds_new == NULL)
2309 return NULL;
2310
2311 ds_new->next = *stackptr;
2312 *stackptr = ds_new;
2313
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002314 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (vim_isAbsName(dirbuf)
2316 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002317 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 (*stackptr)->dirname = vim_strsave(dirbuf);
2319 else
2320 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002321 // Okay we don't have an absolute path.
2322 // dirbuf must be a subdir of one of the directories on the stack.
2323 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 ds_new = (*stackptr)->next;
2325 (*stackptr)->dirname = NULL;
2326 while (ds_new)
2327 {
2328 vim_free((*stackptr)->dirname);
2329 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2330 TRUE);
2331 if (mch_isdir((*stackptr)->dirname) == TRUE)
2332 break;
2333
2334 ds_new = ds_new->next;
2335 }
2336
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002337 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 while ((*stackptr)->next != ds_new)
2339 {
2340 ds_ptr = (*stackptr)->next;
2341 (*stackptr)->next = (*stackptr)->next->next;
2342 vim_free(ds_ptr->dirname);
2343 vim_free(ds_ptr);
2344 }
2345
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002346 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 if (ds_new == NULL)
2348 {
2349 vim_free((*stackptr)->dirname);
2350 (*stackptr)->dirname = vim_strsave(dirbuf);
2351 }
2352 }
2353
2354 if ((*stackptr)->dirname != NULL)
2355 return (*stackptr)->dirname;
2356 else
2357 {
2358 ds_ptr = *stackptr;
2359 *stackptr = (*stackptr)->next;
2360 vim_free(ds_ptr);
2361 return NULL;
2362 }
2363}
2364
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365/*
2366 * pop dirbuf from the directory stack and return previous directory or NULL if
2367 * stack is empty
2368 */
2369 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002370qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
2372 struct dir_stack_T *ds_ptr;
2373
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002374 // TODO: Should we check if dirbuf is the directory on top of the stack?
2375 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002377 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 if (*stackptr != NULL)
2379 {
2380 ds_ptr = *stackptr;
2381 *stackptr = (*stackptr)->next;
2382 vim_free(ds_ptr->dirname);
2383 vim_free(ds_ptr);
2384 }
2385
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002386 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 return *stackptr ? (*stackptr)->dirname : NULL;
2388}
2389
2390/*
2391 * clean up directory stack
2392 */
2393 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002394qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395{
2396 struct dir_stack_T *ds_ptr;
2397
2398 while ((ds_ptr = *stackptr) != NULL)
2399 {
2400 *stackptr = (*stackptr)->next;
2401 vim_free(ds_ptr->dirname);
2402 vim_free(ds_ptr);
2403 }
2404}
2405
2406/*
2407 * Check in which directory of the directory stack the given file can be
2408 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002409 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 * Cleans up intermediate directory entries.
2411 *
2412 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002413 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 * ./
2415 * ./aa
2416 * ./aa/bb
2417 * ./bb
2418 * ./bb/x.c
2419 * and make says:
2420 * making all in aa
2421 * making all in bb
2422 * x.c:9: Error
2423 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2424 * qf_guess_filepath will return NULL.
2425 */
2426 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002427qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428{
2429 struct dir_stack_T *ds_ptr;
2430 struct dir_stack_T *ds_tmp;
2431 char_u *fullname;
2432
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002433 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002434 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 return NULL;
2436
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002437 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 fullname = NULL;
2439 while (ds_ptr)
2440 {
2441 vim_free(fullname);
2442 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2443
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002444 // If concat_fnames failed, just go on. The worst thing that can happen
2445 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2447 break;
2448
2449 ds_ptr = ds_ptr->next;
2450 }
2451
2452 vim_free(fullname);
2453
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002454 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002455 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002457 ds_tmp = qfl->qf_dir_stack->next;
2458 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 vim_free(ds_tmp->dirname);
2460 vim_free(ds_tmp);
2461 }
2462
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002463 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464}
2465
2466/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002467 * Returns TRUE if a quickfix/location list with the given identifier exists.
2468 */
2469 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002470qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002471{
2472 qf_info_T *qi = &ql_info;
2473 int i;
2474
2475 if (wp != NULL)
2476 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002477 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002478 if (qi == NULL)
2479 return FALSE;
2480 }
2481
2482 for (i = 0; i < qi->qf_listcount; ++i)
2483 if (qi->qf_lists[i].qf_id == qf_id)
2484 return TRUE;
2485
2486 return FALSE;
2487}
2488
2489/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002490 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002491 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002492 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002493 * Similar to location list.
2494 */
2495 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002496is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002497{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002498 qfline_T *qfp;
2499 int i;
2500
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002501 // Search for the entry in the current list
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002502 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2503 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002504 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002505 break;
2506
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002507 if (i == qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002508 return FALSE;
2509
2510 return TRUE;
2511}
2512
2513/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002514 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002515 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002516 */
2517 static qfline_T *
2518get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002519 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002520 qfline_T *qf_ptr,
2521 int *qf_index,
2522 int dir)
2523{
2524 int idx;
2525 int old_qf_fnum;
2526
2527 idx = *qf_index;
2528 old_qf_fnum = qf_ptr->qf_fnum;
2529
2530 do
2531 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002532 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002533 return NULL;
2534 ++idx;
2535 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002536 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002537 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2538
2539 *qf_index = idx;
2540 return qf_ptr;
2541}
2542
2543/*
2544 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002545 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002546 */
2547 static qfline_T *
2548get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002549 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002550 qfline_T *qf_ptr,
2551 int *qf_index,
2552 int dir)
2553{
2554 int idx;
2555 int old_qf_fnum;
2556
2557 idx = *qf_index;
2558 old_qf_fnum = qf_ptr->qf_fnum;
2559
2560 do
2561 {
2562 if (idx == 1 || qf_ptr->qf_prev == NULL)
2563 return NULL;
2564 --idx;
2565 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002566 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002567 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2568
2569 *qf_index = idx;
2570 return qf_ptr;
2571}
2572
2573/*
2574 * Get the n'th (errornr) previous/next valid entry from the current entry in
2575 * the quickfix list.
2576 * dir == FORWARD or FORWARD_FILE: next valid entry
2577 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2578 */
2579 static qfline_T *
2580get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002581 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002582 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002583 int dir,
2584 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002585{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002586 qfline_T *qf_ptr = qfl->qf_ptr;
2587 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002588 qfline_T *prev_qf_ptr;
2589 int prev_index;
2590 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2591 char_u *err = e_no_more_items;
2592
2593 while (errornr--)
2594 {
2595 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002596 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002597
2598 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002599 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002600 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002601 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002602 if (qf_ptr == NULL)
2603 {
2604 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002605 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002606 if (err != NULL)
2607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002608 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002609 return NULL;
2610 }
2611 break;
2612 }
2613
2614 err = NULL;
2615 }
2616
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002617 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002618 return qf_ptr;
2619}
2620
2621/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002622 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2623 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002624 */
2625 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002626get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002627{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002628 qfline_T *qf_ptr = qfl->qf_ptr;
2629 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002630
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002631 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002632 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2633 {
2634 --qf_idx;
2635 qf_ptr = qf_ptr->qf_prev;
2636 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002637 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002638 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2639 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002640 {
2641 ++qf_idx;
2642 qf_ptr = qf_ptr->qf_next;
2643 }
2644
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002645 *new_qfidx = qf_idx;
2646 return qf_ptr;
2647}
2648
2649/*
2650 * Get a entry specied by 'errornr' and 'dir' from the current
2651 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2652 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2653 * Returns a pointer to the entry and the index of the new entry is stored in
2654 * 'new_qfidx'.
2655 */
2656 static qfline_T *
2657qf_get_entry(
2658 qf_list_T *qfl,
2659 int errornr,
2660 int dir,
2661 int *new_qfidx)
2662{
2663 qfline_T *qf_ptr = qfl->qf_ptr;
2664 int qfidx = qfl->qf_index;
2665
2666 if (dir != 0) // next/prev valid entry
2667 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2668 else if (errornr != 0) // go to specified number
2669 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2670
2671 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002672 return qf_ptr;
2673}
2674
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002675/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002676 * Find a window displaying a Vim help file.
2677 */
2678 static win_T *
2679qf_find_help_win(void)
2680{
2681 win_T *wp;
2682
2683 FOR_ALL_WINDOWS(wp)
2684 if (bt_help(wp->w_buffer))
2685 return wp;
2686
2687 return NULL;
2688}
2689
2690/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002691 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2692 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002693 */
2694 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002695jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002696{
2697 win_T *wp;
2698 int flags;
2699
Bram Moolenaarb2443732018-11-11 22:50:27 +01002700 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002701 wp = NULL;
2702 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002703 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002704 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2705 win_enter(wp, TRUE);
2706 else
2707 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002708 // Split off help window; put it at far top if no position
2709 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002710 flags = WSP_HELP;
2711 if (cmdmod.split == 0 && curwin->w_width != Columns
2712 && curwin->w_width < 80)
2713 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002714 // If the user asks to open a new window, then copy the location list.
2715 // Otherwise, don't copy the location list.
2716 if (IS_LL_STACK(qi) && !newwin)
2717 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002718
2719 if (win_split(0, flags) == FAIL)
2720 return FAIL;
2721
2722 *opened_window = TRUE;
2723
2724 if (curwin->w_height < p_hh)
2725 win_setheight((int)p_hh);
2726
Bram Moolenaarb2443732018-11-11 22:50:27 +01002727 // When using location list, the new window should use the supplied
2728 // location list. If the user asks to open a new window, then the new
2729 // window will get a copy of the location list.
2730 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002731 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002732 curwin->w_llist = qi;
2733 qi->qf_refcount++;
2734 }
2735 }
2736
2737 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002738 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002739
2740 return OK;
2741}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002742
2743/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002744 * Find a non-quickfix window using the given location list.
2745 * Returns NULL if a matching window is not found.
2746 */
2747 static win_T *
2748qf_find_win_with_loclist(qf_info_T *ll)
2749{
2750 win_T *wp;
2751
2752 FOR_ALL_WINDOWS(wp)
2753 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2754 return wp;
2755
2756 return NULL;
2757}
2758
2759/*
2760 * Find a window containing a normal buffer
2761 */
2762 static win_T *
2763qf_find_win_with_normal_buf(void)
2764{
2765 win_T *wp;
2766
2767 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002768 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002769 return wp;
2770
2771 return NULL;
2772}
2773
2774/*
2775 * Go to a window in any tabpage containing the specified file. Returns TRUE
2776 * if successfully jumped to the window. Otherwise returns FALSE.
2777 */
2778 static int
2779qf_goto_tabwin_with_file(int fnum)
2780{
2781 tabpage_T *tp;
2782 win_T *wp;
2783
2784 FOR_ALL_TAB_WINDOWS(tp, wp)
2785 if (wp->w_buffer->b_fnum == fnum)
2786 {
2787 goto_tabpage_win(tp, wp);
2788 return TRUE;
2789 }
2790
2791 return FALSE;
2792}
2793
2794/*
2795 * Create a new window to show a file above the quickfix window. Called when
2796 * only the quickfix window is present.
2797 */
2798 static int
2799qf_open_new_file_win(qf_info_T *ll_ref)
2800{
2801 int flags;
2802
2803 flags = WSP_ABOVE;
2804 if (ll_ref != NULL)
2805 flags |= WSP_NEWLOC;
2806 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002807 return FAIL; // not enough room for window
2808 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002809 swb_flags = 0;
2810 RESET_BINDING(curwin);
2811 if (ll_ref != NULL)
2812 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002813 // The new window should use the location list from the
2814 // location list window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002815 curwin->w_llist = ll_ref;
2816 ll_ref->qf_refcount++;
2817 }
2818 return OK;
2819}
2820
2821/*
2822 * Go to a window that shows the right buffer. If the window is not found, go
2823 * to the window just above the location list window. This is used for opening
2824 * a file from a location window and not from a quickfix window. If some usable
2825 * window is previously found, then it is supplied in 'use_win'.
2826 */
2827 static void
2828qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2829{
2830 win_T *win = use_win;
2831
2832 if (win == NULL)
2833 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002834 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002835 FOR_ALL_WINDOWS(win)
2836 if (win->w_buffer->b_fnum == qf_fnum)
2837 break;
2838 if (win == NULL)
2839 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002840 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002841 win = curwin;
2842 do
2843 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002844 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002845 break;
2846 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002847 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002848 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002849 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002850 } while (win != curwin);
2851 }
2852 }
2853 win_goto(win);
2854
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002855 // If the location list for the window is not set, then set it
2856 // to the location list from the location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002857 if (win->w_llist == NULL)
2858 {
2859 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002860 if (ll_ref != NULL)
2861 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002862 }
2863}
2864
2865/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002866 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2867 * not found, then go to the window just above the quickfix window. This is
2868 * used for opening a file from a quickfix window and not from a location
2869 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002870 */
2871 static void
2872qf_goto_win_with_qfl_file(int qf_fnum)
2873{
2874 win_T *win;
2875 win_T *altwin;
2876
2877 win = curwin;
2878 altwin = NULL;
2879 for (;;)
2880 {
2881 if (win->w_buffer->b_fnum == qf_fnum)
2882 break;
2883 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002884 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002885 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002886 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002887
2888 if (IS_QF_WINDOW(win))
2889 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002890 // Didn't find it, go to the window before the quickfix
2891 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002892 if (altwin != NULL)
2893 win = altwin;
2894 else if (curwin->w_prev != NULL)
2895 win = curwin->w_prev;
2896 else
2897 win = curwin->w_next;
2898 break;
2899 }
2900
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002901 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02002902 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002903 altwin = win;
2904 }
2905
2906 win_goto(win);
2907}
2908
2909/*
2910 * Find a suitable window for opening a file (qf_fnum) from the
2911 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01002912 * window, jump to it. Otherwise open a new window to display the file. If
2913 * 'newwin' is TRUE, then always open a new window. This is called from either
2914 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002915 */
2916 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002917qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002918{
2919 win_T *usable_win_ptr = NULL;
2920 int usable_win;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002921 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002922 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002923
2924 usable_win = 0;
2925
Bram Moolenaarb2443732018-11-11 22:50:27 +01002926 // If opening a new window, then don't use the location list referred by
2927 // the current window. Otherwise two windows will refer to the same
2928 // location list.
2929 if (!newwin)
2930 ll_ref = curwin->w_llist_ref;
2931
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002932 if (ll_ref != NULL)
2933 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002934 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002935 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2936 if (usable_win_ptr != NULL)
2937 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002938 }
2939
2940 if (!usable_win)
2941 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002942 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002943 win = qf_find_win_with_normal_buf();
2944 if (win != NULL)
2945 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002946 }
2947
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002948 // If no usable window is found and 'switchbuf' contains "usetab"
2949 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002950 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002951 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002952
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002953 // If there is only one window and it is the quickfix window, create a
2954 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01002955 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002956 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002957 if (qf_open_new_file_win(ll_ref) != OK)
2958 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002959 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002960 }
2961 else
2962 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002963 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002964 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002965 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002966 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002967 }
2968
2969 return OK;
2970}
2971
2972/*
2973 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002974 * Returns OK if successfully edited the file, FAIL on failing to open the
2975 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
2976 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002977 */
2978 static int
2979qf_jump_edit_buffer(
2980 qf_info_T *qi,
2981 qfline_T *qf_ptr,
2982 int forceit,
2983 win_T *oldwin,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002984 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002985{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002986 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002987 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002988 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02002989 int old_qf_curlist = qi->qf_curlist;
2990 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002991
2992 if (qf_ptr->qf_type == 1)
2993 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002994 // Open help file (do_ecmd() will set b_help flag, readfile() will
2995 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002996 if (!can_abandon(curbuf, forceit))
2997 {
2998 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02002999 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003000 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003001
3002 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3003 ECMD_HIDE + ECMD_SET_HELP,
3004 oldwin == curwin ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003005 }
3006 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003007 retval = buflist_getfile(qf_ptr->qf_fnum,
3008 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003009
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003010 // If a location list, check whether the associated window is still
3011 // present.
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003012 if (qfl_type == QFLT_LOCATION && !win_valid_any_tab(oldwin))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003013 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003014 emsg(_("E924: Current window was closed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003015 *opened_window = FALSE;
3016 return NOTDONE;
3017 }
3018
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003019 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003021 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003022 return NOTDONE;
3023 }
3024
3025 if (old_qf_curlist != qi->qf_curlist
3026 || !is_qf_entry_present(qfl, qf_ptr))
3027 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003028 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003029 emsg(_("E925: Current quickfix was changed"));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003030 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003031 emsg(_(e_loc_list_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003032 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003033 }
3034
3035 return retval;
3036}
3037
3038/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003039 * Go to the error line in the current file using either line/column number or
3040 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003041 */
3042 static void
3043qf_jump_goto_line(
3044 linenr_T qf_lnum,
3045 int qf_col,
3046 char_u qf_viscol,
3047 char_u *qf_pattern)
3048{
3049 linenr_T i;
3050 char_u *line;
3051 colnr_T screen_col;
3052 colnr_T char_col;
3053
3054 if (qf_pattern == NULL)
3055 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003056 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003057 i = qf_lnum;
3058 if (i > 0)
3059 {
3060 if (i > curbuf->b_ml.ml_line_count)
3061 i = curbuf->b_ml.ml_line_count;
3062 curwin->w_cursor.lnum = i;
3063 }
3064 if (qf_col > 0)
3065 {
3066 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003067 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003068 if (qf_viscol == TRUE)
3069 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003070 // Check each character from the beginning of the error
3071 // line up to the error column. For each tab character
3072 // found, reduce the error column value by the length of
3073 // a tab character.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003074 line = ml_get_curline();
3075 screen_col = 0;
3076 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3077 {
3078 if (*line == NUL)
3079 break;
3080 if (*line++ == '\t')
3081 {
3082 curwin->w_cursor.col -= 7 - (screen_col % 8);
3083 screen_col += 8 - (screen_col % 8);
3084 }
3085 else
3086 ++screen_col;
3087 }
3088 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003089 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003090 check_cursor();
3091 }
3092 else
3093 beginline(BL_WHITE | BL_FIX);
3094 }
3095 else
3096 {
3097 pos_T save_cursor;
3098
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003099 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003100 save_cursor = curwin->w_cursor;
3101 curwin->w_cursor.lnum = 0;
3102 if (!do_search(NULL, '/', qf_pattern, (long)1,
3103 SEARCH_KEEP, NULL, NULL))
3104 curwin->w_cursor = save_cursor;
3105 }
3106}
3107
3108/*
3109 * Display quickfix list index and size message
3110 */
3111 static void
3112qf_jump_print_msg(
3113 qf_info_T *qi,
3114 int qf_index,
3115 qfline_T *qf_ptr,
3116 buf_T *old_curbuf,
3117 linenr_T old_lnum)
3118{
3119 linenr_T i;
3120 int len;
3121
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003122 // Update the screen before showing the message, unless the screen
3123 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003124 if (!msg_scrolled)
3125 update_topline_redraw();
3126 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3127 qi->qf_lists[qi->qf_curlist].qf_count,
3128 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3129 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003130 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003131 len = (int)STRLEN(IObuff);
3132 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3133
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003134 // Output the message. Overwrite to avoid scrolling when the 'O'
3135 // flag is present in 'shortmess'; But when not jumping, print the
3136 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003137 i = msg_scroll;
3138 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3139 msg_scroll = TRUE;
3140 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3141 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003142 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003143 msg_scroll = i;
3144}
3145
3146/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003147 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003148 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3149 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003150 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3151 * able to jump/open a window. Returns NOTDONE if a file is not associated
3152 * with the entry.
3153 */
3154 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003155qf_jump_open_window(
3156 qf_info_T *qi,
3157 qfline_T *qf_ptr,
3158 int newwin,
3159 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003160{
3161 // For ":helpgrep" find a help window or open one.
3162 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003163 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003164 return FAIL;
3165
3166 // If currently in the quickfix window, find another window to show the
3167 // file in.
3168 if (bt_quickfix(curbuf) && !*opened_window)
3169 {
3170 // If there is no file specified, we don't know where to go.
3171 // But do advance, otherwise ":cn" gets stuck.
3172 if (qf_ptr->qf_fnum == 0)
3173 return NOTDONE;
3174
Bram Moolenaarb2443732018-11-11 22:50:27 +01003175 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3176 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003177 return FAIL;
3178 }
3179
3180 return OK;
3181}
3182
3183/*
3184 * Edit a selected file from the quickfix/location list and jump to a
3185 * particular line/column, adjust the folds and display a message about the
3186 * jump.
3187 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3188 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3189 * the file.
3190 */
3191 static int
3192qf_jump_to_buffer(
3193 qf_info_T *qi,
3194 int qf_index,
3195 qfline_T *qf_ptr,
3196 int forceit,
3197 win_T *oldwin,
3198 int *opened_window,
3199 int openfold,
3200 int print_message)
3201{
3202 buf_T *old_curbuf;
3203 linenr_T old_lnum;
3204 int retval = OK;
3205
3206 // If there is a file name, read the wanted file if needed, and check
3207 // autowrite etc.
3208 old_curbuf = curbuf;
3209 old_lnum = curwin->w_cursor.lnum;
3210
3211 if (qf_ptr->qf_fnum != 0)
3212 {
3213 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3214 opened_window);
3215 if (retval != OK)
3216 return retval;
3217 }
3218
3219 // When not switched to another buffer, still need to set pc mark
3220 if (curbuf == old_curbuf)
3221 setpcmark();
3222
3223 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3224 qf_ptr->qf_pattern);
3225
3226#ifdef FEAT_FOLDING
3227 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3228 foldOpenCursor();
3229#endif
3230 if (print_message)
3231 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3232
3233 return retval;
3234}
3235
3236/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003237 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 */
3239 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003240qf_jump(qf_info_T *qi,
3241 int dir,
3242 int errornr,
3243 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003245 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3246}
3247
3248/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003249 * Jump to a quickfix line.
3250 * If dir == 0 go to entry "errornr".
3251 * If dir == FORWARD go "errornr" valid entries forward.
3252 * If dir == BACKWARD go "errornr" valid entries backward.
3253 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3254 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3255 * else if "errornr" is zero, redisplay the same line
3256 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003257 * If 'newwin' is TRUE, then open the file in a new window.
3258 */
3259 void
3260qf_jump_newwin(qf_info_T *qi,
3261 int dir,
3262 int errornr,
3263 int forceit,
3264 int newwin)
3265{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003266 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003267 qfline_T *qf_ptr;
3268 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003271 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003272 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003274 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003277 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003279 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003281 if (qi == NULL)
3282 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003283
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003284 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003286 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 return;
3288 }
3289
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003290 qfl = &qi->qf_lists[qi->qf_curlist];
3291
3292 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003294 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003296
3297 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3298 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003300 qf_ptr = old_qf_ptr;
3301 qf_index = old_qf_index;
3302 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003305 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003306 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003307 // No need to print the error message if it's visible in the error
3308 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 print_message = FALSE;
3310
Bram Moolenaarb2443732018-11-11 22:50:27 +01003311 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003312 if (retval == FAIL)
3313 goto failed;
3314 if (retval == NOTDONE)
3315 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003316
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003317 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, oldwin,
3318 &opened_window, old_KeyTyped, print_message);
3319 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003321 // Quickfix/location list is freed by an autocmd
3322 qi = NULL;
3323 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003326 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003329 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003330 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003332 // Couldn't open file, so put index back where it was. This could
3333 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 qf_ptr = old_qf_ptr;
3336 qf_index = old_qf_index;
3337 }
3338 }
3339theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003340 if (qi != NULL)
3341 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003342 qfl->qf_ptr = qf_ptr;
3343 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (p_swb != old_swb && opened_window)
3346 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003347 // Restore old 'switchbuf' value, but not when an autocommand or
3348 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003352 swb_flags = old_swb_flags;
3353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 else
3355 free_string_option(old_swb);
3356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357}
3358
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003359// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003360static int qfFileAttr;
3361static int qfSepAttr;
3362static int qfLineAttr;
3363
3364/*
3365 * Display information about a single entry from the quickfix/location list.
3366 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003367 * 'cursel' will be set to TRUE for the currently selected entry in the
3368 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003369 */
3370 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003371qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003372{
3373 char_u *fname;
3374 buf_T *buf;
3375 int filter_entry;
3376
3377 fname = NULL;
3378 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3379 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3380 (char *)qfp->qf_module);
3381 else {
3382 if (qfp->qf_fnum != 0
3383 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3384 {
3385 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003386 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003387 fname = gettail(fname);
3388 }
3389 if (fname == NULL)
3390 sprintf((char *)IObuff, "%2d", qf_idx);
3391 else
3392 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3393 qf_idx, (char *)fname);
3394 }
3395
3396 // Support for filtering entries using :filter /pat/ clist
3397 // Match against the module name, file name, search pattern and
3398 // text of the entry.
3399 filter_entry = TRUE;
3400 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3401 filter_entry &= message_filtered(qfp->qf_module);
3402 if (filter_entry && fname != NULL)
3403 filter_entry &= message_filtered(fname);
3404 if (filter_entry && qfp->qf_pattern != NULL)
3405 filter_entry &= message_filtered(qfp->qf_pattern);
3406 if (filter_entry)
3407 filter_entry &= message_filtered(qfp->qf_text);
3408 if (filter_entry)
3409 return;
3410
3411 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003412 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003413
3414 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003415 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003416 if (qfp->qf_lnum == 0)
3417 IObuff[0] = NUL;
3418 else if (qfp->qf_col == 0)
3419 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3420 else
3421 sprintf((char *)IObuff, "%ld col %d",
3422 qfp->qf_lnum, qfp->qf_col);
3423 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3424 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003425 msg_puts_attr((char *)IObuff, qfLineAttr);
3426 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003427 if (qfp->qf_pattern != NULL)
3428 {
3429 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003430 msg_puts((char *)IObuff);
3431 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003432 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003433 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003434
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003435 // Remove newlines and leading whitespace from the text. For an
3436 // unrecognized line keep the indent, the compiler may mark a word
3437 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003438 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3439 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3440 IObuff, IOSIZE);
3441 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003442 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003443}
3444
3445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003447 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 */
3449 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003450qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003452 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003453 qfline_T *qfp;
3454 int i;
3455 int idx1 = 1;
3456 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003457 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003458 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003459 int all = eap->forceit; // if not :cl!, only show
3460 // recognised errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003461 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaar39665952018-08-15 20:59:48 +02003463 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003464 {
3465 qi = GET_LOC_LIST(curwin);
3466 if (qi == NULL)
3467 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003468 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003469 return;
3470 }
3471 }
3472
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003473 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003475 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 return;
3477 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003478 if (*arg == '+')
3479 {
3480 ++arg;
3481 plus = TRUE;
3482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3484 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003485 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 return;
3487 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003488 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003489 if (plus)
3490 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003491 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003492 idx2 = i + idx1;
3493 idx1 = i;
3494 }
3495 else
3496 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003497 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003498 if (idx1 < 0)
3499 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3500 if (idx2 < 0)
3501 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003504 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003505 shorten_fnames(FALSE);
3506
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003507 // Get the attributes for the different quickfix highlight items. Note
3508 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003509 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3510 if (qfFileAttr == 0)
3511 qfFileAttr = HL_ATTR(HLF_D);
3512 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3513 if (qfSepAttr == 0)
3514 qfSepAttr = HL_ATTR(HLF_D);
3515 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3516 if (qfLineAttr == 0)
3517 qfLineAttr = HL_ATTR(HLF_N);
3518
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003519 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003521 qfp = qfl->qf_start;
3522 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
3524 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3525 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003526 if (got_int)
3527 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003528
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003529 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003531
3532 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003533 if (qfp == NULL)
3534 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003535 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 ui_breakcheck();
3537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538}
3539
3540/*
3541 * Remove newlines and leading whitespace from an error message.
3542 * Put the result in "buf[bufsize]".
3543 */
3544 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003545qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546{
3547 int i;
3548 char_u *p = text;
3549
3550 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3551 {
3552 if (*p == '\n')
3553 {
3554 buf[i] = ' ';
3555 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003556 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 break;
3558 }
3559 else
3560 buf[i] = *p++;
3561 }
3562 buf[i] = NUL;
3563}
3564
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003565/*
3566 * Display information (list number, list size and the title) about a
3567 * quickfix/location list.
3568 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003569 static void
3570qf_msg(qf_info_T *qi, int which, char *lead)
3571{
3572 char *title = (char *)qi->qf_lists[which].qf_title;
3573 int count = qi->qf_lists[which].qf_count;
3574 char_u buf[IOSIZE];
3575
3576 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3577 lead,
3578 which + 1,
3579 qi->qf_listcount,
3580 count);
3581
3582 if (title != NULL)
3583 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003584 size_t len = STRLEN(buf);
3585
3586 if (len < 34)
3587 {
3588 vim_memset(buf + len, ' ', 34 - len);
3589 buf[34] = NUL;
3590 }
3591 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003592 }
3593 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003594 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003595}
3596
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597/*
3598 * ":colder [count]": Up in the quickfix stack.
3599 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003600 * ":lolder [count]": Up in the location list stack.
3601 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 */
3603 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003604qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003606 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 int count;
3608
Bram Moolenaar39665952018-08-15 20:59:48 +02003609 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003610 {
3611 qi = GET_LOC_LIST(curwin);
3612 if (qi == NULL)
3613 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003614 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003615 return;
3616 }
3617 }
3618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 if (eap->addr_count != 0)
3620 count = eap->line2;
3621 else
3622 count = 1;
3623 while (count--)
3624 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003625 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003627 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003629 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003630 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003632 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
3634 else
3635 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003636 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003638 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003639 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003641 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
3643 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003644 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003645 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646}
3647
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003648/*
3649 * Display the information about all the quickfix/location lists in the stack
3650 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003651 void
3652qf_history(exarg_T *eap)
3653{
3654 qf_info_T *qi = &ql_info;
3655 int i;
3656
Bram Moolenaar39665952018-08-15 20:59:48 +02003657 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003658 qi = GET_LOC_LIST(curwin);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003659 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003660 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003661 else
3662 for (i = 0; i < qi->qf_listcount; ++i)
3663 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3664}
3665
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003667 * Free all the entries in the error list "idx". Note that other information
3668 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 */
3670 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003671qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003673 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003674 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003675 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003677 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003679 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003680 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003681 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003682 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003683 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003684 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003685 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003686 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003687 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003688 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003689 // Somehow qf_count may have an incorrect value, set it to 1
3690 // to avoid crashing when it's wrong.
3691 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003692 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003693 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003694 qfl->qf_start = qfpnext;
3695 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003697
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003698 qfl->qf_index = 0;
3699 qfl->qf_start = NULL;
3700 qfl->qf_last = NULL;
3701 qfl->qf_ptr = NULL;
3702 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003703
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003704 qf_clean_dir_stack(&qfl->qf_dir_stack);
3705 qfl->qf_directory = NULL;
3706 qf_clean_dir_stack(&qfl->qf_file_stack);
3707 qfl->qf_currfile = NULL;
3708 qfl->qf_multiline = FALSE;
3709 qfl->qf_multiignore = FALSE;
3710 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711}
3712
3713/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003714 * Free error list "idx". Frees all the entries in the quickfix list,
3715 * associated context information and the title.
3716 */
3717 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003718qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003719{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003720 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003721
Bram Moolenaard23a8232018-02-10 18:45:26 +01003722 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003723 free_tv(qfl->qf_ctx);
3724 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003725 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003726 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003727}
3728
3729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 * qf_mark_adjust: adjust marks
3731 */
3732 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003733qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003734 win_T *wp,
3735 linenr_T line1,
3736 linenr_T line2,
3737 long amount,
3738 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003740 int i;
3741 qfline_T *qfp;
3742 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003743 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003744 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003745 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746
Bram Moolenaarc1542742016-07-20 21:44:37 +02003747 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003748 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003749 if (wp != NULL)
3750 {
3751 if (wp->w_llist == NULL)
3752 return;
3753 qi = wp->w_llist;
3754 }
3755
3756 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003757 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003758 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003759 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3760 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 if (qfp->qf_fnum == curbuf->b_fnum)
3762 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003763 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3765 {
3766 if (amount == MAXLNUM)
3767 qfp->qf_cleared = TRUE;
3768 else
3769 qfp->qf_lnum += amount;
3770 }
3771 else if (amount_after && qfp->qf_lnum > line2)
3772 qfp->qf_lnum += amount_after;
3773 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003774
3775 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003776 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777}
3778
3779/*
3780 * Make a nice message out of the error character and the error number:
3781 * char number message
3782 * e or E 0 " error"
3783 * w or W 0 " warning"
3784 * i or I 0 " info"
3785 * 0 0 ""
3786 * other 0 " c"
3787 * e or E n " error n"
3788 * w or W n " warning n"
3789 * i or I n " info n"
3790 * 0 n " error n"
3791 * other n " c n"
3792 * 1 x "" :helpgrep
3793 */
3794 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003795qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796{
3797 static char_u buf[20];
3798 static char_u cc[3];
3799 char_u *p;
3800
3801 if (c == 'W' || c == 'w')
3802 p = (char_u *)" warning";
3803 else if (c == 'I' || c == 'i')
3804 p = (char_u *)" info";
3805 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3806 p = (char_u *)" error";
3807 else if (c == 0 || c == 1)
3808 p = (char_u *)"";
3809 else
3810 {
3811 cc[0] = ' ';
3812 cc[1] = c;
3813 cc[2] = NUL;
3814 p = cc;
3815 }
3816
3817 if (nr <= 0)
3818 return p;
3819
3820 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3821 return buf;
3822}
3823
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003825 * When "split" is FALSE: Open the entry/result under the cursor.
3826 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3827 */
3828 void
3829qf_view_result(int split)
3830{
3831 qf_info_T *qi = &ql_info;
3832
3833 if (!bt_quickfix(curbuf))
3834 return;
3835
3836 if (IS_LL_WINDOW(curwin))
3837 qi = GET_LOC_LIST(curwin);
3838
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003839 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003841 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003842 return;
3843 }
3844
3845 if (split)
3846 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003847 // Open the selected entry in a new window
3848 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3849 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003850 return;
3851 }
3852
3853 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3854}
3855
3856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 * ":cwindow": open the quickfix window if we have errors to display,
3858 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003859 * ":lwindow": open the location list window if we have locations to display,
3860 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 */
3862 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003863ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003865 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003866 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 win_T *win;
3868
Bram Moolenaar39665952018-08-15 20:59:48 +02003869 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003870 {
3871 qi = GET_LOC_LIST(curwin);
3872 if (qi == NULL)
3873 return;
3874 }
3875
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003876 qfl = &qi->qf_lists[qi->qf_curlist];
3877
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003878 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003879 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003881 // If a quickfix window is open but we have no errors to display,
3882 // close the window. If a quickfix window is not open, then open
3883 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003884 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003885 || qfl->qf_nonevalid
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003886 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
3888 if (win != NULL)
3889 ex_cclose(eap);
3890 }
3891 else if (win == NULL)
3892 ex_copen(eap);
3893}
3894
3895/*
3896 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003897 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003900ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003902 win_T *win = NULL;
3903 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
Bram Moolenaar39665952018-08-15 20:59:48 +02003905 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003906 {
3907 qi = GET_LOC_LIST(curwin);
3908 if (qi == NULL)
3909 return;
3910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003912 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003913 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if (win != NULL)
3915 win_close(win, FALSE);
3916}
3917
3918/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003919 * Set "w:quickfix_title" if "qi" has a title.
3920 */
3921 static void
3922qf_set_title_var(qf_list_T *qfl)
3923{
3924 if (qfl->qf_title != NULL)
3925 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3926}
3927
3928/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003929 * Goto a quickfix or location list window (if present).
3930 * Returns OK if the window is found, FAIL otherwise.
3931 */
3932 static int
3933qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3934{
3935 win_T *win;
3936
3937 win = qf_find_win(qi);
3938 if (win == NULL)
3939 return FAIL;
3940
3941 win_goto(win);
3942 if (resize)
3943 {
3944 if (vertsplit)
3945 {
3946 if (sz != win->w_width)
3947 win_setwidth(sz);
3948 }
3949 else if (sz != win->w_height)
3950 win_setheight(sz);
3951 }
3952
3953 return OK;
3954}
3955
3956/*
3957 * Open a new quickfix or location list window, load the quickfix buffer and
3958 * set the appropriate options for the window.
3959 * Returns FAIL if the window could not be opened.
3960 */
3961 static int
3962qf_open_new_cwindow(qf_info_T *qi, int height)
3963{
3964 buf_T *qf_buf;
3965 win_T *oldwin = curwin;
3966 tabpage_T *prevtab = curtab;
3967 int flags = 0;
3968 win_T *win;
3969
3970 qf_buf = qf_find_buf(qi);
3971
3972 // The current window becomes the previous window afterwards.
3973 win = curwin;
3974
3975 if (IS_QF_STACK(qi) && cmdmod.split == 0)
3976 // Create the new quickfix window at the very bottom, except when
3977 // :belowright or :aboveleft is used.
3978 win_goto(lastwin);
3979 // Default is to open the window below the current window
3980 if (cmdmod.split == 0)
3981 flags = WSP_BELOW;
3982 flags |= WSP_NEWLOC;
3983 if (win_split(height, flags) == FAIL)
3984 return FAIL; // not enough room for window
3985 RESET_BINDING(curwin);
3986
3987 if (IS_LL_STACK(qi))
3988 {
3989 // For the location list window, create a reference to the
3990 // location list from the window 'win'.
3991 curwin->w_llist_ref = win->w_llist;
3992 win->w_llist->qf_refcount++;
3993 }
3994
3995 if (oldwin != curwin)
3996 oldwin = NULL; // don't store info when in another window
3997 if (qf_buf != NULL)
3998 {
3999 // Use the existing quickfix buffer
4000 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4001 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4002 }
4003 else
4004 {
4005 // Create a new quickfix buffer
4006 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4007
4008 // switch off 'swapfile'
4009 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4010 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4011 OPT_LOCAL);
4012 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
4013 RESET_BINDING(curwin);
4014#ifdef FEAT_DIFF
4015 curwin->w_p_diff = FALSE;
4016#endif
4017#ifdef FEAT_FOLDING
4018 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4019 OPT_LOCAL);
4020#endif
4021 }
4022
4023 // Only set the height when still in the same tab page and there is no
4024 // window to the side.
4025 if (curtab == prevtab && curwin->w_width == Columns)
4026 win_setheight(height);
4027 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4028 if (win_valid(win))
4029 prevwin = win;
4030
4031 return OK;
4032}
4033
4034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004036 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 */
4038 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004039ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004041 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004042 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004044 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004045 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
Bram Moolenaar39665952018-08-15 20:59:48 +02004047 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004048 {
4049 qi = GET_LOC_LIST(curwin);
4050 if (qi == NULL)
4051 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004052 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004053 return;
4054 }
4055 }
4056
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004057 incr_quickfix_busy();
4058
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (eap->addr_count != 0)
4060 height = eap->line2;
4061 else
4062 height = QF_WINHEIGHT;
4063
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004064 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065#ifdef FEAT_GUI
4066 need_mouse_correct = TRUE;
4067#endif
4068
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004069 // Find an existing quickfix window, or open a new one.
4070 if (cmdmod.tab == 0)
4071 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4072 cmdmod.split & WSP_VERT);
4073 if (status == FAIL)
4074 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004075 {
4076 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004077 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004080 qfl = &qi->qf_lists[qi->qf_curlist];
4081 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004082 // Save the current index here, as updating the quickfix buffer may free
4083 // the quickfix list
4084 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004085
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004086 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004087 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004089 decr_quickfix_busy();
4090
4091 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 curwin->w_cursor.col = 0;
4093 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004094 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095}
4096
4097/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004098 * Move the cursor in the quickfix window to "lnum".
4099 */
4100 static void
4101qf_win_goto(win_T *win, linenr_T lnum)
4102{
4103 win_T *old_curwin = curwin;
4104
4105 curwin = win;
4106 curbuf = win->w_buffer;
4107 curwin->w_cursor.lnum = lnum;
4108 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004109 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004110 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004111 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004112 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004113 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004114 curwin = old_curwin;
4115 curbuf = curwin->w_buffer;
4116}
4117
4118/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004119 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004120 */
4121 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004122ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004123{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004124 qf_info_T *qi = &ql_info;
4125 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004126
Bram Moolenaar39665952018-08-15 20:59:48 +02004127 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004128 {
4129 qi = GET_LOC_LIST(curwin);
4130 if (qi == NULL)
4131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004132 emsg(_(e_loclist));
Bram Moolenaar537ef082016-07-09 17:56:19 +02004133 return;
4134 }
4135 }
4136
4137 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004138 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4139 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4140}
4141
4142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 * Return the number of the current entry (line number in the quickfix
4144 * window).
4145 */
4146 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004147qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004149 qf_info_T *qi = &ql_info;
4150
4151 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004152 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004153 qi = wp->w_llist_ref;
4154
4155 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156}
4157
4158/*
4159 * Update the cursor position in the quickfix window to the current error.
4160 * Return TRUE if there is a quickfix window.
4161 */
4162 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004163qf_win_pos_update(
4164 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004165 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
4167 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004168 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004170 // Put the cursor on the current error in the quickfix window, so that
4171 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004172 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 if (win != NULL
4174 && qf_index <= win->w_buffer->b_ml.ml_line_count
4175 && old_qf_index != qf_index)
4176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 if (qf_index > old_qf_index)
4178 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004179 win->w_redraw_top = old_qf_index;
4180 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182 else
4183 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004184 win->w_redraw_top = qf_index;
4185 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004187 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189 return win != NULL;
4190}
4191
4192/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004193 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004194 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004195 */
4196 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004197is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004198{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004199 // A window displaying the quickfix buffer will have the w_llist_ref field
4200 // set to NULL.
4201 // A window displaying a location list buffer will have the w_llist_ref
4202 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004203 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004204 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4205 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004206 return TRUE;
4207
4208 return FALSE;
4209}
4210
4211/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004212 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004213 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004214 */
4215 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004216qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004217{
4218 win_T *win;
4219
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004220 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004221 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004222 return win;
4223 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004224}
4225
4226/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004227 * Find a quickfix buffer.
4228 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 */
4230 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004231qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004233 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004234 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235
Bram Moolenaar9c102382006-05-03 21:26:49 +00004236 FOR_ALL_TAB_WINDOWS(tp, win)
4237 if (is_qf_win(win, qi))
4238 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004239
Bram Moolenaar9c102382006-05-03 21:26:49 +00004240 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241}
4242
4243/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004244 * Update the w:quickfix_title variable in the quickfix/location list window
4245 */
4246 static void
4247qf_update_win_titlevar(qf_info_T *qi)
4248{
4249 win_T *win;
4250 win_T *curwin_save;
4251
4252 if ((win = qf_find_win(qi)) != NULL)
4253 {
4254 curwin_save = curwin;
4255 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004256 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004257 curwin = curwin_save;
4258 }
4259}
4260
4261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 * Find the quickfix buffer. If it exists, update the contents.
4263 */
4264 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004265qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266{
4267 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004268 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004271 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004272 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 if (buf != NULL)
4274 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004275 linenr_T old_line_count = buf->b_ml.ml_line_count;
4276
4277 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004278 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004279 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
Bram Moolenaard823fa92016-08-12 16:29:27 +02004281 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004282
Bram Moolenaar864293a2016-06-02 13:40:04 +02004283 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004284 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004285
Bram Moolenaar864293a2016-06-02 13:40:04 +02004286 if (old_last == NULL)
4287 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004288 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004289
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004290 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004291 aucmd_restbuf(&aco);
4292 }
4293
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004294 // Only redraw when added lines are visible. This avoids flickering
4295 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004296 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4297 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299}
4300
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004301/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004302 * Add an error line to the quickfix buffer.
4303 */
4304 static int
4305qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4306{
4307 int len;
4308 buf_T *errbuf;
4309
4310 if (qfp->qf_module != NULL)
4311 {
4312 STRCPY(IObuff, qfp->qf_module);
4313 len = (int)STRLEN(IObuff);
4314 }
4315 else if (qfp->qf_fnum != 0
4316 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4317 && errbuf->b_fname != NULL)
4318 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004319 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004320 STRCPY(IObuff, gettail(errbuf->b_fname));
4321 else
4322 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004323 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004324 if (errbuf->b_sfname == NULL
4325 || mch_isFullName(errbuf->b_sfname))
4326 {
4327 if (*dirname == NUL)
4328 mch_dirname(dirname, MAXPATHL);
4329 shorten_buf_fname(errbuf, dirname, FALSE);
4330 }
4331 STRCPY(IObuff, errbuf->b_fname);
4332 }
4333 len = (int)STRLEN(IObuff);
4334 }
4335 else
4336 len = 0;
4337 IObuff[len++] = '|';
4338
4339 if (qfp->qf_lnum > 0)
4340 {
4341 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4342 len += (int)STRLEN(IObuff + len);
4343
4344 if (qfp->qf_col > 0)
4345 {
4346 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4347 len += (int)STRLEN(IObuff + len);
4348 }
4349
4350 sprintf((char *)IObuff + len, "%s",
4351 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4352 len += (int)STRLEN(IObuff + len);
4353 }
4354 else if (qfp->qf_pattern != NULL)
4355 {
4356 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4357 len += (int)STRLEN(IObuff + len);
4358 }
4359 IObuff[len++] = '|';
4360 IObuff[len++] = ' ';
4361
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004362 // Remove newlines and leading whitespace from the text.
4363 // For an unrecognized line keep the indent, the compiler may
4364 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004365 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4366 IObuff + len, IOSIZE - len);
4367
4368 if (ml_append_buf(buf, lnum, IObuff,
4369 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4370 return FAIL;
4371
4372 return OK;
4373}
4374
4375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 * Fill current buffer with quickfix errors, replacing any previous contents.
4377 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004378 * If "old_last" is not NULL append the items after this one.
4379 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4380 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 */
4382 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004383qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004385 linenr_T lnum;
4386 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004387 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
Bram Moolenaar864293a2016-06-02 13:40:04 +02004389 if (old_last == NULL)
4390 {
4391 if (buf != curbuf)
4392 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004393 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004394 return;
4395 }
4396
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004397 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004398 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4399 (void)ml_delete((linenr_T)1, FALSE);
4400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004402 // Check if there is anything to display
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004403 if (!qf_stack_empty(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004405 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4406 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004407
4408 *dirname = NUL;
4409
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004410 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004411 if (old_last == NULL)
4412 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004413 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004414 lnum = 0;
4415 }
4416 else
4417 {
4418 qfp = old_last->qf_next;
4419 lnum = buf->b_ml.ml_line_count;
4420 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004421 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004423 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004425
Bram Moolenaar864293a2016-06-02 13:40:04 +02004426 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004428 if (qfp == NULL)
4429 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004431
4432 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004433 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004434 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
4436
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004437 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 check_lnums(TRUE);
4439
Bram Moolenaar864293a2016-06-02 13:40:04 +02004440 if (old_last == NULL)
4441 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004442 // Set the 'filetype' to "qf" each time after filling the buffer.
4443 // This resembles reading a file into a buffer, it's more logical when
4444 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004445 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004446 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4447 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004449 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004450 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004452 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004454 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004455 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004456
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004457 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004458 redraw_curbuf_later(NOT_VALID);
4459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004461 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 KeyTyped = old_KeyTyped;
4463}
4464
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004465/*
4466 * For every change made to the quickfix list, update the changed tick.
4467 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004468 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004469qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004470{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004471 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004472}
4473
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004475 * Return the quickfix/location list number with the given identifier.
4476 * Returns -1 if list is not found.
4477 */
4478 static int
4479qf_id2nr(qf_info_T *qi, int_u qfid)
4480{
4481 int qf_idx;
4482
4483 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4484 if (qi->qf_lists[qf_idx].qf_id == qfid)
4485 return qf_idx;
4486 return INVALID_QFIDX;
4487}
4488
4489/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004490 * If the current list is not "save_qfid" and we can find the list with that ID
4491 * then make it the current list.
4492 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004493 * Returns OK if successfully restored the list. Returns FAIL if the list with
4494 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004495 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004496 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004497qf_restore_list(qf_info_T *qi, int_u save_qfid)
4498{
4499 int curlist;
4500
4501 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4502 {
4503 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004504 if (curlist < 0)
4505 // list is not present
4506 return FAIL;
4507 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004508 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004509 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004510}
4511
4512/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004513 * Jump to the first entry if there is one.
4514 */
4515 static void
4516qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4517{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004518 if (qf_restore_list(qi, save_qfid) == FAIL)
4519 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004520
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004521 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004522 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004523 qf_jump(qi, 0, 0, forceit);
4524}
4525
4526/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004527 * Return TRUE when using ":vimgrep" for ":grep".
4528 */
4529 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004530grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004531{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004532 return ((cmdidx == CMD_grep
4533 || cmdidx == CMD_lgrep
4534 || cmdidx == CMD_grepadd
4535 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004536 && STRCMP("internal",
4537 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4538}
4539
4540/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004541 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004543 static char_u *
4544make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004546 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004547 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004548 case CMD_make: return (char_u *)"make";
4549 case CMD_lmake: return (char_u *)"lmake";
4550 case CMD_grep: return (char_u *)"grep";
4551 case CMD_lgrep: return (char_u *)"lgrep";
4552 case CMD_grepadd: return (char_u *)"grepadd";
4553 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4554 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556}
4557
4558/*
4559 * Return the name for the errorfile, in allocated memory.
4560 * Find a new unique name when 'makeef' contains "##".
4561 * Returns NULL for error.
4562 */
4563 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004564get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565{
4566 char_u *p;
4567 char_u *name;
4568 static int start = -1;
4569 static int off = 0;
4570#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004571 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572#endif
4573
4574 if (*p_mef == NUL)
4575 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004576 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004578 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 return name;
4580 }
4581
4582 for (p = p_mef; *p; ++p)
4583 if (p[0] == '#' && p[1] == '#')
4584 break;
4585
4586 if (*p == NUL)
4587 return vim_strsave(p_mef);
4588
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004589 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 for (;;)
4591 {
4592 if (start == -1)
4593 start = mch_get_pid();
4594 else
4595 off += 19;
4596
4597 name = alloc((unsigned)STRLEN(p_mef) + 30);
4598 if (name == NULL)
4599 break;
4600 STRCPY(name, p_mef);
4601 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4602 STRCAT(name, p + 2);
4603 if (mch_getperm(name) < 0
4604#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004605 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 && mch_lstat((char *)name, &sb) < 0
4607#endif
4608 )
4609 break;
4610 vim_free(name);
4611 }
4612 return name;
4613}
4614
4615/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004616 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4617 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4618 */
4619 static char_u *
4620make_get_fullcmd(char_u *makecmd, char_u *fname)
4621{
4622 char_u *cmd;
4623 unsigned len;
4624
4625 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4626 if (*p_sp != NUL)
4627 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4628 cmd = alloc(len);
4629 if (cmd == NULL)
4630 return NULL;
4631 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4632 (char *)p_shq);
4633
4634 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4635 if (*p_sp != NUL)
4636 append_redir(cmd, len, p_sp, fname);
4637
4638 // Display the fully formed command. Output a newline if there's something
4639 // else than the :make command that was typed (in which case the cursor is
4640 // in column 0).
4641 if (msg_col == 0)
4642 msg_didout = FALSE;
4643 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004644 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004645 msg_outtrans(cmd); // show what we are doing
4646
4647 return cmd;
4648}
4649
4650/*
4651 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4652 */
4653 void
4654ex_make(exarg_T *eap)
4655{
4656 char_u *fname;
4657 char_u *cmd;
4658 char_u *enc = NULL;
4659 win_T *wp = NULL;
4660 qf_info_T *qi = &ql_info;
4661 int res;
4662 char_u *au_name = NULL;
4663 int_u save_qfid;
4664
4665 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4666 if (grep_internal(eap->cmdidx))
4667 {
4668 ex_vimgrep(eap);
4669 return;
4670 }
4671
4672 au_name = make_get_auname(eap->cmdidx);
4673 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4674 curbuf->b_fname, TRUE, curbuf))
4675 {
4676#ifdef FEAT_EVAL
4677 if (aborting())
4678 return;
4679#endif
4680 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004681 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004682
4683 if (is_loclist_cmd(eap->cmdidx))
4684 wp = curwin;
4685
4686 autowrite_all();
4687 fname = get_mef_name();
4688 if (fname == NULL)
4689 return;
4690 mch_remove(fname); // in case it's not unique
4691
4692 cmd = make_get_fullcmd(eap->arg, fname);
4693 if (cmd == NULL)
4694 return;
4695
4696 // let the shell know if we are redirecting output or not
4697 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4698
4699#ifdef AMIGA
4700 out_flush();
4701 // read window status report and redraw before message
4702 (void)char_avail();
4703#endif
4704
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004705 incr_quickfix_busy();
4706
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004707 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4708 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4709 (eap->cmdidx != CMD_grepadd
4710 && eap->cmdidx != CMD_lgrepadd),
4711 qf_cmdtitle(*eap->cmdlinep), enc);
4712 if (wp != NULL)
4713 {
4714 qi = GET_LOC_LIST(wp);
4715 if (qi == NULL)
4716 goto cleanup;
4717 }
4718 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004719 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004720
4721 // Remember the current quickfix list identifier, so that we can
4722 // check for autocommands changing the current quickfix list.
4723 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4724 if (au_name != NULL)
4725 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4726 curbuf->b_fname, TRUE, curbuf);
4727 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4728 // display the first error
4729 qf_jump_first(qi, save_qfid, FALSE);
4730
4731cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004732 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004733 mch_remove(fname);
4734 vim_free(fname);
4735 vim_free(cmd);
4736}
4737
4738/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004739 * Returns the number of valid entries in the current quickfix/location list.
4740 */
4741 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004742qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004743{
4744 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004745 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004746 qfline_T *qfp;
4747 int i, sz = 0;
4748 int prev_fnum = 0;
4749
Bram Moolenaar39665952018-08-15 20:59:48 +02004750 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004751 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004752 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004753 qi = GET_LOC_LIST(curwin);
4754 if (qi == NULL)
4755 return 0;
4756 }
4757
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004758 qfl = &qi->qf_lists[qi->qf_curlist];
4759 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004760 ++i, qfp = qfp->qf_next)
4761 {
4762 if (qfp->qf_valid)
4763 {
4764 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004765 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004766 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4767 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004768 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004769 sz++;
4770 prev_fnum = qfp->qf_fnum;
4771 }
4772 }
4773 }
4774
4775 return sz;
4776}
4777
4778/*
4779 * Returns the current index of the quickfix/location list.
4780 * Returns 0 if there is an error.
4781 */
4782 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004783qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004784{
4785 qf_info_T *qi = &ql_info;
4786
Bram Moolenaar39665952018-08-15 20:59:48 +02004787 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004788 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004789 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004790 qi = GET_LOC_LIST(curwin);
4791 if (qi == NULL)
4792 return 0;
4793 }
4794
4795 return qi->qf_lists[qi->qf_curlist].qf_index;
4796}
4797
4798/*
4799 * Returns the current index in the quickfix/location list (counting only valid
4800 * entries). If no valid entries are in the list, then returns 1.
4801 */
4802 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004803qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004804{
4805 qf_info_T *qi = &ql_info;
4806 qf_list_T *qfl;
4807 qfline_T *qfp;
4808 int i, eidx = 0;
4809 int prev_fnum = 0;
4810
Bram Moolenaar39665952018-08-15 20:59:48 +02004811 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004812 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004813 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004814 qi = GET_LOC_LIST(curwin);
4815 if (qi == NULL)
4816 return 1;
4817 }
4818
4819 qfl = &qi->qf_lists[qi->qf_curlist];
4820 qfp = qfl->qf_start;
4821
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004822 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004823 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4824 return 1;
4825
4826 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4827 {
4828 if (qfp->qf_valid)
4829 {
4830 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4831 {
4832 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4833 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004834 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004835 eidx++;
4836 prev_fnum = qfp->qf_fnum;
4837 }
4838 }
4839 else
4840 eidx++;
4841 }
4842 }
4843
4844 return eidx ? eidx : 1;
4845}
4846
4847/*
4848 * Get the 'n'th valid error entry in the quickfix or location list.
4849 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4850 * For :cdo and :ldo returns the 'n'th valid error entry.
4851 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4852 */
4853 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004854qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004855{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004856 qfline_T *qfp = qfl->qf_start;
4857 int i, eidx;
4858 int prev_fnum = 0;
4859
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004860 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004861 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4862 return 1;
4863
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004864 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004865 i++, qfp = qfp->qf_next)
4866 {
4867 if (qfp->qf_valid)
4868 {
4869 if (fdo)
4870 {
4871 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4872 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004873 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004874 eidx++;
4875 prev_fnum = qfp->qf_fnum;
4876 }
4877 }
4878 else
4879 eidx++;
4880 }
4881
4882 if (eidx == n)
4883 break;
4884 }
4885
4886 if (i <= qfl->qf_count)
4887 return i;
4888 else
4889 return 1;
4890}
4891
4892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004894 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004895 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 */
4897 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004898ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004900 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004901 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004902
Bram Moolenaar39665952018-08-15 20:59:48 +02004903 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004904 {
4905 qi = GET_LOC_LIST(curwin);
4906 if (qi == NULL)
4907 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004908 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004909 return;
4910 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004911 }
4912
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004913 if (eap->addr_count > 0)
4914 errornr = (int)eap->line2;
4915 else
4916 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004917 switch (eap->cmdidx)
4918 {
4919 case CMD_cc: case CMD_ll:
4920 errornr = 0;
4921 break;
4922 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4923 case CMD_lfirst:
4924 errornr = 1;
4925 break;
4926 default:
4927 errornr = 32767;
4928 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004929 }
4930
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004931 // For cdo and ldo commands, jump to the nth valid error.
4932 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02004933 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4934 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004935 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004936 eap->addr_count > 0 ? (int)eap->line1 : 1,
4937 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4938
4939 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940}
4941
4942/*
4943 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004944 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004945 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 */
4947 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004948ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004950 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004951 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004952 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004953
Bram Moolenaar39665952018-08-15 20:59:48 +02004954 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004955 {
4956 qi = GET_LOC_LIST(curwin);
4957 if (qi == NULL)
4958 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004959 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004960 return;
4961 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004962 }
4963
Bram Moolenaar55b69262017-08-13 13:42:01 +02004964 if (eap->addr_count > 0
4965 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4966 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004967 errornr = (int)eap->line2;
4968 else
4969 errornr = 1;
4970
Bram Moolenaar39665952018-08-15 20:59:48 +02004971 // Depending on the command jump to either next or previous entry/file.
4972 switch (eap->cmdidx)
4973 {
4974 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4975 dir = FORWARD;
4976 break;
4977 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4978 case CMD_lNext:
4979 dir = BACKWARD;
4980 break;
4981 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4982 dir = FORWARD_FILE;
4983 break;
4984 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4985 dir = BACKWARD_FILE;
4986 break;
4987 default:
4988 dir = FORWARD;
4989 break;
4990 }
4991
4992 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993}
4994
4995/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004996 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004997 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 */
4999 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005000ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005002 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005003 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005004 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005005 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005006 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005007 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005008
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005009 switch (eap->cmdidx)
5010 {
5011 case CMD_cfile: au_name = (char_u *)"cfile"; break;
5012 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
5013 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
5014 case CMD_lfile: au_name = (char_u *)"lfile"; break;
5015 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
5016 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
5017 default: break;
5018 }
5019 if (au_name != NULL)
5020 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005021 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005022#ifdef FEAT_BROWSE
5023 if (cmdmod.browse)
5024 {
5025 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005026 NULL, NULL,
5027 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005028 if (browse_file == NULL)
5029 return;
5030 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5031 vim_free(browse_file);
5032 }
5033 else
5034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005036 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005037
Bram Moolenaar39665952018-08-15 20:59:48 +02005038 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005039 wp = curwin;
5040
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005041 incr_quickfix_busy();
5042
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005043 // This function is used by the :cfile, :cgetfile and :caddfile
5044 // commands.
5045 // :cfile always creates a new quickfix list and jumps to the
5046 // first error.
5047 // :cgetfile creates a new quickfix list but doesn't jump to the
5048 // first error.
5049 // :caddfile adds to an existing quickfix list. If there is no
5050 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005051 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005052 && eap->cmdidx != CMD_laddfile),
5053 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005054 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005055 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005056 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005057 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005058 {
5059 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005060 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005061 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005062 }
5063 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005064 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005065 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005066 if (au_name != NULL)
5067 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005068
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005069 // Jump to the first error for a new list and if autocmds didn't
5070 // free the list.
5071 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5072 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005073 // display the first error
5074 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005075
5076 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005077}
5078
5079/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005080 * Return the vimgrep autocmd name.
5081 */
5082 static char_u *
5083vgr_get_auname(cmdidx_T cmdidx)
5084{
5085 switch (cmdidx)
5086 {
5087 case CMD_vimgrep: return (char_u *)"vimgrep";
5088 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5089 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5090 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5091 case CMD_grep: return (char_u *)"grep";
5092 case CMD_lgrep: return (char_u *)"lgrep";
5093 case CMD_grepadd: return (char_u *)"grepadd";
5094 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5095 default: return NULL;
5096 }
5097}
5098
5099/*
5100 * Initialize the regmatch used by vimgrep for pattern "s".
5101 */
5102 static void
5103vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5104{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005105 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005106 regmatch->regprog = NULL;
5107
5108 if (s == NULL || *s == NUL)
5109 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005110 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005111 if (last_search_pat() == NULL)
5112 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005113 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005114 return;
5115 }
5116 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5117 }
5118 else
5119 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5120
5121 regmatch->rmm_ic = p_ic;
5122 regmatch->rmm_maxcol = 0;
5123}
5124
5125/*
5126 * Display a file name when vimgrep is running.
5127 */
5128 static void
5129vgr_display_fname(char_u *fname)
5130{
5131 char_u *p;
5132
5133 msg_start();
5134 p = msg_strtrunc(fname, TRUE);
5135 if (p == NULL)
5136 msg_outtrans(fname);
5137 else
5138 {
5139 msg_outtrans(p);
5140 vim_free(p);
5141 }
5142 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005143 msg_didout = FALSE; // overwrite this message
5144 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005145 msg_col = 0;
5146 out_flush();
5147}
5148
5149/*
5150 * Load a dummy buffer to search for a pattern using vimgrep.
5151 */
5152 static buf_T *
5153vgr_load_dummy_buf(
5154 char_u *fname,
5155 char_u *dirname_start,
5156 char_u *dirname_now)
5157{
5158 int save_mls;
5159#if defined(FEAT_SYN_HL)
5160 char_u *save_ei = NULL;
5161#endif
5162 buf_T *buf;
5163
5164#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005165 // Don't do Filetype autocommands to avoid loading syntax and
5166 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005167 save_ei = au_event_disable(",Filetype");
5168#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005169 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005170 save_mls = p_mls;
5171 p_mls = 0;
5172
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005173 // Load file into a buffer, so that 'fileencoding' is detected,
5174 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005175 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5176
5177 p_mls = save_mls;
5178#if defined(FEAT_SYN_HL)
5179 au_event_restore(save_ei);
5180#endif
5181
5182 return buf;
5183}
5184
5185/*
5186 * Check whether a quickfix/location list valid. Autocmds may remove or change
5187 * a quickfix list when vimgrep is running. If the list is not found, create a
5188 * new list.
5189 */
5190 static int
5191vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005192 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005193 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005194 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005195 char_u *title)
5196{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005197 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005198 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005199 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005200 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005201 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005202 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005203 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005204 return FALSE;
5205 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005206 else
5207 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005208 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005209 qf_new_list(qi, title);
5210 return TRUE;
5211 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005212 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005213
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005214 if (qf_restore_list(qi, qfid) == FAIL)
5215 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005216
5217 return TRUE;
5218}
5219
5220/*
5221 * Search for a pattern in all the lines in a buffer and add the matching lines
5222 * to a quickfix list.
5223 */
5224 static int
5225vgr_match_buflines(
5226 qf_info_T *qi,
5227 char_u *fname,
5228 buf_T *buf,
5229 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005230 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005231 int duplicate_name,
5232 int flags)
5233{
5234 int found_match = FALSE;
5235 long lnum;
5236 colnr_T col;
5237
Bram Moolenaar1c299432018-10-28 14:36:09 +01005238 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005239 {
5240 col = 0;
5241 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5242 col, NULL, NULL) > 0)
5243 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005244 // Pass the buffer number so that it gets used even for a
5245 // dummy buffer, unless duplicate_name is set, then the
5246 // buffer will be wiped out below.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005247 if (qf_add_entry(qi,
5248 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005249 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005250 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005251 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005252 duplicate_name ? 0 : buf->b_fnum,
5253 ml_get_buf(buf,
5254 regmatch->startpos[0].lnum + lnum, FALSE),
5255 regmatch->startpos[0].lnum + lnum,
5256 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005257 FALSE, // vis_col
5258 NULL, // search pattern
5259 0, // nr
5260 0, // type
5261 TRUE // valid
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005262 ) == FAIL)
5263 {
5264 got_int = TRUE;
5265 break;
5266 }
5267 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005268 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005269 break;
5270 if ((flags & VGR_GLOBAL) == 0
5271 || regmatch->endpos[0].lnum > 0)
5272 break;
5273 col = regmatch->endpos[0].col
5274 + (col == regmatch->endpos[0].col);
5275 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5276 break;
5277 }
5278 line_breakcheck();
5279 if (got_int)
5280 break;
5281 }
5282
5283 return found_match;
5284}
5285
5286/*
5287 * Jump to the first match and update the directory.
5288 */
5289 static void
5290vgr_jump_to_match(
5291 qf_info_T *qi,
5292 int forceit,
5293 int *redraw_for_dummy,
5294 buf_T *first_match_buf,
5295 char_u *target_dir)
5296{
5297 buf_T *buf;
5298
5299 buf = curbuf;
5300 qf_jump(qi, 0, 0, forceit);
5301 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005302 // If we jumped to another buffer redrawing will already be
5303 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005304 *redraw_for_dummy = FALSE;
5305
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005306 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005307 if (curbuf == first_match_buf && target_dir != NULL)
5308 {
5309 exarg_T ea;
5310
5311 ea.arg = target_dir;
5312 ea.cmdidx = CMD_lcd;
5313 ex_cd(&ea);
5314 }
5315}
5316
5317/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005318 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005319 * ":vimgrepadd {pattern} file(s)"
5320 * ":lvimgrep {pattern} file(s)"
5321 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005322 */
5323 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005324ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005325{
Bram Moolenaar81695252004-12-29 20:58:21 +00005326 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005327 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005328 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005329 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005330 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005331 char_u *s;
5332 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005333 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005334 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005335 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005336 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005337 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005338 buf_T *buf;
5339 int duplicate_name = FALSE;
5340 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005341 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005342 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005343 buf_T *first_match_buf = NULL;
5344 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005345 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005346 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005347 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005348 char_u *dirname_start = NULL;
5349 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005350 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005351 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005352
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005353 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005354 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5355 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005356 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005357#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005358 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005359 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005360#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005361 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005362
Bram Moolenaar39665952018-08-15 20:59:48 +02005363 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005364 {
5365 qi = ll_get_or_alloc_list(curwin);
5366 if (qi == NULL)
5367 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005368 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005369 }
5370
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005371 if (eap->addr_count > 0)
5372 tomatch = eap->line2;
5373 else
5374 tomatch = MAXLNUM;
5375
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005376 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005377 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005378 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005379 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005380 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005382 emsg(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005383 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005384 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005385
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005386 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005387 if (regmatch.regprog == NULL)
5388 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005389
5390 p = skipwhite(p);
5391 if (*p == NUL)
5392 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005393 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005394 goto theend;
5395 }
5396
Bram Moolenaar55b69262017-08-13 13:42:01 +02005397 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005398 && eap->cmdidx != CMD_vimgrepadd
5399 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005400 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005401 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005402 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005403
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005404 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005405 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005406 goto theend;
5407 if (fcount == 0)
5408 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005409 emsg(_(e_nomatch));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005410 goto theend;
5411 }
5412
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005413 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5414 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005415 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005416 {
5417 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005418 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005419 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005420
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005421 // Remember the current directory, because a BufRead autocommand that does
5422 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005423 mch_dirname(dirname_start, MAXPATHL);
5424
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005425 incr_quickfix_busy();
5426
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005427 // Remember the current quickfix list identifier, so that we can check for
5428 // autocommands changing the current quickfix list.
Bram Moolenaar3c097222017-12-21 20:54:49 +01005429 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005430
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005431 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005432 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005433 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005434 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005435 if (time(NULL) > seconds)
5436 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005437 // Display the file name every second or so, show the user we are
5438 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005439 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005440 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005441 }
5442
Bram Moolenaar81695252004-12-29 20:58:21 +00005443 buf = buflist_findname_exp(fnames[fi]);
5444 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5445 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005446 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005447 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005448 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005449 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005450
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005451 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005452 }
5453 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005454 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005455 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005456
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005457 // Check whether the quickfix list is still valid. When loading a
5458 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005459 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005460 {
5461 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005462 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005463 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005464 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005465 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005466
Bram Moolenaar81695252004-12-29 20:58:21 +00005467 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005468 {
5469 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005470 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005471 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005472 else
5473 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005474 // Try for a match in all lines of the buffer.
5475 // For ":1vimgrep" look for first match only.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005476 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005477 &tomatch, duplicate_name, flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005478
Bram Moolenaar81695252004-12-29 20:58:21 +00005479 if (using_dummy)
5480 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005481 if (found_match && first_match_buf == NULL)
5482 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005483 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005484 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005485 // Never keep a dummy buffer if there is another buffer
5486 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005487 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005488 buf = NULL;
5489 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005490 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005491 || buf->b_p_bh[0] == 'u' // "unload"
5492 || buf->b_p_bh[0] == 'w' // "wipe"
5493 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005494 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005495 // When no match was found we don't need to remember the
5496 // buffer, wipe it out. If there was a match and it
5497 // wasn't the first one or we won't jump there: only
5498 // unload the buffer.
5499 // Ignore 'hidden' here, because it may lead to having too
5500 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005501 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005502 {
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 Moolenaar05159a02005-02-26 23:04:13 +00005506 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005507 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005508 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005509 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005510 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005511 buf = NULL;
5512 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005513 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005514
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005515 if (buf != NULL)
5516 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005517 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005518 buf->b_flags &= ~BF_DUMMY;
5519
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005520 // If the buffer is still loaded we need to use the
5521 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005522 if (buf == first_match_buf
5523 && target_dir == NULL
5524 && STRCMP(dirname_start, dirname_now) != 0)
5525 target_dir = vim_strsave(dirname_now);
5526
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005527 // The buffer is still loaded, the Filetype autocommands
5528 // need to be done now, in that buffer. And the modelines
5529 // need to be done (again). But not the window-local
5530 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005531 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005532#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005533 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5534 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005535#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005536 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005537 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005538 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005539 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005540 }
5541 }
5542
5543 FreeWild(fcount, fnames);
5544
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005545 qfl = &qi->qf_lists[qi->qf_curlist];
5546 qfl->qf_nonevalid = FALSE;
5547 qfl->qf_ptr = qfl->qf_start;
5548 qfl->qf_index = 1;
5549 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005550
Bram Moolenaar864293a2016-06-02 13:40:04 +02005551 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005552
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005553 if (au_name != NULL)
5554 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5555 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005556 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5557 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005558 if (!qflist_valid(wp, save_qfid)
5559 || qf_restore_list(qi, save_qfid) == FAIL)
5560 {
5561 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005562 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005563 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005564
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005565 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005566 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005567 {
5568 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005569 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5570 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005571 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005572 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005573 semsg(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005574
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005575 decr_quickfix_busy();
5576
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005577 // If we loaded a dummy buffer into the current window, the autocommands
5578 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005579 if (redraw_for_dummy)
5580 {
5581#ifdef FEAT_FOLDING
5582 foldUpdateAll(curwin);
5583#else
5584 redraw_later(NOT_VALID);
5585#endif
5586 }
5587
Bram Moolenaar86b68352004-12-27 21:59:20 +00005588theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005589 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005590 vim_free(dirname_now);
5591 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005592 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005593 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005594}
5595
5596/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005597 * Restore current working directory to "dirname_start" if they differ, taking
5598 * into account whether it is set locally or globally.
5599 */
5600 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005601restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005602{
5603 char_u *dirname_now = alloc(MAXPATHL);
5604
5605 if (NULL != dirname_now)
5606 {
5607 mch_dirname(dirname_now, MAXPATHL);
5608 if (STRCMP(dirname_start, dirname_now) != 0)
5609 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005610 // If the directory has changed, change it back by building up an
5611 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005612 exarg_T ea;
5613
5614 ea.arg = dirname_start;
5615 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5616 ex_cd(&ea);
5617 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005618 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005619 }
5620}
5621
5622/*
5623 * Load file "fname" into a dummy buffer and return the buffer pointer,
5624 * placing the directory resulting from the buffer load into the
5625 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5626 * prior to calling this function. Restores directory to "dirname_start" prior
5627 * to returning, if autocmds or the 'autochdir' option have changed it.
5628 *
5629 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5630 * or wipe_dummy_buffer() later!
5631 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005632 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005633 */
5634 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005635load_dummy_buffer(
5636 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005637 char_u *dirname_start, // in: old directory
5638 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005639{
5640 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005641 bufref_T newbufref;
5642 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005643 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005644 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005645 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005646
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005647 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005648 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5649 if (newbuf == NULL)
5650 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005651 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005652
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005653 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005654 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5655
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005656 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005657 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005658 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005659 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005660 ++newbuf->b_locked;
5661
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005662 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005663 aucmd_prepbuf(&aco, newbuf);
5664
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005665 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005666 (void)setfname(curbuf, fname, NULL, FALSE);
5667
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005668 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005669 check_need_swap(TRUE);
5670
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005671 // Remove the "dummy" flag, otherwise autocommands may not
5672 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005673 curbuf->b_flags &= ~BF_DUMMY;
5674
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005675 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005676 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005677 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005678 NULL, READ_NEW | READ_DUMMY);
5679 --newbuf->b_locked;
5680 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005681 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005682 && !(curbuf->b_flags & BF_NEW))
5683 {
5684 failed = FALSE;
5685 if (curbuf != newbuf)
5686 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005687 // Bloody autocommands changed the buffer! Can happen when
5688 // using netrw and editing a remote file. Use the current
5689 // buffer instead, delete the dummy one after restoring the
5690 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005691 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005692 newbuf = curbuf;
5693 }
5694 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005695
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005696 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005697 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005698 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5699 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005700
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005701 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5702 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005703 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005704 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005705
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005706 // When autocommands/'autochdir' option changed directory: go back.
5707 // Let the caller know what the resulting dir was first, in case it is
5708 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005709 mch_dirname(resulting_dir, MAXPATHL);
5710 restore_start_dir(dirname_start);
5711
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005712 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005713 return NULL;
5714 if (failed)
5715 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005716 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005717 return NULL;
5718 }
5719 return newbuf;
5720}
5721
5722/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005723 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5724 * directory to "dirname_start" prior to returning, if autocmds or the
5725 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005726 */
5727 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005728wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005729{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005730 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005731 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005732#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005733 cleanup_T cs;
5734
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005735 // Reset the error/interrupt/exception state here so that aborting()
5736 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5737 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005738 enter_cleanup(&cs);
5739#endif
5740
Bram Moolenaar81695252004-12-29 20:58:21 +00005741 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005742
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005743#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005744 // Restore the error/interrupt/exception state if not discarded by a
5745 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005746 leave_cleanup(&cs);
5747#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005748 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005749 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005750 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005751}
5752
5753/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005754 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5755 * directory to "dirname_start" prior to returning, if autocmds or the
5756 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005757 */
5758 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005759unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005760{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005761 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005762 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005763 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005764
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005765 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005766 restore_start_dir(dirname_start);
5767 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005768}
5769
Bram Moolenaar05159a02005-02-26 23:04:13 +00005770#if defined(FEAT_EVAL) || defined(PROTO)
5771/*
5772 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005773 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005774 */
5775 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005776get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005777{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005778 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005779 dict_T *dict;
5780 char_u buf[2];
5781 qfline_T *qfp;
5782 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005783 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005784
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005785 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005786 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005787 qi = &ql_info;
5788 if (wp != NULL)
5789 {
5790 qi = GET_LOC_LIST(wp);
5791 if (qi == NULL)
5792 return FAIL;
5793 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005794 }
5795
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005796 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005797 qf_idx = qi->qf_curlist;
5798
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005799 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005800 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005801
Bram Moolenaard823fa92016-08-12 16:29:27 +02005802 qfp = qi->qf_lists[qf_idx].qf_start;
5803 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005804 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005805 // Handle entries with a non-existing buffer number.
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005806 bufnum = qfp->qf_fnum;
5807 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5808 bufnum = 0;
5809
Bram Moolenaar05159a02005-02-26 23:04:13 +00005810 if ((dict = dict_alloc()) == NULL)
5811 return FAIL;
5812 if (list_append_dict(list, dict) == FAIL)
5813 return FAIL;
5814
5815 buf[0] = qfp->qf_type;
5816 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005817 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5818 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5819 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5820 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5821 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5822 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5823 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5824 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5825 || dict_add_string(dict, "type", buf) == FAIL
5826 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005827 return FAIL;
5828
5829 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005830 if (qfp == NULL)
5831 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005832 }
5833 return OK;
5834}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005835
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005836// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005837enum {
5838 QF_GETLIST_NONE = 0x0,
5839 QF_GETLIST_TITLE = 0x1,
5840 QF_GETLIST_ITEMS = 0x2,
5841 QF_GETLIST_NR = 0x4,
5842 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005843 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005844 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005845 QF_GETLIST_IDX = 0x40,
5846 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005847 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005848 QF_GETLIST_FILEWINID = 0x200,
5849 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005850};
5851
5852/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005853 * Parse text from 'di' and return the quickfix list items.
5854 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005855 */
5856 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005857qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005858{
5859 int status = FAIL;
5860 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005861 char_u *errorformat = p_efm;
5862 dictitem_T *efm_di;
5863 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005864
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005865 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005866 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005867 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005868 // If errorformat is supplied then use it, otherwise use the 'efm'
5869 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005870 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5871 {
5872 if (efm_di->di_tv.v_type != VAR_STRING ||
5873 efm_di->di_tv.vval.v_string == NULL)
5874 return FAIL;
5875 errorformat = efm_di->di_tv.vval.v_string;
5876 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005877
Bram Moolenaar36538222017-09-02 19:51:44 +02005878 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005879 if (l == NULL)
5880 return FAIL;
5881
Bram Moolenaar2d67d302018-11-16 18:46:02 +01005882 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005883 if (qi != NULL)
5884 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005885 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005886 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5887 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005888 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005889 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005890 }
5891 free(qi);
5892 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005893 dict_add_list(retdict, "items", l);
5894 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005895 }
5896
5897 return status;
5898}
5899
5900/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005901 * Return the quickfix/location list window identifier in the current tabpage.
5902 */
5903 static int
5904qf_winid(qf_info_T *qi)
5905{
5906 win_T *win;
5907
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005908 // The quickfix window can be opened even if the quickfix list is not set
5909 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005910 if (qi == NULL)
5911 return 0;
5912 win = qf_find_win(qi);
5913 if (win != NULL)
5914 return win->w_id;
5915 return 0;
5916}
5917
5918/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005919 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005920 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005921 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005922qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005923{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005924 int flags = QF_GETLIST_NONE;
5925
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005926 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005927 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005928 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005929 if (!loclist)
5930 // File window ID is applicable only to location list windows
5931 flags &= ~ QF_GETLIST_FILEWINID;
5932 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005933
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005934 if (dict_find(what, (char_u *)"title", -1) != NULL)
5935 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005936
Bram Moolenaara6d48492017-12-12 22:45:31 +01005937 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5938 flags |= QF_GETLIST_NR;
5939
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005940 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5941 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005942
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005943 if (dict_find(what, (char_u *)"context", -1) != NULL)
5944 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005945
Bram Moolenaara6d48492017-12-12 22:45:31 +01005946 if (dict_find(what, (char_u *)"id", -1) != NULL)
5947 flags |= QF_GETLIST_ID;
5948
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005949 if (dict_find(what, (char_u *)"items", -1) != NULL)
5950 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005951
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005952 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5953 flags |= QF_GETLIST_IDX;
5954
5955 if (dict_find(what, (char_u *)"size", -1) != NULL)
5956 flags |= QF_GETLIST_SIZE;
5957
Bram Moolenaarb254af32017-12-18 19:48:58 +01005958 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5959 flags |= QF_GETLIST_TICK;
5960
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005961 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5962 flags |= QF_GETLIST_FILEWINID;
5963
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005964 return flags;
5965}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005966
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005967/*
5968 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5969 * If 'nr' and 'id' are not present in 'what' then return the current
5970 * quickfix list index.
5971 * If 'nr' is zero then return the current quickfix list index.
5972 * If 'nr' is '$' then return the last quickfix list index.
5973 * If 'id' is present then return the index of the quickfix list with that id.
5974 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5975 * Return -1, if quickfix list is not present or if the stack is empty.
5976 */
5977 static int
5978qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5979{
5980 int qf_idx;
5981 dictitem_T *di;
5982
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005983 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005984 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5985 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005986 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005987 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005988 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005989 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005990 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005991 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005992 qf_idx = di->di_tv.vval.v_number - 1;
5993 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005994 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005995 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01005996 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005997 else if (di->di_tv.v_type == VAR_STRING
5998 && di->di_tv.vval.v_string != NULL
5999 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006000 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006001 qf_idx = qi->qf_listcount - 1;
6002 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006003 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006004 }
6005
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006006 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6007 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006008 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006009 if (di->di_tv.v_type == VAR_NUMBER)
6010 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006011 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006012 if (di->di_tv.vval.v_number != 0)
6013 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6014 }
6015 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006016 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006017 }
6018
6019 return qf_idx;
6020}
6021
6022/*
6023 * Return default values for quickfix list properties in retdict.
6024 */
6025 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006026qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006027{
6028 int status = OK;
6029
6030 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006031 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006032 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6033 {
6034 list_T *l = list_alloc();
6035 if (l != NULL)
6036 status = dict_add_list(retdict, "items", l);
6037 else
6038 status = FAIL;
6039 }
6040 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006041 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006042 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006043 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006044 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006045 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006046 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006047 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006048 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006049 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006050 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006051 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006052 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006053 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006054 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006055 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006056
6057 return status;
6058}
6059
6060/*
6061 * Return the quickfix list title as 'title' in retdict
6062 */
6063 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006064qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006065{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006066 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006067}
6068
6069/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006070 * Returns the identifier of the window used to display files from a location
6071 * list. If there is no associated window, then returns 0. Useful only when
6072 * called from a location list window.
6073 */
6074 static int
6075qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6076{
6077 int winid = 0;
6078
6079 if (wp != NULL && IS_LL_WINDOW(wp))
6080 {
6081 win_T *ll_wp = qf_find_win_with_loclist(qi);
6082 if (ll_wp != NULL)
6083 winid = ll_wp->w_id;
6084 }
6085
6086 return dict_add_number(retdict, "filewinid", winid);
6087}
6088
6089/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006090 * Return the quickfix list items/entries as 'items' in retdict
6091 */
6092 static int
6093qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6094{
6095 int status = OK;
6096 list_T *l = list_alloc();
6097 if (l != NULL)
6098 {
6099 (void)get_errorlist(qi, NULL, qf_idx, l);
6100 dict_add_list(retdict, "items", l);
6101 }
6102 else
6103 status = FAIL;
6104
6105 return status;
6106}
6107
6108/*
6109 * Return the quickfix list context (if any) as 'context' in retdict.
6110 */
6111 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006112qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006113{
6114 int status;
6115 dictitem_T *di;
6116
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006117 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006118 {
6119 di = dictitem_alloc((char_u *)"context");
6120 if (di != NULL)
6121 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006122 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006123 status = dict_add(retdict, di);
6124 if (status == FAIL)
6125 dictitem_free(di);
6126 }
6127 else
6128 status = FAIL;
6129 }
6130 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006131 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006132
6133 return status;
6134}
6135
6136/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006137 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006138 */
6139 static int
6140qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6141{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006142 int curidx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006143 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006144 // For empty lists, current index is set to 0
6145 curidx = 0;
6146 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006147}
6148
6149/*
6150 * Return quickfix/location list details (title) as a
6151 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6152 * then current list is used. Otherwise the specified list is used.
6153 */
6154 int
6155qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6156{
6157 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006158 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006159 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006160 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006161 dictitem_T *di;
6162 int flags = QF_GETLIST_NONE;
6163
6164 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6165 return qf_get_list_from_lines(what, di, retdict);
6166
6167 if (wp != NULL)
6168 qi = GET_LOC_LIST(wp);
6169
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006170 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006171
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006172 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006173 qf_idx = qf_getprop_qfidx(qi, what);
6174
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006175 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006176 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006177 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006178
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006179 qfl = &qi->qf_lists[qf_idx];
6180
Bram Moolenaard823fa92016-08-12 16:29:27 +02006181 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006182 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006183 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006184 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006185 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006186 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006187 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006188 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006189 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006190 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006191 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006192 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006193 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006194 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006195 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006196 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006197 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006198 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006199 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6200 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006201
Bram Moolenaard823fa92016-08-12 16:29:27 +02006202 return status;
6203}
6204
6205/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006206 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006207 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6208 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006209 */
6210 static int
6211qf_add_entry_from_dict(
6212 qf_info_T *qi,
6213 int qf_idx,
6214 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006215 int first_entry,
6216 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006217{
6218 static int did_bufnr_emsg;
6219 char_u *filename, *module, *pattern, *text, *type;
6220 int bufnum, valid, status, col, vcol, nr;
6221 long lnum;
6222
6223 if (first_entry)
6224 did_bufnr_emsg = FALSE;
6225
Bram Moolenaar8f667172018-12-14 15:38:31 +01006226 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6227 module = dict_get_string(d, (char_u *)"module", TRUE);
6228 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6229 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6230 col = (int)dict_get_number(d, (char_u *)"col");
6231 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6232 nr = (int)dict_get_number(d, (char_u *)"nr");
6233 type = dict_get_string(d, (char_u *)"type", TRUE);
6234 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6235 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006236 if (text == NULL)
6237 text = vim_strsave((char_u *)"");
6238
6239 valid = TRUE;
6240 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6241 valid = FALSE;
6242
6243 // Mark entries with non-existing buffer number as not valid. Give the
6244 // error message only once.
6245 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6246 {
6247 if (!did_bufnr_emsg)
6248 {
6249 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006250 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006251 }
6252 valid = FALSE;
6253 bufnum = 0;
6254 }
6255
6256 // If the 'valid' field is present it overrules the detected value.
6257 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006258 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006259
6260 status = qf_add_entry(qi,
6261 qf_idx,
6262 NULL, // dir
6263 filename,
6264 module,
6265 bufnum,
6266 text,
6267 lnum,
6268 col,
6269 vcol, // vis_col
6270 pattern, // search pattern
6271 nr,
6272 type == NULL ? NUL : *type,
6273 valid);
6274
6275 vim_free(filename);
6276 vim_free(module);
6277 vim_free(pattern);
6278 vim_free(text);
6279 vim_free(type);
6280
Bram Moolenaar9752c722018-12-22 16:49:34 +01006281 if (valid)
6282 *valid_entry = TRUE;
6283
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006284 return status;
6285}
6286
6287/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006288 * Add list of entries to quickfix/location list. Each list entry is
6289 * a dictionary with item information.
6290 */
6291 static int
6292qf_add_entries(
6293 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006294 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006295 list_T *list,
6296 char_u *title,
6297 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006298{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006299 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006300 listitem_T *li;
6301 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006302 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006303 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006304 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006305
Bram Moolenaara3921f42017-06-04 15:30:34 +02006306 if (action == ' ' || qf_idx == qi->qf_listcount)
6307 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006308 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006309 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006310 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006311 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006312 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006313 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006314 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006315 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006316 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006317 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006318 qf_free_items(qfl);
6319 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006320 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006321
6322 for (li = list->lv_first; li != NULL; li = li->li_next)
6323 {
6324 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006325 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006326
6327 d = li->li_tv.vval.v_dict;
6328 if (d == NULL)
6329 continue;
6330
Bram Moolenaar9752c722018-12-22 16:49:34 +01006331 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first,
6332 &valid_entry);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006333 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006334 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006335 }
6336
Bram Moolenaar9752c722018-12-22 16:49:34 +01006337 // Check if any valid error entries are added to the list.
6338 if (valid_entry)
6339 qfl->qf_nonevalid = FALSE;
6340 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006341 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006342 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006343
6344 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006345 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006346 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006347
6348 // Update the current error index if not appending to the list or if the
6349 // list was empty before and it is not empty now.
6350 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qi, qf_idx))
6351 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006352
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006353 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006354 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006355
6356 return retval;
6357}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006358
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006359/*
6360 * Get the quickfix list index from 'nr' or 'id'
6361 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006362 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006363qf_setprop_get_qfidx(
6364 qf_info_T *qi,
6365 dict_T *what,
6366 int action,
6367 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006368{
6369 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006370 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006371
Bram Moolenaard823fa92016-08-12 16:29:27 +02006372 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6373 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006374 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006375 if (di->di_tv.v_type == VAR_NUMBER)
6376 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006377 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006378 if (di->di_tv.vval.v_number != 0)
6379 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006380
Bram Moolenaar55b69262017-08-13 13:42:01 +02006381 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6382 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006383 // When creating a new list, accept qf_idx pointing to the next
6384 // non-available list and add the new list at the end of the
6385 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006386 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006387 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006388 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006389 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006390 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006391 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006392 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006393 }
6394 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006395 && di->di_tv.vval.v_string != NULL
6396 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006397 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006398 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006399 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006400 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006401 qf_idx = 0;
6402 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006403 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006404 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006405 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006406 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006407 }
6408
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006409 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006410 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006411 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006412 if (di->di_tv.v_type != VAR_NUMBER)
6413 return INVALID_QFIDX;
6414
6415 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006416 }
6417
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006418 return qf_idx;
6419}
6420
6421/*
6422 * Set the quickfix list title.
6423 */
6424 static int
6425qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6426{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006427 qf_list_T *qfl = &qi->qf_lists[qf_idx];
6428
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006429 if (di->di_tv.v_type != VAR_STRING)
6430 return FAIL;
6431
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006432 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01006433 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006434 if (qf_idx == qi->qf_curlist)
6435 qf_update_win_titlevar(qi);
6436
6437 return OK;
6438}
6439
6440/*
6441 * Set quickfix list items/entries.
6442 */
6443 static int
6444qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6445{
6446 int retval = FAIL;
6447 char_u *title_save;
6448
6449 if (di->di_tv.v_type != VAR_LIST)
6450 return FAIL;
6451
6452 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6453 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6454 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006455 vim_free(title_save);
6456
6457 return retval;
6458}
6459
6460/*
6461 * Set quickfix list items/entries from a list of lines.
6462 */
6463 static int
6464qf_setprop_items_from_lines(
6465 qf_info_T *qi,
6466 int qf_idx,
6467 dict_T *what,
6468 dictitem_T *di,
6469 int action)
6470{
6471 char_u *errorformat = p_efm;
6472 dictitem_T *efm_di;
6473 int retval = FAIL;
6474
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006475 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006476 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6477 {
6478 if (efm_di->di_tv.v_type != VAR_STRING ||
6479 efm_di->di_tv.vval.v_string == NULL)
6480 return FAIL;
6481 errorformat = efm_di->di_tv.vval.v_string;
6482 }
6483
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006484 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006485 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6486 return FAIL;
6487
6488 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006489 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006490 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6491 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6492 retval = OK;
6493
6494 return retval;
6495}
6496
6497/*
6498 * Set quickfix list context.
6499 */
6500 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006501qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006502{
6503 typval_T *ctx;
6504
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006505 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006506 ctx = alloc_tv();
6507 if (ctx != NULL)
6508 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006509 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006510
6511 return OK;
6512}
6513
6514/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006515 * Set the current index in the specified quickfix list
6516 */
6517 static int
6518qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
6519{
6520 int denote = FALSE;
6521 int newidx;
6522 int old_qfidx;
6523 qfline_T *qf_ptr;
6524
6525 // If the specified index is '$', then use the last entry
6526 if (di->di_tv.v_type == VAR_STRING
6527 && di->di_tv.vval.v_string != NULL
6528 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
6529 newidx = qfl->qf_count;
6530 else
6531 {
6532 // Otherwise use the specified index
6533 newidx = tv_get_number_chk(&di->di_tv, &denote);
6534 if (denote)
6535 return FAIL;
6536 }
6537
6538 if (newidx < 1) // sanity check
6539 return FAIL;
6540 if (newidx > qfl->qf_count)
6541 newidx = qfl->qf_count;
6542
6543 old_qfidx = qfl->qf_index;
6544 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
6545 if (qf_ptr == NULL)
6546 return FAIL;
6547 qfl->qf_ptr = qf_ptr;
6548 qfl->qf_index = newidx;
6549
6550 // If the current list is modified and it is displayed in the quickfix
6551 // window, then Update it.
6552 if (qi->qf_lists[qi->qf_curlist].qf_id == qfl->qf_id)
6553 qf_win_pos_update(qi, old_qfidx);
6554
6555 return OK;
6556}
6557
6558/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006559 * Set quickfix/location list properties (title, items, context).
6560 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006561 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006562 */
6563 static int
6564qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6565{
6566 dictitem_T *di;
6567 int retval = FAIL;
6568 int qf_idx;
6569 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006570 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006571
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006572 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006573 newlist = TRUE;
6574
6575 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006576 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006577 return FAIL;
6578
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006579 if (newlist)
6580 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006581 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006582 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006583 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006584 }
6585
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006586 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaard823fa92016-08-12 16:29:27 +02006587 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006588 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006589 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006590 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006591 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006592 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006593 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006594 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006595 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6596 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006597
Bram Moolenaarb254af32017-12-18 19:48:58 +01006598 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006599 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006600
Bram Moolenaard823fa92016-08-12 16:29:27 +02006601 return retval;
6602}
6603
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006604/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006605 * Find the non-location list window with the specified location list stack in
6606 * the current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006607 */
6608 static win_T *
6609find_win_with_ll(qf_info_T *qi)
6610{
6611 win_T *wp = NULL;
6612
6613 FOR_ALL_WINDOWS(wp)
6614 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6615 return wp;
6616
6617 return NULL;
6618}
6619
6620/*
6621 * Free the entire quickfix/location list stack.
6622 * If the quickfix/location list window is open, then clear it.
6623 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006624 static void
6625qf_free_stack(win_T *wp, qf_info_T *qi)
6626{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006627 win_T *qfwin = qf_find_win(qi);
6628 win_T *llwin = NULL;
6629 win_T *orig_wp = wp;
6630
6631 if (qfwin != NULL)
6632 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006633 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006634 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006635 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006636 qf_update_buffer(qi, NULL);
6637 }
6638
6639 if (wp != NULL && IS_LL_WINDOW(wp))
6640 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006641 // If in the location list window, then use the non-location list
6642 // window with this location list (if present)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006643 llwin = find_win_with_ll(qi);
6644 if (llwin != NULL)
6645 wp = llwin;
6646 }
6647
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006648 qf_free_all(wp);
6649 if (wp == NULL)
6650 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006651 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006652 qi->qf_curlist = 0;
6653 qi->qf_listcount = 0;
6654 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006655 else if (IS_LL_WINDOW(orig_wp))
6656 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006657 // If the location list window is open, then create a new empty
6658 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006659 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006660
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006661 // first free the list reference in the location list window
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006662 ll_free_all(&orig_wp->w_llist_ref);
6663
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006664 orig_wp->w_llist_ref = new_ll;
6665 if (llwin != NULL)
6666 {
6667 llwin->w_llist = new_ll;
6668 new_ll->qf_refcount++;
6669 }
6670 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006671}
6672
Bram Moolenaard823fa92016-08-12 16:29:27 +02006673/*
6674 * Populate the quickfix list with the items supplied in the list
6675 * of dictionaries. "title" will be copied to w:quickfix_title.
6676 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6677 */
6678 int
6679set_errorlist(
6680 win_T *wp,
6681 list_T *list,
6682 int action,
6683 char_u *title,
6684 dict_T *what)
6685{
6686 qf_info_T *qi = &ql_info;
6687 int retval = OK;
6688
6689 if (wp != NULL)
6690 {
6691 qi = ll_get_or_alloc_list(wp);
6692 if (qi == NULL)
6693 return FAIL;
6694 }
6695
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006696 if (action == 'f')
6697 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006698 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006699 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006700 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006701 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006702
6703 incr_quickfix_busy();
6704
6705 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006706 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006707 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006708 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006709 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006710 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006711 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006712 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006713
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006714 decr_quickfix_busy();
6715
Bram Moolenaard823fa92016-08-12 16:29:27 +02006716 return retval;
6717}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006718
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006719/*
6720 * Mark the context as in use for all the lists in a quickfix stack.
6721 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006722 static int
6723mark_quickfix_ctx(qf_info_T *qi, int copyID)
6724{
6725 int i;
6726 int abort = FALSE;
6727 typval_T *ctx;
6728
6729 for (i = 0; i < LISTCOUNT && !abort; ++i)
6730 {
6731 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006732 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6733 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006734 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6735 }
6736
6737 return abort;
6738}
6739
6740/*
6741 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006742 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006743 */
6744 int
6745set_ref_in_quickfix(int copyID)
6746{
6747 int abort = FALSE;
6748 tabpage_T *tp;
6749 win_T *win;
6750
6751 abort = mark_quickfix_ctx(&ql_info, copyID);
6752 if (abort)
6753 return abort;
6754
6755 FOR_ALL_TAB_WINDOWS(tp, win)
6756 {
6757 if (win->w_llist != NULL)
6758 {
6759 abort = mark_quickfix_ctx(win->w_llist, copyID);
6760 if (abort)
6761 return abort;
6762 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006763 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6764 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006765 // In a location list window and none of the other windows is
6766 // referring to this location list. Mark the location list
6767 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006768 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6769 if (abort)
6770 return abort;
6771 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006772 }
6773
6774 return abort;
6775}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006776#endif
6777
Bram Moolenaar81695252004-12-29 20:58:21 +00006778/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006779 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006780 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006781 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006782 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006783 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006784 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006785 */
6786 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006787ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006788{
6789 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006790 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006791 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006792 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006793 int_u save_qfid;
6794 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006795
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006796 switch (eap->cmdidx)
6797 {
6798 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6799 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6800 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6801 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6802 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6803 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6804 default: break;
6805 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006806 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6807 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006808 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006809#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006810 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006811 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006812#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006813 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006814
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006815 // Must come after autocommands.
Bram Moolenaar39665952018-08-15 20:59:48 +02006816 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006817 {
6818 qi = ll_get_or_alloc_list(curwin);
6819 if (qi == NULL)
6820 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006821 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006822 }
6823
Bram Moolenaar86b68352004-12-27 21:59:20 +00006824 if (*eap->arg == NUL)
6825 buf = curbuf;
6826 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6827 buf = buflist_findnr(atoi((char *)eap->arg));
6828 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006829 emsg(_(e_invarg));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006830 else if (buf->b_ml.ml_mfp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006831 emsg(_("E681: Buffer is not loaded"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006832 else
6833 {
6834 if (eap->addr_count == 0)
6835 {
6836 eap->line1 = 1;
6837 eap->line2 = buf->b_ml.ml_line_count;
6838 }
6839 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6840 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006841 emsg(_(e_invrange));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006842 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006843 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006844 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006845
6846 if (buf->b_sfname)
6847 {
6848 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6849 (char *)qf_title, (char *)buf->b_sfname);
6850 qf_title = IObuff;
6851 }
6852
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006853 incr_quickfix_busy();
6854
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006855 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006856 (eap->cmdidx != CMD_caddbuffer
6857 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006858 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006859 qf_title, NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006860 if (qf_stack_empty(qi))
6861 {
6862 decr_quickfix_busy();
6863 return;
6864 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006865 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006866 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006867
6868 // Remember the current quickfix list identifier, so that we can
6869 // check for autocommands changing the current quickfix list.
6870 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006871 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006872 {
6873 buf_T *curbuf_old = curbuf;
6874
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006875 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6876 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006877 if (curbuf != curbuf_old)
6878 // Autocommands changed buffer, don't jump now, "qi" may
6879 // be invalid.
6880 res = 0;
6881 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006882 // Jump to the first error for a new list and if autocmds didn't
6883 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006884 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006885 eap->cmdidx == CMD_lbuffer)
6886 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006887 // display the first error
6888 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006889
6890 decr_quickfix_busy();
Bram Moolenaar754b5602006-02-09 23:53:20 +00006891 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006892 }
6893}
6894
Bram Moolenaar1e015462005-09-25 22:16:38 +00006895#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006896/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006897 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6898 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006899 */
6900 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006901ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006902{
6903 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006904 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006905 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006906 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006907 int_u save_qfid;
6908 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006909
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006910 switch (eap->cmdidx)
6911 {
6912 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6913 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6914 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6915 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6916 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6917 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6918 default: break;
6919 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006920 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6921 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006922 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006923#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006924 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006925 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006926#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006927 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006928
Bram Moolenaar39665952018-08-15 20:59:48 +02006929 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006930 {
6931 qi = ll_get_or_alloc_list(curwin);
6932 if (qi == NULL)
6933 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006934 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006935 }
6936
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006937 // Evaluate the expression. When the result is a string or a list we can
6938 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006939 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006940 if (tv != NULL)
6941 {
6942 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6943 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6944 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006945 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006946 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006947 (eap->cmdidx != CMD_caddexpr
6948 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006949 (linenr_T)0, (linenr_T)0,
6950 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006951 if (qf_stack_empty(qi))
6952 {
6953 decr_quickfix_busy();
6954 goto cleanup;
6955 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006956 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006957 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006958
6959 // Remember the current quickfix list identifier, so that we can
6960 // check for autocommands changing the current quickfix list.
6961 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006962 if (au_name != NULL)
6963 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6964 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006965
6966 // Jump to the first error for a new list and if autocmds didn't
6967 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006968 if (res > 0 && (eap->cmdidx == CMD_cexpr
6969 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006970 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006971 // display the first error
6972 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006973 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006974 }
6975 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006976 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006977cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00006978 free_tv(tv);
6979 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006980}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006981#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006982
6983/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006984 * Get the location list for ":lhelpgrep"
6985 */
6986 static qf_info_T *
6987hgr_get_ll(int *new_ll)
6988{
6989 win_T *wp;
6990 qf_info_T *qi;
6991
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006992 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006993 if (bt_help(curwin->w_buffer))
6994 wp = curwin;
6995 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006996 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006997 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006998
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006999 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007000 qi = NULL;
7001 else
7002 qi = wp->w_llist;
7003
7004 if (qi == NULL)
7005 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007006 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007007 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007008 return NULL;
7009 *new_ll = TRUE;
7010 }
7011
7012 return qi;
7013}
7014
7015/*
7016 * Search for a pattern in a help file.
7017 */
7018 static void
7019hgr_search_file(
7020 qf_info_T *qi,
7021 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007022 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007023 regmatch_T *p_regmatch)
7024{
7025 FILE *fd;
7026 long lnum;
7027
7028 fd = mch_fopen((char *)fname, "r");
7029 if (fd == NULL)
7030 return;
7031
7032 lnum = 1;
7033 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7034 {
7035 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007036
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007037 // Convert a line if 'encoding' is not utf-8 and
7038 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007039 if (p_vc->vc_type != CONV_NONE
7040 && has_non_ascii(IObuff))
7041 {
7042 line = string_convert(p_vc, IObuff, NULL);
7043 if (line == NULL)
7044 line = IObuff;
7045 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007046
7047 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7048 {
7049 int l = (int)STRLEN(line);
7050
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007051 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007052 while (l > 0 && line[l - 1] <= ' ')
7053 line[--l] = NUL;
7054
7055 if (qf_add_entry(qi,
7056 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007057 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007058 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007059 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007060 0,
7061 line,
7062 lnum,
7063 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007064 + 1, // col
7065 FALSE, // vis_col
7066 NULL, // search pattern
7067 0, // nr
7068 1, // type
7069 TRUE // valid
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007070 ) == FAIL)
7071 {
7072 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007073 if (line != IObuff)
7074 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007075 break;
7076 }
7077 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007078 if (line != IObuff)
7079 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007080 ++lnum;
7081 line_breakcheck();
7082 }
7083 fclose(fd);
7084}
7085
7086/*
7087 * Search for a pattern in all the help files in the doc directory under
7088 * the given directory.
7089 */
7090 static void
7091hgr_search_files_in_dir(
7092 qf_info_T *qi,
7093 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007094 regmatch_T *p_regmatch,
7095 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007096#ifdef FEAT_MULTI_LANG
7097 , char_u *lang
7098#endif
7099 )
7100{
7101 int fcount;
7102 char_u **fnames;
7103 int fi;
7104
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007105 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007106 add_pathsep(dirname);
7107 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7108 if (gen_expand_wildcards(1, &dirname, &fcount,
7109 &fnames, EW_FILE|EW_SILENT) == OK
7110 && fcount > 0)
7111 {
7112 for (fi = 0; fi < fcount && !got_int; ++fi)
7113 {
7114#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007115 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007116 if (lang != NULL
7117 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007118 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007119 && !(STRNICMP(lang, "en", 2) == 0
7120 && STRNICMP("txt", fnames[fi]
7121 + STRLEN(fnames[fi]) - 3, 3) == 0))
7122 continue;
7123#endif
7124
Bram Moolenaara12a1612019-01-24 16:39:02 +01007125 hgr_search_file(qi, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007126 }
7127 FreeWild(fcount, fnames);
7128 }
7129}
7130
7131/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007132 * Search for a pattern in all the help files in the 'runtimepath'
7133 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007134 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007135 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007136 */
7137 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007138hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007139{
7140 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007141
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007142 vimconv_T vc;
7143
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007144 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7145 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007146 vc.vc_type = CONV_NONE;
7147 if (!enc_utf8)
7148 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007149
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007150 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007151 p = p_rtp;
7152 while (*p != NUL && !got_int)
7153 {
7154 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7155
Bram Moolenaara12a1612019-01-24 16:39:02 +01007156 hgr_search_files_in_dir(qi, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007157#ifdef FEAT_MULTI_LANG
7158 , lang
7159#endif
7160 );
7161 }
7162
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007163 if (vc.vc_type != CONV_NONE)
7164 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007165}
7166
7167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 * ":helpgrep {pattern}"
7169 */
7170 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007171ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
7173 regmatch_T regmatch;
7174 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007175 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007176 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007177 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007178 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
Bram Moolenaar73633f82012-01-20 13:39:07 +01007180 switch (eap->cmdidx)
7181 {
7182 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7183 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7184 default: break;
7185 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007186 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7187 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007188 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007189#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007190 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007191 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007192#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007193 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007194
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007195 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01007196 save_cpo = p_cpo;
7197 p_cpo = empty_option;
7198
Bram Moolenaar39665952018-08-15 20:59:48 +02007199 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007200 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007201 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007202 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007203 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007204 }
7205
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007206 incr_quickfix_busy();
7207
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007208#ifdef FEAT_MULTI_LANG
7209 // Check for a specified language
7210 lang = check_help_lang(eap->arg);
7211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7213 regmatch.rm_ic = FALSE;
7214 if (regmatch.regprog != NULL)
7215 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007216 qf_list_T *qfl;
7217
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007218 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007219 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007221 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007222
Bram Moolenaar473de612013-06-08 18:19:48 +02007223 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007225 qfl = &qi->qf_lists[qi->qf_curlist];
7226 qfl->qf_nonevalid = FALSE;
7227 qfl->qf_ptr = qfl->qf_start;
7228 qfl->qf_index = 1;
7229 qf_list_changed(qfl);
7230 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 }
7232
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007233 if (p_cpo == empty_option)
7234 p_cpo = save_cpo;
7235 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007236 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007237 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238
Bram Moolenaar73633f82012-01-20 13:39:07 +01007239 if (au_name != NULL)
7240 {
7241 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7242 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007243 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007244 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007245 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007246 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007247 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007248 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007249 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007250
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007251 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007252 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007253 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007254 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007255 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007256
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007257 decr_quickfix_busy();
7258
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007259 if (eap->cmdidx == CMD_lhelpgrep)
7260 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007261 // If the help window is not opened or if it already points to the
7262 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007263 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007264 {
7265 if (new_qi)
7266 ll_free_all(&qi);
7267 }
7268 else if (curwin->w_llist == NULL)
7269 curwin->w_llist = qi;
7270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271}
7272
7273#endif /* FEAT_QUICKFIX */