blob: 5291fb1115989b30c64e563b3c55fcde75d5c23b [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,
242 int round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200243 char_u *errmsg)
244{
245 char_u *srcptr;
246
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200247 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200248 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200249 // Each errorformat pattern can occur only once
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200250 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200251 _("E372: Too many %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200252 EMSG(errmsg);
253 return NULL;
254 }
255 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200256 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200257 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200258 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200259 {
260 sprintf((char *)errmsg,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200261 _("E373: Unexpected %%%c in format string"), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200262 EMSG(errmsg);
263 return NULL;
264 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200265 efminfo->addr[idx] = (char_u)++round;
266 *regpat++ = '\\';
267 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200268#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200269 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200270 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200271 // Also match "c:" in the file name, even when
272 // checking for a colon next: "%f:".
273 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200274 STRCPY(regpat, "\\%(\\a:\\)\\=");
275 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200276 }
277#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200278 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200279 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200280 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200281 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200282 // A file name may contain spaces, but this isn't
283 // in "\f". For "%f:%l:%m" there may be a ":" in
284 // the file name. Use ".\{-1,}x" instead (x is
285 // the next character), the requirement that :999:
286 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200287 STRCPY(regpat, ".\\{-1,}");
288 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200289 }
290 else
291 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200292 // File name followed by '\\' or '%': include as
293 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200294 STRCPY(regpat, "\\f\\+");
295 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200296 }
297 }
298 else
299 {
300 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200301 while ((*regpat = *srcptr++) != NUL)
302 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200303 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200304 *regpat++ = '\\';
305 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200306
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200307 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200308}
309
310/*
311 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200312 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200313 */
314 static char_u *
315scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200316 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200317 char_u *efm,
318 int len,
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200319 char_u *regpat,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200320 char_u *errmsg)
321{
322 char_u *efmp = *pefmp;
323
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200324 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200325 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200326 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200327 {
328 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200329 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200330 if (efmp < efm + len)
331 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200332 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200333 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200334 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200335 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200336 if (efmp == efm + len)
337 {
338 EMSG(_("E374: Missing ] in format string"));
339 return NULL;
340 }
341 }
342 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200343 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200344 *regpat++ = *++efmp;
345 *regpat++ = '\\';
346 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200347 }
348 else
349 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200350 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200351 sprintf((char *)errmsg,
352 _("E375: Unsupported %%%c in format string"), *efmp);
353 EMSG(errmsg);
354 return NULL;
355 }
356
357 *pefmp = efmp;
358
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200359 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200360}
361
362/*
363 * Analyze/parse an errorformat prefix.
364 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200365 static char_u *
366efm_analyze_prefix(char_u *efmp, efm_T *efminfo, char_u *errmsg)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200367{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200368 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200369 efminfo->flags = *efmp++;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200370 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200371 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200372 else
373 {
374 sprintf((char *)errmsg,
375 _("E376: Invalid %%%c in format string prefix"), *efmp);
376 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200377 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200378 }
379
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200380 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200381}
382
383/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200384 * Converts a 'errorformat' string part in 'efm' to a regular expression
385 * pattern. The resulting regex pattern is returned in "regpat". Additional
386 * information about the 'erroformat' pattern is returned in "fmt_ptr".
387 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200388 */
389 static int
390efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200391 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200392 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200393 efm_T *fmt_ptr,
394 char_u *regpat,
395 char_u *errmsg)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200396{
397 char_u *ptr;
398 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200399 int round;
400 int idx = 0;
401
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200402 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200403 ptr = regpat;
404 *ptr++ = '^';
405 round = 0;
406 for (efmp = efm; efmp < efm + len; ++efmp)
407 {
408 if (*efmp == '%')
409 {
410 ++efmp;
411 for (idx = 0; idx < FMT_PATTERNS; ++idx)
412 if (fmt_pat[idx].convchar == *efmp)
413 break;
414 if (idx < FMT_PATTERNS)
415 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200416 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200417 errmsg);
418 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200419 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200420 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200421 }
422 else if (*efmp == '*')
423 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200424 ++efmp;
425 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr, errmsg);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200426 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200427 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200428 }
429 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200430 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200431 else if (*efmp == '#')
432 *ptr++ = '*';
433 else if (*efmp == '>')
434 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200435 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200436 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200437 // prefix is allowed only at the beginning of the errorformat
438 // option part
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200439 efmp = efm_analyze_prefix(efmp, fmt_ptr, errmsg);
440 if (efmp == NULL)
441 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200442 }
443 else
444 {
445 sprintf((char *)errmsg,
446 _("E377: Invalid %%%c in format string"), *efmp);
447 EMSG(errmsg);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200448 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200449 }
450 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200451 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200452 {
453 if (*efmp == '\\' && efmp + 1 < efm + len)
454 ++efmp;
455 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200456 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200457 if (*efmp)
458 *ptr++ = *efmp;
459 }
460 }
461 *ptr++ = '$';
462 *ptr = NUL;
463
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200464 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200465}
466
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200467/*
468 * Free the 'errorformat' information list
469 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200470 static void
471free_efm_list(efm_T **efm_first)
472{
473 efm_T *efm_ptr;
474
475 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
476 {
477 *efm_first = efm_ptr->next;
478 vim_regfree(efm_ptr->prog);
479 vim_free(efm_ptr);
480 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100481 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200482}
483
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200484/*
485 * Compute the size of the buffer used to convert a 'errorformat' pattern into
486 * a regular expression pattern.
487 */
488 static int
489efm_regpat_bufsz(char_u *efm)
490{
491 int sz;
492 int i;
493
494 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
495 for (i = FMT_PATTERNS; i > 0; )
496 sz += (int)STRLEN(fmt_pat[--i].pattern);
497#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200498 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200499#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200500 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200501#endif
502
503 return sz;
504}
505
506/*
507 * Return the length of a 'errorformat' option part (separated by ",").
508 */
509 static int
510efm_option_part_len(char_u *efm)
511{
512 int len;
513
514 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
515 if (efm[len] == '\\' && efm[len + 1] != NUL)
516 ++len;
517
518 return len;
519}
520
521/*
522 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
523 * are parsed and converted to regular expressions. Returns information about
524 * the parsed 'errorformat' option.
525 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200526 static efm_T *
527parse_efm_option(char_u *efm)
528{
529 char_u *errmsg = NULL;
530 int errmsglen;
531 efm_T *fmt_ptr = NULL;
532 efm_T *fmt_first = NULL;
533 efm_T *fmt_last = NULL;
534 char_u *fmtstr = NULL;
535 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200536 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200537
538 errmsglen = CMDBUFFSIZE + 1;
539 errmsg = alloc_id(errmsglen, aid_qf_errmsg);
540 if (errmsg == NULL)
541 goto parse_efm_end;
542
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200543 // Each part of the format string is copied and modified from errorformat
544 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200545
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200546 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200547 sz = efm_regpat_bufsz(efm);
548 if ((fmtstr = alloc(sz)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200549 goto parse_efm_error;
550
551 while (efm[0] != NUL)
552 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200553 // Allocate a new eformat structure and put it at the end of the list
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200554 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
555 if (fmt_ptr == NULL)
556 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200557 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200558 fmt_first = fmt_ptr;
559 else
560 fmt_last->next = fmt_ptr;
561 fmt_last = fmt_ptr;
562
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200563 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200564 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200565
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200566 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200567 goto parse_efm_error;
568 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
569 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200570 // Advance to next part
571 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200572 }
573
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200574 if (fmt_first == NULL) // nothing found
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200575 EMSG(_("E378: 'errorformat' contains no pattern"));
576
577 goto parse_efm_end;
578
579parse_efm_error:
580 free_efm_list(&fmt_first);
581
582parse_efm_end:
583 vim_free(fmtstr);
584 vim_free(errmsg);
585
586 return fmt_first;
587}
588
Bram Moolenaare0d37972016-07-15 22:36:01 +0200589enum {
590 QF_FAIL = 0,
591 QF_OK = 1,
592 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200593 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200594 QF_IGNORE_LINE = 4,
595 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200596};
597
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200598/*
599 * State information used to parse lines and add entries to a quickfix/location
600 * list.
601 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200602typedef struct {
603 char_u *linebuf;
604 int linelen;
605 char_u *growbuf;
606 int growbufsiz;
607 FILE *fd;
608 typval_T *tv;
609 char_u *p_str;
610 listitem_T *p_li;
611 buf_T *buf;
612 linenr_T buflnum;
613 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100614 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200615} qfstate_T;
616
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200617/*
618 * Allocate more memory for the line buffer used for parsing lines.
619 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200620 static char_u *
621qf_grow_linebuf(qfstate_T *state, int newsz)
622{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200623 char_u *p;
624
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200625 // If the line exceeds LINE_MAXLEN exclude the last
626 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200627 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
628 if (state->growbuf == NULL)
629 {
630 state->growbuf = alloc(state->linelen + 1);
631 if (state->growbuf == NULL)
632 return NULL;
633 state->growbufsiz = state->linelen;
634 }
635 else if (state->linelen > state->growbufsiz)
636 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200637 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200638 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200639 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200640 state->growbufsiz = state->linelen;
641 }
642 return state->growbuf;
643}
644
645/*
646 * Get the next string (separated by newline) from state->p_str.
647 */
648 static int
649qf_get_next_str_line(qfstate_T *state)
650{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200651 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200652 char_u *p_str = state->p_str;
653 char_u *p;
654 int len;
655
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200656 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200657 return QF_END_OF_INPUT;
658
659 p = vim_strchr(p_str, '\n');
660 if (p != NULL)
661 len = (int)(p - p_str) + 1;
662 else
663 len = (int)STRLEN(p_str);
664
665 if (len > IOSIZE - 2)
666 {
667 state->linebuf = qf_grow_linebuf(state, len);
668 if (state->linebuf == NULL)
669 return QF_NOMEM;
670 }
671 else
672 {
673 state->linebuf = IObuff;
674 state->linelen = len;
675 }
676 vim_strncpy(state->linebuf, p_str, state->linelen);
677
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200678 // Increment using len in order to discard the rest of the
679 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200680 p_str += len;
681 state->p_str = p_str;
682
683 return QF_OK;
684}
685
686/*
687 * Get the next string from state->p_Li.
688 */
689 static int
690qf_get_next_list_line(qfstate_T *state)
691{
692 listitem_T *p_li = state->p_li;
693 int len;
694
695 while (p_li != NULL
696 && (p_li->li_tv.v_type != VAR_STRING
697 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200698 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200699
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200700 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200701 {
702 state->p_li = NULL;
703 return QF_END_OF_INPUT;
704 }
705
706 len = (int)STRLEN(p_li->li_tv.vval.v_string);
707 if (len > IOSIZE - 2)
708 {
709 state->linebuf = qf_grow_linebuf(state, len);
710 if (state->linebuf == NULL)
711 return QF_NOMEM;
712 }
713 else
714 {
715 state->linebuf = IObuff;
716 state->linelen = len;
717 }
718
719 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
720
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200721 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200722 return QF_OK;
723}
724
725/*
726 * Get the next string from state->buf.
727 */
728 static int
729qf_get_next_buf_line(qfstate_T *state)
730{
731 char_u *p_buf = NULL;
732 int len;
733
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200734 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200735 if (state->buflnum > state->lnumlast)
736 return QF_END_OF_INPUT;
737
738 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
739 state->buflnum += 1;
740
741 len = (int)STRLEN(p_buf);
742 if (len > IOSIZE - 2)
743 {
744 state->linebuf = qf_grow_linebuf(state, len);
745 if (state->linebuf == NULL)
746 return QF_NOMEM;
747 }
748 else
749 {
750 state->linebuf = IObuff;
751 state->linelen = len;
752 }
753 vim_strncpy(state->linebuf, p_buf, state->linelen);
754
755 return QF_OK;
756}
757
758/*
759 * Get the next string from file state->fd.
760 */
761 static int
762qf_get_next_file_line(qfstate_T *state)
763{
764 int discard;
765 int growbuflen;
766
767 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
768 return QF_END_OF_INPUT;
769
770 discard = FALSE;
771 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200772 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200773 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200774 // The current line exceeds IObuff, continue reading using
775 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200776 if (state->growbuf == NULL)
777 {
778 state->growbufsiz = 2 * (IOSIZE - 1);
779 state->growbuf = alloc(state->growbufsiz);
780 if (state->growbuf == NULL)
781 return QF_NOMEM;
782 }
783
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200784 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200785 memcpy(state->growbuf, IObuff, IOSIZE - 1);
786 growbuflen = state->linelen;
787
788 for (;;)
789 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200790 char_u *p;
791
Bram Moolenaare0d37972016-07-15 22:36:01 +0200792 if (fgets((char *)state->growbuf + growbuflen,
793 state->growbufsiz - growbuflen, state->fd) == NULL)
794 break;
795 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
796 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200797 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200798 break;
799 if (state->growbufsiz == LINE_MAXLEN)
800 {
801 discard = TRUE;
802 break;
803 }
804
805 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
806 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200807 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200808 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200809 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200810 }
811
812 while (discard)
813 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200814 // The current line is longer than LINE_MAXLEN, continue
815 // reading but discard everything until EOL or EOF is
816 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200817 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
818 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200819 || IObuff[IOSIZE - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200820 break;
821 }
822
823 state->linebuf = state->growbuf;
824 state->linelen = growbuflen;
825 }
826 else
827 state->linebuf = IObuff;
828
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100829#ifdef FEAT_MBYTE
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200830 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200831 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
832 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100833 char_u *line;
834
835 line = string_convert(&state->vc, state->linebuf, &state->linelen);
836 if (line != NULL)
837 {
838 if (state->linelen < IOSIZE)
839 {
840 STRCPY(state->linebuf, line);
841 vim_free(line);
842 }
843 else
844 {
845 vim_free(state->growbuf);
846 state->linebuf = state->growbuf = line;
847 state->growbufsiz = state->linelen < LINE_MAXLEN
848 ? state->linelen : LINE_MAXLEN;
849 }
850 }
851 }
852#endif
853
Bram Moolenaare0d37972016-07-15 22:36:01 +0200854 return QF_OK;
855}
856
857/*
858 * Get the next string from a file/buffer/list/string.
859 */
860 static int
861qf_get_nextline(qfstate_T *state)
862{
863 int status = QF_FAIL;
864
865 if (state->fd == NULL)
866 {
867 if (state->tv != NULL)
868 {
869 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200870 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200871 status = qf_get_next_str_line(state);
872 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200873 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200874 status = qf_get_next_list_line(state);
875 }
876 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200877 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200878 status = qf_get_next_buf_line(state);
879 }
880 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200881 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200882 status = qf_get_next_file_line(state);
883
884 if (status != QF_OK)
885 return status;
886
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200887 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200888 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200889 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200890 state->linebuf[state->linelen - 1] = NUL;
891#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200892 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
893 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200894#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200895 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200896
897#ifdef FEAT_MBYTE
898 remove_bom(state->linebuf);
899#endif
900
901 return QF_OK;
902}
903
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200904typedef struct {
905 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200906 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200907 char_u *errmsg;
908 int errmsglen;
909 long lnum;
910 int col;
911 char_u use_viscol;
912 char_u *pattern;
913 int enr;
914 int type;
915 int valid;
916} qffields_T;
917
918/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200919 * Parse the match for filename ('%f') pattern in regmatch.
920 * Return the matched value in "fields->namebuf".
921 */
922 static int
923qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
924{
925 int c;
926
927 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
928 return QF_FAIL;
929
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200930 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200931 c = *rmp->endp[midx];
932 *rmp->endp[midx] = NUL;
933 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
934 *rmp->endp[midx] = c;
935
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200936 // For separate filename patterns (%O, %P and %Q), the specified file
937 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200938 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
939 && mch_getperm(fields->namebuf) == -1)
940 return QF_FAIL;
941
942 return QF_OK;
943}
944
945/*
946 * Parse the match for error number ('%n') pattern in regmatch.
947 * Return the matched value in "fields->enr".
948 */
949 static int
950qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
951{
952 if (rmp->startp[midx] == NULL)
953 return QF_FAIL;
954 fields->enr = (int)atol((char *)rmp->startp[midx]);
955 return QF_OK;
956}
957
958/*
959 * Parse the match for line number (%l') pattern in regmatch.
960 * Return the matched value in "fields->lnum".
961 */
962 static int
963qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
964{
965 if (rmp->startp[midx] == NULL)
966 return QF_FAIL;
967 fields->lnum = atol((char *)rmp->startp[midx]);
968 return QF_OK;
969}
970
971/*
972 * Parse the match for column number ('%c') pattern in regmatch.
973 * Return the matched value in "fields->col".
974 */
975 static int
976qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
977{
978 if (rmp->startp[midx] == NULL)
979 return QF_FAIL;
980 fields->col = (int)atol((char *)rmp->startp[midx]);
981 return QF_OK;
982}
983
984/*
985 * Parse the match for error type ('%t') pattern in regmatch.
986 * Return the matched value in "fields->type".
987 */
988 static int
989qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
990{
991 if (rmp->startp[midx] == NULL)
992 return QF_FAIL;
993 fields->type = *rmp->startp[midx];
994 return QF_OK;
995}
996
997/*
998 * Parse the match for '%+' format pattern. The whole matching line is included
999 * in the error string. Return the matched line in "fields->errmsg".
1000 */
1001 static int
1002qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
1003{
1004 char_u *p;
1005
1006 if (linelen >= fields->errmsglen)
1007 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001008 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001009 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1010 return QF_NOMEM;
1011 fields->errmsg = p;
1012 fields->errmsglen = linelen + 1;
1013 }
1014 vim_strncpy(fields->errmsg, linebuf, linelen);
1015 return QF_OK;
1016}
1017
1018/*
1019 * Parse the match for error message ('%m') pattern in regmatch.
1020 * Return the matched value in "fields->errmsg".
1021 */
1022 static int
1023qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1024{
1025 char_u *p;
1026 int len;
1027
1028 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1029 return QF_FAIL;
1030 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1031 if (len >= fields->errmsglen)
1032 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001033 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001034 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1035 return QF_NOMEM;
1036 fields->errmsg = p;
1037 fields->errmsglen = len + 1;
1038 }
1039 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1040 return QF_OK;
1041}
1042
1043/*
1044 * Parse the match for rest of a single-line file message ('%r') pattern.
1045 * Return the matched value in "tail".
1046 */
1047 static int
1048qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1049{
1050 if (rmp->startp[midx] == NULL)
1051 return QF_FAIL;
1052 *tail = rmp->startp[midx];
1053 return QF_OK;
1054}
1055
1056/*
1057 * Parse the match for the pointer line ('%p') pattern in regmatch.
1058 * Return the matched value in "fields->col".
1059 */
1060 static int
1061qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1062{
1063 char_u *match_ptr;
1064
1065 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1066 return QF_FAIL;
1067 fields->col = 0;
1068 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1069 ++match_ptr)
1070 {
1071 ++fields->col;
1072 if (*match_ptr == TAB)
1073 {
1074 fields->col += 7;
1075 fields->col -= fields->col % 8;
1076 }
1077 }
1078 ++fields->col;
1079 fields->use_viscol = TRUE;
1080 return QF_OK;
1081}
1082
1083/*
1084 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1085 * Return the matched value in "fields->col".
1086 */
1087 static int
1088qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1089{
1090 if (rmp->startp[midx] == NULL)
1091 return QF_FAIL;
1092 fields->col = (int)atol((char *)rmp->startp[midx]);
1093 fields->use_viscol = TRUE;
1094 return QF_OK;
1095}
1096
1097/*
1098 * Parse the match for the search text ('%s') pattern in regmatch.
1099 * Return the matched value in "fields->pattern".
1100 */
1101 static int
1102qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1103{
1104 int len;
1105
1106 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1107 return QF_FAIL;
1108 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1109 if (len > CMDBUFFSIZE - 5)
1110 len = CMDBUFFSIZE - 5;
1111 STRCPY(fields->pattern, "^\\V");
1112 STRNCAT(fields->pattern, rmp->startp[midx], len);
1113 fields->pattern[len + 3] = '\\';
1114 fields->pattern[len + 4] = '$';
1115 fields->pattern[len + 5] = NUL;
1116 return QF_OK;
1117}
1118
1119/*
1120 * Parse the match for the module ('%o') pattern in regmatch.
1121 * Return the matched value in "fields->module".
1122 */
1123 static int
1124qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1125{
1126 int len;
1127
1128 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1129 return QF_FAIL;
1130 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1131 if (len > CMDBUFFSIZE)
1132 len = CMDBUFFSIZE;
1133 STRNCAT(fields->module, rmp->startp[midx], len);
1134 return QF_OK;
1135}
1136
1137/*
1138 * 'errorformat' format pattern parser functions.
1139 * The '%f' and '%r' formats are parsed differently from other formats.
1140 * See qf_parse_match() for details.
1141 */
1142static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1143{
1144 NULL,
1145 qf_parse_fmt_n,
1146 qf_parse_fmt_l,
1147 qf_parse_fmt_c,
1148 qf_parse_fmt_t,
1149 qf_parse_fmt_m,
1150 NULL,
1151 qf_parse_fmt_p,
1152 qf_parse_fmt_v,
1153 qf_parse_fmt_s,
1154 qf_parse_fmt_o
1155};
1156
1157/*
1158 * Parse the error format pattern matches in "regmatch" and set the values in
1159 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1160 * match. Returns QF_OK if all the matches are successfully parsed. On
1161 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001162 */
1163 static int
1164qf_parse_match(
1165 char_u *linebuf,
1166 int linelen,
1167 efm_T *fmt_ptr,
1168 regmatch_T *regmatch,
1169 qffields_T *fields,
1170 int qf_multiline,
1171 int qf_multiscan,
1172 char_u **tail)
1173{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001174 int idx = fmt_ptr->prefix;
1175 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001176 int midx;
1177 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001178
1179 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1180 return QF_FAIL;
1181 if (vim_strchr((char_u *)"EWI", idx) != NULL)
1182 fields->type = idx;
1183 else
1184 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001185
1186 // Extract error message data from matched line.
1187 // We check for an actual submatch, because "\[" and "\]" in
1188 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001189 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001190 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001191 status = QF_OK;
1192 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001193 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001194 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1195 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001196 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001197 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001198 status = qf_parse_fmt_plus(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001199 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001200 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001201 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001202 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001203 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001204 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001205 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001206
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001207 if (status != QF_OK)
1208 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001209 }
1210
1211 return QF_OK;
1212}
1213
1214/*
1215 * Parse an error line in 'linebuf' using a single error format string in
1216 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1217 * Returns QF_OK if the efm format matches completely and the fields are
1218 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1219 */
1220 static int
1221qf_parse_get_fields(
1222 char_u *linebuf,
1223 int linelen,
1224 efm_T *fmt_ptr,
1225 qffields_T *fields,
1226 int qf_multiline,
1227 int qf_multiscan,
1228 char_u **tail)
1229{
1230 regmatch_T regmatch;
1231 int status = QF_FAIL;
1232 int r;
1233
1234 if (qf_multiscan &&
1235 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1236 return QF_FAIL;
1237
1238 fields->namebuf[0] = NUL;
1239 fields->module[0] = NUL;
1240 fields->pattern[0] = NUL;
1241 if (!qf_multiscan)
1242 fields->errmsg[0] = NUL;
1243 fields->lnum = 0;
1244 fields->col = 0;
1245 fields->use_viscol = FALSE;
1246 fields->enr = -1;
1247 fields->type = 0;
1248 *tail = NULL;
1249
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001250 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001251 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001252 regmatch.regprog = fmt_ptr->prog;
1253 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1254 fmt_ptr->prog = regmatch.regprog;
1255 if (r)
1256 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1257 fields, qf_multiline, qf_multiscan, tail);
1258
1259 return status;
1260}
1261
1262/*
1263 * Parse directory error format prefixes (%D and %X).
1264 * Push and pop directories from the directory stack when scanning directory
1265 * names.
1266 */
1267 static int
1268qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1269{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001270 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001271 {
1272 if (*fields->namebuf == NUL)
1273 {
1274 EMSG(_("E379: Missing or empty directory name"));
1275 return QF_FAIL;
1276 }
1277 qfl->qf_directory =
1278 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1279 if (qfl->qf_directory == NULL)
1280 return QF_FAIL;
1281 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001282 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001283 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1284
1285 return QF_OK;
1286}
1287
1288/*
1289 * Parse global file name error format prefixes (%O, %P and %Q).
1290 */
1291 static int
1292qf_parse_file_pfx(
1293 int idx,
1294 qffields_T *fields,
1295 qf_list_T *qfl,
1296 char_u *tail)
1297{
1298 fields->valid = FALSE;
1299 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1300 {
1301 if (*fields->namebuf && idx == 'P')
1302 qfl->qf_currfile =
1303 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1304 else if (idx == 'Q')
1305 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1306 *fields->namebuf = NUL;
1307 if (tail && *tail)
1308 {
1309 STRMOVE(IObuff, skipwhite(tail));
1310 qfl->qf_multiscan = TRUE;
1311 return QF_MULTISCAN;
1312 }
1313 }
1314
1315 return QF_OK;
1316}
1317
1318/*
1319 * Parse a non-error line (a line which doesn't match any of the error
1320 * format in 'efm').
1321 */
1322 static int
1323qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1324{
1325 char_u *p;
1326
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001327 fields->namebuf[0] = NUL; // no match found, remove file name
1328 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001329 fields->valid = FALSE;
1330 if (linelen >= fields->errmsglen)
1331 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001332 // linelen + null terminator
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001333 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1334 return QF_NOMEM;
1335 fields->errmsg = p;
1336 fields->errmsglen = linelen + 1;
1337 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001338 // copy whole line to error message
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001339 vim_strncpy(fields->errmsg, linebuf, linelen);
1340
1341 return QF_OK;
1342}
1343
1344/*
1345 * Parse multi-line error format prefixes (%C and %Z)
1346 */
1347 static int
1348qf_parse_multiline_pfx(
1349 qf_info_T *qi,
1350 int qf_idx,
1351 int idx,
1352 qf_list_T *qfl,
1353 qffields_T *fields)
1354{
1355 char_u *ptr;
1356 int len;
1357
1358 if (!qfl->qf_multiignore)
1359 {
1360 qfline_T *qfprev = qfl->qf_last;
1361
1362 if (qfprev == NULL)
1363 return QF_FAIL;
1364 if (*fields->errmsg && !qfl->qf_multiignore)
1365 {
1366 len = (int)STRLEN(qfprev->qf_text);
1367 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2)))
1368 == NULL)
1369 return QF_FAIL;
1370 STRCPY(ptr, qfprev->qf_text);
1371 vim_free(qfprev->qf_text);
1372 qfprev->qf_text = ptr;
1373 *(ptr += len) = '\n';
1374 STRCPY(++ptr, fields->errmsg);
1375 }
1376 if (qfprev->qf_nr == -1)
1377 qfprev->qf_nr = fields->enr;
1378 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001379 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001380 qfprev->qf_type = fields->type;
1381
1382 if (!qfprev->qf_lnum)
1383 qfprev->qf_lnum = fields->lnum;
1384 if (!qfprev->qf_col)
1385 qfprev->qf_col = fields->col;
1386 qfprev->qf_viscol = fields->use_viscol;
1387 if (!qfprev->qf_fnum)
1388 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx,
1389 qfl->qf_directory,
1390 *fields->namebuf || qfl->qf_directory != NULL
1391 ? fields->namebuf
1392 : qfl->qf_currfile != NULL && fields->valid
1393 ? qfl->qf_currfile : 0);
1394 }
1395 if (idx == 'Z')
1396 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1397 line_breakcheck();
1398
1399 return QF_IGNORE_LINE;
1400}
1401
1402/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001403 * Parse a line and get the quickfix fields.
1404 * Return the QF_ status.
1405 */
1406 static int
1407qf_parse_line(
1408 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001409 int qf_idx,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001410 char_u *linebuf,
1411 int linelen,
1412 efm_T *fmt_first,
1413 qffields_T *fields)
1414{
1415 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001416 int idx = 0;
1417 char_u *tail = NULL;
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001418 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001419 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001420
Bram Moolenaare333e792018-04-08 13:27:39 +02001421restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001422 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001423 if (fmt_start == NULL)
1424 fmt_ptr = fmt_first;
1425 else
1426 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001427 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001428 fmt_ptr = fmt_start;
1429 fmt_start = NULL;
1430 }
1431
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001432 // Try to match each part of 'errorformat' until we find a complete
1433 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001434 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001435 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1436 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001437 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001438 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1439 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1440 if (status == QF_NOMEM)
1441 return status;
1442 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001443 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001444 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001445 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001446
1447 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1448 {
1449 if (fmt_ptr != NULL)
1450 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001451 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001452 status = qf_parse_dir_pfx(idx, fields, qfl);
1453 if (status != QF_OK)
1454 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001455 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001456
1457 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1458 if (status != QF_OK)
1459 return status;
1460
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001461 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001462 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001463 }
1464 else if (fmt_ptr != NULL)
1465 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001466 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001467 if (fmt_ptr->conthere)
1468 fmt_start = fmt_ptr;
1469
1470 if (vim_strchr((char_u *)"AEWI", idx) != NULL)
1471 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001472 qfl->qf_multiline = TRUE; // start of a multi-line message
1473 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001474 }
1475 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001476 { // continuation of multi-line msg
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001477 status = qf_parse_multiline_pfx(qi, qf_idx, idx, qfl, fields);
1478 if (status != QF_OK)
1479 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001480 }
1481 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001482 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001483 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1484 if (status == QF_MULTISCAN)
1485 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001486 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001487 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001488 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001489 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001490 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001491 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001492 return QF_IGNORE_LINE;
1493 }
1494 }
1495
1496 return QF_OK;
1497}
1498
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001499/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001500 * Returns TRUE if the specified quickfix/location stack is empty
1501 */
1502 static int
1503qf_stack_empty(qf_info_T *qi)
1504{
1505 return qi == NULL || qi->qf_listcount <= 0;
1506}
1507
1508/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001509 * Returns TRUE if the specified quickfix/location list is empty.
1510 */
1511 static int
1512qf_list_empty(qf_info_T *qi, int qf_idx)
1513{
1514 if (qi == NULL || qf_idx < 0 || qf_idx >= LISTCOUNT)
1515 return TRUE;
1516 return qi->qf_lists[qf_idx].qf_count <= 0;
1517}
1518
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001519/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001520 * Allocate the fields used for parsing lines and populating a quickfix list.
1521 */
1522 static int
1523qf_alloc_fields(qffields_T *pfields)
1524{
1525 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1526 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1527 pfields->errmsglen = CMDBUFFSIZE + 1;
1528 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1529 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1530 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1531 || pfields->pattern == NULL || pfields->module == NULL)
1532 return FAIL;
1533
1534 return OK;
1535}
1536
1537/*
1538 * Free the fields used for parsing lines and populating a quickfix list.
1539 */
1540 static void
1541qf_free_fields(qffields_T *pfields)
1542{
1543 vim_free(pfields->namebuf);
1544 vim_free(pfields->module);
1545 vim_free(pfields->errmsg);
1546 vim_free(pfields->pattern);
1547}
1548
1549/*
1550 * Setup the state information used for parsing lines and populating a
1551 * quickfix list.
1552 */
1553 static int
1554qf_setup_state(
1555 qfstate_T *pstate,
1556 char_u *enc,
1557 char_u *efile,
1558 typval_T *tv,
1559 buf_T *buf,
1560 linenr_T lnumfirst,
1561 linenr_T lnumlast)
1562{
1563#ifdef FEAT_MBYTE
1564 pstate->vc.vc_type = CONV_NONE;
1565 if (enc != NULL && *enc != NUL)
1566 convert_setup(&pstate->vc, enc, p_enc);
1567#endif
1568
1569 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1570 {
1571 EMSG2(_(e_openerrf), efile);
1572 return FAIL;
1573 }
1574
1575 if (tv != NULL)
1576 {
1577 if (tv->v_type == VAR_STRING)
1578 pstate->p_str = tv->vval.v_string;
1579 else if (tv->v_type == VAR_LIST)
1580 pstate->p_li = tv->vval.v_list->lv_first;
1581 pstate->tv = tv;
1582 }
1583 pstate->buf = buf;
1584 pstate->buflnum = lnumfirst;
1585 pstate->lnumlast = lnumlast;
1586
1587 return OK;
1588}
1589
1590/*
1591 * Cleanup the state information used for parsing lines and populating a
1592 * quickfix list.
1593 */
1594 static void
1595qf_cleanup_state(qfstate_T *pstate)
1596{
1597 if (pstate->fd != NULL)
1598 fclose(pstate->fd);
1599
1600 vim_free(pstate->growbuf);
1601#ifdef FEAT_MBYTE
1602 if (pstate->vc.vc_type != CONV_NONE)
1603 convert_setup(&pstate->vc, NULL, NULL);
1604#endif
1605}
1606
1607/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001608 * Read the errorfile "efile" into memory, line by line, building the error
1609 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001610 * Alternative: when "efile" is NULL read errors from buffer "buf".
1611 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001612 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001613 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1614 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001615 * Return -1 for error, number of errors for success.
1616 */
1617 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001618qf_init_ext(
1619 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001620 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001621 char_u *efile,
1622 buf_T *buf,
1623 typval_T *tv,
1624 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001625 int newlist, // TRUE: start a new error list
1626 linenr_T lnumfirst, // first line number to use
1627 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001628 char_u *qf_title,
1629 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001630{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001631 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001632 qfstate_T state;
1633 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001634 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001635 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001636 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001638 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001639 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001640 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001642 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001643 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001644
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001645 vim_memset(&state, 0, sizeof(state));
1646 vim_memset(&fields, 0, sizeof(fields));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001647 if ((qf_alloc_fields(&fields) == FAIL) ||
1648 (qf_setup_state(&state, enc, efile, tv, buf,
1649 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 goto qf_init_end;
1651
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001652 if (newlist || qf_idx == qi->qf_listcount)
1653 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001654 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001655 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001656 qf_idx = qi->qf_curlist;
1657 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001658 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001659 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001660 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001661 adding = TRUE;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001662 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001663 old_last = qi->qf_lists[qf_idx].qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001666 qfl = &qi->qf_lists[qf_idx];
1667
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001668 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001669 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001670 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 else
1672 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001674 // If the errorformat didn't change between calls, then reuse the
1675 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001676 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1677 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001678 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001679 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001680 free_efm_list(&fmt_first);
1681
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001682 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001683 fmt_first = parse_efm_option(efm);
1684 if (fmt_first != NULL)
1685 last_efm = vim_strsave(efm);
1686 }
1687
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001688 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001691 // got_int is reset here, because it was probably set when killing the
1692 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 got_int = FALSE;
1694
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001695 // Read the lines in the error file one by one.
1696 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001697 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001699 // Get the next line from a file/buffer/list/string
Bram Moolenaare0d37972016-07-15 22:36:01 +02001700 status = qf_get_nextline(&state);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001701 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001702 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001703 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001704 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001706 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen,
1707 fmt_first, &fields);
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001708 if (status == QF_FAIL)
1709 goto error2;
1710 if (status == QF_NOMEM)
1711 goto qf_init_end;
1712 if (status == QF_IGNORE_LINE)
1713 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02001715 if (qf_add_entry(qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001716 qf_idx,
1717 qfl->qf_directory,
1718 (*fields.namebuf || qfl->qf_directory != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001719 ? fields.namebuf
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001720 : ((qfl->qf_currfile != NULL && fields.valid)
1721 ? qfl->qf_currfile : (char_u *)NULL),
Bram Moolenaard76ce852018-05-01 15:02:04 +02001722 fields.module,
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00001723 0,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001724 fields.errmsg,
1725 fields.lnum,
1726 fields.col,
1727 fields.use_viscol,
1728 fields.pattern,
1729 fields.enr,
1730 fields.type,
1731 fields.valid) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 goto error2;
1733 line_breakcheck();
1734 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001735 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001737 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001739 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001740 qfl->qf_ptr = qfl->qf_start;
1741 qfl->qf_index = 1;
1742 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
1744 else
1745 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001746 qfl->qf_nonevalid = FALSE;
1747 if (qfl->qf_ptr == NULL)
1748 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001750 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001751 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001752 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
1754 EMSG(_(e_readerrf));
1755error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001756 if (!adding)
1757 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001758 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001759 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001760 qi->qf_listcount--;
1761 if (qi->qf_curlist > 0)
1762 --qi->qf_curlist;
1763 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001764qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001765 if (qf_idx == qi->qf_curlist)
1766 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001767 qf_cleanup_state(&state);
1768 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
1770 return retval;
1771}
1772
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001773/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001774 * Read the errorfile "efile" into memory, line by line, building the error
1775 * list. Set the error list's title to qf_title.
1776 * Return -1 for error, number of errors for success.
1777 */
1778 int
1779qf_init(win_T *wp,
1780 char_u *efile,
1781 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001782 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001783 char_u *qf_title,
1784 char_u *enc)
1785{
1786 qf_info_T *qi = &ql_info;
1787
1788 if (wp != NULL)
1789 {
1790 qi = ll_get_or_alloc_list(wp);
1791 if (qi == NULL)
1792 return FAIL;
1793 }
1794
1795 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1796 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1797}
1798
1799/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001800 * Set the title of the specified quickfix list. Frees the previous title.
1801 * Prepends ':' to the title.
1802 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001803 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001804qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001805{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001806 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001807
Bram Moolenaarfb604092014-07-23 15:55:00 +02001808 if (title != NULL)
1809 {
1810 char_u *p = alloc((int)STRLEN(title) + 2);
1811
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001812 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001813 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001814 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001815 }
1816}
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001819 * The title of a quickfix/location list is set, by default, to the command
1820 * that created the quickfix list with the ":" prefix.
1821 * Create a quickfix list title string by prepending ":" to a user command.
1822 * Returns a pointer to a static buffer with the title.
1823 */
1824 static char_u *
1825qf_cmdtitle(char_u *cmd)
1826{
1827 static char_u qftitle_str[IOSIZE];
1828
1829 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1830 return qftitle_str;
1831}
1832
1833/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001834 * Prepare for adding a new quickfix list. If the current list is in the
1835 * middle of the stack, then all the following lists are freed and then
1836 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 */
1838 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001839qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
1841 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001842 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001844 // If the current entry is not the last entry, delete entries beyond
1845 // the current entry. This makes it possible to browse in a tree-like
1846 // way with ":grep'.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001847 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001848 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001850 // When the stack is full, remove to oldest entry
1851 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001852 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001854 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001856 qi->qf_lists[i - 1] = qi->qf_lists[i];
1857 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 }
1859 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001860 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001861 qfl = &qi->qf_lists[qi->qf_curlist];
1862 vim_memset(qfl, 0, (size_t)(sizeof(qf_list_T)));
1863 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001864 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001865 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866}
1867
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001868/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001869 * Queue location list stack delete request.
1870 */
1871 static void
1872locstack_queue_delreq(qf_info_T *qi)
1873{
1874 qf_delq_T *q;
1875
1876 q = (qf_delq_T *)alloc((unsigned)sizeof(qf_delq_T));
1877 if (q != NULL)
1878 {
1879 q->qi = qi;
1880 q->next = qf_delq_head;
1881 qf_delq_head = q;
1882 }
1883}
1884
1885/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001886 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001887 */
1888 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001889ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001890{
1891 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001892 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001893
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001894 qi = *pqi;
1895 if (qi == NULL)
1896 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001897 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001898
1899 qi->qf_refcount--;
1900 if (qi->qf_refcount < 1)
1901 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001902 // No references to this location list.
1903 // If the location list is still in use, then queue the delete request
1904 // to be processed later.
1905 if (quickfix_busy > 0)
1906 locstack_queue_delreq(qi);
1907 else
1908 {
1909 for (i = 0; i < qi->qf_listcount; ++i)
1910 qf_free(&qi->qf_lists[i]);
1911 vim_free(qi);
1912 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001913 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001914}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001915
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001916/*
1917 * Free all the quickfix/location lists in the stack.
1918 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001919 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001920qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001921{
1922 int i;
1923 qf_info_T *qi = &ql_info;
1924
1925 if (wp != NULL)
1926 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001927 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001928 ll_free_all(&wp->w_llist);
1929 ll_free_all(&wp->w_llist_ref);
1930 }
1931 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001932 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001933 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001934 qf_free(&qi->qf_lists[i]);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001935}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001936
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001938 * Delay freeing of location list stacks when the quickfix code is running.
1939 * Used to avoid problems with autocmds freeing location list stacks when the
1940 * quickfix code is still referencing the stack.
1941 * Must always call decr_quickfix_busy() exactly once after this.
1942 */
1943 static void
1944incr_quickfix_busy(void)
1945{
1946 quickfix_busy++;
1947}
1948
1949/*
1950 * Safe to free location list stacks. Process any delayed delete requests.
1951 */
1952 static void
1953decr_quickfix_busy(void)
1954{
1955 if (--quickfix_busy == 0)
1956 {
1957 // No longer referencing the location lists. Process all the pending
1958 // delete requests.
1959 while (qf_delq_head != NULL)
1960 {
1961 qf_delq_T *q = qf_delq_head;
1962
1963 qf_delq_head = q->next;
1964 ll_free_all(&q->qi);
1965 vim_free(q);
1966 }
1967 }
1968#ifdef ABORT_ON_INTERNAL_ERROR
1969 if (quickfix_busy < 0)
1970 {
1971 EMSG("quickfix_busy has become negative");
1972 abort();
1973 }
1974#endif
1975}
1976
1977#if defined(EXITFREE) || defined(PROTO)
1978 void
1979check_quickfix_busy(void)
1980{
1981 if (quickfix_busy != 0)
1982 {
1983 EMSGN("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
1984# ifdef ABORT_ON_INTERNAL_ERROR
1985 abort();
1986# endif
1987 }
1988}
1989#endif
1990
1991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 * Add an entry to the end of the list of errors.
1993 * Returns OK or FAIL.
1994 */
1995 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001996qf_add_entry(
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001997 qf_info_T *qi, // quickfix list
1998 int qf_idx, // list index
1999 char_u *dir, // optional directory name
2000 char_u *fname, // file name or NULL
2001 char_u *module, // module name or NULL
2002 int bufnum, // buffer number or zero
2003 char_u *mesg, // message
2004 long lnum, // line number
2005 int col, // column
2006 int vis_col, // using visual column
2007 char_u *pattern, // search pattern
2008 int nr, // error number
2009 int type, // type character
2010 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002012 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002013 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002014 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002016 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 return FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002018 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002019 {
2020 buf_T *buf = buflist_findnr(bufnum);
2021
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002022 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002023 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002024 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002025 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002026 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002027 else
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002028 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2030 {
2031 vim_free(qfp);
2032 return FAIL;
2033 }
2034 qfp->qf_lnum = lnum;
2035 qfp->qf_col = col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002036 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002037 if (pattern == NULL || *pattern == NUL)
2038 qfp->qf_pattern = NULL;
2039 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2040 {
2041 vim_free(qfp->qf_text);
2042 vim_free(qfp);
2043 return FAIL;
2044 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002045 if (module == NULL || *module == NUL)
2046 qfp->qf_module = NULL;
2047 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2048 {
2049 vim_free(qfp->qf_text);
2050 vim_free(qfp->qf_pattern);
2051 vim_free(qfp);
2052 return FAIL;
2053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002055 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 type = 0;
2057 qfp->qf_type = type;
2058 qfp->qf_valid = valid;
2059
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002060 lastp = &qfl->qf_last;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002061 if (qf_list_empty(qi, qf_idx)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002063 qfl->qf_start = qfp;
2064 qfl->qf_ptr = qfp;
2065 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002066 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
2068 else
2069 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002070 qfp->qf_prev = *lastp;
2071 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002073 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002075 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002076 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002077 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002079 qfl->qf_index = qfl->qf_count;
2080 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082
2083 return OK;
2084}
2085
2086/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002087 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002088 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002089 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002090qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002091{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002092 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002093
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02002094 qi = (qf_info_T *)alloc_clear((unsigned)sizeof(qf_info_T));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002095 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002096 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002097 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002098 qi->qfl_type = qfltype;
2099 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002100 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002101}
2102
2103/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002104 * Return the location list stack for window 'wp'.
2105 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002106 */
2107 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002108ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002109{
2110 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002111 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002112 return wp->w_llist_ref;
2113
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002114 // For a non-location list window, w_llist_ref should not point to a
2115 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002116 ll_free_all(&wp->w_llist_ref);
2117
2118 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002119 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002120 return wp->w_llist;
2121}
2122
2123/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002124 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2125 */
2126 static int
2127copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2128{
2129 int i;
2130 qfline_T *from_qfp;
2131 qfline_T *prevp;
2132
2133 // copy all the location entries in this list
2134 for (i = 0, from_qfp = from_qfl->qf_start;
2135 i < from_qfl->qf_count && from_qfp != NULL;
2136 ++i, from_qfp = from_qfp->qf_next)
2137 {
2138 if (qf_add_entry(to_qi,
2139 to_qi->qf_curlist,
2140 NULL,
2141 NULL,
2142 from_qfp->qf_module,
2143 0,
2144 from_qfp->qf_text,
2145 from_qfp->qf_lnum,
2146 from_qfp->qf_col,
2147 from_qfp->qf_viscol,
2148 from_qfp->qf_pattern,
2149 from_qfp->qf_nr,
2150 0,
2151 from_qfp->qf_valid) == FAIL)
2152 return FAIL;
2153
2154 // qf_add_entry() will not set the qf_num field, as the
2155 // directory and file names are not supplied. So the qf_fnum
2156 // field is copied here.
2157 prevp = to_qfl->qf_last;
2158 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2159 prevp->qf_type = from_qfp->qf_type; // error type
2160 if (from_qfl->qf_ptr == from_qfp)
2161 to_qfl->qf_ptr = prevp; // current location
2162 }
2163
2164 return OK;
2165}
2166
2167/*
2168 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2169 */
2170 static int
2171copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl, qf_info_T *to_qi)
2172{
2173 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002174 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002175 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2176 to_qfl->qf_count = 0;
2177 to_qfl->qf_index = 0;
2178 to_qfl->qf_start = NULL;
2179 to_qfl->qf_last = NULL;
2180 to_qfl->qf_ptr = NULL;
2181 if (from_qfl->qf_title != NULL)
2182 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2183 else
2184 to_qfl->qf_title = NULL;
2185 if (from_qfl->qf_ctx != NULL)
2186 {
2187 to_qfl->qf_ctx = alloc_tv();
2188 if (to_qfl->qf_ctx != NULL)
2189 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2190 }
2191 else
2192 to_qfl->qf_ctx = NULL;
2193
2194 if (from_qfl->qf_count)
2195 if (copy_loclist_entries(from_qfl, to_qfl, to_qi) == FAIL)
2196 return FAIL;
2197
2198 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2199
2200 // Assign a new ID for the location list
2201 to_qfl->qf_id = ++last_qf_id;
2202 to_qfl->qf_changedtick = 0L;
2203
2204 // When no valid entries are present in the list, qf_ptr points to
2205 // the first item in the list
2206 if (to_qfl->qf_nonevalid)
2207 {
2208 to_qfl->qf_ptr = to_qfl->qf_start;
2209 to_qfl->qf_index = 1;
2210 }
2211
2212 return OK;
2213}
2214
2215/*
2216 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002217 */
2218 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002219copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002220{
2221 qf_info_T *qi;
2222 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002223
Bram Moolenaar09037502018-09-25 22:08:14 +02002224 // When copying from a location list window, copy the referenced
2225 // location list. For other windows, copy the location list for
2226 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002227 if (IS_LL_WINDOW(from))
2228 qi = from->w_llist_ref;
2229 else
2230 qi = from->w_llist;
2231
Bram Moolenaar09037502018-09-25 22:08:14 +02002232 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002233 return;
2234
Bram Moolenaar09037502018-09-25 22:08:14 +02002235 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002236 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002237 return;
2238
2239 to->w_llist->qf_listcount = qi->qf_listcount;
2240
Bram Moolenaar09037502018-09-25 22:08:14 +02002241 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002242 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002243 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002244 to->w_llist->qf_curlist = idx;
2245
Bram Moolenaar09037502018-09-25 22:08:14 +02002246 if (copy_loclist(&qi->qf_lists[idx],
2247 &to->w_llist->qf_lists[idx], to->w_llist) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002248 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002249 qf_free_all(to);
2250 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002251 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002252 }
2253
Bram Moolenaar09037502018-09-25 22:08:14 +02002254 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002255}
2256
2257/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002258 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002259 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 */
2261 static int
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002262qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002264 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar82404332016-07-10 17:00:38 +02002265 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002266 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002267 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002268
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002269 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
Bram Moolenaare60acc12011-05-10 16:41:25 +02002272#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002273 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002274#endif
2275#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002276 if (directory != NULL)
2277 slash_adjust(directory);
2278 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002279#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002280 if (directory != NULL && !vim_isAbsName(fname)
2281 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2282 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002283 // Here we check if the file really exists.
2284 // This should normally be true, but if make works without
2285 // "leaving directory"-messages we might have missed a
2286 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002287 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002290 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002291 if (directory)
2292 ptr = concat_fnames(directory, fname, TRUE);
2293 else
2294 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002296 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002297 bufname = ptr;
2298 }
2299 else
2300 bufname = fname;
2301
2302 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002303 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002304 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002305 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002306 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002308 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002309 {
2310 vim_free(qf_last_bufname);
2311 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2312 if (bufname == ptr)
2313 qf_last_bufname = bufname;
2314 else
2315 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002316 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002317 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002318 if (buf == NULL)
2319 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002320
Bram Moolenaarc1542742016-07-20 21:44:37 +02002321 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002322 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002323 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324}
2325
2326/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002327 * Push dirbuf onto the directory stack and return pointer to actual dir or
2328 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 */
2330 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002331qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332{
2333 struct dir_stack_T *ds_new;
2334 struct dir_stack_T *ds_ptr;
2335
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002336 // allocate new stack element and hook it in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
2338 if (ds_new == NULL)
2339 return NULL;
2340
2341 ds_new->next = *stackptr;
2342 *stackptr = ds_new;
2343
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002344 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (vim_isAbsName(dirbuf)
2346 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002347 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 (*stackptr)->dirname = vim_strsave(dirbuf);
2349 else
2350 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002351 // Okay we don't have an absolute path.
2352 // dirbuf must be a subdir of one of the directories on the stack.
2353 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 ds_new = (*stackptr)->next;
2355 (*stackptr)->dirname = NULL;
2356 while (ds_new)
2357 {
2358 vim_free((*stackptr)->dirname);
2359 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2360 TRUE);
2361 if (mch_isdir((*stackptr)->dirname) == TRUE)
2362 break;
2363
2364 ds_new = ds_new->next;
2365 }
2366
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002367 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 while ((*stackptr)->next != ds_new)
2369 {
2370 ds_ptr = (*stackptr)->next;
2371 (*stackptr)->next = (*stackptr)->next->next;
2372 vim_free(ds_ptr->dirname);
2373 vim_free(ds_ptr);
2374 }
2375
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002376 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 if (ds_new == NULL)
2378 {
2379 vim_free((*stackptr)->dirname);
2380 (*stackptr)->dirname = vim_strsave(dirbuf);
2381 }
2382 }
2383
2384 if ((*stackptr)->dirname != NULL)
2385 return (*stackptr)->dirname;
2386 else
2387 {
2388 ds_ptr = *stackptr;
2389 *stackptr = (*stackptr)->next;
2390 vim_free(ds_ptr);
2391 return NULL;
2392 }
2393}
2394
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395/*
2396 * pop dirbuf from the directory stack and return previous directory or NULL if
2397 * stack is empty
2398 */
2399 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002400qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401{
2402 struct dir_stack_T *ds_ptr;
2403
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002404 // TODO: Should we check if dirbuf is the directory on top of the stack?
2405 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002407 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 if (*stackptr != NULL)
2409 {
2410 ds_ptr = *stackptr;
2411 *stackptr = (*stackptr)->next;
2412 vim_free(ds_ptr->dirname);
2413 vim_free(ds_ptr);
2414 }
2415
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002416 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 return *stackptr ? (*stackptr)->dirname : NULL;
2418}
2419
2420/*
2421 * clean up directory stack
2422 */
2423 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002424qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425{
2426 struct dir_stack_T *ds_ptr;
2427
2428 while ((ds_ptr = *stackptr) != NULL)
2429 {
2430 *stackptr = (*stackptr)->next;
2431 vim_free(ds_ptr->dirname);
2432 vim_free(ds_ptr);
2433 }
2434}
2435
2436/*
2437 * Check in which directory of the directory stack the given file can be
2438 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002439 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 * Cleans up intermediate directory entries.
2441 *
2442 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002443 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 * ./
2445 * ./aa
2446 * ./aa/bb
2447 * ./bb
2448 * ./bb/x.c
2449 * and make says:
2450 * making all in aa
2451 * making all in bb
2452 * x.c:9: Error
2453 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2454 * qf_guess_filepath will return NULL.
2455 */
2456 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002457qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
2459 struct dir_stack_T *ds_ptr;
2460 struct dir_stack_T *ds_tmp;
2461 char_u *fullname;
2462
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002463 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002464 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 return NULL;
2466
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002467 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 fullname = NULL;
2469 while (ds_ptr)
2470 {
2471 vim_free(fullname);
2472 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2473
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002474 // If concat_fnames failed, just go on. The worst thing that can happen
2475 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2477 break;
2478
2479 ds_ptr = ds_ptr->next;
2480 }
2481
2482 vim_free(fullname);
2483
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002484 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002485 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002487 ds_tmp = qfl->qf_dir_stack->next;
2488 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 vim_free(ds_tmp->dirname);
2490 vim_free(ds_tmp);
2491 }
2492
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002493 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494}
2495
2496/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002497 * Returns TRUE if a quickfix/location list with the given identifier exists.
2498 */
2499 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002500qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002501{
2502 qf_info_T *qi = &ql_info;
2503 int i;
2504
2505 if (wp != NULL)
2506 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002507 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002508 if (qi == NULL)
2509 return FALSE;
2510 }
2511
2512 for (i = 0; i < qi->qf_listcount; ++i)
2513 if (qi->qf_lists[i].qf_id == qf_id)
2514 return TRUE;
2515
2516 return FALSE;
2517}
2518
2519/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002520 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002521 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002522 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002523 * Similar to location list.
2524 */
2525 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002526is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002527{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002528 qfline_T *qfp;
2529 int i;
2530
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002531 // Search for the entry in the current list
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002532 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count;
2533 ++i, qfp = qfp->qf_next)
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002534 if (qfp == NULL || qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002535 break;
2536
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002537 if (i == qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002538 return FALSE;
2539
2540 return TRUE;
2541}
2542
2543/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002544 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002545 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002546 */
2547 static qfline_T *
2548get_next_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 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002562 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002563 return NULL;
2564 ++idx;
2565 qf_ptr = qf_ptr->qf_next;
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 == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2568
2569 *qf_index = idx;
2570 return qf_ptr;
2571}
2572
2573/*
2574 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002575 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002576 */
2577 static qfline_T *
2578get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002579 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002580 qfline_T *qf_ptr,
2581 int *qf_index,
2582 int dir)
2583{
2584 int idx;
2585 int old_qf_fnum;
2586
2587 idx = *qf_index;
2588 old_qf_fnum = qf_ptr->qf_fnum;
2589
2590 do
2591 {
2592 if (idx == 1 || qf_ptr->qf_prev == NULL)
2593 return NULL;
2594 --idx;
2595 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002596 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002597 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2598
2599 *qf_index = idx;
2600 return qf_ptr;
2601}
2602
2603/*
2604 * Get the n'th (errornr) previous/next valid entry from the current entry in
2605 * the quickfix list.
2606 * dir == FORWARD or FORWARD_FILE: next valid entry
2607 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2608 */
2609 static qfline_T *
2610get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002611 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002612 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002613 int dir,
2614 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002615{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002616 qfline_T *qf_ptr = qfl->qf_ptr;
2617 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002618 qfline_T *prev_qf_ptr;
2619 int prev_index;
2620 static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
2621 char_u *err = e_no_more_items;
2622
2623 while (errornr--)
2624 {
2625 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002626 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002627
2628 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002629 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002630 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002631 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002632 if (qf_ptr == NULL)
2633 {
2634 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002635 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002636 if (err != NULL)
2637 {
2638 EMSG(_(err));
2639 return NULL;
2640 }
2641 break;
2642 }
2643
2644 err = NULL;
2645 }
2646
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002647 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002648 return qf_ptr;
2649}
2650
2651/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002652 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2653 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002654 */
2655 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002656get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002657{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002658 qfline_T *qf_ptr = qfl->qf_ptr;
2659 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002660
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002661 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002662 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2663 {
2664 --qf_idx;
2665 qf_ptr = qf_ptr->qf_prev;
2666 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002667 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002668 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2669 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002670 {
2671 ++qf_idx;
2672 qf_ptr = qf_ptr->qf_next;
2673 }
2674
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002675 *new_qfidx = qf_idx;
2676 return qf_ptr;
2677}
2678
2679/*
2680 * Get a entry specied by 'errornr' and 'dir' from the current
2681 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2682 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2683 * Returns a pointer to the entry and the index of the new entry is stored in
2684 * 'new_qfidx'.
2685 */
2686 static qfline_T *
2687qf_get_entry(
2688 qf_list_T *qfl,
2689 int errornr,
2690 int dir,
2691 int *new_qfidx)
2692{
2693 qfline_T *qf_ptr = qfl->qf_ptr;
2694 int qfidx = qfl->qf_index;
2695
2696 if (dir != 0) // next/prev valid entry
2697 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2698 else if (errornr != 0) // go to specified number
2699 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2700
2701 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002702 return qf_ptr;
2703}
2704
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002705/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002706 * Find a window displaying a Vim help file.
2707 */
2708 static win_T *
2709qf_find_help_win(void)
2710{
2711 win_T *wp;
2712
2713 FOR_ALL_WINDOWS(wp)
2714 if (bt_help(wp->w_buffer))
2715 return wp;
2716
2717 return NULL;
2718}
2719
2720/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002721 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2722 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002723 */
2724 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002725jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002726{
2727 win_T *wp;
2728 int flags;
2729
Bram Moolenaarb2443732018-11-11 22:50:27 +01002730 if (cmdmod.tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002731 wp = NULL;
2732 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002733 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002734 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2735 win_enter(wp, TRUE);
2736 else
2737 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002738 // Split off help window; put it at far top if no position
2739 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002740 flags = WSP_HELP;
2741 if (cmdmod.split == 0 && curwin->w_width != Columns
2742 && curwin->w_width < 80)
2743 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002744 // If the user asks to open a new window, then copy the location list.
2745 // Otherwise, don't copy the location list.
2746 if (IS_LL_STACK(qi) && !newwin)
2747 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002748
2749 if (win_split(0, flags) == FAIL)
2750 return FAIL;
2751
2752 *opened_window = TRUE;
2753
2754 if (curwin->w_height < p_hh)
2755 win_setheight((int)p_hh);
2756
Bram Moolenaarb2443732018-11-11 22:50:27 +01002757 // When using location list, the new window should use the supplied
2758 // location list. If the user asks to open a new window, then the new
2759 // window will get a copy of the location list.
2760 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002761 {
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002762 curwin->w_llist = qi;
2763 qi->qf_refcount++;
2764 }
2765 }
2766
2767 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002768 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002769
2770 return OK;
2771}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002772
2773/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002774 * Find a non-quickfix window using the given location list.
2775 * Returns NULL if a matching window is not found.
2776 */
2777 static win_T *
2778qf_find_win_with_loclist(qf_info_T *ll)
2779{
2780 win_T *wp;
2781
2782 FOR_ALL_WINDOWS(wp)
2783 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2784 return wp;
2785
2786 return NULL;
2787}
2788
2789/*
2790 * Find a window containing a normal buffer
2791 */
2792 static win_T *
2793qf_find_win_with_normal_buf(void)
2794{
2795 win_T *wp;
2796
2797 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002798 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002799 return wp;
2800
2801 return NULL;
2802}
2803
2804/*
2805 * Go to a window in any tabpage containing the specified file. Returns TRUE
2806 * if successfully jumped to the window. Otherwise returns FALSE.
2807 */
2808 static int
2809qf_goto_tabwin_with_file(int fnum)
2810{
2811 tabpage_T *tp;
2812 win_T *wp;
2813
2814 FOR_ALL_TAB_WINDOWS(tp, wp)
2815 if (wp->w_buffer->b_fnum == fnum)
2816 {
2817 goto_tabpage_win(tp, wp);
2818 return TRUE;
2819 }
2820
2821 return FALSE;
2822}
2823
2824/*
2825 * Create a new window to show a file above the quickfix window. Called when
2826 * only the quickfix window is present.
2827 */
2828 static int
2829qf_open_new_file_win(qf_info_T *ll_ref)
2830{
2831 int flags;
2832
2833 flags = WSP_ABOVE;
2834 if (ll_ref != NULL)
2835 flags |= WSP_NEWLOC;
2836 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002837 return FAIL; // not enough room for window
2838 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002839 swb_flags = 0;
2840 RESET_BINDING(curwin);
2841 if (ll_ref != NULL)
2842 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002843 // The new window should use the location list from the
2844 // location list window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002845 curwin->w_llist = ll_ref;
2846 ll_ref->qf_refcount++;
2847 }
2848 return OK;
2849}
2850
2851/*
2852 * Go to a window that shows the right buffer. If the window is not found, go
2853 * to the window just above the location list window. This is used for opening
2854 * a file from a location window and not from a quickfix window. If some usable
2855 * window is previously found, then it is supplied in 'use_win'.
2856 */
2857 static void
2858qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2859{
2860 win_T *win = use_win;
2861
2862 if (win == NULL)
2863 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002864 // Find the window showing the selected file
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002865 FOR_ALL_WINDOWS(win)
2866 if (win->w_buffer->b_fnum == qf_fnum)
2867 break;
2868 if (win == NULL)
2869 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002870 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002871 win = curwin;
2872 do
2873 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002874 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002875 break;
2876 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002877 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002878 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002879 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002880 } while (win != curwin);
2881 }
2882 }
2883 win_goto(win);
2884
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002885 // If the location list for the window is not set, then set it
2886 // to the location list from the location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002887 if (win->w_llist == NULL)
2888 {
2889 win->w_llist = ll_ref;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002890 if (ll_ref != NULL)
2891 ll_ref->qf_refcount++;
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002892 }
2893}
2894
2895/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002896 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
2897 * not found, then go to the window just above the quickfix window. This is
2898 * used for opening a file from a quickfix window and not from a location
2899 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002900 */
2901 static void
2902qf_goto_win_with_qfl_file(int qf_fnum)
2903{
2904 win_T *win;
2905 win_T *altwin;
2906
2907 win = curwin;
2908 altwin = NULL;
2909 for (;;)
2910 {
2911 if (win->w_buffer->b_fnum == qf_fnum)
2912 break;
2913 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002914 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002915 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002916 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002917
2918 if (IS_QF_WINDOW(win))
2919 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002920 // Didn't find it, go to the window before the quickfix
2921 // window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002922 if (altwin != NULL)
2923 win = altwin;
2924 else if (curwin->w_prev != NULL)
2925 win = curwin->w_prev;
2926 else
2927 win = curwin->w_next;
2928 break;
2929 }
2930
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002931 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02002932 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002933 altwin = win;
2934 }
2935
2936 win_goto(win);
2937}
2938
2939/*
2940 * Find a suitable window for opening a file (qf_fnum) from the
2941 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01002942 * window, jump to it. Otherwise open a new window to display the file. If
2943 * 'newwin' is TRUE, then always open a new window. This is called from either
2944 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002945 */
2946 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002947qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002948{
2949 win_T *usable_win_ptr = NULL;
2950 int usable_win;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002951 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002952 win_T *win;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002953
2954 usable_win = 0;
2955
Bram Moolenaarb2443732018-11-11 22:50:27 +01002956 // If opening a new window, then don't use the location list referred by
2957 // the current window. Otherwise two windows will refer to the same
2958 // location list.
2959 if (!newwin)
2960 ll_ref = curwin->w_llist_ref;
2961
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002962 if (ll_ref != NULL)
2963 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002964 // Find a non-quickfix window with this location list
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002965 usable_win_ptr = qf_find_win_with_loclist(ll_ref);
2966 if (usable_win_ptr != NULL)
2967 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002968 }
2969
2970 if (!usable_win)
2971 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002972 // Locate a window showing a normal buffer
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002973 win = qf_find_win_with_normal_buf();
2974 if (win != NULL)
2975 usable_win = 1;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002976 }
2977
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002978 // If no usable window is found and 'switchbuf' contains "usetab"
2979 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002980 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002981 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002982
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002983 // If there is only one window and it is the quickfix window, create a
2984 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01002985 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002986 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002987 if (qf_open_new_file_win(ll_ref) != OK)
2988 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002989 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002990 }
2991 else
2992 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002993 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002994 qf_goto_win_with_ll_file(usable_win_ptr, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002995 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002996 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002997 }
2998
2999 return OK;
3000}
3001
3002/*
3003 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003004 * Returns OK if successfully edited the file, FAIL on failing to open the
3005 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3006 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003007 */
3008 static int
3009qf_jump_edit_buffer(
3010 qf_info_T *qi,
3011 qfline_T *qf_ptr,
3012 int forceit,
3013 win_T *oldwin,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003014 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003015{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003016 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003017 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003018 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003019 int old_qf_curlist = qi->qf_curlist;
3020 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003021
3022 if (qf_ptr->qf_type == 1)
3023 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003024 // Open help file (do_ecmd() will set b_help flag, readfile() will
3025 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003026 if (!can_abandon(curbuf, forceit))
3027 {
3028 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003029 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003030 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003031
3032 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3033 ECMD_HIDE + ECMD_SET_HELP,
3034 oldwin == curwin ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003035 }
3036 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003037 retval = buflist_getfile(qf_ptr->qf_fnum,
3038 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003039
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003040 // If a location list, check whether the associated window is still
3041 // present.
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003042 if (qfl_type == QFLT_LOCATION && !win_valid_any_tab(oldwin))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003043 {
3044 EMSG(_("E924: Current window was closed"));
3045 *opened_window = FALSE;
3046 return NOTDONE;
3047 }
3048
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003049 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003050 {
3051 EMSG(_("E925: Current quickfix was changed"));
3052 return NOTDONE;
3053 }
3054
3055 if (old_qf_curlist != qi->qf_curlist
3056 || !is_qf_entry_present(qfl, qf_ptr))
3057 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003058 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003059 EMSG(_("E925: Current quickfix was changed"));
3060 else
3061 EMSG(_(e_loc_list_changed));
3062 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003063 }
3064
3065 return retval;
3066}
3067
3068/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003069 * Go to the error line in the current file using either line/column number or
3070 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003071 */
3072 static void
3073qf_jump_goto_line(
3074 linenr_T qf_lnum,
3075 int qf_col,
3076 char_u qf_viscol,
3077 char_u *qf_pattern)
3078{
3079 linenr_T i;
3080 char_u *line;
3081 colnr_T screen_col;
3082 colnr_T char_col;
3083
3084 if (qf_pattern == NULL)
3085 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003086 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003087 i = qf_lnum;
3088 if (i > 0)
3089 {
3090 if (i > curbuf->b_ml.ml_line_count)
3091 i = curbuf->b_ml.ml_line_count;
3092 curwin->w_cursor.lnum = i;
3093 }
3094 if (qf_col > 0)
3095 {
3096 curwin->w_cursor.col = qf_col - 1;
3097#ifdef FEAT_VIRTUALEDIT
3098 curwin->w_cursor.coladd = 0;
3099#endif
3100 if (qf_viscol == TRUE)
3101 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003102 // Check each character from the beginning of the error
3103 // line up to the error column. For each tab character
3104 // found, reduce the error column value by the length of
3105 // a tab character.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003106 line = ml_get_curline();
3107 screen_col = 0;
3108 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
3109 {
3110 if (*line == NUL)
3111 break;
3112 if (*line++ == '\t')
3113 {
3114 curwin->w_cursor.col -= 7 - (screen_col % 8);
3115 screen_col += 8 - (screen_col % 8);
3116 }
3117 else
3118 ++screen_col;
3119 }
3120 }
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003121 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003122 check_cursor();
3123 }
3124 else
3125 beginline(BL_WHITE | BL_FIX);
3126 }
3127 else
3128 {
3129 pos_T save_cursor;
3130
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003131 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003132 save_cursor = curwin->w_cursor;
3133 curwin->w_cursor.lnum = 0;
3134 if (!do_search(NULL, '/', qf_pattern, (long)1,
3135 SEARCH_KEEP, NULL, NULL))
3136 curwin->w_cursor = save_cursor;
3137 }
3138}
3139
3140/*
3141 * Display quickfix list index and size message
3142 */
3143 static void
3144qf_jump_print_msg(
3145 qf_info_T *qi,
3146 int qf_index,
3147 qfline_T *qf_ptr,
3148 buf_T *old_curbuf,
3149 linenr_T old_lnum)
3150{
3151 linenr_T i;
3152 int len;
3153
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003154 // Update the screen before showing the message, unless the screen
3155 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003156 if (!msg_scrolled)
3157 update_topline_redraw();
3158 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
3159 qi->qf_lists[qi->qf_curlist].qf_count,
3160 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3161 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003162 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003163 len = (int)STRLEN(IObuff);
3164 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3165
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003166 // Output the message. Overwrite to avoid scrolling when the 'O'
3167 // flag is present in 'shortmess'; But when not jumping, print the
3168 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003169 i = msg_scroll;
3170 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3171 msg_scroll = TRUE;
3172 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3173 msg_scroll = FALSE;
3174 msg_attr_keep(IObuff, 0, TRUE);
3175 msg_scroll = i;
3176}
3177
3178/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003179 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003180 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3181 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003182 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3183 * able to jump/open a window. Returns NOTDONE if a file is not associated
3184 * with the entry.
3185 */
3186 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003187qf_jump_open_window(
3188 qf_info_T *qi,
3189 qfline_T *qf_ptr,
3190 int newwin,
3191 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003192{
3193 // For ":helpgrep" find a help window or open one.
3194 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003195 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003196 return FAIL;
3197
3198 // If currently in the quickfix window, find another window to show the
3199 // file in.
3200 if (bt_quickfix(curbuf) && !*opened_window)
3201 {
3202 // If there is no file specified, we don't know where to go.
3203 // But do advance, otherwise ":cn" gets stuck.
3204 if (qf_ptr->qf_fnum == 0)
3205 return NOTDONE;
3206
Bram Moolenaarb2443732018-11-11 22:50:27 +01003207 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3208 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003209 return FAIL;
3210 }
3211
3212 return OK;
3213}
3214
3215/*
3216 * Edit a selected file from the quickfix/location list and jump to a
3217 * particular line/column, adjust the folds and display a message about the
3218 * jump.
3219 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3220 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3221 * the file.
3222 */
3223 static int
3224qf_jump_to_buffer(
3225 qf_info_T *qi,
3226 int qf_index,
3227 qfline_T *qf_ptr,
3228 int forceit,
3229 win_T *oldwin,
3230 int *opened_window,
3231 int openfold,
3232 int print_message)
3233{
3234 buf_T *old_curbuf;
3235 linenr_T old_lnum;
3236 int retval = OK;
3237
3238 // If there is a file name, read the wanted file if needed, and check
3239 // autowrite etc.
3240 old_curbuf = curbuf;
3241 old_lnum = curwin->w_cursor.lnum;
3242
3243 if (qf_ptr->qf_fnum != 0)
3244 {
3245 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin,
3246 opened_window);
3247 if (retval != OK)
3248 return retval;
3249 }
3250
3251 // When not switched to another buffer, still need to set pc mark
3252 if (curbuf == old_curbuf)
3253 setpcmark();
3254
3255 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3256 qf_ptr->qf_pattern);
3257
3258#ifdef FEAT_FOLDING
3259 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3260 foldOpenCursor();
3261#endif
3262 if (print_message)
3263 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3264
3265 return retval;
3266}
3267
3268/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01003269 * Jump to a quickfix line.
3270 * If dir == FORWARD go "errornr" valid entries forward.
3271 * If dir == BACKWARD go "errornr" valid entries backward.
3272 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3273 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 * else if "errornr" is zero, redisplay the same line
Bram Moolenaarb2443732018-11-11 22:50:27 +01003275 * else go to entry "errornr".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 */
3277 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003278qf_jump(qf_info_T *qi,
3279 int dir,
3280 int errornr,
3281 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003283 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3284}
3285
3286/*
3287 * As qf_info().
3288 * If 'newwin' is TRUE, then open the file in a new window.
3289 */
3290 void
3291qf_jump_newwin(qf_info_T *qi,
3292 int dir,
3293 int errornr,
3294 int forceit,
3295 int newwin)
3296{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003297 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003298 qfline_T *qf_ptr;
3299 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003302 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003303 unsigned old_swb_flags = swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 int opened_window = FALSE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003305 win_T *oldwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 int print_message = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#ifdef FEAT_FOLDING
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003308 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#endif
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003310 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003312 if (qi == NULL)
3313 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003314
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003315 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
3317 EMSG(_(e_quickfix));
3318 return;
3319 }
3320
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003321 qfl = &qi->qf_lists[qi->qf_curlist];
3322
3323 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003325 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003327
3328 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3329 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003331 qf_ptr = old_qf_ptr;
3332 qf_index = old_qf_index;
3333 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003336 qfl->qf_index = qf_index;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003337 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003338 // No need to print the error message if it's visible in the error
3339 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 print_message = FALSE;
3341
Bram Moolenaarb2443732018-11-11 22:50:27 +01003342 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003343 if (retval == FAIL)
3344 goto failed;
3345 if (retval == NOTDONE)
3346 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003347
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003348 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, oldwin,
3349 &opened_window, old_KeyTyped, print_message);
3350 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003352 // Quickfix/location list is freed by an autocmd
3353 qi = NULL;
3354 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003357 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003360 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003361 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003363 // Couldn't open file, so put index back where it was. This could
3364 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 qf_ptr = old_qf_ptr;
3367 qf_index = old_qf_index;
3368 }
3369 }
3370theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003371 if (qi != NULL)
3372 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003373 qfl->qf_ptr = qf_ptr;
3374 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 if (p_swb != old_swb && opened_window)
3377 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003378 // Restore old 'switchbuf' value, but not when an autocommand or
3379 // modeline has changed the value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 if (p_swb == empty_option)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 p_swb = old_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003383 swb_flags = old_swb_flags;
3384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 else
3386 free_string_option(old_swb);
3387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388}
3389
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003390// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003391static int qfFileAttr;
3392static int qfSepAttr;
3393static int qfLineAttr;
3394
3395/*
3396 * Display information about a single entry from the quickfix/location list.
3397 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003398 * 'cursel' will be set to TRUE for the currently selected entry in the
3399 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003400 */
3401 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003402qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003403{
3404 char_u *fname;
3405 buf_T *buf;
3406 int filter_entry;
3407
3408 fname = NULL;
3409 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3410 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3411 (char *)qfp->qf_module);
3412 else {
3413 if (qfp->qf_fnum != 0
3414 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3415 {
3416 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003417 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003418 fname = gettail(fname);
3419 }
3420 if (fname == NULL)
3421 sprintf((char *)IObuff, "%2d", qf_idx);
3422 else
3423 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3424 qf_idx, (char *)fname);
3425 }
3426
3427 // Support for filtering entries using :filter /pat/ clist
3428 // Match against the module name, file name, search pattern and
3429 // text of the entry.
3430 filter_entry = TRUE;
3431 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3432 filter_entry &= message_filtered(qfp->qf_module);
3433 if (filter_entry && fname != NULL)
3434 filter_entry &= message_filtered(fname);
3435 if (filter_entry && qfp->qf_pattern != NULL)
3436 filter_entry &= message_filtered(qfp->qf_pattern);
3437 if (filter_entry)
3438 filter_entry &= message_filtered(qfp->qf_text);
3439 if (filter_entry)
3440 return;
3441
3442 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003443 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003444
3445 if (qfp->qf_lnum != 0)
3446 msg_puts_attr((char_u *)":", qfSepAttr);
3447 if (qfp->qf_lnum == 0)
3448 IObuff[0] = NUL;
3449 else if (qfp->qf_col == 0)
3450 sprintf((char *)IObuff, "%ld", qfp->qf_lnum);
3451 else
3452 sprintf((char *)IObuff, "%ld col %d",
3453 qfp->qf_lnum, qfp->qf_col);
3454 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3455 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
3456 msg_puts_attr(IObuff, qfLineAttr);
3457 msg_puts_attr((char_u *)":", qfSepAttr);
3458 if (qfp->qf_pattern != NULL)
3459 {
3460 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
3461 msg_puts(IObuff);
3462 msg_puts_attr((char_u *)":", qfSepAttr);
3463 }
3464 msg_puts((char_u *)" ");
3465
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003466 // Remove newlines and leading whitespace from the text. For an
3467 // unrecognized line keep the indent, the compiler may mark a word
3468 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003469 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3470 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3471 IObuff, IOSIZE);
3472 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003473 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003474}
3475
3476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003478 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 */
3480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003481qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003483 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003484 qfline_T *qfp;
3485 int i;
3486 int idx1 = 1;
3487 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003488 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003489 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003490 int all = eap->forceit; // if not :cl!, only show
3491 // recognised errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003492 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
Bram Moolenaar39665952018-08-15 20:59:48 +02003494 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003495 {
3496 qi = GET_LOC_LIST(curwin);
3497 if (qi == NULL)
3498 {
3499 EMSG(_(e_loclist));
3500 return;
3501 }
3502 }
3503
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003504 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 EMSG(_(e_quickfix));
3507 return;
3508 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003509 if (*arg == '+')
3510 {
3511 ++arg;
3512 plus = TRUE;
3513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3515 {
3516 EMSG(_(e_trailing));
3517 return;
3518 }
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003519 qfl = &qi->qf_lists[qi->qf_curlist];
Bram Moolenaare8fea072016-07-01 14:48:27 +02003520 if (plus)
3521 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003522 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003523 idx2 = i + idx1;
3524 idx1 = i;
3525 }
3526 else
3527 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003528 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003529 if (idx1 < 0)
3530 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3531 if (idx2 < 0)
3532 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003535 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003536 shorten_fnames(FALSE);
3537
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003538 // Get the attributes for the different quickfix highlight items. Note
3539 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003540 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3541 if (qfFileAttr == 0)
3542 qfFileAttr = HL_ATTR(HLF_D);
3543 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3544 if (qfSepAttr == 0)
3545 qfSepAttr = HL_ATTR(HLF_D);
3546 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3547 if (qfLineAttr == 0)
3548 qfLineAttr = HL_ATTR(HLF_N);
3549
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003550 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 all = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003552 qfp = qfl->qf_start;
3553 for (i = 1; !got_int && i <= qfl->qf_count; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
3555 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3556 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003557 if (got_int)
3558 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003559
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003560 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003562
3563 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003564 if (qfp == NULL)
3565 break;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003566 ++i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 ui_breakcheck();
3568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569}
3570
3571/*
3572 * Remove newlines and leading whitespace from an error message.
3573 * Put the result in "buf[bufsize]".
3574 */
3575 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003576qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577{
3578 int i;
3579 char_u *p = text;
3580
3581 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3582 {
3583 if (*p == '\n')
3584 {
3585 buf[i] = ' ';
3586 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003587 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 break;
3589 }
3590 else
3591 buf[i] = *p++;
3592 }
3593 buf[i] = NUL;
3594}
3595
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003596/*
3597 * Display information (list number, list size and the title) about a
3598 * quickfix/location list.
3599 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003600 static void
3601qf_msg(qf_info_T *qi, int which, char *lead)
3602{
3603 char *title = (char *)qi->qf_lists[which].qf_title;
3604 int count = qi->qf_lists[which].qf_count;
3605 char_u buf[IOSIZE];
3606
3607 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3608 lead,
3609 which + 1,
3610 qi->qf_listcount,
3611 count);
3612
3613 if (title != NULL)
3614 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003615 size_t len = STRLEN(buf);
3616
3617 if (len < 34)
3618 {
3619 vim_memset(buf + len, ' ', 34 - len);
3620 buf[34] = NUL;
3621 }
3622 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003623 }
3624 trunc_string(buf, buf, Columns - 1, IOSIZE);
3625 msg(buf);
3626}
3627
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628/*
3629 * ":colder [count]": Up in the quickfix stack.
3630 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003631 * ":lolder [count]": Up in the location list stack.
3632 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 */
3634 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003635qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003637 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 int count;
3639
Bram Moolenaar39665952018-08-15 20:59:48 +02003640 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003641 {
3642 qi = GET_LOC_LIST(curwin);
3643 if (qi == NULL)
3644 {
3645 EMSG(_(e_loclist));
3646 return;
3647 }
3648 }
3649
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 if (eap->addr_count != 0)
3651 count = eap->line2;
3652 else
3653 count = 1;
3654 while (count--)
3655 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003656 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003658 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 EMSG(_("E380: At bottom of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003661 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003663 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665 else
3666 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003667 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
3669 EMSG(_("E381: At top of quickfix stack"));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003670 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003672 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 }
3674 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003675 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003676 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677}
3678
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003679/*
3680 * Display the information about all the quickfix/location lists in the stack
3681 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003682 void
3683qf_history(exarg_T *eap)
3684{
3685 qf_info_T *qi = &ql_info;
3686 int i;
3687
Bram Moolenaar39665952018-08-15 20:59:48 +02003688 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003689 qi = GET_LOC_LIST(curwin);
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003690 if (qf_stack_empty(qi) || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003691 MSG(_("No entries"));
3692 else
3693 for (i = 0; i < qi->qf_listcount; ++i)
3694 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3695}
3696
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003698 * Free all the entries in the error list "idx". Note that other information
3699 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 */
3701 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003702qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003704 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003705 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003706 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003708 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003710 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003711 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003712 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003713 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003714 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003715 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003716 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003717 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003718 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003719 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003720 // Somehow qf_count may have an incorrect value, set it to 1
3721 // to avoid crashing when it's wrong.
3722 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003723 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003724 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003725 qfl->qf_start = qfpnext;
3726 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003728
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003729 qfl->qf_index = 0;
3730 qfl->qf_start = NULL;
3731 qfl->qf_last = NULL;
3732 qfl->qf_ptr = NULL;
3733 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003734
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003735 qf_clean_dir_stack(&qfl->qf_dir_stack);
3736 qfl->qf_directory = NULL;
3737 qf_clean_dir_stack(&qfl->qf_file_stack);
3738 qfl->qf_currfile = NULL;
3739 qfl->qf_multiline = FALSE;
3740 qfl->qf_multiignore = FALSE;
3741 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742}
3743
3744/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003745 * Free error list "idx". Frees all the entries in the quickfix list,
3746 * associated context information and the title.
3747 */
3748 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003749qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003750{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003751 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003752
Bram Moolenaard23a8232018-02-10 18:45:26 +01003753 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003754 free_tv(qfl->qf_ctx);
3755 qfl->qf_ctx = NULL;
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003756 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003757 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003758}
3759
3760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 * qf_mark_adjust: adjust marks
3762 */
3763 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003764qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003765 win_T *wp,
3766 linenr_T line1,
3767 linenr_T line2,
3768 long amount,
3769 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003771 int i;
3772 qfline_T *qfp;
3773 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003774 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003775 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003776 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
Bram Moolenaarc1542742016-07-20 21:44:37 +02003778 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003779 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003780 if (wp != NULL)
3781 {
3782 if (wp->w_llist == NULL)
3783 return;
3784 qi = wp->w_llist;
3785 }
3786
3787 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003788 if (!qf_list_empty(qi, idx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003789 for (i = 0, qfp = qi->qf_lists[idx].qf_start;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003790 i < qi->qf_lists[idx].qf_count && qfp != NULL;
3791 ++i, qfp = qfp->qf_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (qfp->qf_fnum == curbuf->b_fnum)
3793 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003794 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3796 {
3797 if (amount == MAXLNUM)
3798 qfp->qf_cleared = TRUE;
3799 else
3800 qfp->qf_lnum += amount;
3801 }
3802 else if (amount_after && qfp->qf_lnum > line2)
3803 qfp->qf_lnum += amount_after;
3804 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003805
3806 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003807 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808}
3809
3810/*
3811 * Make a nice message out of the error character and the error number:
3812 * char number message
3813 * e or E 0 " error"
3814 * w or W 0 " warning"
3815 * i or I 0 " info"
3816 * 0 0 ""
3817 * other 0 " c"
3818 * e or E n " error n"
3819 * w or W n " warning n"
3820 * i or I n " info n"
3821 * 0 n " error n"
3822 * other n " c n"
3823 * 1 x "" :helpgrep
3824 */
3825 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003826qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827{
3828 static char_u buf[20];
3829 static char_u cc[3];
3830 char_u *p;
3831
3832 if (c == 'W' || c == 'w')
3833 p = (char_u *)" warning";
3834 else if (c == 'I' || c == 'i')
3835 p = (char_u *)" info";
3836 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3837 p = (char_u *)" error";
3838 else if (c == 0 || c == 1)
3839 p = (char_u *)"";
3840 else
3841 {
3842 cc[0] = ' ';
3843 cc[1] = c;
3844 cc[2] = NUL;
3845 p = cc;
3846 }
3847
3848 if (nr <= 0)
3849 return p;
3850
3851 sprintf((char *)buf, "%s %3d", (char *)p, nr);
3852 return buf;
3853}
3854
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003856 * When "split" is FALSE: Open the entry/result under the cursor.
3857 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
3858 */
3859 void
3860qf_view_result(int split)
3861{
3862 qf_info_T *qi = &ql_info;
3863
3864 if (!bt_quickfix(curbuf))
3865 return;
3866
3867 if (IS_LL_WINDOW(curwin))
3868 qi = GET_LOC_LIST(curwin);
3869
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003870 if (qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003871 {
3872 EMSG(_(e_quickfix));
3873 return;
3874 }
3875
3876 if (split)
3877 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01003878 // Open the selected entry in a new window
3879 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
3880 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003881 return;
3882 }
3883
3884 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
3885}
3886
3887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 * ":cwindow": open the quickfix window if we have errors to display,
3889 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003890 * ":lwindow": open the location list window if we have locations to display,
3891 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 */
3893 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003894ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003896 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003897 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 win_T *win;
3899
Bram Moolenaar39665952018-08-15 20:59:48 +02003900 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003901 {
3902 qi = GET_LOC_LIST(curwin);
3903 if (qi == NULL)
3904 return;
3905 }
3906
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003907 qfl = &qi->qf_lists[qi->qf_curlist];
3908
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003909 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003910 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003912 // If a quickfix window is open but we have no errors to display,
3913 // close the window. If a quickfix window is not open, then open
3914 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003915 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02003916 || qfl->qf_nonevalid
Bram Moolenaar019dfe62018-10-07 14:38:49 +02003917 || qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 {
3919 if (win != NULL)
3920 ex_cclose(eap);
3921 }
3922 else if (win == NULL)
3923 ex_copen(eap);
3924}
3925
3926/*
3927 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003928 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003931ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003933 win_T *win = NULL;
3934 qf_info_T *qi = &ql_info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
Bram Moolenaar39665952018-08-15 20:59:48 +02003936 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003937 {
3938 qi = GET_LOC_LIST(curwin);
3939 if (qi == NULL)
3940 return;
3941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003943 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003944 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 if (win != NULL)
3946 win_close(win, FALSE);
3947}
3948
3949/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003950 * Set "w:quickfix_title" if "qi" has a title.
3951 */
3952 static void
3953qf_set_title_var(qf_list_T *qfl)
3954{
3955 if (qfl->qf_title != NULL)
3956 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
3957}
3958
3959/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02003960 * Goto a quickfix or location list window (if present).
3961 * Returns OK if the window is found, FAIL otherwise.
3962 */
3963 static int
3964qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
3965{
3966 win_T *win;
3967
3968 win = qf_find_win(qi);
3969 if (win == NULL)
3970 return FAIL;
3971
3972 win_goto(win);
3973 if (resize)
3974 {
3975 if (vertsplit)
3976 {
3977 if (sz != win->w_width)
3978 win_setwidth(sz);
3979 }
3980 else if (sz != win->w_height)
3981 win_setheight(sz);
3982 }
3983
3984 return OK;
3985}
3986
3987/*
3988 * Open a new quickfix or location list window, load the quickfix buffer and
3989 * set the appropriate options for the window.
3990 * Returns FAIL if the window could not be opened.
3991 */
3992 static int
3993qf_open_new_cwindow(qf_info_T *qi, int height)
3994{
3995 buf_T *qf_buf;
3996 win_T *oldwin = curwin;
3997 tabpage_T *prevtab = curtab;
3998 int flags = 0;
3999 win_T *win;
4000
4001 qf_buf = qf_find_buf(qi);
4002
4003 // The current window becomes the previous window afterwards.
4004 win = curwin;
4005
4006 if (IS_QF_STACK(qi) && cmdmod.split == 0)
4007 // Create the new quickfix window at the very bottom, except when
4008 // :belowright or :aboveleft is used.
4009 win_goto(lastwin);
4010 // Default is to open the window below the current window
4011 if (cmdmod.split == 0)
4012 flags = WSP_BELOW;
4013 flags |= WSP_NEWLOC;
4014 if (win_split(height, flags) == FAIL)
4015 return FAIL; // not enough room for window
4016 RESET_BINDING(curwin);
4017
4018 if (IS_LL_STACK(qi))
4019 {
4020 // For the location list window, create a reference to the
4021 // location list from the window 'win'.
4022 curwin->w_llist_ref = win->w_llist;
4023 win->w_llist->qf_refcount++;
4024 }
4025
4026 if (oldwin != curwin)
4027 oldwin = NULL; // don't store info when in another window
4028 if (qf_buf != NULL)
4029 {
4030 // Use the existing quickfix buffer
4031 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
4032 ECMD_HIDE + ECMD_OLDBUF, oldwin);
4033 }
4034 else
4035 {
4036 // Create a new quickfix buffer
4037 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin);
4038
4039 // switch off 'swapfile'
4040 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4041 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4042 OPT_LOCAL);
4043 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
4044 RESET_BINDING(curwin);
4045#ifdef FEAT_DIFF
4046 curwin->w_p_diff = FALSE;
4047#endif
4048#ifdef FEAT_FOLDING
4049 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4050 OPT_LOCAL);
4051#endif
4052 }
4053
4054 // Only set the height when still in the same tab page and there is no
4055 // window to the side.
4056 if (curtab == prevtab && curwin->w_width == Columns)
4057 win_setheight(height);
4058 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4059 if (win_valid(win))
4060 prevwin = win;
4061
4062 return OK;
4063}
4064
4065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004067 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 */
4069 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004070ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004072 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004073 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004075 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004076 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
Bram Moolenaar39665952018-08-15 20:59:48 +02004078 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004079 {
4080 qi = GET_LOC_LIST(curwin);
4081 if (qi == NULL)
4082 {
4083 EMSG(_(e_loclist));
4084 return;
4085 }
4086 }
4087
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004088 incr_quickfix_busy();
4089
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 if (eap->addr_count != 0)
4091 height = eap->line2;
4092 else
4093 height = QF_WINHEIGHT;
4094
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004095 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096#ifdef FEAT_GUI
4097 need_mouse_correct = TRUE;
4098#endif
4099
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004100 // Find an existing quickfix window, or open a new one.
4101 if (cmdmod.tab == 0)
4102 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
4103 cmdmod.split & WSP_VERT);
4104 if (status == FAIL)
4105 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004106 {
4107 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004108 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004111 qfl = &qi->qf_lists[qi->qf_curlist];
4112 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004113 // Save the current index here, as updating the quickfix buffer may free
4114 // the quickfix list
4115 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004116
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004117 // Fill the buffer with the quickfix list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004118 qf_fill_buffer(qi, curbuf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004120 decr_quickfix_busy();
4121
4122 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 curwin->w_cursor.col = 0;
4124 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004125 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126}
4127
4128/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004129 * Move the cursor in the quickfix window to "lnum".
4130 */
4131 static void
4132qf_win_goto(win_T *win, linenr_T lnum)
4133{
4134 win_T *old_curwin = curwin;
4135
4136 curwin = win;
4137 curbuf = win->w_buffer;
4138 curwin->w_cursor.lnum = lnum;
4139 curwin->w_cursor.col = 0;
4140#ifdef FEAT_VIRTUALEDIT
4141 curwin->w_cursor.coladd = 0;
4142#endif
4143 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004144 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004145 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004146 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004147 curwin = old_curwin;
4148 curbuf = curwin->w_buffer;
4149}
4150
4151/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004152 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004153 */
4154 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004155ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004156{
Bram Moolenaar537ef082016-07-09 17:56:19 +02004157 qf_info_T *qi = &ql_info;
4158 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004159
Bram Moolenaar39665952018-08-15 20:59:48 +02004160 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar537ef082016-07-09 17:56:19 +02004161 {
4162 qi = GET_LOC_LIST(curwin);
4163 if (qi == NULL)
4164 {
4165 EMSG(_(e_loclist));
4166 return;
4167 }
4168 }
4169
4170 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004171 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4172 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4173}
4174
4175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 * Return the number of the current entry (line number in the quickfix
4177 * window).
4178 */
4179 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004180qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004182 qf_info_T *qi = &ql_info;
4183
4184 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004185 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004186 qi = wp->w_llist_ref;
4187
4188 return qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189}
4190
4191/*
4192 * Update the cursor position in the quickfix window to the current error.
4193 * Return TRUE if there is a quickfix window.
4194 */
4195 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004196qf_win_pos_update(
4197 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004198 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199{
4200 win_T *win;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004201 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004203 // Put the cursor on the current error in the quickfix window, so that
4204 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004205 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 if (win != NULL
4207 && qf_index <= win->w_buffer->b_ml.ml_line_count
4208 && old_qf_index != qf_index)
4209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 if (qf_index > old_qf_index)
4211 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004212 win->w_redraw_top = old_qf_index;
4213 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215 else
4216 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004217 win->w_redraw_top = qf_index;
4218 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004220 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222 return win != NULL;
4223}
4224
4225/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004226 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004227 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004228 */
4229 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004230is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004231{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004232 // A window displaying the quickfix buffer will have the w_llist_ref field
4233 // set to NULL.
4234 // A window displaying a location list buffer will have the w_llist_ref
4235 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004236 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004237 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4238 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004239 return TRUE;
4240
4241 return FALSE;
4242}
4243
4244/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004245 * Find a window displaying the quickfix/location stack 'qi'
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004246 * Only searches in the current tabpage.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004247 */
4248 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004249qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004250{
4251 win_T *win;
4252
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004253 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004254 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004255 return win;
4256 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004257}
4258
4259/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004260 * Find a quickfix buffer.
4261 * Searches in windows opened in all the tabs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 */
4263 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004264qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004266 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004267 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268
Bram Moolenaar9c102382006-05-03 21:26:49 +00004269 FOR_ALL_TAB_WINDOWS(tp, win)
4270 if (is_qf_win(win, qi))
4271 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004272
Bram Moolenaar9c102382006-05-03 21:26:49 +00004273 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274}
4275
4276/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004277 * Update the w:quickfix_title variable in the quickfix/location list window
4278 */
4279 static void
4280qf_update_win_titlevar(qf_info_T *qi)
4281{
4282 win_T *win;
4283 win_T *curwin_save;
4284
4285 if ((win = qf_find_win(qi)) != NULL)
4286 {
4287 curwin_save = curwin;
4288 curwin = win;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004289 qf_set_title_var(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004290 curwin = curwin_save;
4291 }
4292}
4293
4294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 * Find the quickfix buffer. If it exists, update the contents.
4296 */
4297 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004298qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299{
4300 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004301 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004304 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004305 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 if (buf != NULL)
4307 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004308 linenr_T old_line_count = buf->b_ml.ml_line_count;
4309
4310 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004311 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004312 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
Bram Moolenaard823fa92016-08-12 16:29:27 +02004314 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004315
Bram Moolenaar864293a2016-06-02 13:40:04 +02004316 qf_fill_buffer(qi, buf, old_last);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004317 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004318
Bram Moolenaar864293a2016-06-02 13:40:04 +02004319 if (old_last == NULL)
4320 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004321 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004322
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004323 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004324 aucmd_restbuf(&aco);
4325 }
4326
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004327 // Only redraw when added lines are visible. This avoids flickering
4328 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004329 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4330 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332}
4333
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004334/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004335 * Add an error line to the quickfix buffer.
4336 */
4337 static int
4338qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
4339{
4340 int len;
4341 buf_T *errbuf;
4342
4343 if (qfp->qf_module != NULL)
4344 {
4345 STRCPY(IObuff, qfp->qf_module);
4346 len = (int)STRLEN(IObuff);
4347 }
4348 else if (qfp->qf_fnum != 0
4349 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4350 && errbuf->b_fname != NULL)
4351 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004352 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004353 STRCPY(IObuff, gettail(errbuf->b_fname));
4354 else
4355 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004356 // shorten the file name if not done already
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004357 if (errbuf->b_sfname == NULL
4358 || mch_isFullName(errbuf->b_sfname))
4359 {
4360 if (*dirname == NUL)
4361 mch_dirname(dirname, MAXPATHL);
4362 shorten_buf_fname(errbuf, dirname, FALSE);
4363 }
4364 STRCPY(IObuff, errbuf->b_fname);
4365 }
4366 len = (int)STRLEN(IObuff);
4367 }
4368 else
4369 len = 0;
4370 IObuff[len++] = '|';
4371
4372 if (qfp->qf_lnum > 0)
4373 {
4374 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
4375 len += (int)STRLEN(IObuff + len);
4376
4377 if (qfp->qf_col > 0)
4378 {
4379 sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
4380 len += (int)STRLEN(IObuff + len);
4381 }
4382
4383 sprintf((char *)IObuff + len, "%s",
4384 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
4385 len += (int)STRLEN(IObuff + len);
4386 }
4387 else if (qfp->qf_pattern != NULL)
4388 {
4389 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4390 len += (int)STRLEN(IObuff + len);
4391 }
4392 IObuff[len++] = '|';
4393 IObuff[len++] = ' ';
4394
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004395 // Remove newlines and leading whitespace from the text.
4396 // For an unrecognized line keep the indent, the compiler may
4397 // mark a word with ^^^^.
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004398 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4399 IObuff + len, IOSIZE - len);
4400
4401 if (ml_append_buf(buf, lnum, IObuff,
4402 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4403 return FAIL;
4404
4405 return OK;
4406}
4407
4408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 * Fill current buffer with quickfix errors, replacing any previous contents.
4410 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004411 * If "old_last" is not NULL append the items after this one.
4412 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4413 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 */
4415 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004416qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004418 linenr_T lnum;
4419 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004420 int old_KeyTyped = KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
Bram Moolenaar864293a2016-06-02 13:40:04 +02004422 if (old_last == NULL)
4423 {
4424 if (buf != curbuf)
4425 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004426 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004427 return;
4428 }
4429
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004430 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004431 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
4432 (void)ml_delete((linenr_T)1, FALSE);
4433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004435 // Check if there is anything to display
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004436 if (!qf_stack_empty(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004438 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist];
4439 char_u dirname[MAXPATHL];
Bram Moolenaara796d462018-05-01 14:30:36 +02004440
4441 *dirname = NUL;
4442
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004443 // Add one line for each error
Bram Moolenaar864293a2016-06-02 13:40:04 +02004444 if (old_last == NULL)
4445 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004446 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004447 lnum = 0;
4448 }
4449 else
4450 {
4451 qfp = old_last->qf_next;
4452 lnum = buf->b_ml.ml_line_count;
4453 }
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004454 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 {
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004456 if (qf_buf_add_line(buf, lnum, qfp, dirname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004458
Bram Moolenaar864293a2016-06-02 13:40:04 +02004459 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004461 if (qfp == NULL)
4462 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004464
4465 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004466 // Delete the empty line which is now at the end
Bram Moolenaar864293a2016-06-02 13:40:04 +02004467 (void)ml_delete(lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004470 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 check_lnums(TRUE);
4472
Bram Moolenaar864293a2016-06-02 13:40:04 +02004473 if (old_last == NULL)
4474 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004475 // Set the 'filetype' to "qf" each time after filling the buffer.
4476 // This resembles reading a file into a buffer, it's more logical when
4477 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004478 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004479 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4480 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004482 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004483 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004485 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004487 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004488 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004489
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004490 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004491 redraw_curbuf_later(NOT_VALID);
4492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004494 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 KeyTyped = old_KeyTyped;
4496}
4497
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004498/*
4499 * For every change made to the quickfix list, update the changed tick.
4500 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004501 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004502qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004503{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004504 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004505}
4506
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004508 * Return the quickfix/location list number with the given identifier.
4509 * Returns -1 if list is not found.
4510 */
4511 static int
4512qf_id2nr(qf_info_T *qi, int_u qfid)
4513{
4514 int qf_idx;
4515
4516 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4517 if (qi->qf_lists[qf_idx].qf_id == qfid)
4518 return qf_idx;
4519 return INVALID_QFIDX;
4520}
4521
4522/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004523 * If the current list is not "save_qfid" and we can find the list with that ID
4524 * then make it the current list.
4525 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004526 * Returns OK if successfully restored the list. Returns FAIL if the list with
4527 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004528 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004529 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004530qf_restore_list(qf_info_T *qi, int_u save_qfid)
4531{
4532 int curlist;
4533
4534 if (qi->qf_lists[qi->qf_curlist].qf_id != save_qfid)
4535 {
4536 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004537 if (curlist < 0)
4538 // list is not present
4539 return FAIL;
4540 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004541 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004542 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004543}
4544
4545/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004546 * Jump to the first entry if there is one.
4547 */
4548 static void
4549qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4550{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004551 if (qf_restore_list(qi, save_qfid) == FAIL)
4552 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004553
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004554 // Autocommands might have cleared the list, check for that.
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004555 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004556 qf_jump(qi, 0, 0, forceit);
4557}
4558
4559/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004560 * Return TRUE when using ":vimgrep" for ":grep".
4561 */
4562 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004563grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004564{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004565 return ((cmdidx == CMD_grep
4566 || cmdidx == CMD_lgrep
4567 || cmdidx == CMD_grepadd
4568 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004569 && STRCMP("internal",
4570 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4571}
4572
4573/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004574 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004576 static char_u *
4577make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004579 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004580 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004581 case CMD_make: return (char_u *)"make";
4582 case CMD_lmake: return (char_u *)"lmake";
4583 case CMD_grep: return (char_u *)"grep";
4584 case CMD_lgrep: return (char_u *)"lgrep";
4585 case CMD_grepadd: return (char_u *)"grepadd";
4586 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4587 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589}
4590
4591/*
4592 * Return the name for the errorfile, in allocated memory.
4593 * Find a new unique name when 'makeef' contains "##".
4594 * Returns NULL for error.
4595 */
4596 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004597get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598{
4599 char_u *p;
4600 char_u *name;
4601 static int start = -1;
4602 static int off = 0;
4603#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004604 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605#endif
4606
4607 if (*p_mef == NUL)
4608 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004609 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 if (name == NULL)
4611 EMSG(_(e_notmp));
4612 return name;
4613 }
4614
4615 for (p = p_mef; *p; ++p)
4616 if (p[0] == '#' && p[1] == '#')
4617 break;
4618
4619 if (*p == NUL)
4620 return vim_strsave(p_mef);
4621
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004622 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 for (;;)
4624 {
4625 if (start == -1)
4626 start = mch_get_pid();
4627 else
4628 off += 19;
4629
4630 name = alloc((unsigned)STRLEN(p_mef) + 30);
4631 if (name == NULL)
4632 break;
4633 STRCPY(name, p_mef);
4634 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4635 STRCAT(name, p + 2);
4636 if (mch_getperm(name) < 0
4637#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004638 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 && mch_lstat((char *)name, &sb) < 0
4640#endif
4641 )
4642 break;
4643 vim_free(name);
4644 }
4645 return name;
4646}
4647
4648/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004649 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4650 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4651 */
4652 static char_u *
4653make_get_fullcmd(char_u *makecmd, char_u *fname)
4654{
4655 char_u *cmd;
4656 unsigned len;
4657
4658 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4659 if (*p_sp != NUL)
4660 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4661 cmd = alloc(len);
4662 if (cmd == NULL)
4663 return NULL;
4664 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4665 (char *)p_shq);
4666
4667 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4668 if (*p_sp != NUL)
4669 append_redir(cmd, len, p_sp, fname);
4670
4671 // Display the fully formed command. Output a newline if there's something
4672 // else than the :make command that was typed (in which case the cursor is
4673 // in column 0).
4674 if (msg_col == 0)
4675 msg_didout = FALSE;
4676 msg_start();
4677 MSG_PUTS(":!");
4678 msg_outtrans(cmd); // show what we are doing
4679
4680 return cmd;
4681}
4682
4683/*
4684 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4685 */
4686 void
4687ex_make(exarg_T *eap)
4688{
4689 char_u *fname;
4690 char_u *cmd;
4691 char_u *enc = NULL;
4692 win_T *wp = NULL;
4693 qf_info_T *qi = &ql_info;
4694 int res;
4695 char_u *au_name = NULL;
4696 int_u save_qfid;
4697
4698 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4699 if (grep_internal(eap->cmdidx))
4700 {
4701 ex_vimgrep(eap);
4702 return;
4703 }
4704
4705 au_name = make_get_auname(eap->cmdidx);
4706 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4707 curbuf->b_fname, TRUE, curbuf))
4708 {
4709#ifdef FEAT_EVAL
4710 if (aborting())
4711 return;
4712#endif
4713 }
4714#ifdef FEAT_MBYTE
4715 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
4716#endif
4717
4718 if (is_loclist_cmd(eap->cmdidx))
4719 wp = curwin;
4720
4721 autowrite_all();
4722 fname = get_mef_name();
4723 if (fname == NULL)
4724 return;
4725 mch_remove(fname); // in case it's not unique
4726
4727 cmd = make_get_fullcmd(eap->arg, fname);
4728 if (cmd == NULL)
4729 return;
4730
4731 // let the shell know if we are redirecting output or not
4732 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
4733
4734#ifdef AMIGA
4735 out_flush();
4736 // read window status report and redraw before message
4737 (void)char_avail();
4738#endif
4739
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004740 incr_quickfix_busy();
4741
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004742 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
4743 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
4744 (eap->cmdidx != CMD_grepadd
4745 && eap->cmdidx != CMD_lgrepadd),
4746 qf_cmdtitle(*eap->cmdlinep), enc);
4747 if (wp != NULL)
4748 {
4749 qi = GET_LOC_LIST(wp);
4750 if (qi == NULL)
4751 goto cleanup;
4752 }
4753 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004754 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004755
4756 // Remember the current quickfix list identifier, so that we can
4757 // check for autocommands changing the current quickfix list.
4758 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4759 if (au_name != NULL)
4760 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
4761 curbuf->b_fname, TRUE, curbuf);
4762 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
4763 // display the first error
4764 qf_jump_first(qi, save_qfid, FALSE);
4765
4766cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004767 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004768 mch_remove(fname);
4769 vim_free(fname);
4770 vim_free(cmd);
4771}
4772
4773/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004774 * Returns the number of valid entries in the current quickfix/location list.
4775 */
4776 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004777qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004778{
4779 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004780 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004781 qfline_T *qfp;
4782 int i, sz = 0;
4783 int prev_fnum = 0;
4784
Bram Moolenaar39665952018-08-15 20:59:48 +02004785 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004786 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004787 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004788 qi = GET_LOC_LIST(curwin);
4789 if (qi == NULL)
4790 return 0;
4791 }
4792
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004793 qfl = &qi->qf_lists[qi->qf_curlist];
4794 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004795 ++i, qfp = qfp->qf_next)
4796 {
4797 if (qfp->qf_valid)
4798 {
4799 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004800 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004801 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4802 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004803 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004804 sz++;
4805 prev_fnum = qfp->qf_fnum;
4806 }
4807 }
4808 }
4809
4810 return sz;
4811}
4812
4813/*
4814 * Returns the current index of the quickfix/location list.
4815 * Returns 0 if there is an error.
4816 */
4817 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004818qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004819{
4820 qf_info_T *qi = &ql_info;
4821
Bram Moolenaar39665952018-08-15 20:59:48 +02004822 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004823 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004824 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004825 qi = GET_LOC_LIST(curwin);
4826 if (qi == NULL)
4827 return 0;
4828 }
4829
4830 return qi->qf_lists[qi->qf_curlist].qf_index;
4831}
4832
4833/*
4834 * Returns the current index in the quickfix/location list (counting only valid
4835 * entries). If no valid entries are in the list, then returns 1.
4836 */
4837 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004838qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004839{
4840 qf_info_T *qi = &ql_info;
4841 qf_list_T *qfl;
4842 qfline_T *qfp;
4843 int i, eidx = 0;
4844 int prev_fnum = 0;
4845
Bram Moolenaar39665952018-08-15 20:59:48 +02004846 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004847 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004848 // Location list
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004849 qi = GET_LOC_LIST(curwin);
4850 if (qi == NULL)
4851 return 1;
4852 }
4853
4854 qfl = &qi->qf_lists[qi->qf_curlist];
4855 qfp = qfl->qf_start;
4856
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004857 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004858 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4859 return 1;
4860
4861 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
4862 {
4863 if (qfp->qf_valid)
4864 {
4865 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
4866 {
4867 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4868 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004869 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004870 eidx++;
4871 prev_fnum = qfp->qf_fnum;
4872 }
4873 }
4874 else
4875 eidx++;
4876 }
4877 }
4878
4879 return eidx ? eidx : 1;
4880}
4881
4882/*
4883 * Get the 'n'th valid error entry in the quickfix or location list.
4884 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
4885 * For :cdo and :ldo returns the 'n'th valid error entry.
4886 * For :cfdo and :lfdo returns the 'n'th valid file entry.
4887 */
4888 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004889qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004890{
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004891 qfline_T *qfp = qfl->qf_start;
4892 int i, eidx;
4893 int prev_fnum = 0;
4894
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004895 // check if the list has valid errors
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004896 if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
4897 return 1;
4898
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004899 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004900 i++, qfp = qfp->qf_next)
4901 {
4902 if (qfp->qf_valid)
4903 {
4904 if (fdo)
4905 {
4906 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
4907 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004908 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004909 eidx++;
4910 prev_fnum = qfp->qf_fnum;
4911 }
4912 }
4913 else
4914 eidx++;
4915 }
4916
4917 if (eidx == n)
4918 break;
4919 }
4920
4921 if (i <= qfl->qf_count)
4922 return i;
4923 else
4924 return 1;
4925}
4926
4927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004929 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004930 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 */
4932 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004933ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004935 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004936 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004937
Bram Moolenaar39665952018-08-15 20:59:48 +02004938 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004939 {
4940 qi = GET_LOC_LIST(curwin);
4941 if (qi == NULL)
4942 {
4943 EMSG(_(e_loclist));
4944 return;
4945 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004946 }
4947
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004948 if (eap->addr_count > 0)
4949 errornr = (int)eap->line2;
4950 else
4951 {
Bram Moolenaar39665952018-08-15 20:59:48 +02004952 switch (eap->cmdidx)
4953 {
4954 case CMD_cc: case CMD_ll:
4955 errornr = 0;
4956 break;
4957 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
4958 case CMD_lfirst:
4959 errornr = 1;
4960 break;
4961 default:
4962 errornr = 32767;
4963 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004964 }
4965
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004966 // For cdo and ldo commands, jump to the nth valid error.
4967 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02004968 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
4969 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004970 errornr = qf_get_nth_valid_entry(&qi->qf_lists[qi->qf_curlist],
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004971 eap->addr_count > 0 ? (int)eap->line1 : 1,
4972 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
4973
4974 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975}
4976
4977/*
4978 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004979 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004980 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 */
4982 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004983ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004985 qf_info_T *qi = &ql_info;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004986 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02004987 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004988
Bram Moolenaar39665952018-08-15 20:59:48 +02004989 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004990 {
4991 qi = GET_LOC_LIST(curwin);
4992 if (qi == NULL)
4993 {
4994 EMSG(_(e_loclist));
4995 return;
4996 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004997 }
4998
Bram Moolenaar55b69262017-08-13 13:42:01 +02004999 if (eap->addr_count > 0
5000 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5001 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005002 errornr = (int)eap->line2;
5003 else
5004 errornr = 1;
5005
Bram Moolenaar39665952018-08-15 20:59:48 +02005006 // Depending on the command jump to either next or previous entry/file.
5007 switch (eap->cmdidx)
5008 {
5009 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5010 dir = FORWARD;
5011 break;
5012 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5013 case CMD_lNext:
5014 dir = BACKWARD;
5015 break;
5016 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5017 dir = FORWARD_FILE;
5018 break;
5019 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5020 dir = BACKWARD_FILE;
5021 break;
5022 default:
5023 dir = FORWARD;
5024 break;
5025 }
5026
5027 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028}
5029
5030/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005031 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005032 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 */
5034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005035ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005037 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005038 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005039 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005040 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005041 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005042 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005043
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005044 switch (eap->cmdidx)
5045 {
5046 case CMD_cfile: au_name = (char_u *)"cfile"; break;
5047 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break;
5048 case CMD_caddfile: au_name = (char_u *)"caddfile"; break;
5049 case CMD_lfile: au_name = (char_u *)"lfile"; break;
5050 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break;
5051 case CMD_laddfile: au_name = (char_u *)"laddfile"; break;
5052 default: break;
5053 }
5054 if (au_name != NULL)
5055 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005056#ifdef FEAT_MBYTE
5057 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
5058#endif
Bram Moolenaar9028b102010-07-11 16:58:51 +02005059#ifdef FEAT_BROWSE
5060 if (cmdmod.browse)
5061 {
5062 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005063 NULL, NULL,
5064 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005065 if (browse_file == NULL)
5066 return;
5067 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5068 vim_free(browse_file);
5069 }
5070 else
5071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005073 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005074
Bram Moolenaar39665952018-08-15 20:59:48 +02005075 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005076 wp = curwin;
5077
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005078 incr_quickfix_busy();
5079
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005080 // This function is used by the :cfile, :cgetfile and :caddfile
5081 // commands.
5082 // :cfile always creates a new quickfix list and jumps to the
5083 // first error.
5084 // :cgetfile creates a new quickfix list but doesn't jump to the
5085 // first error.
5086 // :caddfile adds to an existing quickfix list. If there is no
5087 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005088 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005089 && eap->cmdidx != CMD_laddfile),
5090 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005091 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005092 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005093 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005094 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005095 {
5096 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005097 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005098 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005099 }
5100 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005101 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005102 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005103 if (au_name != NULL)
5104 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005105
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005106 // Jump to the first error for a new list and if autocmds didn't
5107 // free the list.
5108 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5109 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005110 // display the first error
5111 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005112
5113 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005114}
5115
5116/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005117 * Return the vimgrep autocmd name.
5118 */
5119 static char_u *
5120vgr_get_auname(cmdidx_T cmdidx)
5121{
5122 switch (cmdidx)
5123 {
5124 case CMD_vimgrep: return (char_u *)"vimgrep";
5125 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5126 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5127 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5128 case CMD_grep: return (char_u *)"grep";
5129 case CMD_lgrep: return (char_u *)"lgrep";
5130 case CMD_grepadd: return (char_u *)"grepadd";
5131 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5132 default: return NULL;
5133 }
5134}
5135
5136/*
5137 * Initialize the regmatch used by vimgrep for pattern "s".
5138 */
5139 static void
5140vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5141{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005142 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005143 regmatch->regprog = NULL;
5144
5145 if (s == NULL || *s == NUL)
5146 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005147 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005148 if (last_search_pat() == NULL)
5149 {
5150 EMSG(_(e_noprevre));
5151 return;
5152 }
5153 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5154 }
5155 else
5156 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5157
5158 regmatch->rmm_ic = p_ic;
5159 regmatch->rmm_maxcol = 0;
5160}
5161
5162/*
5163 * Display a file name when vimgrep is running.
5164 */
5165 static void
5166vgr_display_fname(char_u *fname)
5167{
5168 char_u *p;
5169
5170 msg_start();
5171 p = msg_strtrunc(fname, TRUE);
5172 if (p == NULL)
5173 msg_outtrans(fname);
5174 else
5175 {
5176 msg_outtrans(p);
5177 vim_free(p);
5178 }
5179 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005180 msg_didout = FALSE; // overwrite this message
5181 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005182 msg_col = 0;
5183 out_flush();
5184}
5185
5186/*
5187 * Load a dummy buffer to search for a pattern using vimgrep.
5188 */
5189 static buf_T *
5190vgr_load_dummy_buf(
5191 char_u *fname,
5192 char_u *dirname_start,
5193 char_u *dirname_now)
5194{
5195 int save_mls;
5196#if defined(FEAT_SYN_HL)
5197 char_u *save_ei = NULL;
5198#endif
5199 buf_T *buf;
5200
5201#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005202 // Don't do Filetype autocommands to avoid loading syntax and
5203 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005204 save_ei = au_event_disable(",Filetype");
5205#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005206 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005207 save_mls = p_mls;
5208 p_mls = 0;
5209
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005210 // Load file into a buffer, so that 'fileencoding' is detected,
5211 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005212 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5213
5214 p_mls = save_mls;
5215#if defined(FEAT_SYN_HL)
5216 au_event_restore(save_ei);
5217#endif
5218
5219 return buf;
5220}
5221
5222/*
5223 * Check whether a quickfix/location list valid. Autocmds may remove or change
5224 * a quickfix list when vimgrep is running. If the list is not found, create a
5225 * new list.
5226 */
5227 static int
5228vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005229 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005230 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005231 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005232 char_u *title)
5233{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005234 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005235 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005236 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005237 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005238 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005239 // An autocmd has freed the location list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005240 EMSG(_(e_loc_list_changed));
5241 return FALSE;
5242 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005243 else
5244 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005245 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005246 qf_new_list(qi, title);
5247 return TRUE;
5248 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005249 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005250
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005251 if (qf_restore_list(qi, qfid) == FAIL)
5252 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005253
5254 return TRUE;
5255}
5256
5257/*
5258 * Search for a pattern in all the lines in a buffer and add the matching lines
5259 * to a quickfix list.
5260 */
5261 static int
5262vgr_match_buflines(
5263 qf_info_T *qi,
5264 char_u *fname,
5265 buf_T *buf,
5266 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005267 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005268 int duplicate_name,
5269 int flags)
5270{
5271 int found_match = FALSE;
5272 long lnum;
5273 colnr_T col;
5274
Bram Moolenaar1c299432018-10-28 14:36:09 +01005275 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005276 {
5277 col = 0;
5278 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5279 col, NULL, NULL) > 0)
5280 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005281 // Pass the buffer number so that it gets used even for a
5282 // dummy buffer, unless duplicate_name is set, then the
5283 // buffer will be wiped out below.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005284 if (qf_add_entry(qi,
5285 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005286 NULL, // dir
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005287 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02005288 NULL,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005289 duplicate_name ? 0 : buf->b_fnum,
5290 ml_get_buf(buf,
5291 regmatch->startpos[0].lnum + lnum, FALSE),
5292 regmatch->startpos[0].lnum + lnum,
5293 regmatch->startpos[0].col + 1,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005294 FALSE, // vis_col
5295 NULL, // search pattern
5296 0, // nr
5297 0, // type
5298 TRUE // valid
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005299 ) == FAIL)
5300 {
5301 got_int = TRUE;
5302 break;
5303 }
5304 found_match = TRUE;
Bram Moolenaar1c299432018-10-28 14:36:09 +01005305 if (--*tomatch == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005306 break;
5307 if ((flags & VGR_GLOBAL) == 0
5308 || regmatch->endpos[0].lnum > 0)
5309 break;
5310 col = regmatch->endpos[0].col
5311 + (col == regmatch->endpos[0].col);
5312 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5313 break;
5314 }
5315 line_breakcheck();
5316 if (got_int)
5317 break;
5318 }
5319
5320 return found_match;
5321}
5322
5323/*
5324 * Jump to the first match and update the directory.
5325 */
5326 static void
5327vgr_jump_to_match(
5328 qf_info_T *qi,
5329 int forceit,
5330 int *redraw_for_dummy,
5331 buf_T *first_match_buf,
5332 char_u *target_dir)
5333{
5334 buf_T *buf;
5335
5336 buf = curbuf;
5337 qf_jump(qi, 0, 0, forceit);
5338 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005339 // If we jumped to another buffer redrawing will already be
5340 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005341 *redraw_for_dummy = FALSE;
5342
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005343 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005344 if (curbuf == first_match_buf && target_dir != NULL)
5345 {
5346 exarg_T ea;
5347
5348 ea.arg = target_dir;
5349 ea.cmdidx = CMD_lcd;
5350 ex_cd(&ea);
5351 }
5352}
5353
5354/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005355 * ":vimgrep {pattern} file(s)"
Bram Moolenaara6557602006-02-04 22:43:20 +00005356 * ":vimgrepadd {pattern} file(s)"
5357 * ":lvimgrep {pattern} file(s)"
5358 * ":lvimgrepadd {pattern} file(s)"
Bram Moolenaar86b68352004-12-27 21:59:20 +00005359 */
5360 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005361ex_vimgrep(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005362{
Bram Moolenaar81695252004-12-29 20:58:21 +00005363 regmmatch_T regmatch;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005364 int fcount;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005365 char_u **fnames;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005366 char_u *fname;
Bram Moolenaar5584df62016-03-18 21:00:51 +01005367 char_u *title;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005368 char_u *s;
5369 char_u *p;
Bram Moolenaar748bf032005-02-02 23:04:36 +00005370 int fi;
Bram Moolenaara6557602006-02-04 22:43:20 +00005371 qf_info_T *qi = &ql_info;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005372 qf_list_T *qfl;
Bram Moolenaar3c097222017-12-21 20:54:49 +01005373 int_u save_qfid;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005374 win_T *wp = NULL;
Bram Moolenaar81695252004-12-29 20:58:21 +00005375 buf_T *buf;
5376 int duplicate_name = FALSE;
5377 int using_dummy;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005378 int redraw_for_dummy = FALSE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005379 int found_match;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005380 buf_T *first_match_buf = NULL;
5381 time_t seconds = 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005382 aco_save_T aco;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005383 int flags = 0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005384 long tomatch;
Bram Moolenaard9462e32011-04-11 21:35:11 +02005385 char_u *dirname_start = NULL;
5386 char_u *dirname_now = NULL;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005387 char_u *target_dir = NULL;
Bram Moolenaar15bfa092008-07-24 16:45:38 +00005388 char_u *au_name = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005389
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005390 au_name = vgr_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01005391 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5392 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar7c626922005-02-07 22:01:03 +00005393 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005394#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01005395 if (aborting())
Bram Moolenaar7c626922005-02-07 22:01:03 +00005396 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005397#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005398 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005399
Bram Moolenaar39665952018-08-15 20:59:48 +02005400 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaara6557602006-02-04 22:43:20 +00005401 {
5402 qi = ll_get_or_alloc_list(curwin);
5403 if (qi == NULL)
5404 return;
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005405 wp = curwin;
Bram Moolenaara6557602006-02-04 22:43:20 +00005406 }
5407
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005408 if (eap->addr_count > 0)
5409 tomatch = eap->line2;
5410 else
5411 tomatch = MAXLNUM;
5412
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005413 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar86b68352004-12-27 21:59:20 +00005414 regmatch.regprog = NULL;
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005415 title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar05159a02005-02-26 23:04:13 +00005416 p = skip_vimgrep_pat(eap->arg, &s, &flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00005417 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005418 {
Bram Moolenaar2389c3c2005-05-22 22:07:59 +00005419 EMSG(_(e_invalpat));
Bram Moolenaar748bf032005-02-02 23:04:36 +00005420 goto theend;
Bram Moolenaar81695252004-12-29 20:58:21 +00005421 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01005422
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005423 vgr_init_regmatch(&regmatch, s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005424 if (regmatch.regprog == NULL)
5425 goto theend;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005426
5427 p = skipwhite(p);
5428 if (*p == NUL)
5429 {
5430 EMSG(_("E683: File name missing or invalid pattern"));
5431 goto theend;
5432 }
5433
Bram Moolenaar55b69262017-08-13 13:42:01 +02005434 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005435 && eap->cmdidx != CMD_vimgrepadd
5436 && eap->cmdidx != CMD_lvimgrepadd)
Bram Moolenaar019dfe62018-10-07 14:38:49 +02005437 || qf_stack_empty(qi))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005438 // make place for a new list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005439 qf_new_list(qi, title != NULL ? title : qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar86b68352004-12-27 21:59:20 +00005440
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005441 // parse the list of arguments
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02005442 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005443 goto theend;
5444 if (fcount == 0)
5445 {
5446 EMSG(_(e_nomatch));
5447 goto theend;
5448 }
5449
Bram Moolenaarb86a3432016-01-10 16:00:53 +01005450 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
5451 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005452 if (dirname_start == NULL || dirname_now == NULL)
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005453 {
5454 FreeWild(fcount, fnames);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005455 goto theend;
Bram Moolenaar61ff4dd2016-01-18 20:30:17 +01005456 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02005457
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005458 // Remember the current directory, because a BufRead autocommand that does
5459 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005460 mch_dirname(dirname_start, MAXPATHL);
5461
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005462 incr_quickfix_busy();
5463
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005464 // Remember the current quickfix list identifier, so that we can check for
5465 // autocommands changing the current quickfix list.
Bram Moolenaar3c097222017-12-21 20:54:49 +01005466 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005467
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005468 seconds = (time_t)0;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005469 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005470 {
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005471 fname = shorten_fname1(fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005472 if (time(NULL) > seconds)
5473 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005474 // Display the file name every second or so, show the user we are
5475 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005476 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005477 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005478 }
5479
Bram Moolenaar81695252004-12-29 20:58:21 +00005480 buf = buflist_findname_exp(fnames[fi]);
5481 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5482 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005483 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00005484 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005485 using_dummy = TRUE;
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005486 redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005487
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005488 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00005489 }
5490 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005491 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00005492 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005493
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005494 // Check whether the quickfix list is still valid. When loading a
5495 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005496 if (!vgr_qflist_valid(wp, qi, save_qfid, qf_cmdtitle(*eap->cmdlinep)))
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005497 {
5498 FreeWild(fcount, fnames);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005499 decr_quickfix_busy();
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005500 goto theend;
Bram Moolenaaree5b94a2018-04-12 20:35:05 +02005501 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005502 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01005503
Bram Moolenaar81695252004-12-29 20:58:21 +00005504 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005505 {
5506 if (!got_int)
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005507 smsg((char_u *)_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005508 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005509 else
5510 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005511 // Try for a match in all lines of the buffer.
5512 // For ":1vimgrep" look for first match only.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005513 found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005514 &tomatch, duplicate_name, flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005515
Bram Moolenaar81695252004-12-29 20:58:21 +00005516 if (using_dummy)
5517 {
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005518 if (found_match && first_match_buf == NULL)
5519 first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00005520 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005521 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005522 // Never keep a dummy buffer if there is another buffer
5523 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005524 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005525 buf = NULL;
5526 }
Bram Moolenaara3227e22006-03-08 21:32:40 +00005527 else if (!cmdmod.hide
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005528 || buf->b_p_bh[0] == 'u' // "unload"
5529 || buf->b_p_bh[0] == 'w' // "wipe"
5530 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00005531 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005532 // When no match was found we don't need to remember the
5533 // buffer, wipe it out. If there was a match and it
5534 // wasn't the first one or we won't jump there: only
5535 // unload the buffer.
5536 // Ignore 'hidden' here, because it may lead to having too
5537 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00005538 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005539 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005540 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005541 buf = NULL;
5542 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00005543 else if (buf != first_match_buf || (flags & VGR_NOJUMP))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005544 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005545 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005546 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005547 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005548 buf = NULL;
5549 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005550 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005551
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005552 if (buf != NULL)
5553 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005554 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02005555 buf->b_flags &= ~BF_DUMMY;
5556
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005557 // If the buffer is still loaded we need to use the
5558 // directory we jumped to below.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005559 if (buf == first_match_buf
5560 && target_dir == NULL
5561 && STRCMP(dirname_start, dirname_now) != 0)
5562 target_dir = vim_strsave(dirname_now);
5563
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005564 // The buffer is still loaded, the Filetype autocommands
5565 // need to be done now, in that buffer. And the modelines
5566 // need to be done (again). But not the window-local
5567 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005568 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005569#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005570 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
5571 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00005572#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005573 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005574 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005575 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005576 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005577 }
5578 }
5579
5580 FreeWild(fcount, fnames);
5581
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005582 qfl = &qi->qf_lists[qi->qf_curlist];
5583 qfl->qf_nonevalid = FALSE;
5584 qfl->qf_ptr = qfl->qf_start;
5585 qfl->qf_index = 1;
5586 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005587
Bram Moolenaar864293a2016-06-02 13:40:04 +02005588 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005589
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005590 if (au_name != NULL)
5591 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5592 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005593 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
5594 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005595 if (!qflist_valid(wp, save_qfid)
5596 || qf_restore_list(qi, save_qfid) == FAIL)
5597 {
5598 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01005599 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005600 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005601
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02005602 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005603 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005604 {
5605 if ((flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005606 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
5607 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00005608 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005609 else
5610 EMSG2(_(e_nomatch2), s);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005611
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005612 decr_quickfix_busy();
5613
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005614 // If we loaded a dummy buffer into the current window, the autocommands
5615 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00005616 if (redraw_for_dummy)
5617 {
5618#ifdef FEAT_FOLDING
5619 foldUpdateAll(curwin);
5620#else
5621 redraw_later(NOT_VALID);
5622#endif
5623 }
5624
Bram Moolenaar86b68352004-12-27 21:59:20 +00005625theend:
Bram Moolenaar5584df62016-03-18 21:00:51 +01005626 vim_free(title);
Bram Moolenaard9462e32011-04-11 21:35:11 +02005627 vim_free(dirname_now);
5628 vim_free(dirname_start);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005629 vim_free(target_dir);
Bram Moolenaar473de612013-06-08 18:19:48 +02005630 vim_regfree(regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005631}
5632
5633/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005634 * Restore current working directory to "dirname_start" if they differ, taking
5635 * into account whether it is set locally or globally.
5636 */
5637 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005638restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005639{
5640 char_u *dirname_now = alloc(MAXPATHL);
5641
5642 if (NULL != dirname_now)
5643 {
5644 mch_dirname(dirname_now, MAXPATHL);
5645 if (STRCMP(dirname_start, dirname_now) != 0)
5646 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005647 // If the directory has changed, change it back by building up an
5648 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005649 exarg_T ea;
5650
5651 ea.arg = dirname_start;
5652 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
5653 ex_cd(&ea);
5654 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01005655 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005656 }
5657}
5658
5659/*
5660 * Load file "fname" into a dummy buffer and return the buffer pointer,
5661 * placing the directory resulting from the buffer load into the
5662 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
5663 * prior to calling this function. Restores directory to "dirname_start" prior
5664 * to returning, if autocmds or the 'autochdir' option have changed it.
5665 *
5666 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
5667 * or wipe_dummy_buffer() later!
5668 *
Bram Moolenaar81695252004-12-29 20:58:21 +00005669 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00005670 */
5671 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005672load_dummy_buffer(
5673 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005674 char_u *dirname_start, // in: old directory
5675 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00005676{
5677 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005678 bufref_T newbufref;
5679 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00005680 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00005681 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005682 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00005683
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005684 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00005685 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5686 if (newbuf == NULL)
5687 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005688 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005689
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005690 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00005691 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
5692
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005693 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005694 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00005695 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005696 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005697 ++newbuf->b_locked;
5698
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005699 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005700 aucmd_prepbuf(&aco, newbuf);
5701
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005702 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005703 (void)setfname(curbuf, fname, NULL, FALSE);
5704
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005705 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00005706 check_need_swap(TRUE);
5707
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005708 // Remove the "dummy" flag, otherwise autocommands may not
5709 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00005710 curbuf->b_flags &= ~BF_DUMMY;
5711
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005712 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005713 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00005714 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01005715 NULL, READ_NEW | READ_DUMMY);
5716 --newbuf->b_locked;
5717 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00005718 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00005719 && !(curbuf->b_flags & BF_NEW))
5720 {
5721 failed = FALSE;
5722 if (curbuf != newbuf)
5723 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005724 // Bloody autocommands changed the buffer! Can happen when
5725 // using netrw and editing a remote file. Use the current
5726 // buffer instead, delete the dummy one after restoring the
5727 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005728 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00005729 newbuf = curbuf;
5730 }
5731 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005732
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005733 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005734 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005735 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
5736 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005737
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005738 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
5739 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02005740 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00005741 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005742
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005743 // When autocommands/'autochdir' option changed directory: go back.
5744 // Let the caller know what the resulting dir was first, in case it is
5745 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005746 mch_dirname(resulting_dir, MAXPATHL);
5747 restore_start_dir(dirname_start);
5748
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005749 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00005750 return NULL;
5751 if (failed)
5752 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005753 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00005754 return NULL;
5755 }
5756 return newbuf;
5757}
5758
5759/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005760 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
5761 * directory to "dirname_start" prior to returning, if autocmds or the
5762 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005763 */
5764 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005765wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005766{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005767 if (curbuf != buf) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00005768 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005769#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005770 cleanup_T cs;
5771
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005772 // Reset the error/interrupt/exception state here so that aborting()
5773 // returns FALSE when wiping out the buffer. Otherwise it doesn't
5774 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005775 enter_cleanup(&cs);
5776#endif
5777
Bram Moolenaar81695252004-12-29 20:58:21 +00005778 wipe_buffer(buf, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005779
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005780#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005781 // Restore the error/interrupt/exception state if not discarded by a
5782 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00005783 leave_cleanup(&cs);
5784#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005785 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005786 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005787 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005788}
5789
5790/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005791 * Unload the dummy buffer that load_dummy_buffer() created. Restores
5792 * directory to "dirname_start" prior to returning, if autocmds or the
5793 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00005794 */
5795 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005796unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00005797{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005798 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005799 {
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005800 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005801
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005802 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02005803 restore_start_dir(dirname_start);
5804 }
Bram Moolenaar81695252004-12-29 20:58:21 +00005805}
5806
Bram Moolenaar05159a02005-02-26 23:04:13 +00005807#if defined(FEAT_EVAL) || defined(PROTO)
5808/*
5809 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005810 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar05159a02005-02-26 23:04:13 +00005811 */
5812 int
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005813get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005814{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005815 qf_info_T *qi = qi_arg;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005816 dict_T *dict;
5817 char_u buf[2];
5818 qfline_T *qfp;
5819 int i;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005820 int bufnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005821
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005822 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005823 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005824 qi = &ql_info;
5825 if (wp != NULL)
5826 {
5827 qi = GET_LOC_LIST(wp);
5828 if (qi == NULL)
5829 return FAIL;
5830 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00005831 }
5832
Bram Moolenaar29ce4092018-04-28 21:56:44 +02005833 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005834 qf_idx = qi->qf_curlist;
5835
Bram Moolenaarde3b3672018-08-07 21:54:41 +02005836 if (qf_idx >= qi->qf_listcount || qf_list_empty(qi, qf_idx))
Bram Moolenaar05159a02005-02-26 23:04:13 +00005837 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005838
Bram Moolenaard823fa92016-08-12 16:29:27 +02005839 qfp = qi->qf_lists[qf_idx].qf_start;
5840 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005841 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005842 // Handle entries with a non-existing buffer number.
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00005843 bufnum = qfp->qf_fnum;
5844 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
5845 bufnum = 0;
5846
Bram Moolenaar05159a02005-02-26 23:04:13 +00005847 if ((dict = dict_alloc()) == NULL)
5848 return FAIL;
5849 if (list_append_dict(list, dict) == FAIL)
5850 return FAIL;
5851
5852 buf[0] = qfp->qf_type;
5853 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005854 if ( dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
5855 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
5856 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
5857 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
5858 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
5859 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
5860 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
5861 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
5862 || dict_add_string(dict, "type", buf) == FAIL
5863 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00005864 return FAIL;
5865
5866 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005867 if (qfp == NULL)
5868 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005869 }
5870 return OK;
5871}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005872
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005873// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02005874enum {
5875 QF_GETLIST_NONE = 0x0,
5876 QF_GETLIST_TITLE = 0x1,
5877 QF_GETLIST_ITEMS = 0x2,
5878 QF_GETLIST_NR = 0x4,
5879 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005880 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005881 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005882 QF_GETLIST_IDX = 0x40,
5883 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01005884 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005885 QF_GETLIST_FILEWINID = 0x200,
5886 QF_GETLIST_ALL = 0x3FF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02005887};
5888
5889/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005890 * Parse text from 'di' and return the quickfix list items.
5891 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005892 */
5893 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02005894qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005895{
5896 int status = FAIL;
5897 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02005898 char_u *errorformat = p_efm;
5899 dictitem_T *efm_di;
5900 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005901
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005902 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02005903 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005904 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005905 // If errorformat is supplied then use it, otherwise use the 'efm'
5906 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02005907 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
5908 {
5909 if (efm_di->di_tv.v_type != VAR_STRING ||
5910 efm_di->di_tv.vval.v_string == NULL)
5911 return FAIL;
5912 errorformat = efm_di->di_tv.vval.v_string;
5913 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005914
Bram Moolenaar36538222017-09-02 19:51:44 +02005915 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02005916 if (l == NULL)
5917 return FAIL;
5918
Bram Moolenaar2d67d302018-11-16 18:46:02 +01005919 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005920 if (qi != NULL)
5921 {
Bram Moolenaar36538222017-09-02 19:51:44 +02005922 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005923 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
5924 {
Bram Moolenaarda732532017-08-31 20:58:02 +02005925 (void)get_errorlist(qi, NULL, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005926 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005927 }
5928 free(qi);
5929 }
Bram Moolenaarda732532017-08-31 20:58:02 +02005930 dict_add_list(retdict, "items", l);
5931 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005932 }
5933
5934 return status;
5935}
5936
5937/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005938 * Return the quickfix/location list window identifier in the current tabpage.
5939 */
5940 static int
5941qf_winid(qf_info_T *qi)
5942{
5943 win_T *win;
5944
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005945 // The quickfix window can be opened even if the quickfix list is not set
5946 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01005947 if (qi == NULL)
5948 return 0;
5949 win = qf_find_win(qi);
5950 if (win != NULL)
5951 return win->w_id;
5952 return 0;
5953}
5954
5955/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005956 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005957 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02005958 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005959qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02005960{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005961 int flags = QF_GETLIST_NONE;
5962
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005963 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005964 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005965 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005966 if (!loclist)
5967 // File window ID is applicable only to location list windows
5968 flags &= ~ QF_GETLIST_FILEWINID;
5969 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005970
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005971 if (dict_find(what, (char_u *)"title", -1) != NULL)
5972 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005973
Bram Moolenaara6d48492017-12-12 22:45:31 +01005974 if (dict_find(what, (char_u *)"nr", -1) != NULL)
5975 flags |= QF_GETLIST_NR;
5976
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005977 if (dict_find(what, (char_u *)"winid", -1) != NULL)
5978 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02005979
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005980 if (dict_find(what, (char_u *)"context", -1) != NULL)
5981 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02005982
Bram Moolenaara6d48492017-12-12 22:45:31 +01005983 if (dict_find(what, (char_u *)"id", -1) != NULL)
5984 flags |= QF_GETLIST_ID;
5985
Bram Moolenaara539f4f2017-08-30 20:33:55 +02005986 if (dict_find(what, (char_u *)"items", -1) != NULL)
5987 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005988
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02005989 if (dict_find(what, (char_u *)"idx", -1) != NULL)
5990 flags |= QF_GETLIST_IDX;
5991
5992 if (dict_find(what, (char_u *)"size", -1) != NULL)
5993 flags |= QF_GETLIST_SIZE;
5994
Bram Moolenaarb254af32017-12-18 19:48:58 +01005995 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
5996 flags |= QF_GETLIST_TICK;
5997
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02005998 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
5999 flags |= QF_GETLIST_FILEWINID;
6000
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006001 return flags;
6002}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006003
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006004/*
6005 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6006 * If 'nr' and 'id' are not present in 'what' then return the current
6007 * quickfix list index.
6008 * If 'nr' is zero then return the current quickfix list index.
6009 * If 'nr' is '$' then return the last quickfix list index.
6010 * If 'id' is present then return the index of the quickfix list with that id.
6011 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6012 * Return -1, if quickfix list is not present or if the stack is empty.
6013 */
6014 static int
6015qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6016{
6017 int qf_idx;
6018 dictitem_T *di;
6019
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006020 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006021 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6022 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006023 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006024 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006025 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006026 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006027 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006028 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006029 qf_idx = di->di_tv.vval.v_number - 1;
6030 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006031 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006032 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006033 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006034 else if (di->di_tv.v_type == VAR_STRING
6035 && di->di_tv.vval.v_string != NULL
6036 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006037 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006038 qf_idx = qi->qf_listcount - 1;
6039 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006040 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006041 }
6042
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006043 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6044 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006045 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006046 if (di->di_tv.v_type == VAR_NUMBER)
6047 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006048 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006049 if (di->di_tv.vval.v_number != 0)
6050 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6051 }
6052 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006053 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006054 }
6055
6056 return qf_idx;
6057}
6058
6059/*
6060 * Return default values for quickfix list properties in retdict.
6061 */
6062 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006063qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006064{
6065 int status = OK;
6066
6067 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006068 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006069 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6070 {
6071 list_T *l = list_alloc();
6072 if (l != NULL)
6073 status = dict_add_list(retdict, "items", l);
6074 else
6075 status = FAIL;
6076 }
6077 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006078 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006079 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006080 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006081 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006082 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006083 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006084 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006085 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006086 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006087 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006088 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006089 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006090 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006091 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006092 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006093
6094 return status;
6095}
6096
6097/*
6098 * Return the quickfix list title as 'title' in retdict
6099 */
6100 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006101qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006102{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006103 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006104}
6105
6106/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006107 * Returns the identifier of the window used to display files from a location
6108 * list. If there is no associated window, then returns 0. Useful only when
6109 * called from a location list window.
6110 */
6111 static int
6112qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6113{
6114 int winid = 0;
6115
6116 if (wp != NULL && IS_LL_WINDOW(wp))
6117 {
6118 win_T *ll_wp = qf_find_win_with_loclist(qi);
6119 if (ll_wp != NULL)
6120 winid = ll_wp->w_id;
6121 }
6122
6123 return dict_add_number(retdict, "filewinid", winid);
6124}
6125
6126/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006127 * Return the quickfix list items/entries as 'items' in retdict
6128 */
6129 static int
6130qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
6131{
6132 int status = OK;
6133 list_T *l = list_alloc();
6134 if (l != NULL)
6135 {
6136 (void)get_errorlist(qi, NULL, qf_idx, l);
6137 dict_add_list(retdict, "items", l);
6138 }
6139 else
6140 status = FAIL;
6141
6142 return status;
6143}
6144
6145/*
6146 * Return the quickfix list context (if any) as 'context' in retdict.
6147 */
6148 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006149qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006150{
6151 int status;
6152 dictitem_T *di;
6153
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006154 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006155 {
6156 di = dictitem_alloc((char_u *)"context");
6157 if (di != NULL)
6158 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006159 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006160 status = dict_add(retdict, di);
6161 if (status == FAIL)
6162 dictitem_free(di);
6163 }
6164 else
6165 status = FAIL;
6166 }
6167 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006168 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006169
6170 return status;
6171}
6172
6173/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006174 * Return the current quickfix list index as 'idx' in retdict
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006175 */
6176 static int
6177qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
6178{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006179 int curidx = qi->qf_lists[qf_idx].qf_index;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006180 if (qf_list_empty(qi, qf_idx))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006181 // For empty lists, current index is set to 0
6182 curidx = 0;
6183 return dict_add_number(retdict, "idx", curidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006184}
6185
6186/*
6187 * Return quickfix/location list details (title) as a
6188 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
6189 * then current list is used. Otherwise the specified list is used.
6190 */
6191 int
6192qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
6193{
6194 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006195 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006196 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006197 int qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006198 dictitem_T *di;
6199 int flags = QF_GETLIST_NONE;
6200
6201 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
6202 return qf_get_list_from_lines(what, di, retdict);
6203
6204 if (wp != NULL)
6205 qi = GET_LOC_LIST(wp);
6206
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006207 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006208
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006209 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006210 qf_idx = qf_getprop_qfidx(qi, what);
6211
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006212 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006213 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006214 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01006215
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006216 qfl = &qi->qf_lists[qf_idx];
6217
Bram Moolenaard823fa92016-08-12 16:29:27 +02006218 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006219 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006220 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006221 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006222 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006223 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006224 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006225 status = qf_getprop_items(qi, qf_idx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006226 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006227 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006228 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006229 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006230 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006231 status = qf_getprop_idx(qi, qf_idx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006232 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006233 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006234 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006235 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006236 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
6237 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006238
Bram Moolenaard823fa92016-08-12 16:29:27 +02006239 return status;
6240}
6241
6242/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006243 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
6244 * items in the dict 'd'.
6245 */
6246 static int
6247qf_add_entry_from_dict(
6248 qf_info_T *qi,
6249 int qf_idx,
6250 dict_T *d,
6251 int first_entry)
6252{
6253 static int did_bufnr_emsg;
6254 char_u *filename, *module, *pattern, *text, *type;
6255 int bufnum, valid, status, col, vcol, nr;
6256 long lnum;
6257
6258 if (first_entry)
6259 did_bufnr_emsg = FALSE;
6260
Bram Moolenaar8f667172018-12-14 15:38:31 +01006261 filename = dict_get_string(d, (char_u *)"filename", TRUE);
6262 module = dict_get_string(d, (char_u *)"module", TRUE);
6263 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
6264 lnum = (int)dict_get_number(d, (char_u *)"lnum");
6265 col = (int)dict_get_number(d, (char_u *)"col");
6266 vcol = (int)dict_get_number(d, (char_u *)"vcol");
6267 nr = (int)dict_get_number(d, (char_u *)"nr");
6268 type = dict_get_string(d, (char_u *)"type", TRUE);
6269 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
6270 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006271 if (text == NULL)
6272 text = vim_strsave((char_u *)"");
6273
6274 valid = TRUE;
6275 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
6276 valid = FALSE;
6277
6278 // Mark entries with non-existing buffer number as not valid. Give the
6279 // error message only once.
6280 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6281 {
6282 if (!did_bufnr_emsg)
6283 {
6284 did_bufnr_emsg = TRUE;
6285 EMSGN(_("E92: Buffer %ld not found"), bufnum);
6286 }
6287 valid = FALSE;
6288 bufnum = 0;
6289 }
6290
6291 // If the 'valid' field is present it overrules the detected value.
6292 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01006293 valid = (int)dict_get_number(d, (char_u *)"valid");
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006294
6295 status = qf_add_entry(qi,
6296 qf_idx,
6297 NULL, // dir
6298 filename,
6299 module,
6300 bufnum,
6301 text,
6302 lnum,
6303 col,
6304 vcol, // vis_col
6305 pattern, // search pattern
6306 nr,
6307 type == NULL ? NUL : *type,
6308 valid);
6309
6310 vim_free(filename);
6311 vim_free(module);
6312 vim_free(pattern);
6313 vim_free(text);
6314 vim_free(type);
6315
6316 return status;
6317}
6318
6319/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02006320 * Add list of entries to quickfix/location list. Each list entry is
6321 * a dictionary with item information.
6322 */
6323 static int
6324qf_add_entries(
6325 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02006326 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006327 list_T *list,
6328 char_u *title,
6329 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006330{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006331 qf_list_T *qfl = &qi->qf_lists[qf_idx];
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006332 listitem_T *li;
6333 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02006334 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006335 int retval = OK;
6336
Bram Moolenaara3921f42017-06-04 15:30:34 +02006337 if (action == ' ' || qf_idx == qi->qf_listcount)
6338 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006339 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01006340 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02006341 qf_idx = qi->qf_curlist;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006342 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaara3921f42017-06-04 15:30:34 +02006343 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006344 else if (action == 'a' && !qf_list_empty(qi, qf_idx))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006345 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006346 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006347 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02006348 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006349 qf_free_items(qfl);
6350 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02006351 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006352
6353 for (li = list->lv_first; li != NULL; li = li->li_next)
6354 {
6355 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006356 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006357
6358 d = li->li_tv.vval.v_dict;
6359 if (d == NULL)
6360 continue;
6361
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02006362 retval = qf_add_entry_from_dict(qi, qf_idx, d, li == list->lv_first);
6363 if (retval == FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006364 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006365 }
6366
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006367 if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006368 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006369 qfl->qf_nonevalid = TRUE;
Bram Moolenaarf9ddb942010-05-14 18:10:27 +02006370 else
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006371 qfl->qf_nonevalid = FALSE;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006372 if (action != 'a')
6373 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006374 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02006375 if (!qf_list_empty(qi, qf_idx))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006376 qfl->qf_index = 1;
Bram Moolenaarc1808d52016-04-18 20:04:00 +02006377 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006378
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006379 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02006380 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006381
6382 return retval;
6383}
Bram Moolenaard823fa92016-08-12 16:29:27 +02006384
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006385/*
6386 * Get the quickfix list index from 'nr' or 'id'
6387 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02006388 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006389qf_setprop_get_qfidx(
6390 qf_info_T *qi,
6391 dict_T *what,
6392 int action,
6393 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006394{
6395 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006396 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006397
Bram Moolenaard823fa92016-08-12 16:29:27 +02006398 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6399 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006400 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02006401 if (di->di_tv.v_type == VAR_NUMBER)
6402 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006403 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02006404 if (di->di_tv.vval.v_number != 0)
6405 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006406
Bram Moolenaar55b69262017-08-13 13:42:01 +02006407 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
6408 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006409 // When creating a new list, accept qf_idx pointing to the next
6410 // non-available list and add the new list at the end of the
6411 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006412 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006413 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006414 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006415 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006416 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006417 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006418 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02006419 }
6420 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006421 && di->di_tv.vval.v_string != NULL
6422 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006423 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006424 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02006425 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006426 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02006427 qf_idx = 0;
6428 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006429 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006430 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006431 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006432 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006433 }
6434
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006435 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006436 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006437 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006438 if (di->di_tv.v_type != VAR_NUMBER)
6439 return INVALID_QFIDX;
6440
6441 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006442 }
6443
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006444 return qf_idx;
6445}
6446
6447/*
6448 * Set the quickfix list title.
6449 */
6450 static int
6451qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
6452{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006453 qf_list_T *qfl = &qi->qf_lists[qf_idx];
6454
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006455 if (di->di_tv.v_type != VAR_STRING)
6456 return FAIL;
6457
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006458 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01006459 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006460 if (qf_idx == qi->qf_curlist)
6461 qf_update_win_titlevar(qi);
6462
6463 return OK;
6464}
6465
6466/*
6467 * Set quickfix list items/entries.
6468 */
6469 static int
6470qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
6471{
6472 int retval = FAIL;
6473 char_u *title_save;
6474
6475 if (di->di_tv.v_type != VAR_LIST)
6476 return FAIL;
6477
6478 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
6479 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
6480 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006481 vim_free(title_save);
6482
6483 return retval;
6484}
6485
6486/*
6487 * Set quickfix list items/entries from a list of lines.
6488 */
6489 static int
6490qf_setprop_items_from_lines(
6491 qf_info_T *qi,
6492 int qf_idx,
6493 dict_T *what,
6494 dictitem_T *di,
6495 int action)
6496{
6497 char_u *errorformat = p_efm;
6498 dictitem_T *efm_di;
6499 int retval = FAIL;
6500
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006501 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006502 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6503 {
6504 if (efm_di->di_tv.v_type != VAR_STRING ||
6505 efm_di->di_tv.vval.v_string == NULL)
6506 return FAIL;
6507 errorformat = efm_di->di_tv.vval.v_string;
6508 }
6509
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006510 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006511 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6512 return FAIL;
6513
6514 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006515 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006516 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
6517 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6518 retval = OK;
6519
6520 return retval;
6521}
6522
6523/*
6524 * Set quickfix list context.
6525 */
6526 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006527qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006528{
6529 typval_T *ctx;
6530
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006531 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006532 ctx = alloc_tv();
6533 if (ctx != NULL)
6534 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006535 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006536
6537 return OK;
6538}
6539
6540/*
6541 * Set quickfix/location list properties (title, items, context).
6542 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006543 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006544 */
6545 static int
6546qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
6547{
6548 dictitem_T *di;
6549 int retval = FAIL;
6550 int qf_idx;
6551 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006552 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006553
Bram Moolenaar019dfe62018-10-07 14:38:49 +02006554 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006555 newlist = TRUE;
6556
6557 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006558 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006559 return FAIL;
6560
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006561 if (newlist)
6562 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02006563 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02006564 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02006565 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006566 }
6567
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006568 qfl = &qi->qf_lists[qf_idx];
Bram Moolenaard823fa92016-08-12 16:29:27 +02006569 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006570 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006571 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006572 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006573 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006574 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006575 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006576 retval = qf_setprop_context(qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006577
Bram Moolenaarb254af32017-12-18 19:48:58 +01006578 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006579 qf_list_changed(qfl);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006580
Bram Moolenaard823fa92016-08-12 16:29:27 +02006581 return retval;
6582}
6583
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006584/*
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006585 * Find the non-location list window with the specified location list stack in
6586 * the current tabpage.
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006587 */
6588 static win_T *
6589find_win_with_ll(qf_info_T *qi)
6590{
6591 win_T *wp = NULL;
6592
6593 FOR_ALL_WINDOWS(wp)
6594 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer))
6595 return wp;
6596
6597 return NULL;
6598}
6599
6600/*
6601 * Free the entire quickfix/location list stack.
6602 * If the quickfix/location list window is open, then clear it.
6603 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006604 static void
6605qf_free_stack(win_T *wp, qf_info_T *qi)
6606{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006607 win_T *qfwin = qf_find_win(qi);
6608 win_T *llwin = NULL;
6609 win_T *orig_wp = wp;
6610
6611 if (qfwin != NULL)
6612 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006613 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006614 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006615 qf_free(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006616 qf_update_buffer(qi, NULL);
6617 }
6618
6619 if (wp != NULL && IS_LL_WINDOW(wp))
6620 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006621 // If in the location list window, then use the non-location list
6622 // window with this location list (if present)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006623 llwin = find_win_with_ll(qi);
6624 if (llwin != NULL)
6625 wp = llwin;
6626 }
6627
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006628 qf_free_all(wp);
6629 if (wp == NULL)
6630 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006631 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006632 qi->qf_curlist = 0;
6633 qi->qf_listcount = 0;
6634 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006635 else if (IS_LL_WINDOW(orig_wp))
6636 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006637 // If the location list window is open, then create a new empty
6638 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006639 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02006640
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006641 // first free the list reference in the location list window
Bram Moolenaard788f6f2017-04-23 17:19:43 +02006642 ll_free_all(&orig_wp->w_llist_ref);
6643
Bram Moolenaar69f40be2017-04-02 15:15:49 +02006644 orig_wp->w_llist_ref = new_ll;
6645 if (llwin != NULL)
6646 {
6647 llwin->w_llist = new_ll;
6648 new_ll->qf_refcount++;
6649 }
6650 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006651}
6652
Bram Moolenaard823fa92016-08-12 16:29:27 +02006653/*
6654 * Populate the quickfix list with the items supplied in the list
6655 * of dictionaries. "title" will be copied to w:quickfix_title.
6656 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
6657 */
6658 int
6659set_errorlist(
6660 win_T *wp,
6661 list_T *list,
6662 int action,
6663 char_u *title,
6664 dict_T *what)
6665{
6666 qf_info_T *qi = &ql_info;
6667 int retval = OK;
6668
6669 if (wp != NULL)
6670 {
6671 qi = ll_get_or_alloc_list(wp);
6672 if (qi == NULL)
6673 return FAIL;
6674 }
6675
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006676 if (action == 'f')
6677 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006678 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006679 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006680 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02006681 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006682
6683 incr_quickfix_busy();
6684
6685 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02006686 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02006687 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01006688 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02006689 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006690 if (retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006691 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006692 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006693
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006694 decr_quickfix_busy();
6695
Bram Moolenaard823fa92016-08-12 16:29:27 +02006696 return retval;
6697}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006698
Bram Moolenaar18cebf42018-05-08 22:31:37 +02006699/*
6700 * Mark the context as in use for all the lists in a quickfix stack.
6701 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006702 static int
6703mark_quickfix_ctx(qf_info_T *qi, int copyID)
6704{
6705 int i;
6706 int abort = FALSE;
6707 typval_T *ctx;
6708
6709 for (i = 0; i < LISTCOUNT && !abort; ++i)
6710 {
6711 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02006712 if (ctx != NULL && ctx->v_type != VAR_NUMBER
6713 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006714 abort = set_ref_in_item(ctx, copyID, NULL, NULL);
6715 }
6716
6717 return abort;
6718}
6719
6720/*
6721 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006722 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006723 */
6724 int
6725set_ref_in_quickfix(int copyID)
6726{
6727 int abort = FALSE;
6728 tabpage_T *tp;
6729 win_T *win;
6730
6731 abort = mark_quickfix_ctx(&ql_info, copyID);
6732 if (abort)
6733 return abort;
6734
6735 FOR_ALL_TAB_WINDOWS(tp, win)
6736 {
6737 if (win->w_llist != NULL)
6738 {
6739 abort = mark_quickfix_ctx(win->w_llist, copyID);
6740 if (abort)
6741 return abort;
6742 }
Bram Moolenaar12237442017-12-19 12:38:52 +01006743 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
6744 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006745 // In a location list window and none of the other windows is
6746 // referring to this location list. Mark the location list
6747 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01006748 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
6749 if (abort)
6750 return abort;
6751 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006752 }
6753
6754 return abort;
6755}
Bram Moolenaar05159a02005-02-26 23:04:13 +00006756#endif
6757
Bram Moolenaar81695252004-12-29 20:58:21 +00006758/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00006759 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006760 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006761 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006762 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00006763 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00006764 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006765 */
6766 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006767ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006768{
6769 buf_T *buf = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006770 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006771 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006772 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006773 int_u save_qfid;
6774 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006775
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006776 switch (eap->cmdidx)
6777 {
6778 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break;
6779 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break;
6780 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break;
6781 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break;
6782 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break;
6783 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break;
6784 default: break;
6785 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006786 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6787 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006788 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006789#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006790 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006791 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006792#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006793 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006794
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006795 // Must come after autocommands.
Bram Moolenaar39665952018-08-15 20:59:48 +02006796 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006797 {
6798 qi = ll_get_or_alloc_list(curwin);
6799 if (qi == NULL)
6800 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006801 wp = curwin;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01006802 }
6803
Bram Moolenaar86b68352004-12-27 21:59:20 +00006804 if (*eap->arg == NUL)
6805 buf = curbuf;
6806 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
6807 buf = buflist_findnr(atoi((char *)eap->arg));
6808 if (buf == NULL)
6809 EMSG(_(e_invarg));
6810 else if (buf->b_ml.ml_mfp == NULL)
6811 EMSG(_("E681: Buffer is not loaded"));
6812 else
6813 {
6814 if (eap->addr_count == 0)
6815 {
6816 eap->line1 = 1;
6817 eap->line2 = buf->b_ml.ml_line_count;
6818 }
6819 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
6820 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
6821 EMSG(_(e_invrange));
6822 else
Bram Moolenaar754b5602006-02-09 23:53:20 +00006823 {
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006824 char_u *qf_title = qf_cmdtitle(*eap->cmdlinep);
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006825
6826 if (buf->b_sfname)
6827 {
6828 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
6829 (char *)qf_title, (char *)buf->b_sfname);
6830 qf_title = IObuff;
6831 }
6832
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006833 incr_quickfix_busy();
6834
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006835 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006836 (eap->cmdidx != CMD_caddbuffer
6837 && eap->cmdidx != CMD_laddbuffer),
Bram Moolenaar7fd73202010-07-25 16:58:46 +02006838 eap->line1, eap->line2,
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006839 qf_title, NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006840 if (qf_stack_empty(qi))
6841 {
6842 decr_quickfix_busy();
6843 return;
6844 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006845 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006846 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006847
6848 // Remember the current quickfix list identifier, so that we can
6849 // check for autocommands changing the current quickfix list.
6850 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006851 if (au_name != NULL)
Bram Moolenaar600323b2018-06-16 22:16:47 +02006852 {
6853 buf_T *curbuf_old = curbuf;
6854
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006855 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6856 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar600323b2018-06-16 22:16:47 +02006857 if (curbuf != curbuf_old)
6858 // Autocommands changed buffer, don't jump now, "qi" may
6859 // be invalid.
6860 res = 0;
6861 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006862 // Jump to the first error for a new list and if autocmds didn't
6863 // free the list.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006864 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006865 eap->cmdidx == CMD_lbuffer)
6866 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006867 // display the first error
6868 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006869
6870 decr_quickfix_busy();
Bram Moolenaar754b5602006-02-09 23:53:20 +00006871 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006872 }
6873}
6874
Bram Moolenaar1e015462005-09-25 22:16:38 +00006875#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006876/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00006877 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
6878 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006879 */
6880 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006881ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006882{
6883 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006884 qf_info_T *qi = &ql_info;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006885 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006886 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006887 int_u save_qfid;
6888 win_T *wp = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006889
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006890 switch (eap->cmdidx)
6891 {
6892 case CMD_cexpr: au_name = (char_u *)"cexpr"; break;
6893 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break;
6894 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break;
6895 case CMD_lexpr: au_name = (char_u *)"lexpr"; break;
6896 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break;
6897 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break;
6898 default: break;
6899 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01006900 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6901 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006902 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006903#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01006904 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006905 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006906#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006907 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02006908
Bram Moolenaar39665952018-08-15 20:59:48 +02006909 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar3c097222017-12-21 20:54:49 +01006910 {
6911 qi = ll_get_or_alloc_list(curwin);
6912 if (qi == NULL)
6913 return;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006914 wp = curwin;
Bram Moolenaar3c097222017-12-21 20:54:49 +01006915 }
6916
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006917 // Evaluate the expression. When the result is a string or a list we can
6918 // use it to fill the errorlist.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006919 tv = eval_expr(eap->arg, NULL);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006920 if (tv != NULL)
6921 {
6922 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
6923 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
6924 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006925 incr_quickfix_busy();
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006926 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
Bram Moolenaardb552d602006-03-23 22:59:57 +00006927 (eap->cmdidx != CMD_caddexpr
6928 && eap->cmdidx != CMD_laddexpr),
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006929 (linenr_T)0, (linenr_T)0,
6930 qf_cmdtitle(*eap->cmdlinep), NULL);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006931 if (qf_stack_empty(qi))
6932 {
6933 decr_quickfix_busy();
6934 goto cleanup;
6935 }
Bram Moolenaarb254af32017-12-18 19:48:58 +01006936 if (res >= 0)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006937 qf_list_changed(&qi->qf_lists[qi->qf_curlist]);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006938
6939 // Remember the current quickfix list identifier, so that we can
6940 // check for autocommands changing the current quickfix list.
6941 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006942 if (au_name != NULL)
6943 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6944 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006945
6946 // Jump to the first error for a new list and if autocmds didn't
6947 // free the list.
Bram Moolenaar0366c012018-06-18 20:52:13 +02006948 if (res > 0 && (eap->cmdidx == CMD_cexpr
6949 || eap->cmdidx == CMD_lexpr)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006950 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006951 // display the first error
6952 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006953 decr_quickfix_busy();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006954 }
6955 else
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006956 EMSG(_("E777: String or List expected"));
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006957cleanup:
Bram Moolenaar4770d092006-01-12 23:22:24 +00006958 free_tv(tv);
6959 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006960}
Bram Moolenaar1e015462005-09-25 22:16:38 +00006961#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006962
6963/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006964 * Get the location list for ":lhelpgrep"
6965 */
6966 static qf_info_T *
6967hgr_get_ll(int *new_ll)
6968{
6969 win_T *wp;
6970 qf_info_T *qi;
6971
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006972 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006973 if (bt_help(curwin->w_buffer))
6974 wp = curwin;
6975 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006976 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02006977 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006978
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006979 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006980 qi = NULL;
6981 else
6982 qi = wp->w_llist;
6983
6984 if (qi == NULL)
6985 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006986 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006987 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02006988 return NULL;
6989 *new_ll = TRUE;
6990 }
6991
6992 return qi;
6993}
6994
6995/*
6996 * Search for a pattern in a help file.
6997 */
6998 static void
6999hgr_search_file(
7000 qf_info_T *qi,
7001 char_u *fname,
7002#ifdef FEAT_MBYTE
7003 vimconv_T *p_vc,
7004#endif
7005 regmatch_T *p_regmatch)
7006{
7007 FILE *fd;
7008 long lnum;
7009
7010 fd = mch_fopen((char *)fname, "r");
7011 if (fd == NULL)
7012 return;
7013
7014 lnum = 1;
7015 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
7016 {
7017 char_u *line = IObuff;
7018#ifdef FEAT_MBYTE
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007019 // Convert a line if 'encoding' is not utf-8 and
7020 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007021 if (p_vc->vc_type != CONV_NONE
7022 && has_non_ascii(IObuff))
7023 {
7024 line = string_convert(p_vc, IObuff, NULL);
7025 if (line == NULL)
7026 line = IObuff;
7027 }
7028#endif
7029
7030 if (vim_regexec(p_regmatch, line, (colnr_T)0))
7031 {
7032 int l = (int)STRLEN(line);
7033
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007034 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007035 while (l > 0 && line[l - 1] <= ' ')
7036 line[--l] = NUL;
7037
7038 if (qf_add_entry(qi,
7039 qi->qf_curlist,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007040 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007041 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02007042 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007043 0,
7044 line,
7045 lnum,
7046 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007047 + 1, // col
7048 FALSE, // vis_col
7049 NULL, // search pattern
7050 0, // nr
7051 1, // type
7052 TRUE // valid
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007053 ) == FAIL)
7054 {
7055 got_int = TRUE;
7056#ifdef FEAT_MBYTE
7057 if (line != IObuff)
7058 vim_free(line);
7059#endif
7060 break;
7061 }
7062 }
7063#ifdef FEAT_MBYTE
7064 if (line != IObuff)
7065 vim_free(line);
7066#endif
7067 ++lnum;
7068 line_breakcheck();
7069 }
7070 fclose(fd);
7071}
7072
7073/*
7074 * Search for a pattern in all the help files in the doc directory under
7075 * the given directory.
7076 */
7077 static void
7078hgr_search_files_in_dir(
7079 qf_info_T *qi,
7080 char_u *dirname,
7081 regmatch_T *p_regmatch
7082#ifdef FEAT_MBYTE
7083 , vimconv_T *p_vc
7084#endif
7085#ifdef FEAT_MULTI_LANG
7086 , char_u *lang
7087#endif
7088 )
7089{
7090 int fcount;
7091 char_u **fnames;
7092 int fi;
7093
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007094 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007095 add_pathsep(dirname);
7096 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
7097 if (gen_expand_wildcards(1, &dirname, &fcount,
7098 &fnames, EW_FILE|EW_SILENT) == OK
7099 && fcount > 0)
7100 {
7101 for (fi = 0; fi < fcount && !got_int; ++fi)
7102 {
7103#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007104 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007105 if (lang != NULL
7106 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02007107 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007108 && !(STRNICMP(lang, "en", 2) == 0
7109 && STRNICMP("txt", fnames[fi]
7110 + STRLEN(fnames[fi]) - 3, 3) == 0))
7111 continue;
7112#endif
7113
7114 hgr_search_file(qi, fnames[fi],
7115#ifdef FEAT_MBYTE
7116 p_vc,
7117#endif
7118 p_regmatch);
7119 }
7120 FreeWild(fcount, fnames);
7121 }
7122}
7123
7124/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007125 * Search for a pattern in all the help files in the 'runtimepath'
7126 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007127 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007128 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007129 */
7130 static void
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007131hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007132{
7133 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007134
7135#ifdef FEAT_MBYTE
7136 vimconv_T vc;
7137
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007138 // Help files are in utf-8 or latin1, convert lines when 'encoding'
7139 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007140 vc.vc_type = CONV_NONE;
7141 if (!enc_utf8)
7142 convert_setup(&vc, (char_u *)"utf-8", p_enc);
7143#endif
7144
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007145 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007146 p = p_rtp;
7147 while (*p != NUL && !got_int)
7148 {
7149 copy_option_part(&p, NameBuff, MAXPATHL, ",");
7150
7151 hgr_search_files_in_dir(qi, NameBuff, p_regmatch
7152#ifdef FEAT_MBYTE
7153 , &vc
7154#endif
7155#ifdef FEAT_MULTI_LANG
7156 , lang
7157#endif
7158 );
7159 }
7160
7161#ifdef FEAT_MBYTE
7162 if (vc.vc_type != CONV_NONE)
7163 convert_setup(&vc, NULL, NULL);
7164#endif
7165}
7166
7167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 * ":helpgrep {pattern}"
7169 */
7170 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007171ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
7173 regmatch_T regmatch;
7174 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007175 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007176 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007177 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007178 char_u *lang = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
Bram Moolenaar73633f82012-01-20 13:39:07 +01007180 switch (eap->cmdidx)
7181 {
7182 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
7183 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
7184 default: break;
7185 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01007186 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7187 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01007188 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007189#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007190 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01007191 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01007192#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007193 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007194
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007195 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73633f82012-01-20 13:39:07 +01007196 save_cpo = p_cpo;
7197 p_cpo = empty_option;
7198
Bram Moolenaar39665952018-08-15 20:59:48 +02007199 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007200 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007201 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007202 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02007203 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007204 }
7205
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007206 incr_quickfix_busy();
7207
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007208#ifdef FEAT_MULTI_LANG
7209 // Check for a specified language
7210 lang = check_help_lang(eap->arg);
7211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
7213 regmatch.rm_ic = FALSE;
7214 if (regmatch.regprog != NULL)
7215 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007216 qf_list_T *qfl;
7217
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007218 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02007219 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007221 hgr_search_in_rtp(qi, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01007222
Bram Moolenaar473de612013-06-08 18:19:48 +02007223 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007225 qfl = &qi->qf_lists[qi->qf_curlist];
7226 qfl->qf_nonevalid = FALSE;
7227 qfl->qf_ptr = qfl->qf_start;
7228 qfl->qf_index = 1;
7229 qf_list_changed(qfl);
7230 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 }
7232
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007233 if (p_cpo == empty_option)
7234 p_cpo = save_cpo;
7235 else
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007236 // Darn, some plugin changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007237 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238
Bram Moolenaar73633f82012-01-20 13:39:07 +01007239 if (au_name != NULL)
7240 {
7241 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7242 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar4d77c652018-08-18 19:59:54 +02007243 if (!new_qi && IS_LL_STACK(qi) && qf_find_buf(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007244 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007245 // autocommands made "qi" invalid
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007246 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01007247 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007248 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007249 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01007250
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007251 // Jump to first match.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02007252 if (!qf_list_empty(qi, qi->qf_curlist))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007253 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007254 else
7255 EMSG2(_(e_nomatch2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007256
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007257 decr_quickfix_busy();
7258
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007259 if (eap->cmdidx == CMD_lhelpgrep)
7260 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02007261 // If the help window is not opened or if it already points to the
7262 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02007263 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007264 {
7265 if (new_qi)
7266 ll_free_all(&qi);
7267 }
7268 else if (curwin->w_llist == NULL)
7269 curwin->w_llist = qi;
7270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271}
7272
7273#endif /* FEAT_QUICKFIX */