blob: 8b6c223c3c482f20497518fd9bf69950225a7977 [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;
3067#ifdef FEAT_VIRTUALEDIT
3068 curwin->w_cursor.coladd = 0;
3069#endif
3070 if (qf_viscol == TRUE)
3071 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003072 // Check each character from the beginning of the error
3073 // line up to the error column. For each tab character
3074 // found, reduce the error column value by the length of
3075 // a tab character.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003076 line = ml_get_curline();
3077 screen_col = 0;
3078 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3079 {
3080 if (*line == NUL)
3081 break;
3082 if (*line++ == '\t')
3083 {
3084 curwin->w_cursor.col -= 7 - (screen_col % 8);
3085 screen_col += 8 - (screen_col % 8);
3086 }
3087 else
3088 ++screen_col;
3089 }
3090 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003091 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003092 check_cursor();
3093 }
3094 else
3095 beginline(BL_WHITE | BL_FIX);
3096 }
3097 else
3098 {
3099 pos_T save_cursor;
3100
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003101 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003102 save_cursor = curwin->w_cursor;
3103 curwin->w_cursor.lnum = 0;
3104 if (!do_search(NULL, '/', qf_pattern, (long)1,
3105 SEARCH_KEEP, NULL, NULL))
3106 curwin->w_cursor = save_cursor;
3107 }
3108}
3109
3110/*
3111 * Display quickfix list index and size message
3112 */
3113 static void
3114qf_jump_print_msg(
3115 qf_info_T *qi,
3116 int qf_index,
3117 qfline_T *qf_ptr,
3118 buf_T *old_curbuf,
3119 linenr_T old_lnum)
3120{
3121 linenr_T i;
3122 int len;
3123
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003124 // Update the screen before showing the message, unless the screen
3125 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003126 if (!msg_scrolled)
3127 update_topline_redraw();
3128 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3129 qi->qf_lists[qi->qf_curlist].qf_count,
3130 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3131 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003132 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003133 len = (int)STRLEN(IObuff);
3134 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3135
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003136 // Output the message. Overwrite to avoid scrolling when the 'O'
3137 // flag is present in 'shortmess'; But when not jumping, print the
3138 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003139 i = msg_scroll;
3140 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3141 msg_scroll = TRUE;
3142 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3143 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003144 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003145 msg_scroll = i;
3146}
3147
3148/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003149 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003150 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3151 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003152 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3153 * able to jump/open a window. Returns NOTDONE if a file is not associated
3154 * with the entry.
3155 */
3156 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003157qf_jump_open_window(
3158 qf_info_T *qi,
3159 qfline_T *qf_ptr,
3160 int newwin,
3161 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003162{
3163 // For ":helpgrep" find a help window or open one.
3164 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003165 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003166 return FAIL;
3167
3168 // If currently in the quickfix window, find another window to show the
3169 // file in.
3170 if (bt_quickfix(curbuf) && !*opened_window)
3171 {
3172 // If there is no file specified, we don't know where to go.
3173 // But do advance, otherwise ":cn" gets stuck.
3174 if (qf_ptr->qf_fnum == 0)
3175 return NOTDONE;
3176
Bram Moolenaarb2443732018-11-11 22:50:27 +01003177 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3178 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003179 return FAIL;
3180 }
3181
3182 return OK;
3183}
3184
3185/*
3186 * Edit a selected file from the quickfix/location list and jump to a
3187 * particular line/column, adjust the folds and display a message about the
3188 * jump.
3189 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3190 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3191 * the file.
3192 */
3193 static int
3194qf_jump_to_buffer(
3195 qf_info_T *qi,
3196 int qf_index,
3197 qfline_T *qf_ptr,
3198 int forceit,
3199 win_T *oldwin,
3200 int *opened_window,
3201 int openfold,
3202 int print_message)
3203{
3204 buf_T *old_curbuf;
3205 linenr_T old_lnum;
3206 int retval = OK;
3207
3208 // If there is a file name, read the wanted file if needed, and check
3209 // autowrite etc.
3210 old_curbuf = curbuf;
3211 old_lnum = curwin->w_cursor.lnum;
3212
3213 if (qf_ptr->qf_fnum != 0)
3214 {
3215 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3216 opened_window);
3217 if (retval != OK)
3218 return retval;
3219 }
3220
3221 // When not switched to another buffer, still need to set pc mark
3222 if (curbuf == old_curbuf)
3223 setpcmark();
3224
3225 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3226 qf_ptr->qf_pattern);
3227
3228#ifdef FEAT_FOLDING
3229 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3230 foldOpenCursor();
3231#endif
3232 if (print_message)
3233 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3234
3235 return retval;
3236}
3237
3238/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003239 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 */
3241 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003242qf_jump(qf_info_T *qi,
3243 int dir,
3244 int errornr,
3245 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003247 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3248}
3249
3250/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003251 * Jump to a quickfix line.
3252 * If dir == 0 go to entry "errornr".
3253 * If dir == FORWARD go "errornr" valid entries forward.
3254 * If dir == BACKWARD go "errornr" valid entries backward.
3255 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3256 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3257 * else if "errornr" is zero, redisplay the same line
3258 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003259 * If 'newwin' is TRUE, then open the file in a new window.
3260 */
3261 void
3262qf_jump_newwin(qf_info_T *qi,
3263 int dir,
3264 int errornr,
3265 int forceit,
3266 int newwin)
3267{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003268 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003269 qfline_T *qf_ptr;
3270 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003273 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003274 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003276 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003279 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003281 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003283 if (qi == NULL)
3284 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003285
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003286 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003288 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 return;
3290 }
3291
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003292 qfl = &qi->qf_lists[qi->qf_curlist];
3293
3294 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003296 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003298
3299 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3300 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003302 qf_ptr = old_qf_ptr;
3303 qf_index = old_qf_index;
3304 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003307 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003308 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003309 // No need to print the error message if it's visible in the error
3310 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 print_message = FALSE;
3312
Bram Moolenaarb2443732018-11-11 22:50:27 +01003313 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003314 if (retval == FAIL)
3315 goto failed;
3316 if (retval == NOTDONE)
3317 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003318
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003319 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, oldwin,
3320 &opened_window, old_KeyTyped, print_message);
3321 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003323 // Quickfix/location list is freed by an autocmd
3324 qi = NULL;
3325 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003328 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003331 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003332 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003334 // Couldn't open file, so put index back where it was. This could
3335 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 qf_ptr = old_qf_ptr;
3338 qf_index = old_qf_index;
3339 }
3340 }
3341theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003342 if (qi != NULL)
3343 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003344 qfl->qf_ptr = qf_ptr;
3345 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 if (p_swb != old_swb && opened_window)
3348 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003349 // Restore old 'switchbuf' value, but not when an autocommand or
3350 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003354 swb_flags = old_swb_flags;
3355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 else
3357 free_string_option(old_swb);
3358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359}
3360
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003361// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003362static int qfFileAttr;
3363static int qfSepAttr;
3364static int qfLineAttr;
3365
3366/*
3367 * Display information about a single entry from the quickfix/location list.
3368 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003369 * 'cursel' will be set to TRUE for the currently selected entry in the
3370 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003371 */
3372 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003373qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003374{
3375 char_u *fname;
3376 buf_T *buf;
3377 int filter_entry;
3378
3379 fname = NULL;
3380 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3381 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3382 (char *)qfp->qf_module);
3383 else {
3384 if (qfp->qf_fnum != 0
3385 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3386 {
3387 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003388 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003389 fname = gettail(fname);
3390 }
3391 if (fname == NULL)
3392 sprintf((char *)IObuff, "%2d", qf_idx);
3393 else
3394 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3395 qf_idx, (char *)fname);
3396 }
3397
3398 // Support for filtering entries using :filter /pat/ clist
3399 // Match against the module name, file name, search pattern and
3400 // text of the entry.
3401 filter_entry = TRUE;
3402 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3403 filter_entry &= message_filtered(qfp->qf_module);
3404 if (filter_entry && fname != NULL)
3405 filter_entry &= message_filtered(fname);
3406 if (filter_entry && qfp->qf_pattern != NULL)
3407 filter_entry &= message_filtered(qfp->qf_pattern);
3408 if (filter_entry)
3409 filter_entry &= message_filtered(qfp->qf_text);
3410 if (filter_entry)
3411 return;
3412
3413 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003414 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003415
3416 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003417 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003418 if (qfp->qf_lnum == 0)
3419 IObuff[0] = NUL;
3420 else if (qfp->qf_col == 0)
3421 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3422 else
3423 sprintf((char *)IObuff, "%ld col %d",
3424 qfp->qf_lnum, qfp->qf_col);
3425 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3426 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003427 msg_puts_attr((char *)IObuff, qfLineAttr);
3428 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003429 if (qfp->qf_pattern != NULL)
3430 {
3431 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003432 msg_puts((char *)IObuff);
3433 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003434 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003435 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003436
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003437 // Remove newlines and leading whitespace from the text. For an
3438 // unrecognized line keep the indent, the compiler may mark a word
3439 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003440 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3441 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3442 IObuff, IOSIZE);
3443 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003444 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003445}
3446
3447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003449 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 */
3451 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003452qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003454 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003455 qfline_T *qfp;
3456 int i;
3457 int idx1 = 1;
3458 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003459 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003460 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003461 int all = eap->forceit; // if not :cl!, only show
3462 // recognised errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003463 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
Bram Moolenaar39665952018-08-15 20:59:48 +02003465 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003466 {
3467 qi = GET_LOC_LIST(curwin);
3468 if (qi == NULL)
3469 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003470 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003471 return;
3472 }
3473 }
3474
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003475 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003477 emsg(_(e_quickfix));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 return;
3479 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003480 if (*arg == '+')
3481 {
3482 ++arg;
3483 plus = TRUE;
3484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3486 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003487 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 return;
3489 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003490 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003491 if (plus)
3492 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003493 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003494 idx2 = i + idx1;
3495 idx1 = i;
3496 }
3497 else
3498 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003499 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003500 if (idx1 < 0)
3501 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3502 if (idx2 < 0)
3503 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003506 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003507 shorten_fnames(FALSE);
3508
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003509 // Get the attributes for the different quickfix highlight items. Note
3510 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003511 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3512 if (qfFileAttr == 0)
3513 qfFileAttr = HL_ATTR(HLF_D);
3514 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3515 if (qfSepAttr == 0)
3516 qfSepAttr = HL_ATTR(HLF_D);
3517 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3518 if (qfLineAttr == 0)
3519 qfLineAttr = HL_ATTR(HLF_N);
3520
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003521 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003523 qfp = qfl->qf_start;
3524 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3527 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003528 if (got_int)
3529 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003530
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003531 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003533
3534 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003535 if (qfp == NULL)
3536 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003537 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 ui_breakcheck();
3539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540}
3541
3542/*
3543 * Remove newlines and leading whitespace from an error message.
3544 * Put the result in "buf[bufsize]".
3545 */
3546 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003547qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548{
3549 int i;
3550 char_u *p = text;
3551
3552 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3553 {
3554 if (*p == '\n')
3555 {
3556 buf[i] = ' ';
3557 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003558 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 break;
3560 }
3561 else
3562 buf[i] = *p++;
3563 }
3564 buf[i] = NUL;
3565}
3566
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003567/*
3568 * Display information (list number, list size and the title) about a
3569 * quickfix/location list.
3570 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003571 static void
3572qf_msg(qf_info_T *qi, int which, char *lead)
3573{
3574 char *title = (char *)qi->qf_lists[which].qf_title;
3575 int count = qi->qf_lists[which].qf_count;
3576 char_u buf[IOSIZE];
3577
3578 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3579 lead,
3580 which + 1,
3581 qi->qf_listcount,
3582 count);
3583
3584 if (title != NULL)
3585 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003586 size_t len = STRLEN(buf);
3587
3588 if (len < 34)
3589 {
3590 vim_memset(buf + len, ' ', 34 - len);
3591 buf[34] = NUL;
3592 }
3593 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003594 }
3595 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003596 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003597}
3598
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599/*
3600 * ":colder [count]": Up in the quickfix stack.
3601 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003602 * ":lolder [count]": Up in the location list stack.
3603 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 */
3605 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003606qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003608 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 int count;
3610
Bram Moolenaar39665952018-08-15 20:59:48 +02003611 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003612 {
3613 qi = GET_LOC_LIST(curwin);
3614 if (qi == NULL)
3615 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003616 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003617 return;
3618 }
3619 }
3620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 if (eap->addr_count != 0)
3622 count = eap->line2;
3623 else
3624 count = 1;
3625 while (count--)
3626 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003627 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003629 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003631 emsg(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003632 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003634 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 }
3636 else
3637 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003638 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003640 emsg(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003641 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003643 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 }
3645 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003646 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003647 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648}
3649
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003650/*
3651 * Display the information about all the quickfix/location lists in the stack
3652 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003653 void
3654qf_history(exarg_T *eap)
3655{
3656 qf_info_T *qi = &ql_info;
3657 int i;
3658
Bram Moolenaar39665952018-08-15 20:59:48 +02003659 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003660 qi = GET_LOC_LIST(curwin);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003661 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003662 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003663 else
3664 for (i = 0; i < qi->qf_listcount; ++i)
3665 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3666}
3667
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003669 * Free all the entries in the error list "idx". Note that other information
3670 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 */
3672 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003673qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003675 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003676 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003677 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003679 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003681 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003682 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003683 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003684 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003685 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003686 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003687 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003688 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003689 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003690 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003691 // Somehow qf_count may have an incorrect value, set it to 1
3692 // to avoid crashing when it's wrong.
3693 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003694 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003695 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003696 qfl->qf_start = qfpnext;
3697 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003699
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003700 qfl->qf_index = 0;
3701 qfl->qf_start = NULL;
3702 qfl->qf_last = NULL;
3703 qfl->qf_ptr = NULL;
3704 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003705
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003706 qf_clean_dir_stack(&qfl->qf_dir_stack);
3707 qfl->qf_directory = NULL;
3708 qf_clean_dir_stack(&qfl->qf_file_stack);
3709 qfl->qf_currfile = NULL;
3710 qfl->qf_multiline = FALSE;
3711 qfl->qf_multiignore = FALSE;
3712 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713}
3714
3715/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003716 * Free error list "idx". Frees all the entries in the quickfix list,
3717 * associated context information and the title.
3718 */
3719 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003720qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003721{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003722 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003723
Bram Moolenaard23a8232018-02-10 18:45:26 +01003724 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003725 free_tv(qfl->qf_ctx);
3726 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003727 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003728 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003729}
3730
3731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 * qf_mark_adjust: adjust marks
3733 */
3734 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003735qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003736 win_T *wp,
3737 linenr_T line1,
3738 linenr_T line2,
3739 long amount,
3740 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003742 int i;
3743 qfline_T *qfp;
3744 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003745 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003746 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003747 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
Bram Moolenaarc1542742016-07-20 21:44:37 +02003749 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003750 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003751 if (wp != NULL)
3752 {
3753 if (wp->w_llist == NULL)
3754 return;
3755 qi = wp->w_llist;
3756 }
3757
3758 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003759 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003760 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003761 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3762 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 if (qfp->qf_fnum == curbuf->b_fnum)
3764 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003765 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3767 {
3768 if (amount == MAXLNUM)
3769 qfp->qf_cleared = TRUE;
3770 else
3771 qfp->qf_lnum += amount;
3772 }
3773 else if (amount_after && qfp->qf_lnum > line2)
3774 qfp->qf_lnum += amount_after;
3775 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003776
3777 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003778 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779}
3780
3781/*
3782 * Make a nice message out of the error character and the error number:
3783 * char number message
3784 * e or E 0 " error"
3785 * w or W 0 " warning"
3786 * i or I 0 " info"
3787 * 0 0 ""
3788 * other 0 " c"
3789 * e or E n " error n"
3790 * w or W n " warning n"
3791 * i or I n " info n"
3792 * 0 n " error n"
3793 * other n " c n"
3794 * 1 x "" :helpgrep
3795 */
3796 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003797qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798{
3799 static char_u buf[20];
3800 static char_u cc[3];
3801 char_u *p;
3802
3803 if (c == 'W' || c == 'w')
3804 p = (char_u *)" warning";
3805 else if (c == 'I' || c == 'i')
3806 p = (char_u *)" info";
3807 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3808 p = (char_u *)" error";
3809 else if (c == 0 || c == 1)
3810 p = (char_u *)"";
3811 else
3812 {
3813 cc[0] = ' ';
3814 cc[1] = c;
3815 cc[2] = NUL;
3816 p = cc;
3817 }
3818
3819 if (nr <= 0)
3820 return p;
3821
3822 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3823 return buf;
3824}
3825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003827 * When "split" is FALSE: Open the entry/result under the cursor.
3828 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3829 */
3830 void
3831qf_view_result(int split)
3832{
3833 qf_info_T *qi = &ql_info;
3834
3835 if (!bt_quickfix(curbuf))
3836 return;
3837
3838 if (IS_LL_WINDOW(curwin))
3839 qi = GET_LOC_LIST(curwin);
3840
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003841 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003843 emsg(_(e_quickfix));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003844 return;
3845 }
3846
3847 if (split)
3848 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003849 // Open the selected entry in a new window
3850 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3851 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003852 return;
3853 }
3854
3855 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3856}
3857
3858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 * ":cwindow": open the quickfix window if we have errors to display,
3860 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003861 * ":lwindow": open the location list window if we have locations to display,
3862 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 */
3864 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003865ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003867 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003868 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 win_T *win;
3870
Bram Moolenaar39665952018-08-15 20:59:48 +02003871 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003872 {
3873 qi = GET_LOC_LIST(curwin);
3874 if (qi == NULL)
3875 return;
3876 }
3877
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003878 qfl = &qi->qf_lists[qi->qf_curlist];
3879
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003880 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003881 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003883 // If a quickfix window is open but we have no errors to display,
3884 // close the window. If a quickfix window is not open, then open
3885 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003886 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003887 || qfl->qf_nonevalid
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003888 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
3890 if (win != NULL)
3891 ex_cclose(eap);
3892 }
3893 else if (win == NULL)
3894 ex_copen(eap);
3895}
3896
3897/*
3898 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003899 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003902ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003904 win_T *win = NULL;
3905 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
Bram Moolenaar39665952018-08-15 20:59:48 +02003907 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003908 {
3909 qi = GET_LOC_LIST(curwin);
3910 if (qi == NULL)
3911 return;
3912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003914 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003915 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 if (win != NULL)
3917 win_close(win, FALSE);
3918}
3919
3920/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003921 * Set "w:quickfix_title" if "qi" has a title.
3922 */
3923 static void
3924qf_set_title_var(qf_list_T *qfl)
3925{
3926 if (qfl->qf_title != NULL)
3927 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3928}
3929
3930/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003931 * Goto a quickfix or location list window (if present).
3932 * Returns OK if the window is found, FAIL otherwise.
3933 */
3934 static int
3935qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3936{
3937 win_T *win;
3938
3939 win = qf_find_win(qi);
3940 if (win == NULL)
3941 return FAIL;
3942
3943 win_goto(win);
3944 if (resize)
3945 {
3946 if (vertsplit)
3947 {
3948 if (sz != win->w_width)
3949 win_setwidth(sz);
3950 }
3951 else if (sz != win->w_height)
3952 win_setheight(sz);
3953 }
3954
3955 return OK;
3956}
3957
3958/*
3959 * Open a new quickfix or location list window, load the quickfix buffer and
3960 * set the appropriate options for the window.
3961 * Returns FAIL if the window could not be opened.
3962 */
3963 static int
3964qf_open_new_cwindow(qf_info_T *qi, int height)
3965{
3966 buf_T *qf_buf;
3967 win_T *oldwin = curwin;
3968 tabpage_T *prevtab = curtab;
3969 int flags = 0;
3970 win_T *win;
3971
3972 qf_buf = qf_find_buf(qi);
3973
3974 // The current window becomes the previous window afterwards.
3975 win = curwin;
3976
3977 if (IS_QF_STACK(qi) && cmdmod.split == 0)
3978 // Create the new quickfix window at the very bottom, except when
3979 // :belowright or :aboveleft is used.
3980 win_goto(lastwin);
3981 // Default is to open the window below the current window
3982 if (cmdmod.split == 0)
3983 flags = WSP_BELOW;
3984 flags |= WSP_NEWLOC;
3985 if (win_split(height, flags) == FAIL)
3986 return FAIL; // not enough room for window
3987 RESET_BINDING(curwin);
3988
3989 if (IS_LL_STACK(qi))
3990 {
3991 // For the location list window, create a reference to the
3992 // location list from the window 'win'.
3993 curwin->w_llist_ref = win->w_llist;
3994 win->w_llist->qf_refcount++;
3995 }
3996
3997 if (oldwin != curwin)
3998 oldwin = NULL; // don't store info when in another window
3999 if (qf_buf != NULL)
4000 {
4001 // Use the existing quickfix buffer
4002 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4003 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4004 }
4005 else
4006 {
4007 // Create a new quickfix buffer
4008 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4009
4010 // switch off 'swapfile'
4011 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4012 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4013 OPT_LOCAL);
4014 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
4015 RESET_BINDING(curwin);
4016#ifdef FEAT_DIFF
4017 curwin->w_p_diff = FALSE;
4018#endif
4019#ifdef FEAT_FOLDING
4020 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4021 OPT_LOCAL);
4022#endif
4023 }
4024
4025 // Only set the height when still in the same tab page and there is no
4026 // window to the side.
4027 if (curtab == prevtab && curwin->w_width == Columns)
4028 win_setheight(height);
4029 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4030 if (win_valid(win))
4031 prevwin = win;
4032
4033 return OK;
4034}
4035
4036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004038 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 */
4040 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004041ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004043 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004044 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004046 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004047 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
Bram Moolenaar39665952018-08-15 20:59:48 +02004049 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004050 {
4051 qi = GET_LOC_LIST(curwin);
4052 if (qi == NULL)
4053 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004054 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004055 return;
4056 }
4057 }
4058
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004059 incr_quickfix_busy();
4060
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 if (eap->addr_count != 0)
4062 height = eap->line2;
4063 else
4064 height = QF_WINHEIGHT;
4065
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004066 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067#ifdef FEAT_GUI
4068 need_mouse_correct = TRUE;
4069#endif
4070
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004071 // Find an existing quickfix window, or open a new one.
4072 if (cmdmod.tab == 0)
4073 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4074 cmdmod.split & WSP_VERT);
4075 if (status == FAIL)
4076 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004077 {
4078 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004079 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004082 qfl = &qi->qf_lists[qi->qf_curlist];
4083 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004084 // Save the current index here, as updating the quickfix buffer may free
4085 // the quickfix list
4086 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004087
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004088 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004089 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004091 decr_quickfix_busy();
4092
4093 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 curwin->w_cursor.col = 0;
4095 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004096 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097}
4098
4099/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004100 * Move the cursor in the quickfix window to "lnum".
4101 */
4102 static void
4103qf_win_goto(win_T *win, linenr_T lnum)
4104{
4105 win_T *old_curwin = curwin;
4106
4107 curwin = win;
4108 curbuf = win->w_buffer;
4109 curwin->w_cursor.lnum = lnum;
4110 curwin->w_cursor.col = 0;
4111#ifdef FEAT_VIRTUALEDIT
4112 curwin->w_cursor.coladd = 0;
4113#endif
4114 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004115 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004116 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004117 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004118 curwin = old_curwin;
4119 curbuf = curwin->w_buffer;
4120}
4121
4122/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004123 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004124 */
4125 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004126ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004127{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004128 qf_info_T *qi = &ql_info;
4129 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004130
Bram Moolenaar39665952018-08-15 20:59:48 +02004131 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004132 {
4133 qi = GET_LOC_LIST(curwin);
4134 if (qi == NULL)
4135 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004136 emsg(_(e_loclist));
Bram Moolenaar537ef082016-07-09 17:56:19 +02004137 return;
4138 }
4139 }
4140
4141 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004142 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4143 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4144}
4145
4146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 * Return the number of the current entry (line number in the quickfix
4148 * window).
4149 */
4150 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004151qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004153 qf_info_T *qi = &ql_info;
4154
4155 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004156 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004157 qi = wp->w_llist_ref;
4158
4159 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160}
4161
4162/*
4163 * Update the cursor position in the quickfix window to the current error.
4164 * Return TRUE if there is a quickfix window.
4165 */
4166 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004167qf_win_pos_update(
4168 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004169 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
4171 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004172 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004174 // Put the cursor on the current error in the quickfix window, so that
4175 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004176 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 if (win != NULL
4178 && qf_index <= win->w_buffer->b_ml.ml_line_count
4179 && old_qf_index != qf_index)
4180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 if (qf_index > old_qf_index)
4182 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004183 win->w_redraw_top = old_qf_index;
4184 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
4186 else
4187 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004188 win->w_redraw_top = qf_index;
4189 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004191 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 return win != NULL;
4194}
4195
4196/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004197 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004198 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004199 */
4200 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004201is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004202{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004203 // A window displaying the quickfix buffer will have the w_llist_ref field
4204 // set to NULL.
4205 // A window displaying a location list buffer will have the w_llist_ref
4206 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004207 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004208 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4209 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004210 return TRUE;
4211
4212 return FALSE;
4213}
4214
4215/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004216 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004217 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004218 */
4219 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004220qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004221{
4222 win_T *win;
4223
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004224 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004225 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004226 return win;
4227 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004228}
4229
4230/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004231 * Find a quickfix buffer.
4232 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 */
4234 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004235qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004237 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004238 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
Bram Moolenaar9c102382006-05-03 21:26:49 +00004240 FOR_ALL_TAB_WINDOWS(tp, win)
4241 if (is_qf_win(win, qi))
4242 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004243
Bram Moolenaar9c102382006-05-03 21:26:49 +00004244 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245}
4246
4247/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004248 * Update the w:quickfix_title variable in the quickfix/location list window
4249 */
4250 static void
4251qf_update_win_titlevar(qf_info_T *qi)
4252{
4253 win_T *win;
4254 win_T *curwin_save;
4255
4256 if ((win = qf_find_win(qi)) != NULL)
4257 {
4258 curwin_save = curwin;
4259 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004260 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004261 curwin = curwin_save;
4262 }
4263}
4264
4265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 * Find the quickfix buffer. If it exists, update the contents.
4267 */
4268 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004269qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270{
4271 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004272 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004275 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004276 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 if (buf != NULL)
4278 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004279 linenr_T old_line_count = buf->b_ml.ml_line_count;
4280
4281 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004282 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004283 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284
Bram Moolenaard823fa92016-08-12 16:29:27 +02004285 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004286
Bram Moolenaar864293a2016-06-02 13:40:04 +02004287 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004288 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004289
Bram Moolenaar864293a2016-06-02 13:40:04 +02004290 if (old_last == NULL)
4291 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004292 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004293
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004294 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004295 aucmd_restbuf(&aco);
4296 }
4297
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004298 // Only redraw when added lines are visible. This avoids flickering
4299 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004300 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4301 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303}
4304
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004305/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004306 * Add an error line to the quickfix buffer.
4307 */
4308 static int
4309qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4310{
4311 int len;
4312 buf_T *errbuf;
4313
4314 if (qfp->qf_module != NULL)
4315 {
4316 STRCPY(IObuff, qfp->qf_module);
4317 len = (int)STRLEN(IObuff);
4318 }
4319 else if (qfp->qf_fnum != 0
4320 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4321 && errbuf->b_fname != NULL)
4322 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004323 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004324 STRCPY(IObuff, gettail(errbuf->b_fname));
4325 else
4326 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004327 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004328 if (errbuf->b_sfname == NULL
4329 || mch_isFullName(errbuf->b_sfname))
4330 {
4331 if (*dirname == NUL)
4332 mch_dirname(dirname, MAXPATHL);
4333 shorten_buf_fname(errbuf, dirname, FALSE);
4334 }
4335 STRCPY(IObuff, errbuf->b_fname);
4336 }
4337 len = (int)STRLEN(IObuff);
4338 }
4339 else
4340 len = 0;
4341 IObuff[len++] = '|';
4342
4343 if (qfp->qf_lnum > 0)
4344 {
4345 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4346 len += (int)STRLEN(IObuff + len);
4347
4348 if (qfp->qf_col > 0)
4349 {
4350 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4351 len += (int)STRLEN(IObuff + len);
4352 }
4353
4354 sprintf((char *)IObuff + len, "%s",
4355 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4356 len += (int)STRLEN(IObuff + len);
4357 }
4358 else if (qfp->qf_pattern != NULL)
4359 {
4360 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4361 len += (int)STRLEN(IObuff + len);
4362 }
4363 IObuff[len++] = '|';
4364 IObuff[len++] = ' ';
4365
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004366 // Remove newlines and leading whitespace from the text.
4367 // For an unrecognized line keep the indent, the compiler may
4368 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004369 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4370 IObuff + len, IOSIZE - len);
4371
4372 if (ml_append_buf(buf, lnum, IObuff,
4373 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4374 return FAIL;
4375
4376 return OK;
4377}
4378
4379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 * Fill current buffer with quickfix errors, replacing any previous contents.
4381 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004382 * If "old_last" is not NULL append the items after this one.
4383 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4384 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 */
4386 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004387qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004389 linenr_T lnum;
4390 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004391 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
Bram Moolenaar864293a2016-06-02 13:40:04 +02004393 if (old_last == NULL)
4394 {
4395 if (buf != curbuf)
4396 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004397 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004398 return;
4399 }
4400
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004401 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004402 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4403 (void)ml_delete((linenr_T)1, FALSE);
4404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004406 // Check if there is anything to display
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004407 if (!qf_stack_empty(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004409 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4410 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004411
4412 *dirname = NUL;
4413
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004414 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004415 if (old_last == NULL)
4416 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004417 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004418 lnum = 0;
4419 }
4420 else
4421 {
4422 qfp = old_last->qf_next;
4423 lnum = buf->b_ml.ml_line_count;
4424 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004425 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004427 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004429
Bram Moolenaar864293a2016-06-02 13:40:04 +02004430 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004432 if (qfp == NULL)
4433 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004435
4436 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004437 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004438 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004441 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 check_lnums(TRUE);
4443
Bram Moolenaar864293a2016-06-02 13:40:04 +02004444 if (old_last == NULL)
4445 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004446 // Set the 'filetype' to "qf" each time after filling the buffer.
4447 // This resembles reading a file into a buffer, it's more logical when
4448 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004449 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004450 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4451 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004453 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004454 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004456 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004458 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004459 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004460
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004461 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004462 redraw_curbuf_later(NOT_VALID);
4463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004465 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 KeyTyped = old_KeyTyped;
4467}
4468
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004469/*
4470 * For every change made to the quickfix list, update the changed tick.
4471 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004472 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004473qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004474{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004475 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004476}
4477
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004479 * Return the quickfix/location list number with the given identifier.
4480 * Returns -1 if list is not found.
4481 */
4482 static int
4483qf_id2nr(qf_info_T *qi, int_u qfid)
4484{
4485 int qf_idx;
4486
4487 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4488 if (qi->qf_lists[qf_idx].qf_id == qfid)
4489 return qf_idx;
4490 return INVALID_QFIDX;
4491}
4492
4493/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004494 * If the current list is not "save_qfid" and we can find the list with that ID
4495 * then make it the current list.
4496 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004497 * Returns OK if successfully restored the list. Returns FAIL if the list with
4498 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004499 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004500 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004501qf_restore_list(qf_info_T *qi, int_u save_qfid)
4502{
4503 int curlist;
4504
4505 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4506 {
4507 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004508 if (curlist < 0)
4509 // list is not present
4510 return FAIL;
4511 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004512 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004513 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004514}
4515
4516/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004517 * Jump to the first entry if there is one.
4518 */
4519 static void
4520qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4521{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004522 if (qf_restore_list(qi, save_qfid) == FAIL)
4523 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004524
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004525 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004526 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004527 qf_jump(qi, 0, 0, forceit);
4528}
4529
4530/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004531 * Return TRUE when using ":vimgrep" for ":grep".
4532 */
4533 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004534grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004535{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004536 return ((cmdidx == CMD_grep
4537 || cmdidx == CMD_lgrep
4538 || cmdidx == CMD_grepadd
4539 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004540 && STRCMP("internal",
4541 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4542}
4543
4544/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004545 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004547 static char_u *
4548make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004550 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004551 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004552 case CMD_make: return (char_u *)"make";
4553 case CMD_lmake: return (char_u *)"lmake";
4554 case CMD_grep: return (char_u *)"grep";
4555 case CMD_lgrep: return (char_u *)"lgrep";
4556 case CMD_grepadd: return (char_u *)"grepadd";
4557 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4558 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560}
4561
4562/*
4563 * Return the name for the errorfile, in allocated memory.
4564 * Find a new unique name when 'makeef' contains "##".
4565 * Returns NULL for error.
4566 */
4567 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004568get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569{
4570 char_u *p;
4571 char_u *name;
4572 static int start = -1;
4573 static int off = 0;
4574#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004575 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576#endif
4577
4578 if (*p_mef == NUL)
4579 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004580 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (name == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004582 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 return name;
4584 }
4585
4586 for (p = p_mef; *p; ++p)
4587 if (p[0] == '#' && p[1] == '#')
4588 break;
4589
4590 if (*p == NUL)
4591 return vim_strsave(p_mef);
4592
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004593 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 for (;;)
4595 {
4596 if (start == -1)
4597 start = mch_get_pid();
4598 else
4599 off += 19;
4600
4601 name = alloc((unsigned)STRLEN(p_mef) + 30);
4602 if (name == NULL)
4603 break;
4604 STRCPY(name, p_mef);
4605 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4606 STRCAT(name, p + 2);
4607 if (mch_getperm(name) < 0
4608#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004609 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 && mch_lstat((char *)name, &sb) < 0
4611#endif
4612 )
4613 break;
4614 vim_free(name);
4615 }
4616 return name;
4617}
4618
4619/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004620 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4621 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4622 */
4623 static char_u *
4624make_get_fullcmd(char_u *makecmd, char_u *fname)
4625{
4626 char_u *cmd;
4627 unsigned len;
4628
4629 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4630 if (*p_sp != NUL)
4631 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4632 cmd = alloc(len);
4633 if (cmd == NULL)
4634 return NULL;
4635 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4636 (char *)p_shq);
4637
4638 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4639 if (*p_sp != NUL)
4640 append_redir(cmd, len, p_sp, fname);
4641
4642 // Display the fully formed command. Output a newline if there's something
4643 // else than the :make command that was typed (in which case the cursor is
4644 // in column 0).
4645 if (msg_col == 0)
4646 msg_didout = FALSE;
4647 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004648 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004649 msg_outtrans(cmd); // show what we are doing
4650
4651 return cmd;
4652}
4653
4654/*
4655 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4656 */
4657 void
4658ex_make(exarg_T *eap)
4659{
4660 char_u *fname;
4661 char_u *cmd;
4662 char_u *enc = NULL;
4663 win_T *wp = NULL;
4664 qf_info_T *qi = &ql_info;
4665 int res;
4666 char_u *au_name = NULL;
4667 int_u save_qfid;
4668
4669 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4670 if (grep_internal(eap->cmdidx))
4671 {
4672 ex_vimgrep(eap);
4673 return;
4674 }
4675
4676 au_name = make_get_auname(eap->cmdidx);
4677 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4678 curbuf->b_fname, TRUE, curbuf))
4679 {
4680#ifdef FEAT_EVAL
4681 if (aborting())
4682 return;
4683#endif
4684 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004685 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004686
4687 if (is_loclist_cmd(eap->cmdidx))
4688 wp = curwin;
4689
4690 autowrite_all();
4691 fname = get_mef_name();
4692 if (fname == NULL)
4693 return;
4694 mch_remove(fname); // in case it's not unique
4695
4696 cmd = make_get_fullcmd(eap->arg, fname);
4697 if (cmd == NULL)
4698 return;
4699
4700 // let the shell know if we are redirecting output or not
4701 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4702
4703#ifdef AMIGA
4704 out_flush();
4705 // read window status report and redraw before message
4706 (void)char_avail();
4707#endif
4708
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004709 incr_quickfix_busy();
4710
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004711 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4712 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4713 (eap->cmdidx != CMD_grepadd
4714 && eap->cmdidx != CMD_lgrepadd),
4715 qf_cmdtitle(*eap->cmdlinep), enc);
4716 if (wp != NULL)
4717 {
4718 qi = GET_LOC_LIST(wp);
4719 if (qi == NULL)
4720 goto cleanup;
4721 }
4722 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004723 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004724
4725 // Remember the current quickfix list identifier, so that we can
4726 // check for autocommands changing the current quickfix list.
4727 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4728 if (au_name != NULL)
4729 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4730 curbuf->b_fname, TRUE, curbuf);
4731 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4732 // display the first error
4733 qf_jump_first(qi, save_qfid, FALSE);
4734
4735cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004736 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004737 mch_remove(fname);
4738 vim_free(fname);
4739 vim_free(cmd);
4740}
4741
4742/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004743 * Returns the number of valid entries in the current quickfix/location list.
4744 */
4745 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004746qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004747{
4748 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004749 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004750 qfline_T *qfp;
4751 int i, sz = 0;
4752 int prev_fnum = 0;
4753
Bram Moolenaar39665952018-08-15 20:59:48 +02004754 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004755 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004756 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004757 qi = GET_LOC_LIST(curwin);
4758 if (qi == NULL)
4759 return 0;
4760 }
4761
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004762 qfl = &qi->qf_lists[qi->qf_curlist];
4763 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004764 ++i, qfp = qfp->qf_next)
4765 {
4766 if (qfp->qf_valid)
4767 {
4768 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004769 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004770 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4771 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004772 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004773 sz++;
4774 prev_fnum = qfp->qf_fnum;
4775 }
4776 }
4777 }
4778
4779 return sz;
4780}
4781
4782/*
4783 * Returns the current index of the quickfix/location list.
4784 * Returns 0 if there is an error.
4785 */
4786 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004787qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004788{
4789 qf_info_T *qi = &ql_info;
4790
Bram Moolenaar39665952018-08-15 20:59:48 +02004791 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004792 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004793 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004794 qi = GET_LOC_LIST(curwin);
4795 if (qi == NULL)
4796 return 0;
4797 }
4798
4799 return qi->qf_lists[qi->qf_curlist].qf_index;
4800}
4801
4802/*
4803 * Returns the current index in the quickfix/location list (counting only valid
4804 * entries). If no valid entries are in the list, then returns 1.
4805 */
4806 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004807qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004808{
4809 qf_info_T *qi = &ql_info;
4810 qf_list_T *qfl;
4811 qfline_T *qfp;
4812 int i, eidx = 0;
4813 int prev_fnum = 0;
4814
Bram Moolenaar39665952018-08-15 20:59:48 +02004815 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004816 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004817 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004818 qi = GET_LOC_LIST(curwin);
4819 if (qi == NULL)
4820 return 1;
4821 }
4822
4823 qfl = &qi->qf_lists[qi->qf_curlist];
4824 qfp = qfl->qf_start;
4825
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004826 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004827 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4828 return 1;
4829
4830 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4831 {
4832 if (qfp->qf_valid)
4833 {
4834 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4835 {
4836 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4837 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004838 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004839 eidx++;
4840 prev_fnum = qfp->qf_fnum;
4841 }
4842 }
4843 else
4844 eidx++;
4845 }
4846 }
4847
4848 return eidx ? eidx : 1;
4849}
4850
4851/*
4852 * Get the 'n'th valid error entry in the quickfix or location list.
4853 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4854 * For :cdo and :ldo returns the 'n'th valid error entry.
4855 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4856 */
4857 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004858qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004859{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004860 qfline_T *qfp = qfl->qf_start;
4861 int i, eidx;
4862 int prev_fnum = 0;
4863
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004864 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004865 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4866 return 1;
4867
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004868 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004869 i++, qfp = qfp->qf_next)
4870 {
4871 if (qfp->qf_valid)
4872 {
4873 if (fdo)
4874 {
4875 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4876 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004877 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004878 eidx++;
4879 prev_fnum = qfp->qf_fnum;
4880 }
4881 }
4882 else
4883 eidx++;
4884 }
4885
4886 if (eidx == n)
4887 break;
4888 }
4889
4890 if (i <= qfl->qf_count)
4891 return i;
4892 else
4893 return 1;
4894}
4895
4896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004898 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004899 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 */
4901 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004902ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004904 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004905 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004906
Bram Moolenaar39665952018-08-15 20:59:48 +02004907 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004908 {
4909 qi = GET_LOC_LIST(curwin);
4910 if (qi == NULL)
4911 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004912 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004913 return;
4914 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004915 }
4916
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004917 if (eap->addr_count > 0)
4918 errornr = (int)eap->line2;
4919 else
4920 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004921 switch (eap->cmdidx)
4922 {
4923 case CMD_cc: case CMD_ll:
4924 errornr = 0;
4925 break;
4926 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4927 case CMD_lfirst:
4928 errornr = 1;
4929 break;
4930 default:
4931 errornr = 32767;
4932 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004933 }
4934
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004935 // For cdo and ldo commands, jump to the nth valid error.
4936 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02004937 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4938 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004939 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004940 eap->addr_count > 0 ? (int)eap->line1 : 1,
4941 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4942
4943 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944}
4945
4946/*
4947 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004948 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004949 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 */
4951 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004952ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004954 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004955 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004956 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004957
Bram Moolenaar39665952018-08-15 20:59:48 +02004958 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004959 {
4960 qi = GET_LOC_LIST(curwin);
4961 if (qi == NULL)
4962 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004963 emsg(_(e_loclist));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004964 return;
4965 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004966 }
4967
Bram Moolenaar55b69262017-08-13 13:42:01 +02004968 if (eap->addr_count > 0
4969 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
4970 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004971 errornr = (int)eap->line2;
4972 else
4973 errornr = 1;
4974
Bram Moolenaar39665952018-08-15 20:59:48 +02004975 // Depending on the command jump to either next or previous entry/file.
4976 switch (eap->cmdidx)
4977 {
4978 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
4979 dir = FORWARD;
4980 break;
4981 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
4982 case CMD_lNext:
4983 dir = BACKWARD;
4984 break;
4985 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
4986 dir = FORWARD_FILE;
4987 break;
4988 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
4989 dir = BACKWARD_FILE;
4990 break;
4991 default:
4992 dir = FORWARD;
4993 break;
4994 }
4995
4996 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997}
4998
4999/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005000 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005001 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 */
5003 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005004ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005006 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005007 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005008 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005009 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005010 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005011 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005012
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005013 switch (eap->cmdidx)
5014 {
5015 case CMD_cfile: au_name = (char_u *)"cfile"; break;
5016 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
5017 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
5018 case CMD_lfile: au_name = (char_u *)"lfile"; break;
5019 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
5020 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
5021 default: break;
5022 }
5023 if (au_name != NULL)
5024 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005025 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005026#ifdef FEAT_BROWSE
5027 if (cmdmod.browse)
5028 {
5029 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005030 NULL, NULL,
5031 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005032 if (browse_file == NULL)
5033 return;
5034 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5035 vim_free(browse_file);
5036 }
5037 else
5038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005040 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005041
Bram Moolenaar39665952018-08-15 20:59:48 +02005042 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005043 wp = curwin;
5044
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005045 incr_quickfix_busy();
5046
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005047 // This function is used by the :cfile, :cgetfile and :caddfile
5048 // commands.
5049 // :cfile always creates a new quickfix list and jumps to the
5050 // first error.
5051 // :cgetfile creates a new quickfix list but doesn't jump to the
5052 // first error.
5053 // :caddfile adds to an existing quickfix list. If there is no
5054 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005055 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005056 && eap->cmdidx != CMD_laddfile),
5057 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005058 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005059 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005060 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005061 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005062 {
5063 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005064 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005065 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005066 }
5067 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005068 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005069 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005070 if (au_name != NULL)
5071 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005072
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005073 // Jump to the first error for a new list and if autocmds didn't
5074 // free the list.
5075 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5076 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005077 // display the first error
5078 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005079
5080 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005081}
5082
5083/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005084 * Return the vimgrep autocmd name.
5085 */
5086 static char_u *
5087vgr_get_auname(cmdidx_T cmdidx)
5088{
5089 switch (cmdidx)
5090 {
5091 case CMD_vimgrep: return (char_u *)"vimgrep";
5092 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5093 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5094 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5095 case CMD_grep: return (char_u *)"grep";
5096 case CMD_lgrep: return (char_u *)"lgrep";
5097 case CMD_grepadd: return (char_u *)"grepadd";
5098 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5099 default: return NULL;
5100 }
5101}
5102
5103/*
5104 * Initialize the regmatch used by vimgrep for pattern "s".
5105 */
5106 static void
5107vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5108{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005109 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005110 regmatch->regprog = NULL;
5111
5112 if (s == NULL || *s == NUL)
5113 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005114 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005115 if (last_search_pat() == NULL)
5116 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005117 emsg(_(e_noprevre));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005118 return;
5119 }
5120 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5121 }
5122 else
5123 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5124
5125 regmatch->rmm_ic = p_ic;
5126 regmatch->rmm_maxcol = 0;
5127}
5128
5129/*
5130 * Display a file name when vimgrep is running.
5131 */
5132 static void
5133vgr_display_fname(char_u *fname)
5134{
5135 char_u *p;
5136
5137 msg_start();
5138 p = msg_strtrunc(fname, TRUE);
5139 if (p == NULL)
5140 msg_outtrans(fname);
5141 else
5142 {
5143 msg_outtrans(p);
5144 vim_free(p);
5145 }
5146 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005147 msg_didout = FALSE; // overwrite this message
5148 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005149 msg_col = 0;
5150 out_flush();
5151}
5152
5153/*
5154 * Load a dummy buffer to search for a pattern using vimgrep.
5155 */
5156 static buf_T *
5157vgr_load_dummy_buf(
5158 char_u *fname,
5159 char_u *dirname_start,
5160 char_u *dirname_now)
5161{
5162 int save_mls;
5163#if defined(FEAT_SYN_HL)
5164 char_u *save_ei = NULL;
5165#endif
5166 buf_T *buf;
5167
5168#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005169 // Don't do Filetype autocommands to avoid loading syntax and
5170 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005171 save_ei = au_event_disable(",Filetype");
5172#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005173 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005174 save_mls = p_mls;
5175 p_mls = 0;
5176
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005177 // Load file into a buffer, so that 'fileencoding' is detected,
5178 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005179 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5180
5181 p_mls = save_mls;
5182#if defined(FEAT_SYN_HL)
5183 au_event_restore(save_ei);
5184#endif
5185
5186 return buf;
5187}
5188
5189/*
5190 * Check whether a quickfix/location list valid. Autocmds may remove or change
5191 * a quickfix list when vimgrep is running. If the list is not found, create a
5192 * new list.
5193 */
5194 static int
5195vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005196 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005197 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005198 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005199 char_u *title)
5200{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005201 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005202 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005203 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005204 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005205 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005206 // An autocmd has freed the location list.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005207 emsg(_(e_loc_list_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005208 return FALSE;
5209 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005210 else
5211 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005212 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005213 qf_new_list(qi, title);
5214 return TRUE;
5215 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005216 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005217
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005218 if (qf_restore_list(qi, qfid) == FAIL)
5219 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005220
5221 return TRUE;
5222}
5223
5224/*
5225 * Search for a pattern in all the lines in a buffer and add the matching lines
5226 * to a quickfix list.
5227 */
5228 static int
5229vgr_match_buflines(
5230 qf_info_T *qi,
5231 char_u *fname,
5232 buf_T *buf,
5233 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005234 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005235 int duplicate_name,
5236 int flags)
5237{
5238 int found_match = FALSE;
5239 long lnum;
5240 colnr_T col;
5241
Bram Moolenaar1c299432018-10-28 14:36:09 +01005242 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005243 {
5244 col = 0;
5245 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5246 col, NULL, NULL) > 0)
5247 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005248 // Pass the buffer number so that it gets used even for a
5249 // dummy buffer, unless duplicate_name is set, then the
5250 // buffer will be wiped out below.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005251 if (qf_add_entry(qi,
5252 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005253 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005254 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005255 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005256 duplicate_name ? 0 : buf->b_fnum,
5257 ml_get_buf(buf,
5258 regmatch->startpos[0].lnum + lnum, FALSE),
5259 regmatch->startpos[0].lnum + lnum,
5260 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005261 FALSE, // vis_col
5262 NULL, // search pattern
5263 0, // nr
5264 0, // type
5265 TRUE // valid
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005266 ) == FAIL)
5267 {
5268 got_int = TRUE;
5269 break;
5270 }
5271 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005272 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005273 break;
5274 if ((flags & VGR_GLOBAL) == 0
5275 || regmatch->endpos[0].lnum > 0)
5276 break;
5277 col = regmatch->endpos[0].col
5278 + (col == regmatch->endpos[0].col);
5279 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5280 break;
5281 }
5282 line_breakcheck();
5283 if (got_int)
5284 break;
5285 }
5286
5287 return found_match;
5288}
5289
5290/*
5291 * Jump to the first match and update the directory.
5292 */
5293 static void
5294vgr_jump_to_match(
5295 qf_info_T *qi,
5296 int forceit,
5297 int *redraw_for_dummy,
5298 buf_T *first_match_buf,
5299 char_u *target_dir)
5300{
5301 buf_T *buf;
5302
5303 buf = curbuf;
5304 qf_jump(qi, 0, 0, forceit);
5305 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005306 // If we jumped to another buffer redrawing will already be
5307 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005308 *redraw_for_dummy = FALSE;
5309
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005310 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005311 if (curbuf == first_match_buf && target_dir != NULL)
5312 {
5313 exarg_T ea;
5314
5315 ea.arg = target_dir;
5316 ea.cmdidx = CMD_lcd;
5317 ex_cd(&ea);
5318 }
5319}
5320
5321/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005322 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005323 * ":vimgrepadd {pattern} file(s)"
5324 * ":lvimgrep {pattern} file(s)"
5325 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005326 */
5327 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005328ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005329{
Bram Moolenaar81695252004-12-29 20:58:21 +00005330 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005331 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005332 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005333 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005334 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005335 char_u *s;
5336 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005337 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005338 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005339 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005340 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005341 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005342 buf_T *buf;
5343 int duplicate_name = FALSE;
5344 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005345 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005346 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005347 buf_T *first_match_buf = NULL;
5348 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005349 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005350 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005351 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005352 char_u *dirname_start = NULL;
5353 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005354 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005355 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005356
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005357 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005358 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5359 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005360 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005361#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005362 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005363 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005364#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005365 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005366
Bram Moolenaar39665952018-08-15 20:59:48 +02005367 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005368 {
5369 qi = ll_get_or_alloc_list(curwin);
5370 if (qi == NULL)
5371 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005372 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005373 }
5374
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005375 if (eap->addr_count > 0)
5376 tomatch = eap->line2;
5377 else
5378 tomatch = MAXLNUM;
5379
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005380 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005381 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005382 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005383 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005384 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005385 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005386 emsg(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005387 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005388 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005389
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005390 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005391 if (regmatch.regprog == NULL)
5392 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005393
5394 p = skipwhite(p);
5395 if (*p == NUL)
5396 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005397 emsg(_("E683: File name missing or invalid pattern"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005398 goto theend;
5399 }
5400
Bram Moolenaar55b69262017-08-13 13:42:01 +02005401 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005402 && eap->cmdidx != CMD_vimgrepadd
5403 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005404 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005405 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005406 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005407
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005408 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005409 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005410 goto theend;
5411 if (fcount == 0)
5412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005413 emsg(_(e_nomatch));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005414 goto theend;
5415 }
5416
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005417 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5418 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005419 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005420 {
5421 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005422 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005423 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005424
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005425 // Remember the current directory, because a BufRead autocommand that does
5426 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005427 mch_dirname(dirname_start, MAXPATHL);
5428
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005429 incr_quickfix_busy();
5430
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005431 // Remember the current quickfix list identifier, so that we can check for
5432 // autocommands changing the current quickfix list.
Bram Moolenaar3c097222017-12-21 20:54:49 +01005433 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005434
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005435 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005436 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005437 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005438 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005439 if (time(NULL) > seconds)
5440 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005441 // Display the file name every second or so, show the user we are
5442 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005443 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005444 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005445 }
5446
Bram Moolenaar81695252004-12-29 20:58:21 +00005447 buf = buflist_findname_exp(fnames[fi]);
5448 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5449 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005450 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005451 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005452 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005453 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005454
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005455 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005456 }
5457 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005458 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005459 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005460
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005461 // Check whether the quickfix list is still valid. When loading a
5462 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005463 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005464 {
5465 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005466 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005467 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005468 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005469 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005470
Bram Moolenaar81695252004-12-29 20:58:21 +00005471 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005472 {
5473 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005474 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005475 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005476 else
5477 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005478 // Try for a match in all lines of the buffer.
5479 // For ":1vimgrep" look for first match only.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005480 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005481 &tomatch, duplicate_name, flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005482
Bram Moolenaar81695252004-12-29 20:58:21 +00005483 if (using_dummy)
5484 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005485 if (found_match && first_match_buf == NULL)
5486 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005487 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005488 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005489 // Never keep a dummy buffer if there is another buffer
5490 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005491 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005492 buf = NULL;
5493 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005494 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005495 || buf->b_p_bh[0] == 'u' // "unload"
5496 || buf->b_p_bh[0] == 'w' // "wipe"
5497 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005498 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005499 // When no match was found we don't need to remember the
5500 // buffer, wipe it out. If there was a match and it
5501 // wasn't the first one or we won't jump there: only
5502 // unload the buffer.
5503 // Ignore 'hidden' here, because it may lead to having too
5504 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005505 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005506 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005507 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005508 buf = NULL;
5509 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005510 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005511 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005512 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005513 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005514 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005515 buf = NULL;
5516 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005517 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005518
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005519 if (buf != NULL)
5520 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005521 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005522 buf->b_flags &= ~BF_DUMMY;
5523
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005524 // If the buffer is still loaded we need to use the
5525 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005526 if (buf == first_match_buf
5527 && target_dir == NULL
5528 && STRCMP(dirname_start, dirname_now) != 0)
5529 target_dir = vim_strsave(dirname_now);
5530
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005531 // The buffer is still loaded, the Filetype autocommands
5532 // need to be done now, in that buffer. And the modelines
5533 // need to be done (again). But not the window-local
5534 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005535 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005536#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005537 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5538 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005539#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005540 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005541 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005542 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005543 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005544 }
5545 }
5546
5547 FreeWild(fcount, fnames);
5548
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005549 qfl = &qi->qf_lists[qi->qf_curlist];
5550 qfl->qf_nonevalid = FALSE;
5551 qfl->qf_ptr = qfl->qf_start;
5552 qfl->qf_index = 1;
5553 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005554
Bram Moolenaar864293a2016-06-02 13:40:04 +02005555 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005556
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005557 if (au_name != NULL)
5558 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5559 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005560 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5561 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005562 if (!qflist_valid(wp, save_qfid)
5563 || qf_restore_list(qi, save_qfid) == FAIL)
5564 {
5565 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005566 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005567 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005568
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005569 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005570 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005571 {
5572 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005573 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5574 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005575 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005576 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005577 semsg(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005578
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005579 decr_quickfix_busy();
5580
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005581 // If we loaded a dummy buffer into the current window, the autocommands
5582 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005583 if (redraw_for_dummy)
5584 {
5585#ifdef FEAT_FOLDING
5586 foldUpdateAll(curwin);
5587#else
5588 redraw_later(NOT_VALID);
5589#endif
5590 }
5591
Bram Moolenaar86b68352004-12-27 21:59:20 +00005592theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005593 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005594 vim_free(dirname_now);
5595 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005596 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005597 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005598}
5599
5600/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005601 * Restore current working directory to "dirname_start" if they differ, taking
5602 * into account whether it is set locally or globally.
5603 */
5604 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005605restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005606{
5607 char_u *dirname_now = alloc(MAXPATHL);
5608
5609 if (NULL != dirname_now)
5610 {
5611 mch_dirname(dirname_now, MAXPATHL);
5612 if (STRCMP(dirname_start, dirname_now) != 0)
5613 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005614 // If the directory has changed, change it back by building up an
5615 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005616 exarg_T ea;
5617
5618 ea.arg = dirname_start;
5619 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5620 ex_cd(&ea);
5621 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005622 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005623 }
5624}
5625
5626/*
5627 * Load file "fname" into a dummy buffer and return the buffer pointer,
5628 * placing the directory resulting from the buffer load into the
5629 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5630 * prior to calling this function. Restores directory to "dirname_start" prior
5631 * to returning, if autocmds or the 'autochdir' option have changed it.
5632 *
5633 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5634 * or wipe_dummy_buffer() later!
5635 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005636 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005637 */
5638 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005639load_dummy_buffer(
5640 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005641 char_u *dirname_start, // in: old directory
5642 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005643{
5644 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005645 bufref_T newbufref;
5646 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005647 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005648 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005649 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005650
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005651 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005652 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5653 if (newbuf == NULL)
5654 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005655 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005656
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005657 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005658 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5659
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005660 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005661 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005662 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005663 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005664 ++newbuf->b_locked;
5665
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005666 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005667 aucmd_prepbuf(&aco, newbuf);
5668
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005669 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005670 (void)setfname(curbuf, fname, NULL, FALSE);
5671
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005672 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005673 check_need_swap(TRUE);
5674
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005675 // Remove the "dummy" flag, otherwise autocommands may not
5676 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005677 curbuf->b_flags &= ~BF_DUMMY;
5678
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005679 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005680 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005681 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005682 NULL, READ_NEW | READ_DUMMY);
5683 --newbuf->b_locked;
5684 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005685 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005686 && !(curbuf->b_flags & BF_NEW))
5687 {
5688 failed = FALSE;
5689 if (curbuf != newbuf)
5690 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005691 // Bloody autocommands changed the buffer! Can happen when
5692 // using netrw and editing a remote file. Use the current
5693 // buffer instead, delete the dummy one after restoring the
5694 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005695 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005696 newbuf = curbuf;
5697 }
5698 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005699
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005700 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005701 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005702 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5703 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005704
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005705 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5706 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005707 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005708 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005709
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005710 // When autocommands/'autochdir' option changed directory: go back.
5711 // Let the caller know what the resulting dir was first, in case it is
5712 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005713 mch_dirname(resulting_dir, MAXPATHL);
5714 restore_start_dir(dirname_start);
5715
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005716 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005717 return NULL;
5718 if (failed)
5719 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005720 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005721 return NULL;
5722 }
5723 return newbuf;
5724}
5725
5726/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005727 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5728 * directory to "dirname_start" prior to returning, if autocmds or the
5729 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005730 */
5731 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005732wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005733{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005734 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005735 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005736#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005737 cleanup_T cs;
5738
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005739 // Reset the error/interrupt/exception state here so that aborting()
5740 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5741 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005742 enter_cleanup(&cs);
5743#endif
5744
Bram Moolenaar81695252004-12-29 20:58:21 +00005745 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005746
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005747#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005748 // Restore the error/interrupt/exception state if not discarded by a
5749 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005750 leave_cleanup(&cs);
5751#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005752 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005753 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005754 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005755}
5756
5757/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005758 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5759 * directory to "dirname_start" prior to returning, if autocmds or the
5760 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005761 */
5762 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005763unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005764{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005765 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005766 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005767 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005768
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005769 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005770 restore_start_dir(dirname_start);
5771 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005772}
5773
Bram Moolenaar05159a02005-02-26 23:04:13 +00005774#if defined(FEAT_EVAL) || defined(PROTO)
5775/*
5776 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005777 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005778 */
5779 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005780get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005781{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005782 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005783 dict_T *dict;
5784 char_u buf[2];
5785 qfline_T *qfp;
5786 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005787 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005788
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005789 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005790 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005791 qi = &ql_info;
5792 if (wp != NULL)
5793 {
5794 qi = GET_LOC_LIST(wp);
5795 if (qi == NULL)
5796 return FAIL;
5797 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005798 }
5799
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005800 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005801 qf_idx = qi->qf_curlist;
5802
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005803 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005804 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005805
Bram Moolenaard823fa92016-08-12 16:29:27 +02005806 qfp = qi->qf_lists[qf_idx].qf_start;
5807 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005808 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005809 // Handle entries with a non-existing buffer number.
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005810 bufnum = qfp->qf_fnum;
5811 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5812 bufnum = 0;
5813
Bram Moolenaar05159a02005-02-26 23:04:13 +00005814 if ((dict = dict_alloc()) == NULL)
5815 return FAIL;
5816 if (list_append_dict(list, dict) == FAIL)
5817 return FAIL;
5818
5819 buf[0] = qfp->qf_type;
5820 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005821 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5822 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5823 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5824 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5825 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5826 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5827 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5828 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5829 || dict_add_string(dict, "type", buf) == FAIL
5830 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005831 return FAIL;
5832
5833 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005834 if (qfp == NULL)
5835 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005836 }
5837 return OK;
5838}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005839
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005840// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005841enum {
5842 QF_GETLIST_NONE = 0x0,
5843 QF_GETLIST_TITLE = 0x1,
5844 QF_GETLIST_ITEMS = 0x2,
5845 QF_GETLIST_NR = 0x4,
5846 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005847 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005848 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005849 QF_GETLIST_IDX = 0x40,
5850 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005851 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005852 QF_GETLIST_FILEWINID = 0x200,
5853 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005854};
5855
5856/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005857 * Parse text from 'di' and return the quickfix list items.
5858 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005859 */
5860 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005861qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005862{
5863 int status = FAIL;
5864 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005865 char_u *errorformat = p_efm;
5866 dictitem_T *efm_di;
5867 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005868
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005869 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005870 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005871 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005872 // If errorformat is supplied then use it, otherwise use the 'efm'
5873 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005874 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5875 {
5876 if (efm_di->di_tv.v_type != VAR_STRING ||
5877 efm_di->di_tv.vval.v_string == NULL)
5878 return FAIL;
5879 errorformat = efm_di->di_tv.vval.v_string;
5880 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005881
Bram Moolenaar36538222017-09-02 19:51:44 +02005882 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005883 if (l == NULL)
5884 return FAIL;
5885
Bram Moolenaar2d67d302018-11-16 18:46:02 +01005886 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005887 if (qi != NULL)
5888 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005889 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005890 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5891 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005892 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005893 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005894 }
5895 free(qi);
5896 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005897 dict_add_list(retdict, "items", l);
5898 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005899 }
5900
5901 return status;
5902}
5903
5904/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005905 * Return the quickfix/location list window identifier in the current tabpage.
5906 */
5907 static int
5908qf_winid(qf_info_T *qi)
5909{
5910 win_T *win;
5911
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005912 // The quickfix window can be opened even if the quickfix list is not set
5913 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005914 if (qi == NULL)
5915 return 0;
5916 win = qf_find_win(qi);
5917 if (win != NULL)
5918 return win->w_id;
5919 return 0;
5920}
5921
5922/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005923 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005924 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005925 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005926qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005927{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005928 int flags = QF_GETLIST_NONE;
5929
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005930 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005931 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005932 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005933 if (!loclist)
5934 // File window ID is applicable only to location list windows
5935 flags &= ~ QF_GETLIST_FILEWINID;
5936 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005937
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005938 if (dict_find(what, (char_u *)"title", -1) != NULL)
5939 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005940
Bram Moolenaara6d48492017-12-12 22:45:31 +01005941 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5942 flags |= QF_GETLIST_NR;
5943
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005944 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5945 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005946
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005947 if (dict_find(what, (char_u *)"context", -1) != NULL)
5948 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005949
Bram Moolenaara6d48492017-12-12 22:45:31 +01005950 if (dict_find(what, (char_u *)"id", -1) != NULL)
5951 flags |= QF_GETLIST_ID;
5952
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005953 if (dict_find(what, (char_u *)"items", -1) != NULL)
5954 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005955
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005956 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5957 flags |= QF_GETLIST_IDX;
5958
5959 if (dict_find(what, (char_u *)"size", -1) != NULL)
5960 flags |= QF_GETLIST_SIZE;
5961
Bram Moolenaarb254af32017-12-18 19:48:58 +01005962 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5963 flags |= QF_GETLIST_TICK;
5964
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005965 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5966 flags |= QF_GETLIST_FILEWINID;
5967
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005968 return flags;
5969}
Bram Moolenaara6d48492017-12-12 22:45:31 +01005970
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005971/*
5972 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
5973 * If 'nr' and 'id' are not present in 'what' then return the current
5974 * quickfix list index.
5975 * If 'nr' is zero then return the current quickfix list index.
5976 * If 'nr' is '$' then return the last quickfix list index.
5977 * If 'id' is present then return the index of the quickfix list with that id.
5978 * If 'id' is zero then return the quickfix list index specified by 'nr'.
5979 * Return -1, if quickfix list is not present or if the stack is empty.
5980 */
5981 static int
5982qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5983{
5984 int qf_idx;
5985 dictitem_T *di;
5986
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005987 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005988 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5989 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005990 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005991 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005992 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005993 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005994 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01005995 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005996 qf_idx = di->di_tv.vval.v_number - 1;
5997 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02005998 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01005999 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006000 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006001 else if (di->di_tv.v_type == VAR_STRING
6002 && di->di_tv.vval.v_string != NULL
6003 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006004 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006005 qf_idx = qi->qf_listcount - 1;
6006 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006007 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006008 }
6009
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006010 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6011 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006012 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006013 if (di->di_tv.v_type == VAR_NUMBER)
6014 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006015 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006016 if (di->di_tv.vval.v_number != 0)
6017 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6018 }
6019 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006020 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006021 }
6022
6023 return qf_idx;
6024}
6025
6026/*
6027 * Return default values for quickfix list properties in retdict.
6028 */
6029 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006030qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006031{
6032 int status = OK;
6033
6034 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006035 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006036 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6037 {
6038 list_T *l = list_alloc();
6039 if (l != NULL)
6040 status = dict_add_list(retdict, "items", l);
6041 else
6042 status = FAIL;
6043 }
6044 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006045 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006046 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006047 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006048 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006049 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006050 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006051 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006052 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006053 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006054 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006055 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006056 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006057 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006058 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006059 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006060
6061 return status;
6062}
6063
6064/*
6065 * Return the quickfix list title as 'title' in retdict
6066 */
6067 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006068qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006069{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006070 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006071}
6072
6073/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006074 * Returns the identifier of the window used to display files from a location
6075 * list. If there is no associated window, then returns 0. Useful only when
6076 * called from a location list window.
6077 */
6078 static int
6079qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6080{
6081 int winid = 0;
6082
6083 if (wp != NULL && IS_LL_WINDOW(wp))
6084 {
6085 win_T *ll_wp = qf_find_win_with_loclist(qi);
6086 if (ll_wp != NULL)
6087 winid = ll_wp->w_id;
6088 }
6089
6090 return dict_add_number(retdict, "filewinid", winid);
6091}
6092
6093/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006094 * Return the quickfix list items/entries as 'items' in retdict
6095 */
6096 static int
6097qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6098{
6099 int status = OK;
6100 list_T *l = list_alloc();
6101 if (l != NULL)
6102 {
6103 (void)get_errorlist(qi, NULL, qf_idx, l);
6104 dict_add_list(retdict, "items", l);
6105 }
6106 else
6107 status = FAIL;
6108
6109 return status;
6110}
6111
6112/*
6113 * Return the quickfix list context (if any) as 'context' in retdict.
6114 */
6115 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006116qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006117{
6118 int status;
6119 dictitem_T *di;
6120
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006121 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006122 {
6123 di = dictitem_alloc((char_u *)"context");
6124 if (di != NULL)
6125 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006126 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006127 status = dict_add(retdict, di);
6128 if (status == FAIL)
6129 dictitem_free(di);
6130 }
6131 else
6132 status = FAIL;
6133 }
6134 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006135 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006136
6137 return status;
6138}
6139
6140/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006141 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006142 */
6143 static int
6144qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6145{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006146 int curidx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006147 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006148 // For empty lists, current index is set to 0
6149 curidx = 0;
6150 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006151}
6152
6153/*
6154 * Return quickfix/location list details (title) as a
6155 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6156 * then current list is used. Otherwise the specified list is used.
6157 */
6158 int
6159qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6160{
6161 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006162 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006163 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006164 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006165 dictitem_T *di;
6166 int flags = QF_GETLIST_NONE;
6167
6168 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6169 return qf_get_list_from_lines(what, di, retdict);
6170
6171 if (wp != NULL)
6172 qi = GET_LOC_LIST(wp);
6173
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006174 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006175
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006176 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006177 qf_idx = qf_getprop_qfidx(qi, what);
6178
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006179 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006180 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006181 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006182
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006183 qfl = &qi->qf_lists[qf_idx];
6184
Bram Moolenaard823fa92016-08-12 16:29:27 +02006185 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006186 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006187 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006188 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006189 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006190 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006191 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006192 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006193 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006194 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006195 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006196 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006197 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006198 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006199 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006200 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006201 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006202 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006203 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6204 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006205
Bram Moolenaard823fa92016-08-12 16:29:27 +02006206 return status;
6207}
6208
6209/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006210 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01006211 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
6212 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006213 */
6214 static int
6215qf_add_entry_from_dict(
6216 qf_info_T *qi,
6217 int qf_idx,
6218 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01006219 int first_entry,
6220 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006221{
6222 static int did_bufnr_emsg;
6223 char_u *filename, *module, *pattern, *text, *type;
6224 int bufnum, valid, status, col, vcol, nr;
6225 long lnum;
6226
6227 if (first_entry)
6228 did_bufnr_emsg = FALSE;
6229
Bram Moolenaar8f667172018-12-14 15:38:31 +01006230 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6231 module = dict_get_string(d, (char_u *)"module", TRUE);
6232 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6233 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6234 col = (int)dict_get_number(d, (char_u *)"col");
6235 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6236 nr = (int)dict_get_number(d, (char_u *)"nr");
6237 type = dict_get_string(d, (char_u *)"type", TRUE);
6238 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6239 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006240 if (text == NULL)
6241 text = vim_strsave((char_u *)"");
6242
6243 valid = TRUE;
6244 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6245 valid = FALSE;
6246
6247 // Mark entries with non-existing buffer number as not valid. Give the
6248 // error message only once.
6249 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6250 {
6251 if (!did_bufnr_emsg)
6252 {
6253 did_bufnr_emsg = TRUE;
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006254 semsg(_("E92: Buffer %d not found"), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006255 }
6256 valid = FALSE;
6257 bufnum = 0;
6258 }
6259
6260 // If the 'valid' field is present it overrules the detected value.
6261 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006262 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006263
6264 status = qf_add_entry(qi,
6265 qf_idx,
6266 NULL, // dir
6267 filename,
6268 module,
6269 bufnum,
6270 text,
6271 lnum,
6272 col,
6273 vcol, // vis_col
6274 pattern, // search pattern
6275 nr,
6276 type == NULL ? NUL : *type,
6277 valid);
6278
6279 vim_free(filename);
6280 vim_free(module);
6281 vim_free(pattern);
6282 vim_free(text);
6283 vim_free(type);
6284
Bram Moolenaar9752c722018-12-22 16:49:34 +01006285 if (valid)
6286 *valid_entry = TRUE;
6287
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006288 return status;
6289}
6290
6291/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006292 * Add list of entries to quickfix/location list. Each list entry is
6293 * a dictionary with item information.
6294 */
6295 static int
6296qf_add_entries(
6297 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006298 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006299 list_T *list,
6300 char_u *title,
6301 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006302{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006303 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006304 listitem_T *li;
6305 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006306 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006307 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006308 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006309
Bram Moolenaara3921f42017-06-04 15:30:34 +02006310 if (action == ' ' || qf_idx == qi->qf_listcount)
6311 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006312 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006313 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006314 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006315 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006316 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006317 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006318 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006319 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006320 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006321 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006322 qf_free_items(qfl);
6323 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006324 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006325
6326 for (li = list->lv_first; li != NULL; li = li->li_next)
6327 {
6328 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006329 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006330
6331 d = li->li_tv.vval.v_dict;
6332 if (d == NULL)
6333 continue;
6334
Bram Moolenaar9752c722018-12-22 16:49:34 +01006335 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first,
6336 &valid_entry);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006337 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006338 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006339 }
6340
Bram Moolenaar9752c722018-12-22 16:49:34 +01006341 // Check if any valid error entries are added to the list.
6342 if (valid_entry)
6343 qfl->qf_nonevalid = FALSE;
6344 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006345 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006346 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006347
6348 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006349 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006350 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01006351
6352 // Update the current error index if not appending to the list or if the
6353 // list was empty before and it is not empty now.
6354 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qi, qf_idx))
6355 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006356
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006357 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006358 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006359
6360 return retval;
6361}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006362
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006363/*
6364 * Get the quickfix list index from 'nr' or 'id'
6365 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006366 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006367qf_setprop_get_qfidx(
6368 qf_info_T *qi,
6369 dict_T *what,
6370 int action,
6371 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006372{
6373 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006374 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006375
Bram Moolenaard823fa92016-08-12 16:29:27 +02006376 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6377 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006378 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006379 if (di->di_tv.v_type == VAR_NUMBER)
6380 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006381 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006382 if (di->di_tv.vval.v_number != 0)
6383 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006384
Bram Moolenaar55b69262017-08-13 13:42:01 +02006385 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6386 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006387 // When creating a new list, accept qf_idx pointing to the next
6388 // non-available list and add the new list at the end of the
6389 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006390 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006391 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006392 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006393 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006394 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006395 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006396 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006397 }
6398 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006399 && di->di_tv.vval.v_string != NULL
6400 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006401 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006402 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006403 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006404 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006405 qf_idx = 0;
6406 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006407 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006408 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006409 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006410 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006411 }
6412
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006413 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006414 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006415 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006416 if (di->di_tv.v_type != VAR_NUMBER)
6417 return INVALID_QFIDX;
6418
6419 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006420 }
6421
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006422 return qf_idx;
6423}
6424
6425/*
6426 * Set the quickfix list title.
6427 */
6428 static int
6429qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6430{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006431 qf_list_T *qfl = &qi->qf_lists[qf_idx];
6432
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006433 if (di->di_tv.v_type != VAR_STRING)
6434 return FAIL;
6435
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006436 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01006437 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006438 if (qf_idx == qi->qf_curlist)
6439 qf_update_win_titlevar(qi);
6440
6441 return OK;
6442}
6443
6444/*
6445 * Set quickfix list items/entries.
6446 */
6447 static int
6448qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6449{
6450 int retval = FAIL;
6451 char_u *title_save;
6452
6453 if (di->di_tv.v_type != VAR_LIST)
6454 return FAIL;
6455
6456 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6457 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6458 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006459 vim_free(title_save);
6460
6461 return retval;
6462}
6463
6464/*
6465 * Set quickfix list items/entries from a list of lines.
6466 */
6467 static int
6468qf_setprop_items_from_lines(
6469 qf_info_T *qi,
6470 int qf_idx,
6471 dict_T *what,
6472 dictitem_T *di,
6473 int action)
6474{
6475 char_u *errorformat = p_efm;
6476 dictitem_T *efm_di;
6477 int retval = FAIL;
6478
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006479 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006480 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6481 {
6482 if (efm_di->di_tv.v_type != VAR_STRING ||
6483 efm_di->di_tv.vval.v_string == NULL)
6484 return FAIL;
6485 errorformat = efm_di->di_tv.vval.v_string;
6486 }
6487
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006488 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006489 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6490 return FAIL;
6491
6492 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006493 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006494 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6495 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6496 retval = OK;
6497
6498 return retval;
6499}
6500
6501/*
6502 * Set quickfix list context.
6503 */
6504 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006505qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006506{
6507 typval_T *ctx;
6508
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006509 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006510 ctx = alloc_tv();
6511 if (ctx != NULL)
6512 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006513 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006514
6515 return OK;
6516}
6517
6518/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006519 * Set the current index in the specified quickfix list
6520 */
6521 static int
6522qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
6523{
6524 int denote = FALSE;
6525 int newidx;
6526 int old_qfidx;
6527 qfline_T *qf_ptr;
6528
6529 // If the specified index is '$', then use the last entry
6530 if (di->di_tv.v_type == VAR_STRING
6531 && di->di_tv.vval.v_string != NULL
6532 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
6533 newidx = qfl->qf_count;
6534 else
6535 {
6536 // Otherwise use the specified index
6537 newidx = tv_get_number_chk(&di->di_tv, &denote);
6538 if (denote)
6539 return FAIL;
6540 }
6541
6542 if (newidx < 1) // sanity check
6543 return FAIL;
6544 if (newidx > qfl->qf_count)
6545 newidx = qfl->qf_count;
6546
6547 old_qfidx = qfl->qf_index;
6548 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
6549 if (qf_ptr == NULL)
6550 return FAIL;
6551 qfl->qf_ptr = qf_ptr;
6552 qfl->qf_index = newidx;
6553
6554 // If the current list is modified and it is displayed in the quickfix
6555 // window, then Update it.
6556 if (qi->qf_lists[qi->qf_curlist].qf_id == qfl->qf_id)
6557 qf_win_pos_update(qi, old_qfidx);
6558
6559 return OK;
6560}
6561
6562/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006563 * Set quickfix/location list properties (title, items, context).
6564 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006565 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006566 */
6567 static int
6568qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6569{
6570 dictitem_T *di;
6571 int retval = FAIL;
6572 int qf_idx;
6573 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006574 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006575
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006576 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006577 newlist = TRUE;
6578
6579 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006580 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006581 return FAIL;
6582
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006583 if (newlist)
6584 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006585 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006586 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006587 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006588 }
6589
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006590 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaard823fa92016-08-12 16:29:27 +02006591 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006592 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006593 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006594 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006595 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006596 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006597 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006598 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01006599 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
6600 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006601
Bram Moolenaarb254af32017-12-18 19:48:58 +01006602 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006603 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006604
Bram Moolenaard823fa92016-08-12 16:29:27 +02006605 return retval;
6606}
6607
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006608/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006609 * Find the non-location list window with the specified location list stack in
6610 * the current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006611 */
6612 static win_T *
6613find_win_with_ll(qf_info_T *qi)
6614{
6615 win_T *wp = NULL;
6616
6617 FOR_ALL_WINDOWS(wp)
6618 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6619 return wp;
6620
6621 return NULL;
6622}
6623
6624/*
6625 * Free the entire quickfix/location list stack.
6626 * If the quickfix/location list window is open, then clear it.
6627 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006628 static void
6629qf_free_stack(win_T *wp, qf_info_T *qi)
6630{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006631 win_T *qfwin = qf_find_win(qi);
6632 win_T *llwin = NULL;
6633 win_T *orig_wp = wp;
6634
6635 if (qfwin != NULL)
6636 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006637 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006638 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006639 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006640 qf_update_buffer(qi, NULL);
6641 }
6642
6643 if (wp != NULL && IS_LL_WINDOW(wp))
6644 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006645 // If in the location list window, then use the non-location list
6646 // window with this location list (if present)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006647 llwin = find_win_with_ll(qi);
6648 if (llwin != NULL)
6649 wp = llwin;
6650 }
6651
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006652 qf_free_all(wp);
6653 if (wp == NULL)
6654 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006655 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006656 qi->qf_curlist = 0;
6657 qi->qf_listcount = 0;
6658 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006659 else if (IS_LL_WINDOW(orig_wp))
6660 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006661 // If the location list window is open, then create a new empty
6662 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006663 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006664
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006665 // first free the list reference in the location list window
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006666 ll_free_all(&orig_wp->w_llist_ref);
6667
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006668 orig_wp->w_llist_ref = new_ll;
6669 if (llwin != NULL)
6670 {
6671 llwin->w_llist = new_ll;
6672 new_ll->qf_refcount++;
6673 }
6674 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006675}
6676
Bram Moolenaard823fa92016-08-12 16:29:27 +02006677/*
6678 * Populate the quickfix list with the items supplied in the list
6679 * of dictionaries. "title" will be copied to w:quickfix_title.
6680 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6681 */
6682 int
6683set_errorlist(
6684 win_T *wp,
6685 list_T *list,
6686 int action,
6687 char_u *title,
6688 dict_T *what)
6689{
6690 qf_info_T *qi = &ql_info;
6691 int retval = OK;
6692
6693 if (wp != NULL)
6694 {
6695 qi = ll_get_or_alloc_list(wp);
6696 if (qi == NULL)
6697 return FAIL;
6698 }
6699
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006700 if (action == 'f')
6701 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006702 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006703 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006704 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006705 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006706
6707 incr_quickfix_busy();
6708
6709 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006710 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006711 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006712 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006713 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006714 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006715 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006716 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006717
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006718 decr_quickfix_busy();
6719
Bram Moolenaard823fa92016-08-12 16:29:27 +02006720 return retval;
6721}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006722
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006723/*
6724 * Mark the context as in use for all the lists in a quickfix stack.
6725 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006726 static int
6727mark_quickfix_ctx(qf_info_T *qi, int copyID)
6728{
6729 int i;
6730 int abort = FALSE;
6731 typval_T *ctx;
6732
6733 for (i = 0; i < LISTCOUNT && !abort; ++i)
6734 {
6735 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006736 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6737 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006738 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6739 }
6740
6741 return abort;
6742}
6743
6744/*
6745 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006746 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006747 */
6748 int
6749set_ref_in_quickfix(int copyID)
6750{
6751 int abort = FALSE;
6752 tabpage_T *tp;
6753 win_T *win;
6754
6755 abort = mark_quickfix_ctx(&ql_info, copyID);
6756 if (abort)
6757 return abort;
6758
6759 FOR_ALL_TAB_WINDOWS(tp, win)
6760 {
6761 if (win->w_llist != NULL)
6762 {
6763 abort = mark_quickfix_ctx(win->w_llist, copyID);
6764 if (abort)
6765 return abort;
6766 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006767 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6768 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006769 // In a location list window and none of the other windows is
6770 // referring to this location list. Mark the location list
6771 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006772 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6773 if (abort)
6774 return abort;
6775 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006776 }
6777
6778 return abort;
6779}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006780#endif
6781
Bram Moolenaar81695252004-12-29 20:58:21 +00006782/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006783 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006784 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006785 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006786 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006787 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006788 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006789 */
6790 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006791ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006792{
6793 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006794 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006795 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006796 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006797 int_u save_qfid;
6798 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006799
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006800 switch (eap->cmdidx)
6801 {
6802 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6803 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6804 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6805 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6806 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6807 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6808 default: break;
6809 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006810 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6811 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006812 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006813#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006814 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006815 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006816#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006817 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006818
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006819 // Must come after autocommands.
Bram Moolenaar39665952018-08-15 20:59:48 +02006820 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006821 {
6822 qi = ll_get_or_alloc_list(curwin);
6823 if (qi == NULL)
6824 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006825 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006826 }
6827
Bram Moolenaar86b68352004-12-27 21:59:20 +00006828 if (*eap->arg == NUL)
6829 buf = curbuf;
6830 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6831 buf = buflist_findnr(atoi((char *)eap->arg));
6832 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006833 emsg(_(e_invarg));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006834 else if (buf->b_ml.ml_mfp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006835 emsg(_("E681: Buffer is not loaded"));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006836 else
6837 {
6838 if (eap->addr_count == 0)
6839 {
6840 eap->line1 = 1;
6841 eap->line2 = buf->b_ml.ml_line_count;
6842 }
6843 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6844 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006845 emsg(_(e_invrange));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006846 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006847 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006848 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006849
6850 if (buf->b_sfname)
6851 {
6852 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6853 (char *)qf_title, (char *)buf->b_sfname);
6854 qf_title = IObuff;
6855 }
6856
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006857 incr_quickfix_busy();
6858
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006859 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006860 (eap->cmdidx != CMD_caddbuffer
6861 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006862 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006863 qf_title, NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006864 if (qf_stack_empty(qi))
6865 {
6866 decr_quickfix_busy();
6867 return;
6868 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006869 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006870 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006871
6872 // Remember the current quickfix list identifier, so that we can
6873 // check for autocommands changing the current quickfix list.
6874 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006875 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006876 {
6877 buf_T *curbuf_old = curbuf;
6878
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006879 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6880 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006881 if (curbuf != curbuf_old)
6882 // Autocommands changed buffer, don't jump now, "qi" may
6883 // be invalid.
6884 res = 0;
6885 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006886 // Jump to the first error for a new list and if autocmds didn't
6887 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006888 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006889 eap->cmdidx == CMD_lbuffer)
6890 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006891 // display the first error
6892 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006893
6894 decr_quickfix_busy();
Bram Moolenaar754b5602006-02-09 23:53:20 +00006895 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006896 }
6897}
6898
Bram Moolenaar1e015462005-09-25 22:16:38 +00006899#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006900/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006901 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6902 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006903 */
6904 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006905ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006906{
6907 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006908 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006909 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006910 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006911 int_u save_qfid;
6912 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006913
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006914 switch (eap->cmdidx)
6915 {
6916 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6917 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6918 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6919 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6920 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6921 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6922 default: break;
6923 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006924 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6925 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006926 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006927#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006928 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006929 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006930#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006931 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006932
Bram Moolenaar39665952018-08-15 20:59:48 +02006933 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006934 {
6935 qi = ll_get_or_alloc_list(curwin);
6936 if (qi == NULL)
6937 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006938 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006939 }
6940
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006941 // Evaluate the expression. When the result is a string or a list we can
6942 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006943 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006944 if (tv != NULL)
6945 {
6946 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6947 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6948 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006949 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006950 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006951 (eap->cmdidx != CMD_caddexpr
6952 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006953 (linenr_T)0, (linenr_T)0,
6954 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006955 if (qf_stack_empty(qi))
6956 {
6957 decr_quickfix_busy();
6958 goto cleanup;
6959 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006960 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006961 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006962
6963 // Remember the current quickfix list identifier, so that we can
6964 // check for autocommands changing the current quickfix list.
6965 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006966 if (au_name != NULL)
6967 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6968 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006969
6970 // Jump to the first error for a new list and if autocmds didn't
6971 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006972 if (res > 0 && (eap->cmdidx == CMD_cexpr
6973 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006974 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006975 // display the first error
6976 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006977 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006978 }
6979 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006980 emsg(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006981cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00006982 free_tv(tv);
6983 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006984}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006985#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006986
6987/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006988 * Get the location list for ":lhelpgrep"
6989 */
6990 static qf_info_T *
6991hgr_get_ll(int *new_ll)
6992{
6993 win_T *wp;
6994 qf_info_T *qi;
6995
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006996 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006997 if (bt_help(curwin->w_buffer))
6998 wp = curwin;
6999 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007000 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007001 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007002
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007003 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007004 qi = NULL;
7005 else
7006 qi = wp->w_llist;
7007
7008 if (qi == NULL)
7009 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007010 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007011 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007012 return NULL;
7013 *new_ll = TRUE;
7014 }
7015
7016 return qi;
7017}
7018
7019/*
7020 * Search for a pattern in a help file.
7021 */
7022 static void
7023hgr_search_file(
7024 qf_info_T *qi,
7025 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007026 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007027 regmatch_T *p_regmatch)
7028{
7029 FILE *fd;
7030 long lnum;
7031
7032 fd = mch_fopen((char *)fname, "r");
7033 if (fd == NULL)
7034 return;
7035
7036 lnum = 1;
7037 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7038 {
7039 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007040
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007041 // Convert a line if 'encoding' is not utf-8 and
7042 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007043 if (p_vc->vc_type != CONV_NONE
7044 && has_non_ascii(IObuff))
7045 {
7046 line = string_convert(p_vc, IObuff, NULL);
7047 if (line == NULL)
7048 line = IObuff;
7049 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007050
7051 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7052 {
7053 int l = (int)STRLEN(line);
7054
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007055 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007056 while (l > 0 && line[l - 1] <= ' ')
7057 line[--l] = NUL;
7058
7059 if (qf_add_entry(qi,
7060 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007061 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007062 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007063 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007064 0,
7065 line,
7066 lnum,
7067 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007068 + 1, // col
7069 FALSE, // vis_col
7070 NULL, // search pattern
7071 0, // nr
7072 1, // type
7073 TRUE // valid
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007074 ) == FAIL)
7075 {
7076 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007077 if (line != IObuff)
7078 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007079 break;
7080 }
7081 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007082 if (line != IObuff)
7083 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007084 ++lnum;
7085 line_breakcheck();
7086 }
7087 fclose(fd);
7088}
7089
7090/*
7091 * Search for a pattern in all the help files in the doc directory under
7092 * the given directory.
7093 */
7094 static void
7095hgr_search_files_in_dir(
7096 qf_info_T *qi,
7097 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01007098 regmatch_T *p_regmatch,
7099 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007100#ifdef FEAT_MULTI_LANG
7101 , char_u *lang
7102#endif
7103 )
7104{
7105 int fcount;
7106 char_u **fnames;
7107 int fi;
7108
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007109 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007110 add_pathsep(dirname);
7111 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7112 if (gen_expand_wildcards(1, &dirname, &fcount,
7113 &fnames, EW_FILE|EW_SILENT) == OK
7114 && fcount > 0)
7115 {
7116 for (fi = 0; fi < fcount && !got_int; ++fi)
7117 {
7118#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007119 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007120 if (lang != NULL
7121 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007122 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007123 && !(STRNICMP(lang, "en", 2) == 0
7124 && STRNICMP("txt", fnames[fi]
7125 + STRLEN(fnames[fi]) - 3, 3) == 0))
7126 continue;
7127#endif
7128
Bram Moolenaara12a1612019-01-24 16:39:02 +01007129 hgr_search_file(qi, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007130 }
7131 FreeWild(fcount, fnames);
7132 }
7133}
7134
7135/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007136 * Search for a pattern in all the help files in the 'runtimepath'
7137 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007138 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007139 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007140 */
7141 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007142hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007143{
7144 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007145
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007146 vimconv_T vc;
7147
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007148 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7149 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007150 vc.vc_type = CONV_NONE;
7151 if (!enc_utf8)
7152 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007153
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007154 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007155 p = p_rtp;
7156 while (*p != NUL && !got_int)
7157 {
7158 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7159
Bram Moolenaara12a1612019-01-24 16:39:02 +01007160 hgr_search_files_in_dir(qi, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007161#ifdef FEAT_MULTI_LANG
7162 , lang
7163#endif
7164 );
7165 }
7166
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007167 if (vc.vc_type != CONV_NONE)
7168 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007169}
7170
7171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 * ":helpgrep {pattern}"
7173 */
7174 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007175ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176{
7177 regmatch_T regmatch;
7178 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007179 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007180 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007181 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007182 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
Bram Moolenaar73633f82012-01-20 13:39:07 +01007184 switch (eap->cmdidx)
7185 {
7186 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7187 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7188 default: break;
7189 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007190 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7191 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007192 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007193#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007194 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007195 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007196#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007197 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007198
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007199 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01007200 save_cpo = p_cpo;
7201 p_cpo = empty_option;
7202
Bram Moolenaar39665952018-08-15 20:59:48 +02007203 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007204 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007205 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007206 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007207 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007208 }
7209
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007210 incr_quickfix_busy();
7211
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007212#ifdef FEAT_MULTI_LANG
7213 // Check for a specified language
7214 lang = check_help_lang(eap->arg);
7215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7217 regmatch.rm_ic = FALSE;
7218 if (regmatch.regprog != NULL)
7219 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007220 qf_list_T *qfl;
7221
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007222 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007223 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007225 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007226
Bram Moolenaar473de612013-06-08 18:19:48 +02007227 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007229 qfl = &qi->qf_lists[qi->qf_curlist];
7230 qfl->qf_nonevalid = FALSE;
7231 qfl->qf_ptr = qfl->qf_start;
7232 qfl->qf_index = 1;
7233 qf_list_changed(qfl);
7234 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 }
7236
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007237 if (p_cpo == empty_option)
7238 p_cpo = save_cpo;
7239 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007240 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007241 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
Bram Moolenaar73633f82012-01-20 13:39:07 +01007243 if (au_name != NULL)
7244 {
7245 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7246 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007247 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007248 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007249 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007250 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007251 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007252 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007253 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007254
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007255 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007256 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007257 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007258 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007259 semsg(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007260
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007261 decr_quickfix_busy();
7262
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007263 if (eap->cmdidx == CMD_lhelpgrep)
7264 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007265 // If the help window is not opened or if it already points to the
7266 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007267 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007268 {
7269 if (new_qi)
7270 ll_free_all(&qi);
7271 }
7272 else if (curwin->w_llist == NULL)
7273 curwin->w_llist = qi;
7274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275}
7276
7277#endif /* FEAT_QUICKFIX */