blob: f7d35f8054da455c0187a60f5953d2b0577b2783 [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
thinca6864efa2021-06-19 20:45:20 +020033 linenr_T qf_end_lnum; // line number when the error has range or zero
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020034 int qf_fnum; // file number for the line
35 int qf_col; // column where the error occurred
thinca6864efa2021-06-19 20:45:20 +020036 int qf_end_col; // column when the error has range or zero
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020037 int qf_nr; // error number
38 char_u *qf_module; // module name for this error
39 char_u *qf_pattern; // search pattern for the error
40 char_u *qf_text; // description of the error
thinca6864efa2021-06-19 20:45:20 +020041 char_u qf_viscol; // set to TRUE if qf_col and qf_end_col is
42 // screen column
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020043 char_u qf_cleared; // set to TRUE if line has been deleted
44 char_u qf_type; // type of the error (mostly 'E'); 1 for
45 // :helpgrep
46 char_u qf_valid; // valid error message detected
Bram Moolenaar071d4272004-06-13 20:20:40 +000047};
48
49/*
50 * There is a stack of error lists.
51 */
52#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020053#define INVALID_QFIDX (-1)
Bram Moolenaaree8188f2019-02-05 21:23:04 +010054#define INVALID_QFBUFNR (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020056/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010057 * Quickfix list type.
58 */
59typedef enum
60{
61 QFLT_QUICKFIX, // Quickfix list - global list
62 QFLT_LOCATION, // Location list - per window list
63 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
64} qfltype_T;
65
66/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020067 * Quickfix/Location list definition
68 * Contains a list of entries (qfline_T). qf_start points to the first entry
69 * and qf_last points to the last entry. qf_count contains the list size.
70 *
71 * Usually the list contains one or more entries. But an empty list can be
72 * created using setqflist()/setloclist() with a title and/or user context
73 * information and entries can be added later using setqflist()/setloclist().
74 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000075typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020077 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010078 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020079 qfline_T *qf_start; // pointer to the first error
80 qfline_T *qf_last; // pointer to the last error
81 qfline_T *qf_ptr; // pointer to the current error
82 int qf_count; // number of errors (0 means empty list)
83 int qf_index; // current index in the error list
84 int qf_nonevalid; // TRUE if not a single valid entry found
85 char_u *qf_title; // title derived from the command that created
86 // the error list or set by setqflist
87 typval_T *qf_ctx; // context set by setqflist/setloclist
Bram Moolenaard43906d2020-07-20 21:31:32 +020088 callback_T qftf_cb; // 'quickfixtextfunc' callback function
Bram Moolenaara7df8c72017-07-19 13:23:06 +020089
90 struct dir_stack_T *qf_dir_stack;
91 char_u *qf_directory;
92 struct dir_stack_T *qf_file_stack;
93 char_u *qf_currfile;
94 int qf_multiline;
95 int qf_multiignore;
96 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010097 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +000098} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar6a8958d2017-06-22 21:33:20 +0200100/*
101 * Quickfix/Location list stack definition
102 * Contains a list of quickfix/location lists (qf_list_T)
103 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000104struct qf_info_S
105{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200106 // Count of references to this list. Used only for location lists.
107 // When a location list window reference this list, qf_refcount
108 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
109 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200111 int qf_listcount; // current number of lists
112 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000113 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100114 qfltype_T qfl_type; // type of list
Bram Moolenaaree8188f2019-02-05 21:23:04 +0100115 int qf_bufnr; // quickfix window buffer number
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000116};
117
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200118static qf_info_T ql_info; // global quickfix list
119static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200121#define FMT_PATTERNS 11 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
124 * Structure used to hold the info of one part of 'errorformat'
125 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000126typedef struct efm_S efm_T;
127struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200129 regprog_T *prog; // pre-formatted part of 'errorformat'
130 efm_T *next; // pointer to next (NULL if last)
131 char_u addr[FMT_PATTERNS]; // indices of used % patterns
132 char_u prefix; // prefix of this format line:
133 // 'D' enter directory
134 // 'X' leave directory
135 // 'A' start of multi-line message
136 // 'E' error message
137 // 'W' warning message
138 // 'I' informational message
Bram Moolenaare9283662020-06-07 14:10:47 +0200139 // 'N' note message
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200140 // 'C' continuation line
141 // 'Z' end of multi-line message
142 // 'G' general, unspecific message
143 // 'P' push file (partial) message
144 // 'Q' pop/quit file (partial) message
145 // 'O' overread (partial) message
146 char_u flags; // additional flags given in prefix
147 // '-' do not include this line
148 // '+' include whole line in message
149 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150};
151
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200152// List of location lists to be deleted.
153// Used to delay the deletion of locations lists by autocmds.
154typedef struct qf_delq_S
155{
156 struct qf_delq_S *next;
157 qf_info_T *qi;
158} qf_delq_T;
159static qf_delq_T *qf_delq_head = NULL;
160
161// Counter to prevent autocmds from freeing up location lists when they are
162// still being used.
163static int quickfix_busy = 0;
164
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200165static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100166
Bram Moolenaard43906d2020-07-20 21:31:32 +0200167// callback function for 'quickfixtextfunc'
168static callback_T qftf_cb;
169
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void qf_new_list(qf_info_T *qi, char_u *qf_title);
thinca6864efa2021-06-19 20:45:20 +0200171static int qf_add_entry(qf_list_T *qfl, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, long end_lnum, int col, int end_col, int vis_col, char_u *pattern, int nr, int type, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200172static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100174static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200175static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100176static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200177static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200178static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, int newwin);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100179static void qf_fmt_text(char_u *text, char_u *buf, int bufsize);
thinca6864efa2021-06-19 20:45:20 +0200180static void qf_range_text(qfline_T *qfp, char_u *buf, int bufsize);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100181static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100182static win_T *qf_find_win(qf_info_T *qi);
183static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200184static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +0200185static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100186static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
187static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
188static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
189static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200191// Quickfix window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000192#define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200193// Location list window check helper macro
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000194#define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200195
196// Quickfix and location list stack check helper macros
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100197#define IS_QF_STACK(qi) (qi->qfl_type == QFLT_QUICKFIX)
198#define IS_LL_STACK(qi) (qi->qfl_type == QFLT_LOCATION)
199#define IS_QF_LIST(qfl) (qfl->qfl_type == QFLT_QUICKFIX)
200#define IS_LL_LIST(qfl) (qfl->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200201
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000202/*
203 * Return location list for window 'wp'
204 * For location list window, return the referenced location list
205 */
206#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
207
Bram Moolenaar95946f12019-03-31 15:31:59 +0200208// Macro to loop through all the items in a quickfix list
209// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100210#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
Bram Moolenaar95946f12019-03-31 15:31:59 +0200211 for (i = 1, qfp = qfl->qf_start; \
212 !got_int && i <= qfl->qf_count && qfp != NULL; \
Bram Moolenaara16123a2019-03-28 20:31:07 +0100213 ++i, qfp = qfp->qf_next)
214
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200216 * Looking up a buffer can be slow if there are many. Remember the last one
217 * to make this a lot faster if there are multiple matches in the same file.
218 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200219static char_u *qf_last_bufname = NULL;
220static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200221
Bram Moolenaar4d170af2020-09-13 22:21:22 +0200222static char *e_current_quickfix_list_was_changed =
223 N_("E925: Current quickfix list was changed");
224static char *e_current_location_list_was_changed =
Bram Moolenaar3c097222017-12-21 20:54:49 +0100225 N_("E926: Current location list was changed");
226
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200227/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200228 * Maximum number of bytes allowed per line while reading a errorfile.
229 */
230#define LINE_MAXLEN 4096
231
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200232static struct fmtpattern
233{
234 char_u convchar;
235 char *pattern;
236} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200237 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200238 {'f', ".\\+"}, // only used when at end
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200239 {'n', "\\d\\+"},
240 {'l', "\\d\\+"},
241 {'c', "\\d\\+"},
242 {'t', "."},
243 {'m', ".\\+"},
244 {'r', ".*"},
245 {'p', "[- .]*"},
246 {'v', "\\d\\+"},
247 {'s', ".\\+"},
248 {'o', ".\\+"}
249 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200250
251/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200252 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200253 * See fmt_pat definition above for the list of supported patterns. The
254 * pattern specifier is supplied in "efmpat". The converted pattern is stored
255 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200256 */
257 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200258efmpat_to_regpat(
259 char_u *efmpat,
260 char_u *regpat,
261 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200262 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100263 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200264{
265 char_u *srcptr;
266
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200267 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200268 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200269 // Each errorformat pattern can occur only once
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000270 semsg(_(e_too_many_chr_in_format_string), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200271 return NULL;
272 }
273 if ((idx && idx < 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200274 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200275 || (idx == 6
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200276 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200277 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000278 semsg(_(e_unexpected_chr_in_format_str), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200279 return NULL;
280 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200281 efminfo->addr[idx] = (char_u)++round;
282 *regpat++ = '\\';
283 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200284#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200285 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200286 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200287 // Also match "c:" in the file name, even when
288 // checking for a colon next: "%f:".
289 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200290 STRCPY(regpat, "\\%(\\a:\\)\\=");
291 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200292 }
293#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200294 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200295 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200296 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200297 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200298 // A file name may contain spaces, but this isn't
299 // in "\f". For "%f:%l:%m" there may be a ":" in
300 // the file name. Use ".\{-1,}x" instead (x is
301 // the next character), the requirement that :999:
302 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200303 STRCPY(regpat, ".\\{-1,}");
304 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200305 }
306 else
307 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200308 // File name followed by '\\' or '%': include as
309 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200310 STRCPY(regpat, "\\f\\+");
311 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200312 }
313 }
314 else
315 {
316 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200317 while ((*regpat = *srcptr++) != NUL)
318 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200319 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200320 *regpat++ = '\\';
321 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200322
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200323 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200324}
325
326/*
327 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200328 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200329 */
330 static char_u *
331scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200332 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200333 char_u *efm,
334 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100335 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200336{
337 char_u *efmp = *pefmp;
338
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200339 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200340 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200341 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200342 {
343 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200344 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200345 if (efmp < efm + len)
346 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200347 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200348 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200349 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200350 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200351 if (efmp == efm + len)
352 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000353 emsg(_(e_missing_rsb_in_format_string));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200354 return NULL;
355 }
356 }
357 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200358 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200359 *regpat++ = *++efmp;
360 *regpat++ = '\\';
361 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200362 }
363 else
364 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200365 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000366 semsg(_(e_unsupported_chr_in_format_string), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200367 return NULL;
368 }
369
370 *pefmp = efmp;
371
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200372 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200373}
374
375/*
376 * Analyze/parse an errorformat prefix.
377 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200378 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100379efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200380{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200381 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200382 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200383 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200384 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200385 else
386 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000387 semsg(_(e_invalid_chr_in_format_string_prefix), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200388 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200389 }
390
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200391 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200392}
393
394/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200395 * Converts a 'errorformat' string part in 'efm' to a regular expression
396 * pattern. The resulting regex pattern is returned in "regpat". Additional
397 * information about the 'erroformat' pattern is returned in "fmt_ptr".
398 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200399 */
400 static int
401efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200402 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200403 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200404 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100405 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200406{
407 char_u *ptr;
408 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200409 int round;
410 int idx = 0;
411
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200412 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200413 ptr = regpat;
414 *ptr++ = '^';
415 round = 0;
416 for (efmp = efm; efmp < efm + len; ++efmp)
417 {
418 if (*efmp == '%')
419 {
420 ++efmp;
421 for (idx = 0; idx < FMT_PATTERNS; ++idx)
422 if (fmt_pat[idx].convchar == *efmp)
423 break;
424 if (idx < FMT_PATTERNS)
425 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100426 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200427 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200428 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200429 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200430 }
431 else if (*efmp == '*')
432 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200433 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100434 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200435 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200436 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200437 }
438 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200439 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200440 else if (*efmp == '#')
441 *ptr++ = '*';
442 else if (*efmp == '>')
443 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200444 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200445 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200446 // prefix is allowed only at the beginning of the errorformat
447 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100448 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200449 if (efmp == NULL)
450 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200451 }
452 else
453 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000454 semsg(_(e_invalid_chr_in_format_string), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200455 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200456 }
457 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200458 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200459 {
460 if (*efmp == '\\' && efmp + 1 < efm + len)
461 ++efmp;
462 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200463 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200464 if (*efmp)
465 *ptr++ = *efmp;
466 }
467 }
468 *ptr++ = '$';
469 *ptr = NUL;
470
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200471 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200472}
473
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200474/*
475 * Free the 'errorformat' information list
476 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200477 static void
478free_efm_list(efm_T **efm_first)
479{
480 efm_T *efm_ptr;
481
482 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
483 {
484 *efm_first = efm_ptr->next;
485 vim_regfree(efm_ptr->prog);
486 vim_free(efm_ptr);
487 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100488 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200489}
490
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200491/*
492 * Compute the size of the buffer used to convert a 'errorformat' pattern into
493 * a regular expression pattern.
494 */
495 static int
496efm_regpat_bufsz(char_u *efm)
497{
498 int sz;
499 int i;
500
501 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
502 for (i = FMT_PATTERNS; i > 0; )
503 sz += (int)STRLEN(fmt_pat[--i].pattern);
504#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200505 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200506#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200507 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200508#endif
509
510 return sz;
511}
512
513/*
514 * Return the length of a 'errorformat' option part (separated by ",").
515 */
516 static int
517efm_option_part_len(char_u *efm)
518{
519 int len;
520
521 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
522 if (efm[len] == '\\' && efm[len + 1] != NUL)
523 ++len;
524
525 return len;
526}
527
528/*
529 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
530 * are parsed and converted to regular expressions. Returns information about
531 * the parsed 'errorformat' option.
532 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200533 static efm_T *
534parse_efm_option(char_u *efm)
535{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200536 efm_T *fmt_ptr = NULL;
537 efm_T *fmt_first = NULL;
538 efm_T *fmt_last = NULL;
539 char_u *fmtstr = NULL;
540 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200541 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200542
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 Moolenaarc799fe22019-05-28 23:08:19 +0200554 fmt_ptr = ALLOC_CLEAR_ONE(efm_T);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200555 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 Moolenaarf9e3e092019-01-13 23:38:42 +0100566 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == 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 Moolenaarac78dd42022-01-02 19:25:26 +0000575 emsg(_(e_errorformat_contains_no_pattern));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200576
577 goto parse_efm_end;
578
579parse_efm_error:
580 free_efm_list(&fmt_first);
581
582parse_efm_end:
583 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200584
585 return fmt_first;
586}
587
Bram Moolenaare0d37972016-07-15 22:36:01 +0200588enum {
589 QF_FAIL = 0,
590 QF_OK = 1,
591 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200592 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200593 QF_IGNORE_LINE = 4,
594 QF_MULTISCAN = 5,
Bram Moolenaare0d37972016-07-15 22:36:01 +0200595};
596
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200597/*
598 * State information used to parse lines and add entries to a quickfix/location
599 * list.
600 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200601typedef struct {
602 char_u *linebuf;
603 int linelen;
604 char_u *growbuf;
605 int growbufsiz;
606 FILE *fd;
607 typval_T *tv;
608 char_u *p_str;
609 listitem_T *p_li;
610 buf_T *buf;
611 linenr_T buflnum;
612 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100613 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200614} qfstate_T;
615
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200616/*
617 * Allocate more memory for the line buffer used for parsing lines.
618 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200619 static char_u *
620qf_grow_linebuf(qfstate_T *state, int newsz)
621{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200622 char_u *p;
623
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200624 // If the line exceeds LINE_MAXLEN exclude the last
625 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200626 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
627 if (state->growbuf == NULL)
628 {
629 state->growbuf = alloc(state->linelen + 1);
630 if (state->growbuf == NULL)
631 return NULL;
632 state->growbufsiz = state->linelen;
633 }
634 else if (state->linelen > state->growbufsiz)
635 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200636 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200637 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200638 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200639 state->growbufsiz = state->linelen;
640 }
641 return state->growbuf;
642}
643
644/*
645 * Get the next string (separated by newline) from state->p_str.
646 */
647 static int
648qf_get_next_str_line(qfstate_T *state)
649{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200650 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200651 char_u *p_str = state->p_str;
652 char_u *p;
653 int len;
654
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200655 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200656 return QF_END_OF_INPUT;
657
658 p = vim_strchr(p_str, '\n');
659 if (p != NULL)
660 len = (int)(p - p_str) + 1;
661 else
662 len = (int)STRLEN(p_str);
663
664 if (len > IOSIZE - 2)
665 {
666 state->linebuf = qf_grow_linebuf(state, len);
667 if (state->linebuf == NULL)
668 return QF_NOMEM;
669 }
670 else
671 {
672 state->linebuf = IObuff;
673 state->linelen = len;
674 }
675 vim_strncpy(state->linebuf, p_str, state->linelen);
676
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200677 // Increment using len in order to discard the rest of the
678 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200679 p_str += len;
680 state->p_str = p_str;
681
682 return QF_OK;
683}
684
685/*
686 * Get the next string from state->p_Li.
687 */
688 static int
689qf_get_next_list_line(qfstate_T *state)
690{
691 listitem_T *p_li = state->p_li;
692 int len;
693
694 while (p_li != NULL
695 && (p_li->li_tv.v_type != VAR_STRING
696 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200697 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200698
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200699 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200700 {
701 state->p_li = NULL;
702 return QF_END_OF_INPUT;
703 }
704
705 len = (int)STRLEN(p_li->li_tv.vval.v_string);
706 if (len > IOSIZE - 2)
707 {
708 state->linebuf = qf_grow_linebuf(state, len);
709 if (state->linebuf == NULL)
710 return QF_NOMEM;
711 }
712 else
713 {
714 state->linebuf = IObuff;
715 state->linelen = len;
716 }
717
718 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
719
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200720 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200721 return QF_OK;
722}
723
724/*
725 * Get the next string from state->buf.
726 */
727 static int
728qf_get_next_buf_line(qfstate_T *state)
729{
730 char_u *p_buf = NULL;
731 int len;
732
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200733 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200734 if (state->buflnum > state->lnumlast)
735 return QF_END_OF_INPUT;
736
737 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
738 state->buflnum += 1;
739
740 len = (int)STRLEN(p_buf);
741 if (len > IOSIZE - 2)
742 {
743 state->linebuf = qf_grow_linebuf(state, len);
744 if (state->linebuf == NULL)
745 return QF_NOMEM;
746 }
747 else
748 {
749 state->linebuf = IObuff;
750 state->linelen = len;
751 }
752 vim_strncpy(state->linebuf, p_buf, state->linelen);
753
754 return QF_OK;
755}
756
757/*
758 * Get the next string from file state->fd.
759 */
760 static int
761qf_get_next_file_line(qfstate_T *state)
762{
763 int discard;
764 int growbuflen;
765
766 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
767 return QF_END_OF_INPUT;
768
769 discard = FALSE;
770 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200771 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200772 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200773 // The current line exceeds IObuff, continue reading using
774 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200775 if (state->growbuf == NULL)
776 {
777 state->growbufsiz = 2 * (IOSIZE - 1);
778 state->growbuf = alloc(state->growbufsiz);
779 if (state->growbuf == NULL)
780 return QF_NOMEM;
781 }
782
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200783 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200784 memcpy(state->growbuf, IObuff, IOSIZE - 1);
785 growbuflen = state->linelen;
786
787 for (;;)
788 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200789 char_u *p;
790
Bram Moolenaare0d37972016-07-15 22:36:01 +0200791 if (fgets((char *)state->growbuf + growbuflen,
792 state->growbufsiz - growbuflen, state->fd) == NULL)
793 break;
794 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
795 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200796 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200797 break;
798 if (state->growbufsiz == LINE_MAXLEN)
799 {
800 discard = TRUE;
801 break;
802 }
803
804 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
805 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200806 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200807 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200808 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200809 }
810
811 while (discard)
812 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200813 // The current line is longer than LINE_MAXLEN, continue
814 // reading but discard everything until EOL or EOF is
815 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200816 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
817 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar59941cb2020-09-05 17:03:40 +0200818 || IObuff[IOSIZE - 2] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200819 break;
820 }
821
822 state->linebuf = state->growbuf;
823 state->linelen = growbuflen;
824 }
825 else
826 state->linebuf = IObuff;
827
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200828 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200829 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
830 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100831 char_u *line;
832
833 line = string_convert(&state->vc, state->linebuf, &state->linelen);
834 if (line != NULL)
835 {
836 if (state->linelen < IOSIZE)
837 {
838 STRCPY(state->linebuf, line);
839 vim_free(line);
840 }
841 else
842 {
843 vim_free(state->growbuf);
844 state->linebuf = state->growbuf = line;
845 state->growbufsiz = state->linelen < LINE_MAXLEN
846 ? state->linelen : LINE_MAXLEN;
847 }
848 }
849 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100850
Bram Moolenaare0d37972016-07-15 22:36:01 +0200851 return QF_OK;
852}
853
854/*
855 * Get the next string from a file/buffer/list/string.
856 */
857 static int
858qf_get_nextline(qfstate_T *state)
859{
860 int status = QF_FAIL;
861
862 if (state->fd == NULL)
863 {
864 if (state->tv != NULL)
865 {
866 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200867 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200868 status = qf_get_next_str_line(state);
869 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200870 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200871 status = qf_get_next_list_line(state);
872 }
873 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200874 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200875 status = qf_get_next_buf_line(state);
876 }
877 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200878 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200879 status = qf_get_next_file_line(state);
880
881 if (status != QF_OK)
882 return status;
883
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200884 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200885 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200886 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200887 state->linebuf[state->linelen - 1] = NUL;
888#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200889 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
890 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200891#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200892 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200893
Bram Moolenaare0d37972016-07-15 22:36:01 +0200894 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200895
896 return QF_OK;
897}
898
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200899typedef struct {
900 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200901 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200902 char_u *errmsg;
903 int errmsglen;
904 long lnum;
thinca6864efa2021-06-19 20:45:20 +0200905 long end_lnum;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200906 int col;
thinca6864efa2021-06-19 20:45:20 +0200907 int end_col;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200908 char_u use_viscol;
909 char_u *pattern;
910 int enr;
911 int type;
912 int valid;
913} qffields_T;
914
915/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200916 * Parse the match for filename ('%f') pattern in regmatch.
917 * Return the matched value in "fields->namebuf".
918 */
919 static int
920qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
921{
922 int c;
923
924 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
925 return QF_FAIL;
926
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200927 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200928 c = *rmp->endp[midx];
929 *rmp->endp[midx] = NUL;
930 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
931 *rmp->endp[midx] = c;
932
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200933 // For separate filename patterns (%O, %P and %Q), the specified file
934 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200935 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
936 && mch_getperm(fields->namebuf) == -1)
937 return QF_FAIL;
938
939 return QF_OK;
940}
941
942/*
943 * Parse the match for error number ('%n') pattern in regmatch.
944 * Return the matched value in "fields->enr".
945 */
946 static int
947qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
948{
949 if (rmp->startp[midx] == NULL)
950 return QF_FAIL;
951 fields->enr = (int)atol((char *)rmp->startp[midx]);
952 return QF_OK;
953}
954
955/*
956 * Parse the match for line number (%l') pattern in regmatch.
957 * Return the matched value in "fields->lnum".
958 */
959 static int
960qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
961{
962 if (rmp->startp[midx] == NULL)
963 return QF_FAIL;
964 fields->lnum = atol((char *)rmp->startp[midx]);
965 return QF_OK;
966}
967
968/*
969 * Parse the match for column number ('%c') pattern in regmatch.
970 * Return the matched value in "fields->col".
971 */
972 static int
973qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
974{
975 if (rmp->startp[midx] == NULL)
976 return QF_FAIL;
977 fields->col = (int)atol((char *)rmp->startp[midx]);
978 return QF_OK;
979}
980
981/*
982 * Parse the match for error type ('%t') pattern in regmatch.
983 * Return the matched value in "fields->type".
984 */
985 static int
986qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
987{
988 if (rmp->startp[midx] == NULL)
989 return QF_FAIL;
990 fields->type = *rmp->startp[midx];
991 return QF_OK;
992}
993
994/*
Bram Moolenaarf4140482020-02-15 23:06:45 +0100995 * Copy a non-error line into the error string. Return the matched line in
996 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200997 */
998 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +0100999copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001000{
1001 char_u *p;
1002
1003 if (linelen >= fields->errmsglen)
1004 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001005 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001006 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1007 return QF_NOMEM;
1008 fields->errmsg = p;
1009 fields->errmsglen = linelen + 1;
1010 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001011 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001012 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001013
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001014 return QF_OK;
1015}
1016
1017/*
1018 * Parse the match for error message ('%m') pattern in regmatch.
1019 * Return the matched value in "fields->errmsg".
1020 */
1021 static int
1022qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1023{
1024 char_u *p;
1025 int len;
1026
1027 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1028 return QF_FAIL;
1029 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1030 if (len >= fields->errmsglen)
1031 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001032 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001033 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1034 return QF_NOMEM;
1035 fields->errmsg = p;
1036 fields->errmsglen = len + 1;
1037 }
1038 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1039 return QF_OK;
1040}
1041
1042/*
1043 * Parse the match for rest of a single-line file message ('%r') pattern.
1044 * Return the matched value in "tail".
1045 */
1046 static int
1047qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1048{
1049 if (rmp->startp[midx] == NULL)
1050 return QF_FAIL;
1051 *tail = rmp->startp[midx];
1052 return QF_OK;
1053}
1054
1055/*
1056 * Parse the match for the pointer line ('%p') pattern in regmatch.
1057 * Return the matched value in "fields->col".
1058 */
1059 static int
1060qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1061{
1062 char_u *match_ptr;
1063
1064 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1065 return QF_FAIL;
1066 fields->col = 0;
1067 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1068 ++match_ptr)
1069 {
1070 ++fields->col;
1071 if (*match_ptr == TAB)
1072 {
1073 fields->col += 7;
1074 fields->col -= fields->col % 8;
1075 }
1076 }
1077 ++fields->col;
1078 fields->use_viscol = TRUE;
1079 return QF_OK;
1080}
1081
1082/*
1083 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1084 * Return the matched value in "fields->col".
1085 */
1086 static int
1087qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1088{
1089 if (rmp->startp[midx] == NULL)
1090 return QF_FAIL;
1091 fields->col = (int)atol((char *)rmp->startp[midx]);
1092 fields->use_viscol = TRUE;
1093 return QF_OK;
1094}
1095
1096/*
1097 * Parse the match for the search text ('%s') pattern in regmatch.
1098 * Return the matched value in "fields->pattern".
1099 */
1100 static int
1101qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1102{
1103 int len;
1104
1105 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1106 return QF_FAIL;
1107 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1108 if (len > CMDBUFFSIZE - 5)
1109 len = CMDBUFFSIZE - 5;
1110 STRCPY(fields->pattern, "^\\V");
1111 STRNCAT(fields->pattern, rmp->startp[midx], len);
1112 fields->pattern[len + 3] = '\\';
1113 fields->pattern[len + 4] = '$';
1114 fields->pattern[len + 5] = NUL;
1115 return QF_OK;
1116}
1117
1118/*
1119 * Parse the match for the module ('%o') pattern in regmatch.
1120 * Return the matched value in "fields->module".
1121 */
1122 static int
1123qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1124{
1125 int len;
1126
1127 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1128 return QF_FAIL;
1129 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1130 if (len > CMDBUFFSIZE)
1131 len = CMDBUFFSIZE;
1132 STRNCAT(fields->module, rmp->startp[midx], len);
1133 return QF_OK;
1134}
1135
1136/*
1137 * 'errorformat' format pattern parser functions.
1138 * The '%f' and '%r' formats are parsed differently from other formats.
1139 * See qf_parse_match() for details.
1140 */
1141static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1142{
1143 NULL,
1144 qf_parse_fmt_n,
1145 qf_parse_fmt_l,
1146 qf_parse_fmt_c,
1147 qf_parse_fmt_t,
1148 qf_parse_fmt_m,
1149 NULL,
1150 qf_parse_fmt_p,
1151 qf_parse_fmt_v,
1152 qf_parse_fmt_s,
1153 qf_parse_fmt_o
1154};
1155
1156/*
1157 * Parse the error format pattern matches in "regmatch" and set the values in
1158 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1159 * match. Returns QF_OK if all the matches are successfully parsed. On
1160 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001161 */
1162 static int
1163qf_parse_match(
1164 char_u *linebuf,
1165 int linelen,
1166 efm_T *fmt_ptr,
1167 regmatch_T *regmatch,
1168 qffields_T *fields,
1169 int qf_multiline,
1170 int qf_multiscan,
1171 char_u **tail)
1172{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001173 int idx = fmt_ptr->prefix;
1174 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001175 int midx;
1176 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001177
1178 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1179 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001180 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001181 fields->type = idx;
1182 else
1183 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001184
1185 // Extract error message data from matched line.
1186 // We check for an actual submatch, because "\[" and "\]" in
1187 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001188 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001189 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001190 status = QF_OK;
1191 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001192 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001193 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
1194 else if (i == 5)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001195 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001196 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001197 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001198 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001199 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001200 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001201 else if (i == 6 && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001202 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001203 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001204 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001205
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001206 if (status != QF_OK)
1207 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001208 }
1209
1210 return QF_OK;
1211}
1212
1213/*
1214 * Parse an error line in 'linebuf' using a single error format string in
1215 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1216 * Returns QF_OK if the efm format matches completely and the fields are
1217 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1218 */
1219 static int
1220qf_parse_get_fields(
1221 char_u *linebuf,
1222 int linelen,
1223 efm_T *fmt_ptr,
1224 qffields_T *fields,
1225 int qf_multiline,
1226 int qf_multiscan,
1227 char_u **tail)
1228{
1229 regmatch_T regmatch;
1230 int status = QF_FAIL;
1231 int r;
1232
1233 if (qf_multiscan &&
1234 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1235 return QF_FAIL;
1236
1237 fields->namebuf[0] = NUL;
1238 fields->module[0] = NUL;
1239 fields->pattern[0] = NUL;
1240 if (!qf_multiscan)
1241 fields->errmsg[0] = NUL;
1242 fields->lnum = 0;
thinca6864efa2021-06-19 20:45:20 +02001243 fields->end_lnum = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001244 fields->col = 0;
thinca6864efa2021-06-19 20:45:20 +02001245 fields->end_col = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001246 fields->use_viscol = FALSE;
1247 fields->enr = -1;
1248 fields->type = 0;
1249 *tail = NULL;
1250
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001251 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001252 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001253 regmatch.regprog = fmt_ptr->prog;
1254 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1255 fmt_ptr->prog = regmatch.regprog;
1256 if (r)
1257 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1258 fields, qf_multiline, qf_multiscan, tail);
1259
1260 return status;
1261}
1262
1263/*
1264 * Parse directory error format prefixes (%D and %X).
1265 * Push and pop directories from the directory stack when scanning directory
1266 * names.
1267 */
1268 static int
1269qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1270{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001271 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001272 {
1273 if (*fields->namebuf == NUL)
1274 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001275 emsg(_(e_missing_or_empty_directory_name));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001276 return QF_FAIL;
1277 }
1278 qfl->qf_directory =
1279 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1280 if (qfl->qf_directory == NULL)
1281 return QF_FAIL;
1282 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001283 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001284 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1285
1286 return QF_OK;
1287}
1288
1289/*
1290 * Parse global file name error format prefixes (%O, %P and %Q).
1291 */
1292 static int
1293qf_parse_file_pfx(
1294 int idx,
1295 qffields_T *fields,
1296 qf_list_T *qfl,
1297 char_u *tail)
1298{
1299 fields->valid = FALSE;
1300 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1301 {
1302 if (*fields->namebuf && idx == 'P')
1303 qfl->qf_currfile =
1304 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1305 else if (idx == 'Q')
1306 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1307 *fields->namebuf = NUL;
1308 if (tail && *tail)
1309 {
1310 STRMOVE(IObuff, skipwhite(tail));
1311 qfl->qf_multiscan = TRUE;
1312 return QF_MULTISCAN;
1313 }
1314 }
1315
1316 return QF_OK;
1317}
1318
1319/*
1320 * Parse a non-error line (a line which doesn't match any of the error
1321 * format in 'efm').
1322 */
1323 static int
1324qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1325{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001326 fields->namebuf[0] = NUL; // no match found, remove file name
1327 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001328 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001329
Bram Moolenaarf4140482020-02-15 23:06:45 +01001330 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001331}
1332
1333/*
1334 * Parse multi-line error format prefixes (%C and %Z)
1335 */
1336 static int
1337qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001338 int idx,
1339 qf_list_T *qfl,
1340 qffields_T *fields)
1341{
1342 char_u *ptr;
1343 int len;
1344
1345 if (!qfl->qf_multiignore)
1346 {
1347 qfline_T *qfprev = qfl->qf_last;
1348
1349 if (qfprev == NULL)
1350 return QF_FAIL;
1351 if (*fields->errmsg && !qfl->qf_multiignore)
1352 {
1353 len = (int)STRLEN(qfprev->qf_text);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001354 if ((ptr = alloc(len + STRLEN(fields->errmsg) + 2))
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001355 == NULL)
1356 return QF_FAIL;
1357 STRCPY(ptr, qfprev->qf_text);
1358 vim_free(qfprev->qf_text);
1359 qfprev->qf_text = ptr;
1360 *(ptr += len) = '\n';
1361 STRCPY(++ptr, fields->errmsg);
1362 }
1363 if (qfprev->qf_nr == -1)
1364 qfprev->qf_nr = fields->enr;
1365 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001366 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001367 qfprev->qf_type = fields->type;
1368
1369 if (!qfprev->qf_lnum)
1370 qfprev->qf_lnum = fields->lnum;
1371 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001372 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001373 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001374 qfprev->qf_viscol = fields->use_viscol;
1375 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001376 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001377 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001378 qfl->qf_directory,
1379 *fields->namebuf || qfl->qf_directory != NULL
1380 ? fields->namebuf
1381 : qfl->qf_currfile != NULL && fields->valid
1382 ? qfl->qf_currfile : 0);
1383 }
1384 if (idx == 'Z')
1385 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1386 line_breakcheck();
1387
1388 return QF_IGNORE_LINE;
1389}
1390
1391/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001392 * Parse a line and get the quickfix fields.
1393 * Return the QF_ status.
1394 */
1395 static int
1396qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001397 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001398 char_u *linebuf,
1399 int linelen,
1400 efm_T *fmt_first,
1401 qffields_T *fields)
1402{
1403 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001404 int idx = 0;
1405 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001406 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001407
Bram Moolenaare333e792018-04-08 13:27:39 +02001408restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001409 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001410 if (fmt_start == NULL)
1411 fmt_ptr = fmt_first;
1412 else
1413 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001414 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001415 fmt_ptr = fmt_start;
1416 fmt_start = NULL;
1417 }
1418
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001419 // Try to match each part of 'errorformat' until we find a complete
1420 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001421 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001422 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1423 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001424 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001425 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1426 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1427 if (status == QF_NOMEM)
1428 return status;
1429 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001430 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001431 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001432 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001433
1434 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1435 {
1436 if (fmt_ptr != NULL)
1437 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001438 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001439 status = qf_parse_dir_pfx(idx, fields, qfl);
1440 if (status != QF_OK)
1441 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001442 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001443
1444 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1445 if (status != QF_OK)
1446 return status;
1447
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001448 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001449 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001450 }
1451 else if (fmt_ptr != NULL)
1452 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001453 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001454 if (fmt_ptr->conthere)
1455 fmt_start = fmt_ptr;
1456
Bram Moolenaare9283662020-06-07 14:10:47 +02001457 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001458 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001459 qfl->qf_multiline = TRUE; // start of a multi-line message
1460 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001461 }
1462 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001463 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001464 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001465 if (status != QF_OK)
1466 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001467 }
1468 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001469 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001470 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1471 if (status == QF_MULTISCAN)
1472 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001473 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001474 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001475 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001476 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001477 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001478 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001479 return QF_IGNORE_LINE;
1480 }
1481 }
1482
1483 return QF_OK;
1484}
1485
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001486/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001487 * Returns TRUE if the specified quickfix/location stack is empty
1488 */
1489 static int
1490qf_stack_empty(qf_info_T *qi)
1491{
1492 return qi == NULL || qi->qf_listcount <= 0;
1493}
1494
1495/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001496 * Returns TRUE if the specified quickfix/location list is empty.
1497 */
1498 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001499qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001500{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001501 return qfl == NULL || qfl->qf_count <= 0;
1502}
1503
1504/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001505 * Returns TRUE if the specified quickfix/location list is not empty and
1506 * has valid entries.
1507 */
1508 static int
1509qf_list_has_valid_entries(qf_list_T *qfl)
1510{
1511 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1512}
1513
1514/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001515 * Return a pointer to a list in the specified quickfix stack
1516 */
1517 static qf_list_T *
1518qf_get_list(qf_info_T *qi, int idx)
1519{
1520 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001521}
1522
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001523/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001524 * Allocate the fields used for parsing lines and populating a quickfix list.
1525 */
1526 static int
1527qf_alloc_fields(qffields_T *pfields)
1528{
1529 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1530 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1531 pfields->errmsglen = CMDBUFFSIZE + 1;
1532 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1533 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1534 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1535 || pfields->pattern == NULL || pfields->module == NULL)
1536 return FAIL;
1537
1538 return OK;
1539}
1540
1541/*
1542 * Free the fields used for parsing lines and populating a quickfix list.
1543 */
1544 static void
1545qf_free_fields(qffields_T *pfields)
1546{
1547 vim_free(pfields->namebuf);
1548 vim_free(pfields->module);
1549 vim_free(pfields->errmsg);
1550 vim_free(pfields->pattern);
1551}
1552
1553/*
1554 * Setup the state information used for parsing lines and populating a
1555 * quickfix list.
1556 */
1557 static int
1558qf_setup_state(
1559 qfstate_T *pstate,
1560 char_u *enc,
1561 char_u *efile,
1562 typval_T *tv,
1563 buf_T *buf,
1564 linenr_T lnumfirst,
1565 linenr_T lnumlast)
1566{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001567 pstate->vc.vc_type = CONV_NONE;
1568 if (enc != NULL && *enc != NUL)
1569 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001570
1571 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1572 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001573 semsg(_(e_cant_open_errorfile_str), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001574 return FAIL;
1575 }
1576
1577 if (tv != NULL)
1578 {
1579 if (tv->v_type == VAR_STRING)
1580 pstate->p_str = tv->vval.v_string;
1581 else if (tv->v_type == VAR_LIST)
1582 pstate->p_li = tv->vval.v_list->lv_first;
1583 pstate->tv = tv;
1584 }
1585 pstate->buf = buf;
1586 pstate->buflnum = lnumfirst;
1587 pstate->lnumlast = lnumlast;
1588
1589 return OK;
1590}
1591
1592/*
1593 * Cleanup the state information used for parsing lines and populating a
1594 * quickfix list.
1595 */
1596 static void
1597qf_cleanup_state(qfstate_T *pstate)
1598{
1599 if (pstate->fd != NULL)
1600 fclose(pstate->fd);
1601
1602 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001603 if (pstate->vc.vc_type != CONV_NONE)
1604 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001605}
1606
1607/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001608 * Process the next line from a file/buffer/list/string and add it
1609 * to the quickfix list 'qfl'.
1610 */
1611 static int
1612qf_init_process_nextline(
1613 qf_list_T *qfl,
1614 efm_T *fmt_first,
1615 qfstate_T *state,
1616 qffields_T *fields)
1617{
1618 int status;
1619
1620 // Get the next line from a file/buffer/list/string
1621 status = qf_get_nextline(state);
1622 if (status != QF_OK)
1623 return status;
1624
1625 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1626 fmt_first, fields);
1627 if (status != QF_OK)
1628 return status;
1629
1630 return qf_add_entry(qfl,
1631 qfl->qf_directory,
1632 (*fields->namebuf || qfl->qf_directory != NULL)
1633 ? fields->namebuf
1634 : ((qfl->qf_currfile != NULL && fields->valid)
1635 ? qfl->qf_currfile : (char_u *)NULL),
1636 fields->module,
1637 0,
1638 fields->errmsg,
1639 fields->lnum,
thinca6864efa2021-06-19 20:45:20 +02001640 fields->end_lnum,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001641 fields->col,
thinca6864efa2021-06-19 20:45:20 +02001642 fields->end_col,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001643 fields->use_viscol,
1644 fields->pattern,
1645 fields->enr,
1646 fields->type,
1647 fields->valid);
1648}
1649
1650/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001651 * Read the errorfile "efile" into memory, line by line, building the error
1652 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001653 * Alternative: when "efile" is NULL read errors from buffer "buf".
1654 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001655 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001656 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1657 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001658 * Return -1 for error, number of errors for success.
1659 */
1660 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001661qf_init_ext(
1662 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001663 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001664 char_u *efile,
1665 buf_T *buf,
1666 typval_T *tv,
1667 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001668 int newlist, // TRUE: start a new error list
1669 linenr_T lnumfirst, // first line number to use
1670 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001671 char_u *qf_title,
1672 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001673{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001674 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001675 qfstate_T state;
1676 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001677 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001678 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001679 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001681 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001682 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001683 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001685 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001686 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001687
Bram Moolenaara80faa82020-04-12 19:37:17 +02001688 CLEAR_FIELD(state);
1689 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001690 if ((qf_alloc_fields(&fields) == FAIL) ||
1691 (qf_setup_state(&state, enc, efile, tv, buf,
1692 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 goto qf_init_end;
1694
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001695 if (newlist || qf_idx == qi->qf_listcount)
1696 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001697 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001698 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001699 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001700 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001701 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001702 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001703 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001704 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001705 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001706 qfl = qf_get_list(qi, qf_idx);
1707 if (!qf_list_empty(qfl))
1708 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001711 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001712 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001713 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 else
1715 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001717 // If the errorformat didn't change between calls, then reuse the
1718 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001719 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1720 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001721 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001722 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001723 free_efm_list(&fmt_first);
1724
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001725 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001726 fmt_first = parse_efm_option(efm);
1727 if (fmt_first != NULL)
1728 last_efm = vim_strsave(efm);
1729 }
1730
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001731 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001734 // got_int is reset here, because it was probably set when killing the
1735 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 got_int = FALSE;
1737
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001738 // Read the lines in the error file one by one.
1739 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001740 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001742 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001743 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001744 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001745 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001746 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001747 if (status == QF_FAIL)
1748 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 line_breakcheck();
1751 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001752 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001754 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001756 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001757 qfl->qf_ptr = qfl->qf_start;
1758 qfl->qf_index = 1;
1759 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761 else
1762 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001763 qfl->qf_nonevalid = FALSE;
1764 if (qfl->qf_ptr == NULL)
1765 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001767 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001768 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001769 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 }
Bram Moolenaard8e44472021-07-21 22:20:33 +02001771 emsg(_(e_error_while_reading_errorfile));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001773 if (!adding)
1774 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001775 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001776 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001777 qi->qf_listcount--;
1778 if (qi->qf_curlist > 0)
1779 --qi->qf_curlist;
1780 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001781qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001782 if (qf_idx == qi->qf_curlist)
1783 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001784 qf_cleanup_state(&state);
1785 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
1787 return retval;
1788}
1789
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001790/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001791 * Read the errorfile "efile" into memory, line by line, building the error
1792 * list. Set the error list's title to qf_title.
1793 * Return -1 for error, number of errors for success.
1794 */
1795 int
1796qf_init(win_T *wp,
1797 char_u *efile,
1798 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001799 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001800 char_u *qf_title,
1801 char_u *enc)
1802{
1803 qf_info_T *qi = &ql_info;
1804
1805 if (wp != NULL)
1806 {
1807 qi = ll_get_or_alloc_list(wp);
1808 if (qi == NULL)
1809 return FAIL;
1810 }
1811
1812 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1813 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1814}
1815
1816/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001817 * Set the title of the specified quickfix list. Frees the previous title.
1818 * Prepends ':' to the title.
1819 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001820 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001821qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001822{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001823 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001824
Bram Moolenaarfb604092014-07-23 15:55:00 +02001825 if (title != NULL)
1826 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02001827 char_u *p = alloc(STRLEN(title) + 2);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001828
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001829 qfl->qf_title = p;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001830 if (p != NULL)
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001831 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001832 }
1833}
1834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001836 * The title of a quickfix/location list is set, by default, to the command
1837 * that created the quickfix list with the ":" prefix.
1838 * Create a quickfix list title string by prepending ":" to a user command.
1839 * Returns a pointer to a static buffer with the title.
1840 */
1841 static char_u *
1842qf_cmdtitle(char_u *cmd)
1843{
1844 static char_u qftitle_str[IOSIZE];
1845
1846 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1847 return qftitle_str;
1848}
1849
1850/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001851 * Return a pointer to the current list in the specified quickfix stack
1852 */
1853 static qf_list_T *
1854qf_get_curlist(qf_info_T *qi)
1855{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001856 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001857}
1858
1859/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001860 * Prepare for adding a new quickfix list. If the current list is in the
1861 * middle of the stack, then all the following lists are freed and then
1862 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 */
1864 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001865qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
1867 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001868 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001870 // If the current entry is not the last entry, delete entries beyond
1871 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001872 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001873 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001874 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001876 // When the stack is full, remove to oldest entry
1877 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001878 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001880 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001882 qi->qf_lists[i - 1] = qi->qf_lists[i];
1883 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 }
1885 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001886 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001887 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001888 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001889 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001890 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001891 qfl->qf_id = ++last_qf_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892}
1893
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001894/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001895 * Queue location list stack delete request.
1896 */
1897 static void
1898locstack_queue_delreq(qf_info_T *qi)
1899{
1900 qf_delq_T *q;
1901
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001902 q = ALLOC_ONE(qf_delq_T);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001903 if (q != NULL)
1904 {
1905 q->qi = qi;
1906 q->next = qf_delq_head;
1907 qf_delq_head = q;
1908 }
1909}
1910
1911/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001912 * Return the global quickfix stack window buffer number.
1913 */
1914 int
1915qf_stack_get_bufnr(void)
1916{
1917 return ql_info.qf_bufnr;
1918}
1919
1920/*
1921 * Wipe the quickfix window buffer (if present) for the specified
1922 * quickfix/location list.
1923 */
1924 static void
1925wipe_qf_buffer(qf_info_T *qi)
1926{
1927 buf_T *qfbuf;
1928
1929 if (qi->qf_bufnr != INVALID_QFBUFNR)
1930 {
1931 qfbuf = buflist_findnr(qi->qf_bufnr);
1932 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
1933 {
1934 // If the quickfix buffer is not loaded in any window, then
1935 // wipe the buffer.
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001936 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001937 qi->qf_bufnr = INVALID_QFBUFNR;
1938 }
1939 }
1940}
1941
1942/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001943 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001944 */
1945 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001946ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001947{
1948 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001949 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001950
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001951 qi = *pqi;
1952 if (qi == NULL)
1953 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001954 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001955
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001956 // If the location list is still in use, then queue the delete request
1957 // to be processed later.
1958 if (quickfix_busy > 0)
1959 {
1960 locstack_queue_delreq(qi);
1961 return;
1962 }
1963
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001964 qi->qf_refcount--;
1965 if (qi->qf_refcount < 1)
1966 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001967 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001968 // If the quickfix window buffer is loaded, then wipe it
1969 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001970
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001971 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001972 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001973 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001974 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001975}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001976
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001977/*
1978 * Free all the quickfix/location lists in the stack.
1979 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001980 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001981qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001982{
1983 int i;
1984 qf_info_T *qi = &ql_info;
1985
1986 if (wp != NULL)
1987 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001988 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001989 ll_free_all(&wp->w_llist);
1990 ll_free_all(&wp->w_llist_ref);
1991 }
1992 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001993 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001994 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001995 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001996}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001997
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001999 * Delay freeing of location list stacks when the quickfix code is running.
2000 * Used to avoid problems with autocmds freeing location list stacks when the
2001 * quickfix code is still referencing the stack.
2002 * Must always call decr_quickfix_busy() exactly once after this.
2003 */
2004 static void
2005incr_quickfix_busy(void)
2006{
2007 quickfix_busy++;
2008}
2009
2010/*
2011 * Safe to free location list stacks. Process any delayed delete requests.
2012 */
2013 static void
2014decr_quickfix_busy(void)
2015{
2016 if (--quickfix_busy == 0)
2017 {
2018 // No longer referencing the location lists. Process all the pending
2019 // delete requests.
2020 while (qf_delq_head != NULL)
2021 {
2022 qf_delq_T *q = qf_delq_head;
2023
2024 qf_delq_head = q->next;
2025 ll_free_all(&q->qi);
2026 vim_free(q);
2027 }
2028 }
2029#ifdef ABORT_ON_INTERNAL_ERROR
2030 if (quickfix_busy < 0)
2031 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002032 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002033 abort();
2034 }
2035#endif
2036}
2037
2038#if defined(EXITFREE) || defined(PROTO)
2039 void
2040check_quickfix_busy(void)
2041{
2042 if (quickfix_busy != 0)
2043 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002044 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002045# ifdef ABORT_ON_INTERNAL_ERROR
2046 abort();
2047# endif
2048 }
2049}
2050#endif
2051
2052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 * Add an entry to the end of the list of errors.
Bram Moolenaar95946f12019-03-31 15:31:59 +02002054 * Returns QF_OK or QF_FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 */
2056 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002057qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002058 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002059 char_u *dir, // optional directory name
2060 char_u *fname, // file name or NULL
2061 char_u *module, // module name or NULL
2062 int bufnum, // buffer number or zero
2063 char_u *mesg, // message
2064 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002065 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002066 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002067 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002068 int vis_col, // using visual column
2069 char_u *pattern, // search pattern
2070 int nr, // error number
2071 int type, // type character
2072 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002074 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002075 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002077 if ((qfp = ALLOC_ONE(qfline_T)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002078 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002079 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002080 {
2081 buf_T *buf = buflist_findnr(bufnum);
2082
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002083 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002084 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002085 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002086 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002087 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002088 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002089 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2091 {
2092 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002093 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
2095 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002096 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002098 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002099 qfp->qf_viscol = vis_col;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002100 if (pattern == NULL || *pattern == NUL)
2101 qfp->qf_pattern = NULL;
2102 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2103 {
2104 vim_free(qfp->qf_text);
2105 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002106 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002107 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002108 if (module == NULL || *module == NUL)
2109 qfp->qf_module = NULL;
2110 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2111 {
2112 vim_free(qfp->qf_text);
2113 vim_free(qfp->qf_pattern);
2114 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002115 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002118 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 type = 0;
2120 qfp->qf_type = type;
2121 qfp->qf_valid = valid;
2122
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002123 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002124 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002126 qfl->qf_start = qfp;
2127 qfl->qf_ptr = qfp;
2128 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002129 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
2131 else
2132 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002133 qfp->qf_prev = *lastp;
2134 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002136 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002138 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002139 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002140 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002142 qfl->qf_index = qfl->qf_count;
2143 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 }
2145
Bram Moolenaar95946f12019-03-31 15:31:59 +02002146 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147}
2148
2149/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002150 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002151 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002152 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002153qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002154{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002155 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002156
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002157 qi = ALLOC_CLEAR_ONE(qf_info_T);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002158 if (qi != NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002159 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002160 qi->qf_refcount++;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002161 qi->qfl_type = qfltype;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002162 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002163 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002164 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002165}
2166
2167/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002168 * Return the location list stack for window 'wp'.
2169 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002170 */
2171 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002172ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002173{
2174 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002175 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002176 return wp->w_llist_ref;
2177
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002178 // For a non-location list window, w_llist_ref should not point to a
2179 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002180 ll_free_all(&wp->w_llist_ref);
2181
2182 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002183 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002184 return wp->w_llist;
2185}
2186
2187/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002188 * Get the quickfix/location list stack to use for the specified Ex command.
2189 * For a location list command, returns the stack for the current window. If
2190 * the location list is not found, then returns NULL and prints an error
2191 * message if 'print_emsg' is TRUE.
2192 */
2193 static qf_info_T *
2194qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2195{
2196 qf_info_T *qi = &ql_info;
2197
2198 if (is_loclist_cmd(eap->cmdidx))
2199 {
2200 qi = GET_LOC_LIST(curwin);
2201 if (qi == NULL)
2202 {
2203 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002204 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002205 return NULL;
2206 }
2207 }
2208
2209 return qi;
2210}
2211
2212/*
2213 * Get the quickfix/location list stack to use for the specified Ex command.
2214 * For a location list command, returns the stack for the current window.
2215 * If the location list is not present, then allocates a new one.
2216 * Returns NULL if the allocation fails. For a location list command, sets
2217 * 'pwinp' to curwin.
2218 */
2219 static qf_info_T *
2220qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2221{
2222 qf_info_T *qi = &ql_info;
2223
2224 if (is_loclist_cmd(eap->cmdidx))
2225 {
2226 qi = ll_get_or_alloc_list(curwin);
2227 if (qi == NULL)
2228 return NULL;
2229 *pwinp = curwin;
2230 }
2231
2232 return qi;
2233}
2234
2235/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002236 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2237 */
2238 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002239copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002240{
2241 int i;
2242 qfline_T *from_qfp;
2243 qfline_T *prevp;
2244
2245 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002246 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002247 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002248 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002249 NULL,
2250 NULL,
2251 from_qfp->qf_module,
2252 0,
2253 from_qfp->qf_text,
2254 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002255 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002256 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002257 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002258 from_qfp->qf_viscol,
2259 from_qfp->qf_pattern,
2260 from_qfp->qf_nr,
2261 0,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002262 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002263 return FAIL;
2264
2265 // qf_add_entry() will not set the qf_num field, as the
2266 // directory and file names are not supplied. So the qf_fnum
2267 // field is copied here.
2268 prevp = to_qfl->qf_last;
2269 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2270 prevp->qf_type = from_qfp->qf_type; // error type
2271 if (from_qfl->qf_ptr == from_qfp)
2272 to_qfl->qf_ptr = prevp; // current location
2273 }
2274
2275 return OK;
2276}
2277
2278/*
2279 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2280 */
2281 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002282copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002283{
2284 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002285 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002286 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
2287 to_qfl->qf_count = 0;
2288 to_qfl->qf_index = 0;
2289 to_qfl->qf_start = NULL;
2290 to_qfl->qf_last = NULL;
2291 to_qfl->qf_ptr = NULL;
2292 if (from_qfl->qf_title != NULL)
2293 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2294 else
2295 to_qfl->qf_title = NULL;
2296 if (from_qfl->qf_ctx != NULL)
2297 {
2298 to_qfl->qf_ctx = alloc_tv();
2299 if (to_qfl->qf_ctx != NULL)
2300 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2301 }
2302 else
2303 to_qfl->qf_ctx = NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02002304 if (from_qfl->qftf_cb.cb_name != NULL)
2305 copy_callback(&to_qfl->qftf_cb, &from_qfl->qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002306 else
Bram Moolenaard43906d2020-07-20 21:31:32 +02002307 to_qfl->qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002308
2309 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002310 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002311 return FAIL;
2312
2313 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2314
2315 // Assign a new ID for the location list
2316 to_qfl->qf_id = ++last_qf_id;
2317 to_qfl->qf_changedtick = 0L;
2318
2319 // When no valid entries are present in the list, qf_ptr points to
2320 // the first item in the list
2321 if (to_qfl->qf_nonevalid)
2322 {
2323 to_qfl->qf_ptr = to_qfl->qf_start;
2324 to_qfl->qf_index = 1;
2325 }
2326
2327 return OK;
2328}
2329
2330/*
2331 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002332 */
2333 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002334copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002335{
2336 qf_info_T *qi;
2337 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002338
Bram Moolenaar09037502018-09-25 22:08:14 +02002339 // When copying from a location list window, copy the referenced
2340 // location list. For other windows, copy the location list for
2341 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002342 if (IS_LL_WINDOW(from))
2343 qi = from->w_llist_ref;
2344 else
2345 qi = from->w_llist;
2346
Bram Moolenaar09037502018-09-25 22:08:14 +02002347 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002348 return;
2349
Bram Moolenaar09037502018-09-25 22:08:14 +02002350 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002351 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002352 return;
2353
2354 to->w_llist->qf_listcount = qi->qf_listcount;
2355
Bram Moolenaar09037502018-09-25 22:08:14 +02002356 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002357 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002358 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002359 to->w_llist->qf_curlist = idx;
2360
Bram Moolenaar0398e002019-03-21 21:12:49 +01002361 if (copy_loclist(qf_get_list(qi, idx),
2362 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002363 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002364 qf_free_all(to);
2365 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002366 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002367 }
2368
Bram Moolenaar09037502018-09-25 22:08:14 +02002369 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002370}
2371
2372/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002373 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002374 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 */
2376 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002377qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
Bram Moolenaar82404332016-07-10 17:00:38 +02002379 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002380 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002381 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002382
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002383 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
Bram Moolenaare60acc12011-05-10 16:41:25 +02002386#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002387 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002388#endif
2389#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002390 if (directory != NULL)
2391 slash_adjust(directory);
2392 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002393#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002394 if (directory != NULL && !vim_isAbsName(fname)
2395 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2396 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002397 // Here we check if the file really exists.
2398 // This should normally be true, but if make works without
2399 // "leaving directory"-messages we might have missed a
2400 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002401 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002404 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002405 if (directory)
2406 ptr = concat_fnames(directory, fname, TRUE);
2407 else
2408 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002410 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002411 bufname = ptr;
2412 }
2413 else
2414 bufname = fname;
2415
2416 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002417 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002418 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002419 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002420 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002422 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002423 {
2424 vim_free(qf_last_bufname);
2425 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2426 if (bufname == ptr)
2427 qf_last_bufname = bufname;
2428 else
2429 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002430 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002431 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002432 if (buf == NULL)
2433 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002434
Bram Moolenaarc1542742016-07-20 21:44:37 +02002435 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002436 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002437 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438}
2439
2440/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002441 * Push dirbuf onto the directory stack and return pointer to actual dir or
2442 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 */
2444 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002445qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446{
2447 struct dir_stack_T *ds_new;
2448 struct dir_stack_T *ds_ptr;
2449
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002450 // allocate new stack element and hook it in
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002451 ds_new = ALLOC_ONE(struct dir_stack_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 if (ds_new == NULL)
2453 return NULL;
2454
2455 ds_new->next = *stackptr;
2456 *stackptr = ds_new;
2457
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002458 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 if (vim_isAbsName(dirbuf)
2460 || (*stackptr)->next == NULL
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002461 || (*stackptr && is_file_stack))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 (*stackptr)->dirname = vim_strsave(dirbuf);
2463 else
2464 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002465 // Okay we don't have an absolute path.
2466 // dirbuf must be a subdir of one of the directories on the stack.
2467 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 ds_new = (*stackptr)->next;
2469 (*stackptr)->dirname = NULL;
2470 while (ds_new)
2471 {
2472 vim_free((*stackptr)->dirname);
2473 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2474 TRUE);
2475 if (mch_isdir((*stackptr)->dirname) == TRUE)
2476 break;
2477
2478 ds_new = ds_new->next;
2479 }
2480
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002481 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 while ((*stackptr)->next != ds_new)
2483 {
2484 ds_ptr = (*stackptr)->next;
2485 (*stackptr)->next = (*stackptr)->next->next;
2486 vim_free(ds_ptr->dirname);
2487 vim_free(ds_ptr);
2488 }
2489
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002490 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (ds_new == NULL)
2492 {
2493 vim_free((*stackptr)->dirname);
2494 (*stackptr)->dirname = vim_strsave(dirbuf);
2495 }
2496 }
2497
2498 if ((*stackptr)->dirname != NULL)
2499 return (*stackptr)->dirname;
2500 else
2501 {
2502 ds_ptr = *stackptr;
2503 *stackptr = (*stackptr)->next;
2504 vim_free(ds_ptr);
2505 return NULL;
2506 }
2507}
2508
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509/*
2510 * pop dirbuf from the directory stack and return previous directory or NULL if
2511 * stack is empty
2512 */
2513 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002514qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516 struct dir_stack_T *ds_ptr;
2517
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002518 // TODO: Should we check if dirbuf is the directory on top of the stack?
2519 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002521 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (*stackptr != NULL)
2523 {
2524 ds_ptr = *stackptr;
2525 *stackptr = (*stackptr)->next;
2526 vim_free(ds_ptr->dirname);
2527 vim_free(ds_ptr);
2528 }
2529
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002530 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 return *stackptr ? (*stackptr)->dirname : NULL;
2532}
2533
2534/*
2535 * clean up directory stack
2536 */
2537 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002538qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
2540 struct dir_stack_T *ds_ptr;
2541
2542 while ((ds_ptr = *stackptr) != NULL)
2543 {
2544 *stackptr = (*stackptr)->next;
2545 vim_free(ds_ptr->dirname);
2546 vim_free(ds_ptr);
2547 }
2548}
2549
2550/*
2551 * Check in which directory of the directory stack the given file can be
2552 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002553 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 * Cleans up intermediate directory entries.
2555 *
2556 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002557 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 * ./
2559 * ./aa
2560 * ./aa/bb
2561 * ./bb
2562 * ./bb/x.c
2563 * and make says:
2564 * making all in aa
2565 * making all in bb
2566 * x.c:9: Error
2567 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2568 * qf_guess_filepath will return NULL.
2569 */
2570 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002571qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
2573 struct dir_stack_T *ds_ptr;
2574 struct dir_stack_T *ds_tmp;
2575 char_u *fullname;
2576
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002577 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002578 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 return NULL;
2580
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002581 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 fullname = NULL;
2583 while (ds_ptr)
2584 {
2585 vim_free(fullname);
2586 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2587
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002588 // If concat_fnames failed, just go on. The worst thing that can happen
2589 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2591 break;
2592
2593 ds_ptr = ds_ptr->next;
2594 }
2595
2596 vim_free(fullname);
2597
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002598 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002599 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002601 ds_tmp = qfl->qf_dir_stack->next;
2602 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 vim_free(ds_tmp->dirname);
2604 vim_free(ds_tmp);
2605 }
2606
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002607 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608}
2609
2610/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002611 * Returns TRUE if a quickfix/location list with the given identifier exists.
2612 */
2613 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002614qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002615{
2616 qf_info_T *qi = &ql_info;
2617 int i;
2618
2619 if (wp != NULL)
2620 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002621 if (!win_valid(wp))
2622 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002623 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002624 if (qi == NULL)
2625 return FALSE;
2626 }
2627
2628 for (i = 0; i < qi->qf_listcount; ++i)
2629 if (qi->qf_lists[i].qf_id == qf_id)
2630 return TRUE;
2631
2632 return FALSE;
2633}
2634
2635/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002636 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002637 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002638 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002639 * Similar to location list.
2640 */
2641 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002642is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002643{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002644 qfline_T *qfp;
2645 int i;
2646
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002647 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002648 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2649 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002650 break;
2651
Bram Moolenaar95946f12019-03-31 15:31:59 +02002652 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002653 return FALSE;
2654
2655 return TRUE;
2656}
2657
2658/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002659 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002660 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002661 */
2662 static qfline_T *
2663get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002664 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002665 qfline_T *qf_ptr,
2666 int *qf_index,
2667 int dir)
2668{
2669 int idx;
2670 int old_qf_fnum;
2671
2672 idx = *qf_index;
2673 old_qf_fnum = qf_ptr->qf_fnum;
2674
2675 do
2676 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002677 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002678 return NULL;
2679 ++idx;
2680 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002681 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002682 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2683
2684 *qf_index = idx;
2685 return qf_ptr;
2686}
2687
2688/*
2689 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002690 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002691 */
2692 static qfline_T *
2693get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002694 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002695 qfline_T *qf_ptr,
2696 int *qf_index,
2697 int dir)
2698{
2699 int idx;
2700 int old_qf_fnum;
2701
2702 idx = *qf_index;
2703 old_qf_fnum = qf_ptr->qf_fnum;
2704
2705 do
2706 {
2707 if (idx == 1 || qf_ptr->qf_prev == NULL)
2708 return NULL;
2709 --idx;
2710 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002711 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002712 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2713
2714 *qf_index = idx;
2715 return qf_ptr;
2716}
2717
2718/*
2719 * Get the n'th (errornr) previous/next valid entry from the current entry in
2720 * the quickfix list.
2721 * dir == FORWARD or FORWARD_FILE: next valid entry
2722 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2723 */
2724 static qfline_T *
2725get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002726 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002727 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002728 int dir,
2729 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002730{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002731 qfline_T *qf_ptr = qfl->qf_ptr;
2732 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002733 qfline_T *prev_qf_ptr;
2734 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002735 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002736
2737 while (errornr--)
2738 {
2739 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002740 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002741
2742 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002743 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002744 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002745 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002746 if (qf_ptr == NULL)
2747 {
2748 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002749 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002750 if (err != NULL)
2751 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002752 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002753 return NULL;
2754 }
2755 break;
2756 }
2757
2758 err = NULL;
2759 }
2760
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002761 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002762 return qf_ptr;
2763}
2764
2765/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002766 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2767 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002768 */
2769 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002770get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002771{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002772 qfline_T *qf_ptr = qfl->qf_ptr;
2773 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002774
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002775 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002776 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2777 {
2778 --qf_idx;
2779 qf_ptr = qf_ptr->qf_prev;
2780 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002781 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002782 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2783 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002784 {
2785 ++qf_idx;
2786 qf_ptr = qf_ptr->qf_next;
2787 }
2788
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002789 *new_qfidx = qf_idx;
2790 return qf_ptr;
2791}
2792
2793/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002794 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002795 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2796 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2797 * Returns a pointer to the entry and the index of the new entry is stored in
2798 * 'new_qfidx'.
2799 */
2800 static qfline_T *
2801qf_get_entry(
2802 qf_list_T *qfl,
2803 int errornr,
2804 int dir,
2805 int *new_qfidx)
2806{
2807 qfline_T *qf_ptr = qfl->qf_ptr;
2808 int qfidx = qfl->qf_index;
2809
2810 if (dir != 0) // next/prev valid entry
2811 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2812 else if (errornr != 0) // go to specified number
2813 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2814
2815 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002816 return qf_ptr;
2817}
2818
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002819/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002820 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002821 */
2822 static win_T *
2823qf_find_help_win(void)
2824{
2825 win_T *wp;
2826
2827 FOR_ALL_WINDOWS(wp)
2828 if (bt_help(wp->w_buffer))
2829 return wp;
2830
2831 return NULL;
2832}
2833
2834/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002835 * Set the location list for the specified window to 'qi'.
2836 */
2837 static void
2838win_set_loclist(win_T *wp, qf_info_T *qi)
2839{
2840 wp->w_llist = qi;
2841 qi->qf_refcount++;
2842}
2843
2844/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002845 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2846 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002847 */
2848 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002849jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002850{
2851 win_T *wp;
2852 int flags;
2853
Bram Moolenaare1004402020-10-24 20:49:43 +02002854 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002855 wp = NULL;
2856 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002857 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002858 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2859 win_enter(wp, TRUE);
2860 else
2861 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002862 // Split off help window; put it at far top if no position
2863 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002864 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02002865 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002866 && curwin->w_width < 80)
2867 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002868 // If the user asks to open a new window, then copy the location list.
2869 // Otherwise, don't copy the location list.
2870 if (IS_LL_STACK(qi) && !newwin)
2871 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002872
2873 if (win_split(0, flags) == FAIL)
2874 return FAIL;
2875
2876 *opened_window = TRUE;
2877
2878 if (curwin->w_height < p_hh)
2879 win_setheight((int)p_hh);
2880
Bram Moolenaarb2443732018-11-11 22:50:27 +01002881 // When using location list, the new window should use the supplied
2882 // location list. If the user asks to open a new window, then the new
2883 // window will get a copy of the location list.
2884 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002885 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002886 }
2887
2888 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002889 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002890
2891 return OK;
2892}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002893
2894/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002895 * Find a non-quickfix window using the given location list stack in the
2896 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002897 * Returns NULL if a matching window is not found.
2898 */
2899 static win_T *
2900qf_find_win_with_loclist(qf_info_T *ll)
2901{
2902 win_T *wp;
2903
2904 FOR_ALL_WINDOWS(wp)
2905 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2906 return wp;
2907
2908 return NULL;
2909}
2910
2911/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002912 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002913 */
2914 static win_T *
2915qf_find_win_with_normal_buf(void)
2916{
2917 win_T *wp;
2918
2919 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02002920 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002921 return wp;
2922
2923 return NULL;
2924}
2925
2926/*
2927 * Go to a window in any tabpage containing the specified file. Returns TRUE
2928 * if successfully jumped to the window. Otherwise returns FALSE.
2929 */
2930 static int
2931qf_goto_tabwin_with_file(int fnum)
2932{
2933 tabpage_T *tp;
2934 win_T *wp;
2935
2936 FOR_ALL_TAB_WINDOWS(tp, wp)
2937 if (wp->w_buffer->b_fnum == fnum)
2938 {
2939 goto_tabpage_win(tp, wp);
2940 return TRUE;
2941 }
2942
2943 return FALSE;
2944}
2945
2946/*
2947 * Create a new window to show a file above the quickfix window. Called when
2948 * only the quickfix window is present.
2949 */
2950 static int
2951qf_open_new_file_win(qf_info_T *ll_ref)
2952{
2953 int flags;
2954
2955 flags = WSP_ABOVE;
2956 if (ll_ref != NULL)
2957 flags |= WSP_NEWLOC;
2958 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002959 return FAIL; // not enough room for window
2960 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002961 swb_flags = 0;
2962 RESET_BINDING(curwin);
2963 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002964 // The new window should use the location list from the
2965 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002966 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002967 return OK;
2968}
2969
2970/*
2971 * Go to a window that shows the right buffer. If the window is not found, go
2972 * to the window just above the location list window. This is used for opening
2973 * a file from a location window and not from a quickfix window. If some usable
2974 * window is previously found, then it is supplied in 'use_win'.
2975 */
2976 static void
2977qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
2978{
2979 win_T *win = use_win;
2980
2981 if (win == NULL)
2982 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002983 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002984 FOR_ALL_WINDOWS(win)
2985 if (win->w_buffer->b_fnum == qf_fnum)
2986 break;
2987 if (win == NULL)
2988 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002989 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002990 win = curwin;
2991 do
2992 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02002993 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002994 break;
2995 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002996 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002997 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002998 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002999 } while (win != curwin);
3000 }
3001 }
3002 win_goto(win);
3003
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003004 // If the location list for the window is not set, then set it
3005 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003006 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003007 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003008}
3009
3010/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003011 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3012 * not found, then go to the window just above the quickfix window. This is
3013 * used for opening a file from a quickfix window and not from a location
3014 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003015 */
3016 static void
3017qf_goto_win_with_qfl_file(int qf_fnum)
3018{
3019 win_T *win;
3020 win_T *altwin;
3021
3022 win = curwin;
3023 altwin = NULL;
3024 for (;;)
3025 {
3026 if (win->w_buffer->b_fnum == qf_fnum)
3027 break;
3028 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003029 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003030 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003031 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003032
3033 if (IS_QF_WINDOW(win))
3034 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003035 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003036 // window, unless 'switchbuf' contains 'uselast': in this case we
3037 // try to jump to the previously used window first.
3038 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3039 win = prevwin;
3040 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003041 win = altwin;
3042 else if (curwin->w_prev != NULL)
3043 win = curwin->w_prev;
3044 else
3045 win = curwin->w_next;
3046 break;
3047 }
3048
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003049 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003050 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003051 altwin = win;
3052 }
3053
3054 win_goto(win);
3055}
3056
3057/*
3058 * Find a suitable window for opening a file (qf_fnum) from the
3059 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003060 * window, jump to it. Otherwise open a new window to display the file. If
3061 * 'newwin' is TRUE, then always open a new window. This is called from either
3062 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003063 */
3064 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003065qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003066{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003067 win_T *usable_wp = NULL;
3068 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003069 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003070
Bram Moolenaarb2443732018-11-11 22:50:27 +01003071 // If opening a new window, then don't use the location list referred by
3072 // the current window. Otherwise two windows will refer to the same
3073 // location list.
3074 if (!newwin)
3075 ll_ref = curwin->w_llist_ref;
3076
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003077 if (ll_ref != NULL)
3078 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003079 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003080 usable_wp = qf_find_win_with_loclist(ll_ref);
3081 if (usable_wp != NULL)
3082 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003083 }
3084
3085 if (!usable_win)
3086 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003087 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003088 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003089 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003090 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003091 }
3092
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003093 // If no usable window is found and 'switchbuf' contains "usetab"
3094 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003095 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003096 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003097
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003098 // If there is only one window and it is the quickfix window, create a
3099 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003100 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003101 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003102 if (qf_open_new_file_win(ll_ref) != OK)
3103 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003104 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003105 }
3106 else
3107 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003108 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003109 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003110 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003111 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003112 }
3113
3114 return OK;
3115}
3116
3117/*
3118 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003119 * Returns OK if successfully edited the file, FAIL on failing to open the
3120 * buffer and NOTDONE if the quickfix/location list was freed by an autocmd
3121 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003122 */
3123 static int
3124qf_jump_edit_buffer(
3125 qf_info_T *qi,
3126 qfline_T *qf_ptr,
3127 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003128 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003129 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003130{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003131 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003132 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003133 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003134 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003135 int old_qf_curlist = qi->qf_curlist;
3136 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003137
3138 if (qf_ptr->qf_type == 1)
3139 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003140 // Open help file (do_ecmd() will set b_help flag, readfile() will
3141 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003142 if (!can_abandon(curbuf, forceit))
3143 {
3144 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003145 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003146 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003147
3148 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3149 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003150 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003151 }
3152 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003153 retval = buflist_getfile(qf_ptr->qf_fnum,
3154 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003155
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003156 // If a location list, check whether the associated window is still
3157 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003158 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003159 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003160 win_T *wp = win_id2wp(prev_winid);
3161 if (wp == NULL && curwin->w_llist != qi)
3162 {
3163 emsg(_("E924: Current window was closed"));
3164 *opened_window = FALSE;
3165 return NOTDONE;
3166 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003167 }
3168
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003169 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003170 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003171 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003172 return NOTDONE;
3173 }
3174
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003175 // Check if the list was changed. The pointers may happen to be identical,
3176 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003177 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003178 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003179 || !is_qf_entry_present(qfl, qf_ptr))
3180 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003181 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003182 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003183 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003184 emsg(_(e_current_location_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003185 return NOTDONE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003186 }
3187
3188 return retval;
3189}
3190
3191/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003192 * Go to the error line in the current file using either line/column number or
3193 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003194 */
3195 static void
3196qf_jump_goto_line(
3197 linenr_T qf_lnum,
3198 int qf_col,
3199 char_u qf_viscol,
3200 char_u *qf_pattern)
3201{
3202 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003203
3204 if (qf_pattern == NULL)
3205 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003206 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003207 i = qf_lnum;
3208 if (i > 0)
3209 {
3210 if (i > curbuf->b_ml.ml_line_count)
3211 i = curbuf->b_ml.ml_line_count;
3212 curwin->w_cursor.lnum = i;
3213 }
3214 if (qf_col > 0)
3215 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003216 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003217 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003218 coladvance(qf_col - 1);
3219 else
3220 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003221 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003222 check_cursor();
3223 }
3224 else
3225 beginline(BL_WHITE | BL_FIX);
3226 }
3227 else
3228 {
3229 pos_T save_cursor;
3230
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003231 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003232 save_cursor = curwin->w_cursor;
3233 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003234 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003235 curwin->w_cursor = save_cursor;
3236 }
3237}
3238
3239/*
3240 * Display quickfix list index and size message
3241 */
3242 static void
3243qf_jump_print_msg(
3244 qf_info_T *qi,
3245 int qf_index,
3246 qfline_T *qf_ptr,
3247 buf_T *old_curbuf,
3248 linenr_T old_lnum)
3249{
3250 linenr_T i;
3251 int len;
3252
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003253 // Update the screen before showing the message, unless the screen
3254 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003255 if (!msg_scrolled)
3256 update_topline_redraw();
3257 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003258 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003259 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3260 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003261 // Add the message, skipping leading whitespace and newlines.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003262 len = (int)STRLEN(IObuff);
3263 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
3264
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003265 // Output the message. Overwrite to avoid scrolling when the 'O'
3266 // flag is present in 'shortmess'; But when not jumping, print the
3267 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003268 i = msg_scroll;
3269 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3270 msg_scroll = TRUE;
3271 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3272 msg_scroll = FALSE;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003273 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003274 msg_scroll = i;
3275}
3276
3277/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003278 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003279 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3280 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003281 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3282 * able to jump/open a window. Returns NOTDONE if a file is not associated
3283 * with the entry.
3284 */
3285 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003286qf_jump_open_window(
3287 qf_info_T *qi,
3288 qfline_T *qf_ptr,
3289 int newwin,
3290 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003291{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003292 qf_list_T *qfl = qf_get_curlist(qi);
3293 int old_changedtick = qfl->qf_changedtick;
3294 int old_qf_curlist = qi->qf_curlist;
3295 qfltype_T qfl_type = qfl->qfl_type;
3296
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003297 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003298 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3299 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003300 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003301 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003302 if (old_qf_curlist != qi->qf_curlist
3303 || old_changedtick != qfl->qf_changedtick
3304 || !is_qf_entry_present(qfl, qf_ptr))
3305 {
3306 if (qfl_type == QFLT_QUICKFIX)
3307 emsg(_(e_current_quickfix_list_was_changed));
3308 else
3309 emsg(_(e_current_location_list_was_changed));
3310 return FAIL;
3311 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003312
3313 // If currently in the quickfix window, find another window to show the
3314 // file in.
3315 if (bt_quickfix(curbuf) && !*opened_window)
3316 {
3317 // If there is no file specified, we don't know where to go.
3318 // But do advance, otherwise ":cn" gets stuck.
3319 if (qf_ptr->qf_fnum == 0)
3320 return NOTDONE;
3321
Bram Moolenaarb2443732018-11-11 22:50:27 +01003322 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3323 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003324 return FAIL;
3325 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003326 if (old_qf_curlist != qi->qf_curlist
3327 || old_changedtick != qfl->qf_changedtick
3328 || !is_qf_entry_present(qfl, qf_ptr))
3329 {
3330 if (qfl_type == QFLT_QUICKFIX)
3331 emsg(_(e_current_quickfix_list_was_changed));
3332 else
3333 emsg(_(e_current_location_list_was_changed));
3334 return FAIL;
3335 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003336
3337 return OK;
3338}
3339
3340/*
3341 * Edit a selected file from the quickfix/location list and jump to a
3342 * particular line/column, adjust the folds and display a message about the
3343 * jump.
3344 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
3345 * NOTDONE if the quickfix/location list is freed by an autocmd when opening
3346 * the file.
3347 */
3348 static int
3349qf_jump_to_buffer(
3350 qf_info_T *qi,
3351 int qf_index,
3352 qfline_T *qf_ptr,
3353 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003354 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003355 int *opened_window,
3356 int openfold,
3357 int print_message)
3358{
3359 buf_T *old_curbuf;
3360 linenr_T old_lnum;
3361 int retval = OK;
3362
3363 // If there is a file name, read the wanted file if needed, and check
3364 // autowrite etc.
3365 old_curbuf = curbuf;
3366 old_lnum = curwin->w_cursor.lnum;
3367
3368 if (qf_ptr->qf_fnum != 0)
3369 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003370 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003371 opened_window);
3372 if (retval != OK)
3373 return retval;
3374 }
3375
3376 // When not switched to another buffer, still need to set pc mark
3377 if (curbuf == old_curbuf)
3378 setpcmark();
3379
3380 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3381 qf_ptr->qf_pattern);
3382
3383#ifdef FEAT_FOLDING
3384 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3385 foldOpenCursor();
3386#endif
3387 if (print_message)
3388 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3389
3390 return retval;
3391}
3392
3393/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003394 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 */
3396 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003397qf_jump(qf_info_T *qi,
3398 int dir,
3399 int errornr,
3400 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003402 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3403}
3404
3405/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003406 * Jump to a quickfix line.
3407 * If dir == 0 go to entry "errornr".
3408 * If dir == FORWARD go "errornr" valid entries forward.
3409 * If dir == BACKWARD go "errornr" valid entries backward.
3410 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3411 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3412 * else if "errornr" is zero, redisplay the same line
3413 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003414 * If 'newwin' is TRUE, then open the file in a new window.
3415 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003416 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003417qf_jump_newwin(qf_info_T *qi,
3418 int dir,
3419 int errornr,
3420 int forceit,
3421 int newwin)
3422{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003423 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003424 qfline_T *qf_ptr;
3425 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003428 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003429 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003430 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003433 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003434 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003436 if (qi == NULL)
3437 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003438
Bram Moolenaar0398e002019-03-21 21:12:49 +01003439 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003441 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 return;
3443 }
3444
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003445 incr_quickfix_busy();
3446
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003447 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003448
3449 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003451 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003453
3454 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3455 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003457 qf_ptr = old_qf_ptr;
3458 qf_index = old_qf_index;
3459 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003462 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003463 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003464 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003465 // No need to print the error message if it's visible in the error
3466 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 print_message = FALSE;
3468
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003469 prev_winid = curwin->w_id;
3470
Bram Moolenaarb2443732018-11-11 22:50:27 +01003471 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003472 if (retval == FAIL)
3473 goto failed;
3474 if (retval == NOTDONE)
3475 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003476
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003477 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003478 &opened_window, old_KeyTyped, print_message);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003479 if (retval == NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003481 // Quickfix/location list is freed by an autocmd
3482 qi = NULL;
3483 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003486 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003489 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003490 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003492 // Couldn't open file, so put index back where it was. This could
3493 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 qf_ptr = old_qf_ptr;
3496 qf_index = old_qf_index;
3497 }
3498 }
3499theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003500 if (qi != NULL)
3501 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003502 qfl->qf_ptr = qf_ptr;
3503 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003504 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003505 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003507 // Restore old 'switchbuf' value, but not when an autocommand or
3508 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003509 p_swb = old_swb;
3510 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003512 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513}
3514
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003515// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003516static int qfFileAttr;
3517static int qfSepAttr;
3518static int qfLineAttr;
3519
3520/*
3521 * Display information about a single entry from the quickfix/location list.
3522 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003523 * 'cursel' will be set to TRUE for the currently selected entry in the
3524 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003525 */
3526 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003527qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003528{
3529 char_u *fname;
3530 buf_T *buf;
3531 int filter_entry;
3532
3533 fname = NULL;
3534 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3535 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3536 (char *)qfp->qf_module);
3537 else {
3538 if (qfp->qf_fnum != 0
3539 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3540 {
3541 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003542 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003543 fname = gettail(fname);
3544 }
3545 if (fname == NULL)
3546 sprintf((char *)IObuff, "%2d", qf_idx);
3547 else
3548 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3549 qf_idx, (char *)fname);
3550 }
3551
3552 // Support for filtering entries using :filter /pat/ clist
3553 // Match against the module name, file name, search pattern and
3554 // text of the entry.
3555 filter_entry = TRUE;
3556 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3557 filter_entry &= message_filtered(qfp->qf_module);
3558 if (filter_entry && fname != NULL)
3559 filter_entry &= message_filtered(fname);
3560 if (filter_entry && qfp->qf_pattern != NULL)
3561 filter_entry &= message_filtered(qfp->qf_pattern);
3562 if (filter_entry)
3563 filter_entry &= message_filtered(qfp->qf_text);
3564 if (filter_entry)
3565 return;
3566
3567 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003568 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003569
3570 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003571 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003572 if (qfp->qf_lnum == 0)
3573 IObuff[0] = NUL;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003574 else
thinca6864efa2021-06-19 20:45:20 +02003575 qf_range_text(qfp, IObuff, IOSIZE);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003576 sprintf((char *)IObuff + STRLEN(IObuff), "%s",
3577 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003578 msg_puts_attr((char *)IObuff, qfLineAttr);
3579 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003580 if (qfp->qf_pattern != NULL)
3581 {
3582 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003583 msg_puts((char *)IObuff);
3584 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003585 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003586 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003587
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003588 // Remove newlines and leading whitespace from the text. For an
3589 // unrecognized line keep the indent, the compiler may mark a word
3590 // with ^^^^.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003591 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
3592 ? skipwhite(qfp->qf_text) : qfp->qf_text,
3593 IObuff, IOSIZE);
3594 msg_prt_line(IObuff, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003595 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003596}
3597
3598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003600 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 */
3602 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003603qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003605 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003606 qfline_T *qfp;
3607 int i;
3608 int idx1 = 1;
3609 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003610 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003611 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003612 int all = eap->forceit; // if not :cl!, only show
3613 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003614 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003616 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3617 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003618
Bram Moolenaar0398e002019-03-21 21:12:49 +01003619 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003621 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 return;
3623 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003624 if (*arg == '+')
3625 {
3626 ++arg;
3627 plus = TRUE;
3628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3630 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003631 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 return;
3633 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003634 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003635 if (plus)
3636 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003637 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003638 idx2 = i + idx1;
3639 idx1 = i;
3640 }
3641 else
3642 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003643 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003644 if (idx1 < 0)
3645 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3646 if (idx2 < 0)
3647 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003650 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003651 shorten_fnames(FALSE);
3652
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003653 // Get the attributes for the different quickfix highlight items. Note
3654 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003655 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3656 if (qfFileAttr == 0)
3657 qfFileAttr = HL_ATTR(HLF_D);
3658 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3659 if (qfSepAttr == 0)
3660 qfSepAttr = HL_ATTR(HLF_D);
3661 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3662 if (qfLineAttr == 0)
3663 qfLineAttr = HL_ATTR(HLF_N);
3664
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003665 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003667 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
3669 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003670 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003671
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 ui_breakcheck();
3673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674}
3675
3676/*
3677 * Remove newlines and leading whitespace from an error message.
3678 * Put the result in "buf[bufsize]".
3679 */
3680 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003681qf_fmt_text(char_u *text, char_u *buf, int bufsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682{
3683 int i;
3684 char_u *p = text;
3685
3686 for (i = 0; *p != NUL && i < bufsize - 1; ++i)
3687 {
3688 if (*p == '\n')
3689 {
3690 buf[i] = ' ';
3691 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003692 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 break;
3694 }
3695 else
3696 buf[i] = *p++;
3697 }
3698 buf[i] = NUL;
3699}
3700
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003701/*
thinca6864efa2021-06-19 20:45:20 +02003702 * Range information from lnum, col, end_lnum, and end_col.
3703 * Put the result in "buf[bufsize]".
3704 */
3705 static void
3706qf_range_text(qfline_T *qfp, char_u *buf, int bufsize)
3707{
3708 int len;
3709 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3710 len = (int)STRLEN(buf);
3711
3712 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3713 {
3714 vim_snprintf((char *)buf + len, bufsize - len,
3715 "-%ld", qfp->qf_end_lnum);
3716 len += (int)STRLEN(buf + len);
3717 }
3718 if (qfp->qf_col > 0)
3719 {
3720 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3721 len += (int)STRLEN(buf + len);
3722 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3723 {
3724 vim_snprintf((char *)buf + len, bufsize - len,
3725 "-%d", qfp->qf_end_col);
3726 len += (int)STRLEN(buf + len);
3727 }
3728 }
3729 buf[len] = NUL;
3730}
3731
3732/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003733 * Display information (list number, list size and the title) about a
3734 * quickfix/location list.
3735 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003736 static void
3737qf_msg(qf_info_T *qi, int which, char *lead)
3738{
3739 char *title = (char *)qi->qf_lists[which].qf_title;
3740 int count = qi->qf_lists[which].qf_count;
3741 char_u buf[IOSIZE];
3742
3743 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3744 lead,
3745 which + 1,
3746 qi->qf_listcount,
3747 count);
3748
3749 if (title != NULL)
3750 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003751 size_t len = STRLEN(buf);
3752
3753 if (len < 34)
3754 {
3755 vim_memset(buf + len, ' ', 34 - len);
3756 buf[34] = NUL;
3757 }
3758 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003759 }
3760 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003761 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003762}
3763
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764/*
3765 * ":colder [count]": Up in the quickfix stack.
3766 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003767 * ":lolder [count]": Up in the location list stack.
3768 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 */
3770 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003771qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003773 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 int count;
3775
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003776 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3777 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003778
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 if (eap->addr_count != 0)
3780 count = eap->line2;
3781 else
3782 count = 1;
3783 while (count--)
3784 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003785 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003787 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003789 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003790 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003792 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
3794 else
3795 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003796 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003798 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003799 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003801 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
3803 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003804 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003805 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806}
3807
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003808/*
3809 * Display the information about all the quickfix/location lists in the stack
3810 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003811 void
3812qf_history(exarg_T *eap)
3813{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003814 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003815 int i;
3816
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003817 if (eap->addr_count > 0)
3818 {
3819 if (qi == NULL)
3820 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003821 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003822 return;
3823 }
3824
3825 // Jump to the specified quickfix list
3826 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3827 {
3828 qi->qf_curlist = eap->line2 - 1;
3829 qf_msg(qi, qi->qf_curlist, "");
3830 qf_update_buffer(qi, NULL);
3831 }
3832 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003833 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003834
3835 return;
3836 }
3837
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003838 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003839 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003840 else
3841 for (i = 0; i < qi->qf_listcount; ++i)
3842 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3843}
3844
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003846 * Free all the entries in the error list "idx". Note that other information
3847 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 */
3849 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003850qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003852 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003853 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003854 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003856 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003858 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003859 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003860 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003861 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003862 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003863 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003864 vim_free(qfp->qf_pattern);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003865 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003866 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003867 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003868 // Somehow qf_count may have an incorrect value, set it to 1
3869 // to avoid crashing when it's wrong.
3870 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003871 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003872 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003873 qfl->qf_start = qfpnext;
3874 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003876
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003877 qfl->qf_index = 0;
3878 qfl->qf_start = NULL;
3879 qfl->qf_last = NULL;
3880 qfl->qf_ptr = NULL;
3881 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003882
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003883 qf_clean_dir_stack(&qfl->qf_dir_stack);
3884 qfl->qf_directory = NULL;
3885 qf_clean_dir_stack(&qfl->qf_file_stack);
3886 qfl->qf_currfile = NULL;
3887 qfl->qf_multiline = FALSE;
3888 qfl->qf_multiignore = FALSE;
3889 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890}
3891
3892/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003893 * Free error list "idx". Frees all the entries in the quickfix list,
3894 * associated context information and the title.
3895 */
3896 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003897qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003898{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003899 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003900
Bram Moolenaard23a8232018-02-10 18:45:26 +01003901 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003902 free_tv(qfl->qf_ctx);
3903 qfl->qf_ctx = NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02003904 free_callback(&qfl->qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003905 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01003906 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003907}
3908
3909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 * qf_mark_adjust: adjust marks
3911 */
3912 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003913qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003914 win_T *wp,
3915 linenr_T line1,
3916 linenr_T line2,
3917 long amount,
3918 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003920 int i;
3921 qfline_T *qfp;
3922 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003923 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003924 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02003925 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
Bram Moolenaarc1542742016-07-20 21:44:37 +02003927 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003928 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003929 if (wp != NULL)
3930 {
3931 if (wp->w_llist == NULL)
3932 return;
3933 qi = wp->w_llist;
3934 }
3935
3936 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01003937 {
3938 qf_list_T *qfl = qf_get_list(qi, idx);
3939
3940 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01003941 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if (qfp->qf_fnum == curbuf->b_fnum)
3943 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003944 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
3946 {
3947 if (amount == MAXLNUM)
3948 qfp->qf_cleared = TRUE;
3949 else
3950 qfp->qf_lnum += amount;
3951 }
3952 else if (amount_after && qfp->qf_lnum > line2)
3953 qfp->qf_lnum += amount_after;
3954 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01003955 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02003956
3957 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02003958 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959}
3960
3961/*
3962 * Make a nice message out of the error character and the error number:
3963 * char number message
3964 * e or E 0 " error"
3965 * w or W 0 " warning"
3966 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02003967 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 * 0 0 ""
3969 * other 0 " c"
3970 * e or E n " error n"
3971 * w or W n " warning n"
3972 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02003973 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 * 0 n " error n"
3975 * other n " c n"
3976 * 1 x "" :helpgrep
3977 */
3978 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003979qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980{
3981 static char_u buf[20];
3982 static char_u cc[3];
3983 char_u *p;
3984
3985 if (c == 'W' || c == 'w')
3986 p = (char_u *)" warning";
3987 else if (c == 'I' || c == 'i')
3988 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02003989 else if (c == 'N' || c == 'n')
3990 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
3992 p = (char_u *)" error";
3993 else if (c == 0 || c == 1)
3994 p = (char_u *)"";
3995 else
3996 {
3997 cc[0] = ' ';
3998 cc[1] = c;
3999 cc[2] = NUL;
4000 p = cc;
4001 }
4002
4003 if (nr <= 0)
4004 return p;
4005
4006 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4007 return buf;
4008}
4009
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004011 * When "split" is FALSE: Open the entry/result under the cursor.
4012 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4013 */
4014 void
4015qf_view_result(int split)
4016{
4017 qf_info_T *qi = &ql_info;
4018
4019 if (!bt_quickfix(curbuf))
4020 return;
4021
4022 if (IS_LL_WINDOW(curwin))
4023 qi = GET_LOC_LIST(curwin);
4024
Bram Moolenaar0398e002019-03-21 21:12:49 +01004025 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004026 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004027 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004028 return;
4029 }
4030
4031 if (split)
4032 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004033 // Open the selected entry in a new window
4034 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4035 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004036 return;
4037 }
4038
4039 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4040}
4041
4042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 * ":cwindow": open the quickfix window if we have errors to display,
4044 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004045 * ":lwindow": open the location list window if we have locations to display,
4046 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 */
4048 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004049ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004051 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004052 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 win_T *win;
4054
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004055 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4056 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004057
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004058 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004059
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004060 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004061 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004063 // If a quickfix window is open but we have no errors to display,
4064 // close the window. If a quickfix window is not open, then open
4065 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004066 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004067 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004068 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
4070 if (win != NULL)
4071 ex_cclose(eap);
4072 }
4073 else if (win == NULL)
4074 ex_copen(eap);
4075}
4076
4077/*
4078 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004079 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004082ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004084 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004085 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004087 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4088 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004090 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004091 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 if (win != NULL)
4093 win_close(win, FALSE);
4094}
4095
4096/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004097 * Set "w:quickfix_title" if "qi" has a title.
4098 */
4099 static void
4100qf_set_title_var(qf_list_T *qfl)
4101{
4102 if (qfl->qf_title != NULL)
4103 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4104}
4105
4106/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004107 * Goto a quickfix or location list window (if present).
4108 * Returns OK if the window is found, FAIL otherwise.
4109 */
4110 static int
4111qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4112{
4113 win_T *win;
4114
4115 win = qf_find_win(qi);
4116 if (win == NULL)
4117 return FAIL;
4118
4119 win_goto(win);
4120 if (resize)
4121 {
4122 if (vertsplit)
4123 {
4124 if (sz != win->w_width)
4125 win_setwidth(sz);
4126 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004127 else if (sz != win->w_height && win->w_height
4128 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004129 win_setheight(sz);
4130 }
4131
4132 return OK;
4133}
4134
4135/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004136 * Set options for the buffer in the quickfix or location list window.
4137 */
4138 static void
4139qf_set_cwindow_options(void)
4140{
4141 // switch off 'swapfile'
4142 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4143 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
4144 OPT_LOCAL);
4145 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
4146 RESET_BINDING(curwin);
4147#ifdef FEAT_DIFF
4148 curwin->w_p_diff = FALSE;
4149#endif
4150#ifdef FEAT_FOLDING
4151 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual",
4152 OPT_LOCAL);
4153#endif
4154}
4155
4156/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004157 * Open a new quickfix or location list window, load the quickfix buffer and
4158 * set the appropriate options for the window.
4159 * Returns FAIL if the window could not be opened.
4160 */
4161 static int
4162qf_open_new_cwindow(qf_info_T *qi, int height)
4163{
4164 buf_T *qf_buf;
4165 win_T *oldwin = curwin;
4166 tabpage_T *prevtab = curtab;
4167 int flags = 0;
4168 win_T *win;
4169
4170 qf_buf = qf_find_buf(qi);
4171
4172 // The current window becomes the previous window afterwards.
4173 win = curwin;
4174
Bram Moolenaare1004402020-10-24 20:49:43 +02004175 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004176 // Create the new quickfix window at the very bottom, except when
4177 // :belowright or :aboveleft is used.
4178 win_goto(lastwin);
4179 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004180 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004181 flags = WSP_BELOW;
4182 flags |= WSP_NEWLOC;
4183 if (win_split(height, flags) == FAIL)
4184 return FAIL; // not enough room for window
4185 RESET_BINDING(curwin);
4186
4187 if (IS_LL_STACK(qi))
4188 {
4189 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004190 // location list stack from the window 'win'.
4191 curwin->w_llist_ref = qi;
4192 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004193 }
4194
4195 if (oldwin != curwin)
4196 oldwin = NULL; // don't store info when in another window
4197 if (qf_buf != NULL)
4198 {
4199 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004200 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004201 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004202 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004203 }
4204 else
4205 {
4206 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004207 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4208 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004209 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004210
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004211 // save the number of the new buffer
4212 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004213 }
4214
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004215 // Set the options for the quickfix buffer/window (if not already done)
4216 // Do this even if the quickfix buffer was already present, as an autocmd
4217 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004218 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004219 qf_set_cwindow_options();
4220
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004221 // Only set the height when still in the same tab page and there is no
4222 // window to the side.
4223 if (curtab == prevtab && curwin->w_width == Columns)
4224 win_setheight(height);
4225 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4226 if (win_valid(win))
4227 prevwin = win;
4228
4229 return OK;
4230}
4231
4232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004234 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 */
4236 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004237ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004239 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004240 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004242 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004243 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004245 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4246 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004247
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004248 incr_quickfix_busy();
4249
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 if (eap->addr_count != 0)
4251 height = eap->line2;
4252 else
4253 height = QF_WINHEIGHT;
4254
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004255 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#ifdef FEAT_GUI
4257 need_mouse_correct = TRUE;
4258#endif
4259
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004260 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004261 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004262 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004263 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004264 if (status == FAIL)
4265 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004266 {
4267 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004268 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004271 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004272 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004273 // Save the current index here, as updating the quickfix buffer may free
4274 // the quickfix list
4275 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004276
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004277 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004278 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004280 decr_quickfix_busy();
4281
4282 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 curwin->w_cursor.col = 0;
4284 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004285 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286}
4287
4288/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004289 * Move the cursor in the quickfix window to "lnum".
4290 */
4291 static void
4292qf_win_goto(win_T *win, linenr_T lnum)
4293{
4294 win_T *old_curwin = curwin;
4295
4296 curwin = win;
4297 curbuf = win->w_buffer;
4298 curwin->w_cursor.lnum = lnum;
4299 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004300 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004301 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004302 update_topline(); // scroll to show the line
Bram Moolenaardcb17002016-07-07 18:58:59 +02004303 redraw_later(VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004304 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004305 curwin = old_curwin;
4306 curbuf = curwin->w_buffer;
4307}
4308
4309/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004310 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004311 */
4312 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004313ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004314{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004315 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004316 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004317
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004318 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4319 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004320
4321 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004322 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4323 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4324}
4325
4326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 * Return the number of the current entry (line number in the quickfix
4328 * window).
4329 */
4330 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004331qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004333 qf_info_T *qi = &ql_info;
4334
4335 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004336 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004337 qi = wp->w_llist_ref;
4338
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004339 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340}
4341
4342/*
4343 * Update the cursor position in the quickfix window to the current error.
4344 * Return TRUE if there is a quickfix window.
4345 */
4346 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004347qf_win_pos_update(
4348 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004349 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350{
4351 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004352 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004354 // Put the cursor on the current error in the quickfix window, so that
4355 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004356 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 if (win != NULL
4358 && qf_index <= win->w_buffer->b_ml.ml_line_count
4359 && old_qf_index != qf_index)
4360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 if (qf_index > old_qf_index)
4362 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004363 win->w_redraw_top = old_qf_index;
4364 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 else
4367 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004368 win->w_redraw_top = qf_index;
4369 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004371 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
4373 return win != NULL;
4374}
4375
4376/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004377 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004378 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004379 */
4380 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004381is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004382{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004383 // A window displaying the quickfix buffer will have the w_llist_ref field
4384 // set to NULL.
4385 // A window displaying a location list buffer will have the w_llist_ref
4386 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004387 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004388 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4389 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004390 return TRUE;
4391
4392 return FALSE;
4393}
4394
4395/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004396 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4397 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004398 */
4399 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004400qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004401{
4402 win_T *win;
4403
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004404 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004405 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004406 return win;
4407 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004408}
4409
4410/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004411 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004412 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 */
4414 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004415qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004417 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004418 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004420 if (qi->qf_bufnr != INVALID_QFBUFNR)
4421 {
4422 buf_T *qfbuf;
4423 qfbuf = buflist_findnr(qi->qf_bufnr);
4424 if (qfbuf != NULL)
4425 return qfbuf;
4426 // buffer is no longer present
4427 qi->qf_bufnr = INVALID_QFBUFNR;
4428 }
4429
Bram Moolenaar9c102382006-05-03 21:26:49 +00004430 FOR_ALL_TAB_WINDOWS(tp, win)
4431 if (is_qf_win(win, qi))
4432 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004433
Bram Moolenaar9c102382006-05-03 21:26:49 +00004434 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435}
4436
4437/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004438 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004439 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004440 */
4441 int
4442qf_process_qftf_option(void)
4443{
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004444 return option_set_callback_func(p_qftf, &qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004445}
4446
4447/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004448 * Update the w:quickfix_title variable in the quickfix/location list window in
4449 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004450 */
4451 static void
4452qf_update_win_titlevar(qf_info_T *qi)
4453{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004454 qf_list_T *qfl = qf_get_curlist(qi);
4455 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004456 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004457 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004458
Bram Moolenaar530bed92020-12-16 21:02:56 +01004459 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004460 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004461 if (is_qf_win(win, qi))
4462 {
4463 curwin = win;
4464 qf_set_title_var(qfl);
4465 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004466 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004467 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004468}
4469
4470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 * Find the quickfix buffer. If it exists, update the contents.
4472 */
4473 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004474qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475{
4476 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004477 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004480 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004481 buf = qf_find_buf(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 if (buf != NULL)
4483 {
Bram Moolenaar864293a2016-06-02 13:40:04 +02004484 linenr_T old_line_count = buf->b_ml.ml_line_count;
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004485 int qf_winid = 0;
4486
4487 if (IS_LL_STACK(qi))
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004488 {
4489 if (curwin->w_llist == qi)
4490 win = curwin;
4491 else
4492 {
4493 win = qf_find_win_with_loclist(qi);
4494 if (win == NULL)
4495 return;
4496 }
4497 qf_winid = win->w_id;
4498 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004499
4500 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004501 // set curwin/curbuf to buf and save a few things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004502 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
Bram Moolenaard823fa92016-08-12 16:29:27 +02004504 qf_update_win_titlevar(qi);
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004505
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004506 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
Bram Moolenaara8788f42017-07-19 17:06:20 +02004507 ++CHANGEDTICK(buf);
Bram Moolenaar6920c722016-01-22 22:44:10 +01004508
Bram Moolenaar864293a2016-06-02 13:40:04 +02004509 if (old_last == NULL)
4510 {
Bram Moolenaarc1808d52016-04-18 20:04:00 +02004511 (void)qf_win_pos_update(qi, 0);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004512
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004513 // restore curwin/curbuf and a few other things
Bram Moolenaar864293a2016-06-02 13:40:04 +02004514 aucmd_restbuf(&aco);
4515 }
4516
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004517 // Only redraw when added lines are visible. This avoids flickering
4518 // when the added lines are not visible.
Bram Moolenaar864293a2016-06-02 13:40:04 +02004519 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4520 redraw_buf_later(buf, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 }
4522}
4523
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004524/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004525 * Add an error line to the quickfix buffer.
4526 */
4527 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004528qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004529 buf_T *buf, // quickfix window buffer
4530 linenr_T lnum,
4531 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004532 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004533 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004534 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004535{
4536 int len;
4537 buf_T *errbuf;
4538
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004539 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004540 // for this entry, then use it.
4541 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004542 vim_strncpy(IObuff, qftf_str, IOSIZE - 1);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004543 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004544 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004545 if (qfp->qf_module != NULL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004546 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004547 vim_strncpy(IObuff, qfp->qf_module, IOSIZE - 1);
4548 len = (int)STRLEN(IObuff);
4549 }
4550 else if (qfp->qf_fnum != 0
4551 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4552 && errbuf->b_fname != NULL)
4553 {
4554 if (qfp->qf_type == 1) // :helpgrep
4555 vim_strncpy(IObuff, gettail(errbuf->b_fname), IOSIZE - 1);
4556 else
4557 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004558 // Shorten the file name if not done already.
4559 // For optimization, do this only for the first entry in a
4560 // buffer.
4561 if (first_bufline && (errbuf->b_sfname == NULL
4562 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004563 {
4564 if (*dirname == NUL)
4565 mch_dirname(dirname, MAXPATHL);
4566 shorten_buf_fname(errbuf, dirname, FALSE);
4567 }
4568 vim_strncpy(IObuff, errbuf->b_fname, IOSIZE - 1);
4569 }
4570 len = (int)STRLEN(IObuff);
4571 }
4572 else
4573 len = 0;
4574
4575 if (len < IOSIZE - 1)
4576 IObuff[len++] = '|';
4577
4578 if (qfp->qf_lnum > 0)
4579 {
thinca6864efa2021-06-19 20:45:20 +02004580 qf_range_text(qfp, IObuff + len, IOSIZE - len);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004581 len += (int)STRLEN(IObuff + len);
4582
Bram Moolenaar858ba062020-05-31 23:11:59 +02004583 vim_snprintf((char *)IObuff + len, IOSIZE - len, "%s",
4584 (char *)qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004585 len += (int)STRLEN(IObuff + len);
4586 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004587 else if (qfp->qf_pattern != NULL)
4588 {
4589 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
4590 len += (int)STRLEN(IObuff + len);
4591 }
4592 if (len < IOSIZE - 2)
4593 {
4594 IObuff[len++] = '|';
4595 IObuff[len++] = ' ';
4596 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004597
Bram Moolenaar858ba062020-05-31 23:11:59 +02004598 // Remove newlines and leading whitespace from the text.
4599 // For an unrecognized line keep the indent, the compiler may
4600 // mark a word with ^^^^.
4601 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
4602 IObuff + len, IOSIZE - len);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004603 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004604
4605 if (ml_append_buf(buf, lnum, IObuff,
4606 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL)
4607 return FAIL;
4608
4609 return OK;
4610}
4611
Bram Moolenaard43906d2020-07-20 21:31:32 +02004612/*
4613 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4614 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4615 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004616 static list_T *
4617call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4618{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004619 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004620 list_T *qftf_list = NULL;
4621
4622 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4623 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4624 // set.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004625 if (qfl->qftf_cb.cb_name != NULL)
4626 cb = &qfl->qftf_cb;
4627 if (cb != NULL && cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004628 {
4629 typval_T args[1];
4630 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004631 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004632
4633 // create the dict argument
4634 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
4635 return NULL;
4636 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4637 dict_add_number(d, "winid", (long)qf_winid);
4638 dict_add_number(d, "id", (long)qfl->qf_id);
4639 dict_add_number(d, "start_idx", start_idx);
4640 dict_add_number(d, "end_idx", end_idx);
4641 ++d->dv_refcount;
4642 args[0].v_type = VAR_DICT;
4643 args[0].vval.v_dict = d;
4644
Bram Moolenaard43906d2020-07-20 21:31:32 +02004645 qftf_list = NULL;
4646 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4647 {
4648 if (rettv.v_type == VAR_LIST)
4649 {
4650 qftf_list = rettv.vval.v_list;
4651 qftf_list->lv_refcount++;
4652 }
4653 clear_tv(&rettv);
4654 }
4655 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004656 }
4657
4658 return qftf_list;
4659}
4660
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 * Fill current buffer with quickfix errors, replacing any previous contents.
4663 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004664 * If "old_last" is not NULL append the items after this one.
4665 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4666 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 */
4668 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004669qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004671 linenr_T lnum;
4672 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004673 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004674 list_T *qftf_list = NULL;
4675 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
Bram Moolenaar864293a2016-06-02 13:40:04 +02004677 if (old_last == NULL)
4678 {
4679 if (buf != curbuf)
4680 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004681 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004682 return;
4683 }
4684
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004685 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004686 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004687 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004690 // Check if there is anything to display
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004691 if (qfl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004693 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004694 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004695 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004696
4697 *dirname = NUL;
4698
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004699 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004700 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004701 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004702 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004703 lnum = 0;
4704 }
4705 else
4706 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004707 if (old_last->qf_next != NULL)
4708 qfp = old_last->qf_next;
4709 else
4710 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004711 lnum = buf->b_ml.ml_line_count;
4712 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004713
4714 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4715 (long)qfl->qf_count);
4716 if (qftf_list != NULL)
4717 qftf_li = qftf_list->lv_first;
4718
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004719 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004721 char_u *qftf_str = NULL;
4722
Bram Moolenaard43906d2020-07-20 21:31:32 +02004723 // Use the text supplied by the user defined function (if any).
4724 // If the returned value is not string, then ignore the rest
4725 // of the returned values and use the default.
4726 if (qftf_li != NULL && !invalid_val)
4727 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004728 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004729 if (qftf_str == NULL)
4730 invalid_val = TRUE;
4731 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004732
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004733 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4734 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004736
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004737 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004738 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004740 if (qfp == NULL)
4741 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004742
4743 if (qftf_li != NULL)
4744 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004746
4747 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004748 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004749 (void)ml_delete(lnum + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 }
4751
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004752 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 check_lnums(TRUE);
4754
Bram Moolenaar864293a2016-06-02 13:40:04 +02004755 if (old_last == NULL)
4756 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004757 // Set the 'filetype' to "qf" each time after filling the buffer.
4758 // This resembles reading a file into a buffer, it's more logical when
4759 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004760 ++curbuf_lock;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004761 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
4762 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004764 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004765 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004767 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004769 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004770 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004771
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004772 // make sure it will be redrawn
Bram Moolenaar864293a2016-06-02 13:40:04 +02004773 redraw_curbuf_later(NOT_VALID);
4774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004776 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 KeyTyped = old_KeyTyped;
4778}
4779
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004780/*
4781 * For every change made to the quickfix list, update the changed tick.
4782 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004783 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004784qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004785{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004786 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004787}
4788
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004790 * Return the quickfix/location list number with the given identifier.
4791 * Returns -1 if list is not found.
4792 */
4793 static int
4794qf_id2nr(qf_info_T *qi, int_u qfid)
4795{
4796 int qf_idx;
4797
4798 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4799 if (qi->qf_lists[qf_idx].qf_id == qfid)
4800 return qf_idx;
4801 return INVALID_QFIDX;
4802}
4803
4804/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004805 * If the current list is not "save_qfid" and we can find the list with that ID
4806 * then make it the current list.
4807 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004808 * Returns OK if successfully restored the list. Returns FAIL if the list with
4809 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004810 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004811 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004812qf_restore_list(qf_info_T *qi, int_u save_qfid)
4813{
4814 int curlist;
4815
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004816 if (qf_get_curlist(qi)->qf_id != save_qfid)
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004817 {
4818 curlist = qf_id2nr(qi, save_qfid);
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004819 if (curlist < 0)
4820 // list is not present
4821 return FAIL;
4822 qi->qf_curlist = curlist;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004823 }
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004824 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004825}
4826
4827/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004828 * Jump to the first entry if there is one.
4829 */
4830 static void
4831qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4832{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004833 if (qf_restore_list(qi, save_qfid) == FAIL)
4834 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004835
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004836 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004837 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004838 qf_jump(qi, 0, 0, forceit);
4839}
4840
4841/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004842 * Return TRUE when using ":vimgrep" for ":grep".
4843 */
4844 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004845grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004846{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004847 return ((cmdidx == CMD_grep
4848 || cmdidx == CMD_lgrep
4849 || cmdidx == CMD_grepadd
4850 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004851 && STRCMP("internal",
4852 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4853}
4854
4855/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004856 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004858 static char_u *
4859make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004861 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004862 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004863 case CMD_make: return (char_u *)"make";
4864 case CMD_lmake: return (char_u *)"lmake";
4865 case CMD_grep: return (char_u *)"grep";
4866 case CMD_lgrep: return (char_u *)"lgrep";
4867 case CMD_grepadd: return (char_u *)"grepadd";
4868 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4869 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871}
4872
4873/*
4874 * Return the name for the errorfile, in allocated memory.
4875 * Find a new unique name when 'makeef' contains "##".
4876 * Returns NULL for error.
4877 */
4878 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004879get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
4881 char_u *p;
4882 char_u *name;
4883 static int start = -1;
4884 static int off = 0;
4885#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004886 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887#endif
4888
4889 if (*p_mef == NUL)
4890 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02004891 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004893 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 return name;
4895 }
4896
4897 for (p = p_mef; *p; ++p)
4898 if (p[0] == '#' && p[1] == '#')
4899 break;
4900
4901 if (*p == NUL)
4902 return vim_strsave(p_mef);
4903
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004904 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 for (;;)
4906 {
4907 if (start == -1)
4908 start = mch_get_pid();
4909 else
4910 off += 19;
4911
Bram Moolenaar964b3742019-05-24 18:54:09 +02004912 name = alloc(STRLEN(p_mef) + 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 if (name == NULL)
4914 break;
4915 STRCPY(name, p_mef);
4916 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
4917 STRCAT(name, p + 2);
4918 if (mch_getperm(name) < 0
4919#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004920 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 && mch_lstat((char *)name, &sb) < 0
4922#endif
4923 )
4924 break;
4925 vim_free(name);
4926 }
4927 return name;
4928}
4929
4930/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004931 * Form the complete command line to invoke 'make'/'grep'. Quote the command
4932 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
4933 */
4934 static char_u *
4935make_get_fullcmd(char_u *makecmd, char_u *fname)
4936{
4937 char_u *cmd;
4938 unsigned len;
4939
4940 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
4941 if (*p_sp != NUL)
4942 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
4943 cmd = alloc(len);
4944 if (cmd == NULL)
4945 return NULL;
4946 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
4947 (char *)p_shq);
4948
4949 // If 'shellpipe' empty: don't redirect to 'errorfile'.
4950 if (*p_sp != NUL)
4951 append_redir(cmd, len, p_sp, fname);
4952
4953 // Display the fully formed command. Output a newline if there's something
4954 // else than the :make command that was typed (in which case the cursor is
4955 // in column 0).
4956 if (msg_col == 0)
4957 msg_didout = FALSE;
4958 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004959 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004960 msg_outtrans(cmd); // show what we are doing
4961
4962 return cmd;
4963}
4964
4965/*
4966 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
4967 */
4968 void
4969ex_make(exarg_T *eap)
4970{
4971 char_u *fname;
4972 char_u *cmd;
4973 char_u *enc = NULL;
4974 win_T *wp = NULL;
4975 qf_info_T *qi = &ql_info;
4976 int res;
4977 char_u *au_name = NULL;
4978 int_u save_qfid;
4979
4980 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
4981 if (grep_internal(eap->cmdidx))
4982 {
4983 ex_vimgrep(eap);
4984 return;
4985 }
4986
4987 au_name = make_get_auname(eap->cmdidx);
4988 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
4989 curbuf->b_fname, TRUE, curbuf))
4990 {
4991#ifdef FEAT_EVAL
4992 if (aborting())
4993 return;
4994#endif
4995 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004996 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004997
4998 if (is_loclist_cmd(eap->cmdidx))
4999 wp = curwin;
5000
5001 autowrite_all();
5002 fname = get_mef_name();
5003 if (fname == NULL)
5004 return;
5005 mch_remove(fname); // in case it's not unique
5006
5007 cmd = make_get_fullcmd(eap->arg, fname);
5008 if (cmd == NULL)
5009 return;
5010
5011 // let the shell know if we are redirecting output or not
5012 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5013
5014#ifdef AMIGA
5015 out_flush();
5016 // read window status report and redraw before message
5017 (void)char_avail();
5018#endif
5019
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005020 incr_quickfix_busy();
5021
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005022 res = qf_init(wp, fname, (eap->cmdidx != CMD_make
5023 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm,
5024 (eap->cmdidx != CMD_grepadd
5025 && eap->cmdidx != CMD_lgrepadd),
5026 qf_cmdtitle(*eap->cmdlinep), enc);
5027 if (wp != NULL)
5028 {
5029 qi = GET_LOC_LIST(wp);
5030 if (qi == NULL)
5031 goto cleanup;
5032 }
5033 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005034 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005035
5036 // Remember the current quickfix list identifier, so that we can
5037 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005038 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005039 if (au_name != NULL)
5040 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5041 curbuf->b_fname, TRUE, curbuf);
5042 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5043 // display the first error
5044 qf_jump_first(qi, save_qfid, FALSE);
5045
5046cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005047 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005048 mch_remove(fname);
5049 vim_free(fname);
5050 vim_free(cmd);
5051}
5052
5053/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005054 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005055 */
5056 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005057qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005058{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005059 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005060
5061 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5062 return 0;
5063 return qf_get_curlist(qi)->qf_count;
5064}
5065
5066/*
5067 * Returns the number of valid entries in the current quickfix/location list.
5068 */
5069 int
5070qf_get_valid_size(exarg_T *eap)
5071{
5072 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005073 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005074 qfline_T *qfp;
5075 int i, sz = 0;
5076 int prev_fnum = 0;
5077
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005078 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5079 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005080
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005081 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005082 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005083 {
5084 if (qfp->qf_valid)
5085 {
5086 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005087 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005088 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5089 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005090 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005091 sz++;
5092 prev_fnum = qfp->qf_fnum;
5093 }
5094 }
5095 }
5096
5097 return sz;
5098}
5099
5100/*
5101 * Returns the current index of the quickfix/location list.
5102 * Returns 0 if there is an error.
5103 */
5104 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005105qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005106{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005107 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005108
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005109 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5110 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005111
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005112 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005113}
5114
5115/*
5116 * Returns the current index in the quickfix/location list (counting only valid
5117 * entries). If no valid entries are in the list, then returns 1.
5118 */
5119 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005120qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005121{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005122 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005123 qf_list_T *qfl;
5124 qfline_T *qfp;
5125 int i, eidx = 0;
5126 int prev_fnum = 0;
5127
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005128 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5129 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005130
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005131 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005132 qfp = qfl->qf_start;
5133
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005134 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005135 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005136 return 1;
5137
5138 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5139 {
5140 if (qfp->qf_valid)
5141 {
5142 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5143 {
5144 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5145 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005146 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005147 eidx++;
5148 prev_fnum = qfp->qf_fnum;
5149 }
5150 }
5151 else
5152 eidx++;
5153 }
5154 }
5155
5156 return eidx ? eidx : 1;
5157}
5158
5159/*
5160 * Get the 'n'th valid error entry in the quickfix or location list.
5161 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5162 * For :cdo and :ldo returns the 'n'th valid error entry.
5163 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5164 */
5165 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005166qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005167{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005168 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005169 int i, eidx;
5170 int prev_fnum = 0;
5171
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005172 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005173 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005174 return 1;
5175
Bram Moolenaar95946f12019-03-31 15:31:59 +02005176 eidx = 0;
5177 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005178 {
5179 if (qfp->qf_valid)
5180 {
5181 if (fdo)
5182 {
5183 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5184 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005185 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005186 eidx++;
5187 prev_fnum = qfp->qf_fnum;
5188 }
5189 }
5190 else
5191 eidx++;
5192 }
5193
5194 if (eidx == n)
5195 break;
5196 }
5197
5198 if (i <= qfl->qf_count)
5199 return i;
5200 else
5201 return 1;
5202}
5203
5204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005206 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005207 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 */
5209 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005210ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005212 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005213 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005214
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005215 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5216 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005217
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005218 if (eap->addr_count > 0)
5219 errornr = (int)eap->line2;
5220 else
5221 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005222 switch (eap->cmdidx)
5223 {
5224 case CMD_cc: case CMD_ll:
5225 errornr = 0;
5226 break;
5227 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5228 case CMD_lfirst:
5229 errornr = 1;
5230 break;
5231 default:
5232 errornr = 32767;
5233 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005234 }
5235
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005236 // For cdo and ldo commands, jump to the nth valid error.
5237 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005238 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5239 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005240 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005241 eap->addr_count > 0 ? (int)eap->line1 : 1,
5242 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5243
5244 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245}
5246
5247/*
5248 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005249 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005250 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 */
5252 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005253ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005255 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005256 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005257 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005258
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005259 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5260 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005261
Bram Moolenaar55b69262017-08-13 13:42:01 +02005262 if (eap->addr_count > 0
5263 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5264 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005265 errornr = (int)eap->line2;
5266 else
5267 errornr = 1;
5268
Bram Moolenaar39665952018-08-15 20:59:48 +02005269 // Depending on the command jump to either next or previous entry/file.
5270 switch (eap->cmdidx)
5271 {
5272 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5273 dir = FORWARD;
5274 break;
5275 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5276 case CMD_lNext:
5277 dir = BACKWARD;
5278 break;
5279 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5280 dir = FORWARD_FILE;
5281 break;
5282 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5283 dir = BACKWARD_FILE;
5284 break;
5285 default:
5286 dir = FORWARD;
5287 break;
5288 }
5289
5290 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291}
5292
5293/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005294 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5295 * The index of the entry is stored in 'errornr'.
5296 * Returns NULL if an entry is not found.
5297 */
5298 static qfline_T *
5299qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5300{
5301 qfline_T *qfp = NULL;
5302 int idx = 0;
5303
5304 // Find the first entry in this file
5305 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5306 if (qfp->qf_fnum == bnr)
5307 break;
5308
5309 *errornr = idx;
5310 return qfp;
5311}
5312
5313/*
5314 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5315 * with the error number for the first entry. Assumes the entries are sorted in
5316 * the quickfix list by line number.
5317 */
5318 static qfline_T *
5319qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5320{
5321 while (!got_int
5322 && entry->qf_prev != NULL
5323 && entry->qf_fnum == entry->qf_prev->qf_fnum
5324 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5325 {
5326 entry = entry->qf_prev;
5327 --*errornr;
5328 }
5329
5330 return entry;
5331}
5332
5333/*
5334 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5335 * with the error number for the last entry. Assumes the entries are sorted in
5336 * the quickfix list by line number.
5337 */
5338 static qfline_T *
5339qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5340{
5341 while (!got_int &&
5342 entry->qf_next != NULL
5343 && entry->qf_fnum == entry->qf_next->qf_fnum
5344 && entry->qf_lnum == entry->qf_next->qf_lnum)
5345 {
5346 entry = entry->qf_next;
5347 ++*errornr;
5348 }
5349
5350 return entry;
5351}
5352
5353/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005354 * Returns TRUE if the specified quickfix entry is
5355 * after the given line (linewise is TRUE)
5356 * or after the line and column.
5357 */
5358 static int
5359qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5360{
5361 if (linewise)
5362 return qfp->qf_lnum > pos->lnum;
5363 else
5364 return (qfp->qf_lnum > pos->lnum ||
5365 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5366}
5367
5368/*
5369 * Returns TRUE if the specified quickfix entry is
5370 * before the given line (linewise is TRUE)
5371 * or before the line and column.
5372 */
5373 static int
5374qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5375{
5376 if (linewise)
5377 return qfp->qf_lnum < pos->lnum;
5378 else
5379 return (qfp->qf_lnum < pos->lnum ||
5380 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5381}
5382
5383/*
5384 * Returns TRUE if the specified quickfix entry is
5385 * on or after the given line (linewise is TRUE)
5386 * or on or after the line and column.
5387 */
5388 static int
5389qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5390{
5391 if (linewise)
5392 return qfp->qf_lnum >= pos->lnum;
5393 else
5394 return (qfp->qf_lnum > pos->lnum ||
5395 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5396}
5397
5398/*
5399 * Returns TRUE if the specified quickfix entry is
5400 * on or before the given line (linewise is TRUE)
5401 * or on or before the line and column.
5402 */
5403 static int
5404qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5405{
5406 if (linewise)
5407 return qfp->qf_lnum <= pos->lnum;
5408 else
5409 return (qfp->qf_lnum < pos->lnum ||
5410 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5411}
5412
5413/*
5414 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5415 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5416 * multiple entries on a single line as one. Otherwise returns the entry after
5417 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005418 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5419 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005420 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005421 */
5422 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005423qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005424 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005425 pos_T *pos,
5426 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005427 qfline_T *qfp,
5428 int *errornr)
5429{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005430 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005431 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005432 return qfp;
5433
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005434 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005435 while (qfp->qf_next != NULL
5436 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005437 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005438 {
5439 qfp = qfp->qf_next;
5440 ++*errornr;
5441 }
5442
5443 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005444 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005445 return NULL;
5446
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005447 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005448 qfp = qfp->qf_next;
5449 ++*errornr;
5450
5451 return qfp;
5452}
5453
5454/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005455 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5456 * If 'linewise' is TRUE, returns the entry before the specified line and
5457 * treats multiple entries on a single line as one. Otherwise returns the entry
5458 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005459 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5460 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005461 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005462 */
5463 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005464qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005465 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005466 pos_T *pos,
5467 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005468 qfline_T *qfp,
5469 int *errornr)
5470{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005471 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005472 while (qfp->qf_next != NULL
5473 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005474 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005475 {
5476 qfp = qfp->qf_next;
5477 ++*errornr;
5478 }
5479
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005480 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005481 return NULL;
5482
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005483 if (linewise)
5484 // If multiple entries are on the same line, then use the first entry
5485 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005486
5487 return qfp;
5488}
5489
5490/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005491 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005492 * the direction 'dir'.
5493 */
5494 static qfline_T *
5495qf_find_closest_entry(
5496 qf_list_T *qfl,
5497 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005498 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005499 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005500 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005501 int *errornr)
5502{
5503 qfline_T *qfp;
5504
5505 *errornr = 0;
5506
5507 // Find the first entry in this file
5508 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5509 if (qfp == NULL)
5510 return NULL; // no entry in this file
5511
5512 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005513 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005514 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005515 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005516
5517 return qfp;
5518}
5519
5520/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005521 * Get the nth quickfix entry below the specified entry. Searches forward in
5522 * the list. If linewise is TRUE, then treat multiple entries on a single line
5523 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005524 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005525 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005526qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005527{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005528 qfline_T *entry = entry_arg;
5529
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005530 while (n-- > 0 && !got_int)
5531 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005532 int first_errornr = *errornr;
5533
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005534 if (linewise)
5535 // Treat all the entries on the same line in this file as one
5536 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005537
5538 if (entry->qf_next == NULL
5539 || entry->qf_next->qf_fnum != entry->qf_fnum)
5540 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005541 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005542 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005543 break;
5544 }
5545
5546 entry = entry->qf_next;
5547 ++*errornr;
5548 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005549}
5550
5551/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005552 * Get the nth quickfix entry above the specified entry. Searches backwards in
5553 * the list. If linewise is TRUE, then treat multiple entries on a single line
5554 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005555 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005556 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005557qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005558{
5559 while (n-- > 0 && !got_int)
5560 {
5561 if (entry->qf_prev == NULL
5562 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5563 break;
5564
5565 entry = entry->qf_prev;
5566 --*errornr;
5567
5568 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005569 if (linewise)
5570 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005571 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005572}
5573
5574/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005575 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5576 * the specified direction. Returns the error number in the quickfix list or 0
5577 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005578 */
5579 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005580qf_find_nth_adj_entry(
5581 qf_list_T *qfl,
5582 int bnr,
5583 pos_T *pos,
5584 int n,
5585 int dir,
5586 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005587{
5588 qfline_T *adj_entry;
5589 int errornr;
5590
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005591 // Find an entry closest to the specified position
5592 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005593 if (adj_entry == NULL)
5594 return 0;
5595
5596 if (--n > 0)
5597 {
5598 // Go to the n'th entry in the current buffer
5599 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005600 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005601 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005602 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005603 }
5604
5605 return errornr;
5606}
5607
5608/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005609 * Jump to a quickfix entry in the current file nearest to the current line or
5610 * current line/col.
5611 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5612 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005613 */
5614 void
5615ex_cbelow(exarg_T *eap)
5616{
5617 qf_info_T *qi;
5618 qf_list_T *qfl;
5619 int dir;
5620 int buf_has_flag;
5621 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005622 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005623
5624 if (eap->addr_count > 0 && eap->line2 <= 0)
5625 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005626 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005627 return;
5628 }
5629
5630 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005631 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5632 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005633 buf_has_flag = BUF_HAS_QF_ENTRY;
5634 else
5635 buf_has_flag = BUF_HAS_LL_ENTRY;
5636 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5637 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005638 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005639 return;
5640 }
5641
5642 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5643 return;
5644
5645 qfl = qf_get_curlist(qi);
5646 // check if the list has valid errors
5647 if (!qf_list_has_valid_entries(qfl))
5648 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005649 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005650 return;
5651 }
5652
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005653 if (eap->cmdidx == CMD_cbelow
5654 || eap->cmdidx == CMD_lbelow
5655 || eap->cmdidx == CMD_cafter
5656 || eap->cmdidx == CMD_lafter)
5657 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005658 dir = FORWARD;
5659 else
5660 dir = BACKWARD;
5661
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005662 pos = curwin->w_cursor;
5663 // A quickfix entry column number is 1 based whereas cursor column
5664 // number is 0 based. Adjust the column number.
5665 pos.col++;
5666 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5667 eap->addr_count > 0 ? eap->line2 : 0, dir,
5668 eap->cmdidx == CMD_cbelow
5669 || eap->cmdidx == CMD_lbelow
5670 || eap->cmdidx == CMD_cabove
5671 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005672
5673 if (errornr > 0)
5674 qf_jump(qi, 0, errornr, FALSE);
5675 else
5676 emsg(_(e_no_more_items));
5677}
5678
5679/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005680 * Return the autocmd name for the :cfile Ex commands
5681 */
5682 static char_u *
5683cfile_get_auname(cmdidx_T cmdidx)
5684{
5685 switch (cmdidx)
5686 {
5687 case CMD_cfile: return (char_u *)"cfile";
5688 case CMD_cgetfile: return (char_u *)"cgetfile";
5689 case CMD_caddfile: return (char_u *)"caddfile";
5690 case CMD_lfile: return (char_u *)"lfile";
5691 case CMD_lgetfile: return (char_u *)"lgetfile";
5692 case CMD_laddfile: return (char_u *)"laddfile";
5693 default: return NULL;
5694 }
5695}
5696
5697/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005698 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005699 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 */
5701 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005702ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005704 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005705 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005706 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005707 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005708 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005709 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005710
Bram Moolenaara16123a2019-03-28 20:31:07 +01005711 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005712 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5713 NULL, FALSE, curbuf))
5714 {
5715#ifdef FEAT_EVAL
5716 if (aborting())
5717 return;
5718#endif
5719 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005720
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005721 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005722#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005723 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005724 {
5725 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005726 NULL, NULL,
5727 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005728 if (browse_file == NULL)
5729 return;
5730 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5731 vim_free(browse_file);
5732 }
5733 else
5734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005736 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005737
Bram Moolenaar39665952018-08-15 20:59:48 +02005738 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005739 wp = curwin;
5740
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005741 incr_quickfix_busy();
5742
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005743 // This function is used by the :cfile, :cgetfile and :caddfile
5744 // commands.
5745 // :cfile always creates a new quickfix list and jumps to the
5746 // first error.
5747 // :cgetfile creates a new quickfix list but doesn't jump to the
5748 // first error.
5749 // :caddfile adds to an existing quickfix list. If there is no
5750 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005751 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005752 && eap->cmdidx != CMD_laddfile),
5753 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005754 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005755 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005756 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005757 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005758 {
5759 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005760 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005761 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005762 }
5763 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005764 qf_list_changed(qf_get_curlist(qi));
5765 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005766 if (au_name != NULL)
5767 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005768
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005769 // Jump to the first error for a new list and if autocmds didn't
5770 // free the list.
5771 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5772 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005773 // display the first error
5774 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005775
5776 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005777}
5778
5779/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005780 * Return the vimgrep autocmd name.
5781 */
5782 static char_u *
5783vgr_get_auname(cmdidx_T cmdidx)
5784{
5785 switch (cmdidx)
5786 {
5787 case CMD_vimgrep: return (char_u *)"vimgrep";
5788 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5789 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5790 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5791 case CMD_grep: return (char_u *)"grep";
5792 case CMD_lgrep: return (char_u *)"lgrep";
5793 case CMD_grepadd: return (char_u *)"grepadd";
5794 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5795 default: return NULL;
5796 }
5797}
5798
5799/*
5800 * Initialize the regmatch used by vimgrep for pattern "s".
5801 */
5802 static void
5803vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5804{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005805 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005806 regmatch->regprog = NULL;
5807
5808 if (s == NULL || *s == NUL)
5809 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005810 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005811 if (last_search_pat() == NULL)
5812 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005813 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005814 return;
5815 }
5816 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5817 }
5818 else
5819 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5820
5821 regmatch->rmm_ic = p_ic;
5822 regmatch->rmm_maxcol = 0;
5823}
5824
5825/*
5826 * Display a file name when vimgrep is running.
5827 */
5828 static void
5829vgr_display_fname(char_u *fname)
5830{
5831 char_u *p;
5832
5833 msg_start();
5834 p = msg_strtrunc(fname, TRUE);
5835 if (p == NULL)
5836 msg_outtrans(fname);
5837 else
5838 {
5839 msg_outtrans(p);
5840 vim_free(p);
5841 }
5842 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005843 msg_didout = FALSE; // overwrite this message
5844 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005845 msg_col = 0;
5846 out_flush();
5847}
5848
5849/*
5850 * Load a dummy buffer to search for a pattern using vimgrep.
5851 */
5852 static buf_T *
5853vgr_load_dummy_buf(
5854 char_u *fname,
5855 char_u *dirname_start,
5856 char_u *dirname_now)
5857{
5858 int save_mls;
5859#if defined(FEAT_SYN_HL)
5860 char_u *save_ei = NULL;
5861#endif
5862 buf_T *buf;
5863
5864#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005865 // Don't do Filetype autocommands to avoid loading syntax and
5866 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005867 save_ei = au_event_disable(",Filetype");
5868#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005869 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005870 save_mls = p_mls;
5871 p_mls = 0;
5872
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005873 // Load file into a buffer, so that 'fileencoding' is detected,
5874 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005875 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
5876
5877 p_mls = save_mls;
5878#if defined(FEAT_SYN_HL)
5879 au_event_restore(save_ei);
5880#endif
5881
5882 return buf;
5883}
5884
5885/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005886 * Check whether a quickfix/location list is valid. Autocmds may remove or
5887 * change a quickfix list when vimgrep is running. If the list is not found,
5888 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005889 */
5890 static int
5891vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005892 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005893 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005894 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005895 char_u *title)
5896{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005897 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005898 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005899 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005900 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005901 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005902 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02005903 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005904 return FALSE;
5905 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005906 else
5907 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005908 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005909 qf_new_list(qi, title);
5910 return TRUE;
5911 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005912 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005913
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005914 if (qf_restore_list(qi, qfid) == FAIL)
5915 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005916
5917 return TRUE;
5918}
5919
5920/*
5921 * Search for a pattern in all the lines in a buffer and add the matching lines
5922 * to a quickfix list.
5923 */
5924 static int
5925vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01005926 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005927 char_u *fname,
5928 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005929 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005930 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01005931 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005932 int duplicate_name,
5933 int flags)
5934{
5935 int found_match = FALSE;
5936 long lnum;
5937 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02005938 int pat_len = (int)STRLEN(spat);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005939
Bram Moolenaar1c299432018-10-28 14:36:09 +01005940 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005941 {
5942 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005943 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005944 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005945 // Regular expression match
5946 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
5947 col, NULL, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005948 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005949 // Pass the buffer number so that it gets used even for a
5950 // dummy buffer, unless duplicate_name is set, then the
5951 // buffer will be wiped out below.
5952 if (qf_add_entry(qfl,
5953 NULL, // dir
5954 fname,
5955 NULL,
5956 duplicate_name ? 0 : buf->b_fnum,
5957 ml_get_buf(buf,
5958 regmatch->startpos[0].lnum + lnum, FALSE),
5959 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02005960 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005961 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02005962 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005963 FALSE, // vis_col
5964 NULL, // search pattern
5965 0, // nr
5966 0, // type
5967 TRUE // valid
5968 ) == QF_FAIL)
5969 {
5970 got_int = TRUE;
5971 break;
5972 }
5973 found_match = TRUE;
5974 if (--*tomatch == 0)
5975 break;
5976 if ((flags & VGR_GLOBAL) == 0
5977 || regmatch->endpos[0].lnum > 0)
5978 break;
5979 col = regmatch->endpos[0].col
5980 + (col == regmatch->endpos[0].col);
5981 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
5982 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005983 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005984 }
5985 else
5986 {
5987 char_u *str = ml_get_buf(buf, lnum, FALSE);
5988 int score;
5989 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02005990 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005991
5992 // Fuzzy string match
5993 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
5994 {
5995 // Pass the buffer number so that it gets used even for a
5996 // dummy buffer, unless duplicate_name is set, then the
5997 // buffer will be wiped out below.
5998 if (qf_add_entry(qfl,
5999 NULL, // dir
6000 fname,
6001 NULL,
6002 duplicate_name ? 0 : buf->b_fnum,
6003 str,
6004 lnum,
thinca6864efa2021-06-19 20:45:20 +02006005 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006006 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006007 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006008 FALSE, // vis_col
6009 NULL, // search pattern
6010 0, // nr
6011 0, // type
6012 TRUE // valid
6013 ) == QF_FAIL)
6014 {
6015 got_int = TRUE;
6016 break;
6017 }
6018 found_match = TRUE;
6019 if (--*tomatch == 0)
6020 break;
6021 if ((flags & VGR_GLOBAL) == 0)
6022 break;
6023 col = matches[pat_len - 1] + col + 1;
6024 if (col > (colnr_T)STRLEN(str))
6025 break;
6026 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006027 }
6028 line_breakcheck();
6029 if (got_int)
6030 break;
6031 }
6032
6033 return found_match;
6034}
6035
6036/*
6037 * Jump to the first match and update the directory.
6038 */
6039 static void
6040vgr_jump_to_match(
6041 qf_info_T *qi,
6042 int forceit,
6043 int *redraw_for_dummy,
6044 buf_T *first_match_buf,
6045 char_u *target_dir)
6046{
6047 buf_T *buf;
6048
6049 buf = curbuf;
6050 qf_jump(qi, 0, 0, forceit);
6051 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006052 // If we jumped to another buffer redrawing will already be
6053 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006054 *redraw_for_dummy = FALSE;
6055
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006056 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006057 if (curbuf == first_match_buf && target_dir != NULL)
6058 {
6059 exarg_T ea;
6060
Bram Moolenaara80faa82020-04-12 19:37:17 +02006061 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006062 ea.arg = target_dir;
6063 ea.cmdidx = CMD_lcd;
6064 ex_cd(&ea);
6065 }
6066}
6067
6068/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006069 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006070 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006071typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006072{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006073 long tomatch; // maximum number of matches to find
6074 char_u *spat; // search pattern
6075 int flags; // search modifier
6076 char_u **fnames; // list of files to search
6077 int fcount; // number of files
6078 regmmatch_T regmatch; // compiled search pattern
6079 char_u *qf_title; // quickfix list title
6080} vgr_args_T;
6081
6082/*
6083 * Process :vimgrep command arguments. The command syntax is:
6084 *
6085 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6086 */
6087 static int
6088vgr_process_args(
6089 exarg_T *eap,
6090 vgr_args_T *args)
6091{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006092 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006093
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006094 vim_memset(args, 0, sizeof(*args));
Bram Moolenaar86b68352004-12-27 21:59:20 +00006095
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006096 args->regmatch.regprog = NULL;
6097 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006098
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006099 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006100 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006101 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006102 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006103
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006104 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006105 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006106 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006107 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006108 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006109 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006110 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006111
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006112 vgr_init_regmatch(&args->regmatch, args->spat);
6113 if (args->regmatch.regprog == NULL)
6114 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006115
6116 p = skipwhite(p);
6117 if (*p == NUL)
6118 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006119 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006120 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006121 }
6122
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006123 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006124 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6125 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006126 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006127 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006128 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006129 }
6130
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006131 return OK;
6132}
6133
6134/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006135 * Return TRUE if "buf" had an existing swap file, the current swap file does
6136 * not end in ".swp".
6137 */
6138 static int
6139existing_swapfile(buf_T *buf)
6140{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006141 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006142 {
6143 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6144 size_t len = STRLEN(fname);
6145
6146 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6147 }
6148 return FALSE;
6149}
6150
6151/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006152 * Search for a pattern in a list of files and populate the quickfix list with
6153 * the matches.
6154 */
6155 static int
6156vgr_process_files(
6157 win_T *wp,
6158 qf_info_T *qi,
6159 vgr_args_T *cmd_args,
6160 int *redraw_for_dummy,
6161 buf_T **first_match_buf,
6162 char_u **target_dir)
6163{
6164 int status = FAIL;
6165 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6166 time_t seconds = 0;
6167 char_u *fname;
6168 int fi;
6169 buf_T *buf;
6170 int duplicate_name = FALSE;
6171 int using_dummy;
6172 char_u *dirname_start = NULL;
6173 char_u *dirname_now = NULL;
6174 int found_match;
6175 aco_save_T aco;
6176
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006177 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6178 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006179 if (dirname_start == NULL || dirname_now == NULL)
6180 goto theend;
6181
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006182 // Remember the current directory, because a BufRead autocommand that does
6183 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006184 mch_dirname(dirname_start, MAXPATHL);
6185
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006186 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006187 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6188 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006189 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006190 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006191 if (time(NULL) > seconds)
6192 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006193 // Display the file name every second or so, show the user we are
6194 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006195 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006196 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006197 }
6198
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006199 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006200 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6201 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006202 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006203 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006204 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006205 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006206
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006207 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006208 }
6209 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006210 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006211 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006212
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006213 // Check whether the quickfix list is still valid. When loading a
6214 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006215 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006216 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006217
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006218 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006219
Bram Moolenaar81695252004-12-29 20:58:21 +00006220 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006221 {
6222 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006223 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006224 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006225 else
6226 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006227 // Try for a match in all lines of the buffer.
6228 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006229 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006230 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006231 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006232
Bram Moolenaar81695252004-12-29 20:58:21 +00006233 if (using_dummy)
6234 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006235 if (found_match && *first_match_buf == NULL)
6236 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006237 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006238 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006239 // Never keep a dummy buffer if there is another buffer
6240 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006241 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006242 buf = NULL;
6243 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006244 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006245 || buf->b_p_bh[0] == 'u' // "unload"
6246 || buf->b_p_bh[0] == 'w' // "wipe"
6247 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006248 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006249 // When no match was found we don't need to remember the
6250 // buffer, wipe it out. If there was a match and it
6251 // wasn't the first one or we won't jump there: only
6252 // unload the buffer.
6253 // Ignore 'hidden' here, because it may lead to having too
6254 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006255 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006256 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006257 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006258 buf = NULL;
6259 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006260 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006261 || (cmd_args->flags & VGR_NOJUMP)
6262 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006263 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006264 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006265 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006266 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006267 buf = NULL;
6268 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006269 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006270
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006271 if (buf != NULL)
6272 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006273 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006274 buf->b_flags &= ~BF_DUMMY;
6275
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006276 // If the buffer is still loaded we need to use the
6277 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006278 if (buf == *first_match_buf
6279 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006280 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006281 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006282
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006283 // The buffer is still loaded, the Filetype autocommands
6284 // need to be done now, in that buffer. And the modelines
6285 // need to be done (again). But not the window-local
6286 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006287 aucmd_prepbuf(&aco, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006288#if defined(FEAT_SYN_HL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006289 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
6290 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006291#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00006292 do_modelines(OPT_NOWIN);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006293 aucmd_restbuf(&aco);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006294 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006295 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006296 }
6297 }
6298
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006299 status = OK;
6300
6301theend:
6302 vim_free(dirname_now);
6303 vim_free(dirname_start);
6304 return status;
6305}
6306
6307/*
6308 * ":vimgrep {pattern} file(s)"
6309 * ":vimgrepadd {pattern} file(s)"
6310 * ":lvimgrep {pattern} file(s)"
6311 * ":lvimgrepadd {pattern} file(s)"
6312 */
6313 void
6314ex_vimgrep(exarg_T *eap)
6315{
6316 vgr_args_T args;
6317 qf_info_T *qi;
6318 qf_list_T *qfl;
6319 int_u save_qfid;
6320 win_T *wp = NULL;
6321 int redraw_for_dummy = FALSE;
6322 buf_T *first_match_buf = NULL;
6323 char_u *target_dir = NULL;
6324 char_u *au_name = NULL;
6325 int status;
6326
6327 au_name = vgr_get_auname(eap->cmdidx);
6328 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6329 curbuf->b_fname, TRUE, curbuf))
6330 {
6331#ifdef FEAT_EVAL
6332 if (aborting())
6333 return;
6334#endif
6335 }
6336
6337 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6338 if (qi == NULL)
6339 return;
6340
6341 if (vgr_process_args(eap, &args) == FAIL)
6342 goto theend;
6343
6344 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6345 && eap->cmdidx != CMD_vimgrepadd
6346 && eap->cmdidx != CMD_lvimgrepadd)
6347 || qf_stack_empty(qi))
6348 // make place for a new list
6349 qf_new_list(qi, args.qf_title);
6350
6351 incr_quickfix_busy();
6352
6353 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6354 &first_match_buf, &target_dir);
6355 if (status != OK)
6356 {
6357 FreeWild(args.fcount, args.fnames);
6358 decr_quickfix_busy();
6359 goto theend;
6360 }
6361
6362 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006363
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006364 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006365 qfl->qf_nonevalid = FALSE;
6366 qfl->qf_ptr = qfl->qf_start;
6367 qfl->qf_index = 1;
6368 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006369
Bram Moolenaar864293a2016-06-02 13:40:04 +02006370 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006371
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006372 // Remember the current quickfix list identifier, so that we can check for
6373 // autocommands changing the current quickfix list.
6374 save_qfid = qf_get_curlist(qi)->qf_id;
6375
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006376 if (au_name != NULL)
6377 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6378 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006379 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6380 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006381 if (!qflist_valid(wp, save_qfid)
6382 || qf_restore_list(qi, save_qfid) == FAIL)
6383 {
6384 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006385 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006386 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006387
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006388 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006389 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006390 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006391 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006392 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6393 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006394 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006395 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006396 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006397
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006398 decr_quickfix_busy();
6399
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006400 // If we loaded a dummy buffer into the current window, the autocommands
6401 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006402 if (redraw_for_dummy)
6403 {
6404#ifdef FEAT_FOLDING
6405 foldUpdateAll(curwin);
6406#else
6407 redraw_later(NOT_VALID);
6408#endif
6409 }
6410
Bram Moolenaar86b68352004-12-27 21:59:20 +00006411theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006412 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006413 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006414 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006415}
6416
6417/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006418 * Restore current working directory to "dirname_start" if they differ, taking
6419 * into account whether it is set locally or globally.
6420 */
6421 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006422restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006423{
6424 char_u *dirname_now = alloc(MAXPATHL);
6425
6426 if (NULL != dirname_now)
6427 {
6428 mch_dirname(dirname_now, MAXPATHL);
6429 if (STRCMP(dirname_start, dirname_now) != 0)
6430 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006431 // If the directory has changed, change it back by building up an
6432 // appropriate ex command and executing it.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006433 exarg_T ea;
6434
Bram Moolenaara80faa82020-04-12 19:37:17 +02006435 CLEAR_FIELD(ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006436 ea.arg = dirname_start;
6437 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6438 ex_cd(&ea);
6439 }
Bram Moolenaarf1354352012-11-28 22:12:44 +01006440 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006441 }
6442}
6443
6444/*
6445 * Load file "fname" into a dummy buffer and return the buffer pointer,
6446 * placing the directory resulting from the buffer load into the
6447 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6448 * prior to calling this function. Restores directory to "dirname_start" prior
6449 * to returning, if autocmds or the 'autochdir' option have changed it.
6450 *
6451 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6452 * or wipe_dummy_buffer() later!
6453 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006454 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006455 */
6456 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006457load_dummy_buffer(
6458 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006459 char_u *dirname_start, // in: old directory
6460 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006461{
6462 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006463 bufref_T newbufref;
6464 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006465 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006466 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006467 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006468
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006469 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006470 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6471 if (newbuf == NULL)
6472 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006473 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006474
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006475 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006476 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6477
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006478 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006479 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006480 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006481 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006482 ++newbuf->b_locked;
6483
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006484 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006485 aucmd_prepbuf(&aco, newbuf);
6486
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006487 // Need to set the filename for autocommands.
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006488 (void)setfname(curbuf, fname, NULL, FALSE);
6489
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006490 // Create swap file now to avoid the ATTENTION message.
Bram Moolenaar81695252004-12-29 20:58:21 +00006491 check_need_swap(TRUE);
6492
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006493 // Remove the "dummy" flag, otherwise autocommands may not
6494 // work.
Bram Moolenaar81695252004-12-29 20:58:21 +00006495 curbuf->b_flags &= ~BF_DUMMY;
6496
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006497 newbuf_to_wipe.br_buf = NULL;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006498 readfile_result = readfile(fname, NULL,
Bram Moolenaar81695252004-12-29 20:58:21 +00006499 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006500 NULL, READ_NEW | READ_DUMMY);
6501 --newbuf->b_locked;
6502 if (readfile_result == OK
Bram Moolenaard68071d2006-05-02 22:08:30 +00006503 && !got_int
Bram Moolenaar81695252004-12-29 20:58:21 +00006504 && !(curbuf->b_flags & BF_NEW))
6505 {
6506 failed = FALSE;
6507 if (curbuf != newbuf)
6508 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006509 // Bloody autocommands changed the buffer! Can happen when
6510 // using netrw and editing a remote file. Use the current
6511 // buffer instead, delete the dummy one after restoring the
6512 // window stuff.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006513 set_bufref(&newbuf_to_wipe, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006514 newbuf = curbuf;
6515 }
6516 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006517
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006518 // restore curwin/curbuf and a few other things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006519 aucmd_restbuf(&aco);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006520 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6521 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006522
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006523 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6524 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006525 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006526 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006527
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006528 // When autocommands/'autochdir' option changed directory: go back.
6529 // Let the caller know what the resulting dir was first, in case it is
6530 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006531 mch_dirname(resulting_dir, MAXPATHL);
6532 restore_start_dir(dirname_start);
6533
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006534 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006535 return NULL;
6536 if (failed)
6537 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006538 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006539 return NULL;
6540 }
6541 return newbuf;
6542}
6543
6544/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006545 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6546 * directory to "dirname_start" prior to returning, if autocmds or the
6547 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006548 */
6549 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006550wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006551{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006552 // If any autocommand opened a window on the dummy buffer, close that
6553 // window. If we can't close them all then give up.
6554 while (buf->b_nwindows > 0)
6555 {
6556 int did_one = FALSE;
6557 win_T *wp;
6558
6559 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006560 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006561 if (wp->w_buffer == buf)
6562 {
6563 if (win_close(wp, FALSE) == OK)
6564 did_one = TRUE;
6565 break;
6566 }
6567 if (!did_one)
6568 return;
6569 }
6570
6571 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006572 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006573#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006574 cleanup_T cs;
6575
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006576 // Reset the error/interrupt/exception state here so that aborting()
6577 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6578 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006579 enter_cleanup(&cs);
6580#endif
6581
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006582 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006583
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006584#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006585 // Restore the error/interrupt/exception state if not discarded by a
6586 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006587 leave_cleanup(&cs);
6588#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006589 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006590 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006591 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006592}
6593
6594/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006595 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6596 * directory to "dirname_start" prior to returning, if autocmds or the
6597 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006598 */
6599 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006600unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006601{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006602 if (curbuf != buf) // safety check
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006603 {
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006604 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006605
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006606 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006607 restore_start_dir(dirname_start);
6608 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006609}
6610
Bram Moolenaar05159a02005-02-26 23:04:13 +00006611#if defined(FEAT_EVAL) || defined(PROTO)
6612/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006613 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006614 * to 'list'. Returns OK on success.
6615 */
6616 static int
6617get_qfline_items(qfline_T *qfp, list_T *list)
6618{
6619 int bufnum;
6620 dict_T *dict;
6621 char_u buf[2];
6622
6623 // Handle entries with a non-existing buffer number.
6624 bufnum = qfp->qf_fnum;
6625 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6626 bufnum = 0;
6627
6628 if ((dict = dict_alloc()) == NULL)
6629 return FAIL;
6630 if (list_append_dict(list, dict) == FAIL)
6631 return FAIL;
6632
6633 buf[0] = qfp->qf_type;
6634 buf[1] = NUL;
6635 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006636 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6637 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6638 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6639 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6640 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6641 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006642 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6643 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6644 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6645 || dict_add_string(dict, "type", buf) == FAIL
6646 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6647 return FAIL;
6648
6649 return OK;
6650}
6651
6652/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006653 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006654 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006655 * If eidx is not 0, then return only the specified entry. Otherwise return
6656 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006657 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006658 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006659get_errorlist(
6660 qf_info_T *qi_arg,
6661 win_T *wp,
6662 int qf_idx,
6663 int eidx,
6664 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006665{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006666 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006667 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006668 qfline_T *qfp;
6669 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006670
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006671 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006672 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006673 qi = &ql_info;
6674 if (wp != NULL)
6675 {
6676 qi = GET_LOC_LIST(wp);
6677 if (qi == NULL)
6678 return FAIL;
6679 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006680 }
6681
Bram Moolenaar858ba062020-05-31 23:11:59 +02006682 if (eidx < 0)
6683 return OK;
6684
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006685 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006686 qf_idx = qi->qf_curlist;
6687
Bram Moolenaar0398e002019-03-21 21:12:49 +01006688 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006689 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006690
Bram Moolenaar0398e002019-03-21 21:12:49 +01006691 qfl = qf_get_list(qi, qf_idx);
6692 if (qf_list_empty(qfl))
6693 return FAIL;
6694
Bram Moolenaara16123a2019-03-28 20:31:07 +01006695 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006696 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006697 if (eidx > 0)
6698 {
6699 if (eidx == i)
6700 return get_qfline_items(qfp, list);
6701 }
6702 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006703 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006704 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006705
Bram Moolenaar05159a02005-02-26 23:04:13 +00006706 return OK;
6707}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006708
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006709// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006710enum {
6711 QF_GETLIST_NONE = 0x0,
6712 QF_GETLIST_TITLE = 0x1,
6713 QF_GETLIST_ITEMS = 0x2,
6714 QF_GETLIST_NR = 0x4,
6715 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006716 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006717 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006718 QF_GETLIST_IDX = 0x40,
6719 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006720 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006721 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006722 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006723 QF_GETLIST_QFTF = 0x800,
6724 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006725};
6726
6727/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006728 * Parse text from 'di' and return the quickfix list items.
6729 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006730 */
6731 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006732qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006733{
6734 int status = FAIL;
6735 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006736 char_u *errorformat = p_efm;
6737 dictitem_T *efm_di;
6738 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006739
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006740 // Only a List value is supported
Bram Moolenaar2c809b72017-09-01 18:34:02 +02006741 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006742 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006743 // If errorformat is supplied then use it, otherwise use the 'efm'
6744 // option setting
Bram Moolenaar36538222017-09-02 19:51:44 +02006745 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
6746 {
6747 if (efm_di->di_tv.v_type != VAR_STRING ||
6748 efm_di->di_tv.vval.v_string == NULL)
6749 return FAIL;
6750 errorformat = efm_di->di_tv.vval.v_string;
6751 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006752
Bram Moolenaar36538222017-09-02 19:51:44 +02006753 l = list_alloc();
Bram Moolenaarda732532017-08-31 20:58:02 +02006754 if (l == NULL)
6755 return FAIL;
6756
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006757 qi = qf_alloc_stack(QFLT_INTERNAL);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006758 if (qi != NULL)
6759 {
Bram Moolenaar36538222017-09-02 19:51:44 +02006760 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006761 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6762 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006763 (void)get_errorlist(qi, NULL, 0, 0, l);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02006764 qf_free(&qi->qf_lists[0]);
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006765 }
6766 free(qi);
6767 }
Bram Moolenaarda732532017-08-31 20:58:02 +02006768 dict_add_list(retdict, "items", l);
6769 status = OK;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006770 }
6771
6772 return status;
6773}
6774
6775/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006776 * Return the quickfix/location list window identifier in the current tabpage.
6777 */
6778 static int
6779qf_winid(qf_info_T *qi)
6780{
6781 win_T *win;
6782
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006783 // The quickfix window can be opened even if the quickfix list is not set
6784 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006785 if (qi == NULL)
6786 return 0;
6787 win = qf_find_win(qi);
6788 if (win != NULL)
6789 return win->w_id;
6790 return 0;
6791}
6792
6793/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006794 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006795 * window. If there is no buffer associated with the list or the buffer is
6796 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006797 */
6798 static int
6799qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6800{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006801 int bufnum = 0;
6802
6803 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
6804 bufnum = qi->qf_bufnr;
6805
6806 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006807}
6808
6809/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006810 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006811 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006812 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006813qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006814{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006815 int flags = QF_GETLIST_NONE;
6816
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006817 if (dict_find(what, (char_u *)"all", -1) != NULL)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006818 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006819 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006820 if (!loclist)
6821 // File window ID is applicable only to location list windows
6822 flags &= ~ QF_GETLIST_FILEWINID;
6823 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006824
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006825 if (dict_find(what, (char_u *)"title", -1) != NULL)
6826 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006827
Bram Moolenaara6d48492017-12-12 22:45:31 +01006828 if (dict_find(what, (char_u *)"nr", -1) != NULL)
6829 flags |= QF_GETLIST_NR;
6830
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006831 if (dict_find(what, (char_u *)"winid", -1) != NULL)
6832 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006833
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006834 if (dict_find(what, (char_u *)"context", -1) != NULL)
6835 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006836
Bram Moolenaara6d48492017-12-12 22:45:31 +01006837 if (dict_find(what, (char_u *)"id", -1) != NULL)
6838 flags |= QF_GETLIST_ID;
6839
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006840 if (dict_find(what, (char_u *)"items", -1) != NULL)
6841 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006842
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006843 if (dict_find(what, (char_u *)"idx", -1) != NULL)
6844 flags |= QF_GETLIST_IDX;
6845
6846 if (dict_find(what, (char_u *)"size", -1) != NULL)
6847 flags |= QF_GETLIST_SIZE;
6848
Bram Moolenaarb254af32017-12-18 19:48:58 +01006849 if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
6850 flags |= QF_GETLIST_TICK;
6851
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006852 if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
6853 flags |= QF_GETLIST_FILEWINID;
6854
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006855 if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
6856 flags |= QF_GETLIST_QFBUFNR;
6857
Bram Moolenaard43906d2020-07-20 21:31:32 +02006858 if (dict_find(what, (char_u *)"quickfixtextfunc", -1) != NULL)
6859 flags |= QF_GETLIST_QFTF;
6860
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006861 return flags;
6862}
Bram Moolenaara6d48492017-12-12 22:45:31 +01006863
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006864/*
6865 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
6866 * If 'nr' and 'id' are not present in 'what' then return the current
6867 * quickfix list index.
6868 * If 'nr' is zero then return the current quickfix list index.
6869 * If 'nr' is '$' then return the last quickfix list index.
6870 * If 'id' is present then return the index of the quickfix list with that id.
6871 * If 'id' is zero then return the quickfix list index specified by 'nr'.
6872 * Return -1, if quickfix list is not present or if the stack is empty.
6873 */
6874 static int
6875qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
6876{
6877 int qf_idx;
6878 dictitem_T *di;
6879
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006880 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006881 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
6882 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006883 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006884 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006885 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006886 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006887 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01006888 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006889 qf_idx = di->di_tv.vval.v_number - 1;
6890 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006891 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006892 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01006893 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006894 else if (di->di_tv.v_type == VAR_STRING
6895 && di->di_tv.vval.v_string != NULL
6896 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006897 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006898 qf_idx = qi->qf_listcount - 1;
6899 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006900 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01006901 }
6902
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006903 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
6904 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006905 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006906 if (di->di_tv.v_type == VAR_NUMBER)
6907 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006908 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006909 if (di->di_tv.vval.v_number != 0)
6910 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
6911 }
6912 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02006913 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006914 }
6915
6916 return qf_idx;
6917}
6918
6919/*
6920 * Return default values for quickfix list properties in retdict.
6921 */
6922 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006923qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006924{
6925 int status = OK;
6926
6927 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006928 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006929 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
6930 {
6931 list_T *l = list_alloc();
6932 if (l != NULL)
6933 status = dict_add_list(retdict, "items", l);
6934 else
6935 status = FAIL;
6936 }
6937 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006938 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006939 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006940 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006941 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006942 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006943 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006944 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006945 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006946 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006947 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006948 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006949 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02006950 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01006951 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006952 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006953 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
6954 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02006955 if ((status == OK) && (flags & QF_GETLIST_QFTF))
6956 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006957
6958 return status;
6959}
6960
6961/*
6962 * Return the quickfix list title as 'title' in retdict
6963 */
6964 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006965qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006966{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006967 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006968}
6969
6970/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006971 * Returns the identifier of the window used to display files from a location
6972 * list. If there is no associated window, then returns 0. Useful only when
6973 * called from a location list window.
6974 */
6975 static int
6976qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
6977{
6978 int winid = 0;
6979
6980 if (wp != NULL && IS_LL_WINDOW(wp))
6981 {
6982 win_T *ll_wp = qf_find_win_with_loclist(qi);
6983 if (ll_wp != NULL)
6984 winid = ll_wp->w_id;
6985 }
6986
6987 return dict_add_number(retdict, "filewinid", winid);
6988}
6989
6990/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02006991 * Return the quickfix list items/entries as 'items' in retdict.
6992 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006993 */
6994 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006995qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006996{
6997 int status = OK;
6998 list_T *l = list_alloc();
6999 if (l != NULL)
7000 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007001 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007002 dict_add_list(retdict, "items", l);
7003 }
7004 else
7005 status = FAIL;
7006
7007 return status;
7008}
7009
7010/*
7011 * Return the quickfix list context (if any) as 'context' in retdict.
7012 */
7013 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007014qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007015{
7016 int status;
7017 dictitem_T *di;
7018
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007019 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007020 {
7021 di = dictitem_alloc((char_u *)"context");
7022 if (di != NULL)
7023 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007024 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007025 status = dict_add(retdict, di);
7026 if (status == FAIL)
7027 dictitem_free(di);
7028 }
7029 else
7030 status = FAIL;
7031 }
7032 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007033 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007034
7035 return status;
7036}
7037
7038/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007039 * Return the current quickfix list index as 'idx' in retdict.
7040 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007041 */
7042 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007043qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007044{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007045 if (eidx == 0)
7046 {
7047 eidx = qfl->qf_index;
7048 if (qf_list_empty(qfl))
7049 // For empty lists, current index is set to 0
7050 eidx = 0;
7051 }
7052 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007053}
7054
7055/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007056 * Return the 'quickfixtextfunc' function of a quickfix/location list
7057 */
7058 static int
7059qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7060{
7061 int status;
7062
7063 if (qfl->qftf_cb.cb_name != NULL)
7064 {
7065 typval_T tv;
7066
7067 put_callback(&qfl->qftf_cb, &tv);
7068 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7069 clear_tv(&tv);
7070 }
7071 else
7072 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7073
7074 return status;
7075}
7076
7077/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007078 * Return quickfix/location list details (title) as a
7079 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7080 * then current list is used. Otherwise the specified list is used.
7081 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007082 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007083qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7084{
7085 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007086 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007087 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007088 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007089 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007090 dictitem_T *di;
7091 int flags = QF_GETLIST_NONE;
7092
7093 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7094 return qf_get_list_from_lines(what, di, retdict);
7095
7096 if (wp != NULL)
7097 qi = GET_LOC_LIST(wp);
7098
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007099 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007100
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007101 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007102 qf_idx = qf_getprop_qfidx(qi, what);
7103
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007104 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007105 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007106 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007107
Bram Moolenaar0398e002019-03-21 21:12:49 +01007108 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007109
Bram Moolenaar858ba062020-05-31 23:11:59 +02007110 // If an entry index is specified, use that
7111 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7112 {
7113 if (di->di_tv.v_type != VAR_NUMBER)
7114 return FAIL;
7115 eidx = di->di_tv.vval.v_number;
7116 }
7117
Bram Moolenaard823fa92016-08-12 16:29:27 +02007118 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007119 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007120 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007121 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007122 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007123 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007124 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007125 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007126 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007127 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007128 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007129 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007130 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007131 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007132 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007133 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007134 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007135 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007136 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7137 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007138 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7139 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007140 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7141 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007142
Bram Moolenaard823fa92016-08-12 16:29:27 +02007143 return status;
7144}
7145
7146/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007147 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007148 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7149 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007150 */
7151 static int
7152qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007153 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007154 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007155 int first_entry,
7156 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007157{
7158 static int did_bufnr_emsg;
7159 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007160 int bufnum, valid, status, col, end_col, vcol, nr;
7161 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007162
7163 if (first_entry)
7164 did_bufnr_emsg = FALSE;
7165
Bram Moolenaar8f667172018-12-14 15:38:31 +01007166 filename = dict_get_string(d, (char_u *)"filename", TRUE);
7167 module = dict_get_string(d, (char_u *)"module", TRUE);
7168 bufnum = (int)dict_get_number(d, (char_u *)"bufnr");
7169 lnum = (int)dict_get_number(d, (char_u *)"lnum");
thinca6864efa2021-06-19 20:45:20 +02007170 end_lnum = (int)dict_get_number(d, (char_u *)"end_lnum");
Bram Moolenaar8f667172018-12-14 15:38:31 +01007171 col = (int)dict_get_number(d, (char_u *)"col");
thinca6864efa2021-06-19 20:45:20 +02007172 end_col = (int)dict_get_number(d, (char_u *)"end_col");
Bram Moolenaar8f667172018-12-14 15:38:31 +01007173 vcol = (int)dict_get_number(d, (char_u *)"vcol");
7174 nr = (int)dict_get_number(d, (char_u *)"nr");
7175 type = dict_get_string(d, (char_u *)"type", TRUE);
7176 pattern = dict_get_string(d, (char_u *)"pattern", TRUE);
7177 text = dict_get_string(d, (char_u *)"text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007178 if (text == NULL)
7179 text = vim_strsave((char_u *)"");
7180
7181 valid = TRUE;
7182 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7183 valid = FALSE;
7184
7185 // Mark entries with non-existing buffer number as not valid. Give the
7186 // error message only once.
7187 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7188 {
7189 if (!did_bufnr_emsg)
7190 {
7191 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007192 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007193 }
7194 valid = FALSE;
7195 bufnum = 0;
7196 }
7197
7198 // If the 'valid' field is present it overrules the detected value.
7199 if ((dict_find(d, (char_u *)"valid", -1)) != NULL)
Bram Moolenaar401f0c02020-09-05 22:37:39 +02007200 valid = (int)dict_get_bool(d, (char_u *)"valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007201
Bram Moolenaar0398e002019-03-21 21:12:49 +01007202 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007203 NULL, // dir
7204 filename,
7205 module,
7206 bufnum,
7207 text,
7208 lnum,
thinca6864efa2021-06-19 20:45:20 +02007209 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007210 col,
thinca6864efa2021-06-19 20:45:20 +02007211 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007212 vcol, // vis_col
7213 pattern, // search pattern
7214 nr,
7215 type == NULL ? NUL : *type,
7216 valid);
7217
7218 vim_free(filename);
7219 vim_free(module);
7220 vim_free(pattern);
7221 vim_free(text);
7222 vim_free(type);
7223
Bram Moolenaar9752c722018-12-22 16:49:34 +01007224 if (valid)
7225 *valid_entry = TRUE;
7226
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007227 return status;
7228}
7229
7230/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007231 * Add list of entries to quickfix/location list. Each list entry is
7232 * a dictionary with item information.
7233 */
7234 static int
7235qf_add_entries(
7236 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007237 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007238 list_T *list,
7239 char_u *title,
7240 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007241{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007242 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007243 listitem_T *li;
7244 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007245 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007246 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007247 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007248
Bram Moolenaara3921f42017-06-04 15:30:34 +02007249 if (action == ' ' || qf_idx == qi->qf_listcount)
7250 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007251 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007252 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007253 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007254 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007255 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007256 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007257 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007258 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007259 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007260 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007261 qf_free_items(qfl);
7262 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007263 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007264
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007265 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007266 {
7267 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007268 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007269
7270 d = li->li_tv.vval.v_dict;
7271 if (d == NULL)
7272 continue;
7273
Bram Moolenaar0398e002019-03-21 21:12:49 +01007274 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007275 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007276 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007277 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007278 }
7279
Bram Moolenaar9752c722018-12-22 16:49:34 +01007280 // Check if any valid error entries are added to the list.
7281 if (valid_entry)
7282 qfl->qf_nonevalid = FALSE;
7283 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007284 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007285 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007286
7287 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007288 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007289 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007290
7291 // Update the current error index if not appending to the list or if the
7292 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007293 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007294 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007295
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007296 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007297 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007298
7299 return retval;
7300}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007301
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007302/*
7303 * Get the quickfix list index from 'nr' or 'id'
7304 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007305 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007306qf_setprop_get_qfidx(
7307 qf_info_T *qi,
7308 dict_T *what,
7309 int action,
7310 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007311{
7312 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007313 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007314
Bram Moolenaard823fa92016-08-12 16:29:27 +02007315 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7316 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007317 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007318 if (di->di_tv.v_type == VAR_NUMBER)
7319 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007320 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007321 if (di->di_tv.vval.v_number != 0)
7322 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007323
Bram Moolenaar55b69262017-08-13 13:42:01 +02007324 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7325 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007326 // When creating a new list, accept qf_idx pointing to the next
7327 // non-available list and add the new list at the end of the
7328 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007329 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007330 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007331 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007332 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007333 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007334 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007335 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007336 }
7337 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007338 && di->di_tv.vval.v_string != NULL
7339 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007340 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007341 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007342 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007343 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007344 qf_idx = 0;
7345 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007346 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007347 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007348 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007349 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007350 }
7351
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007352 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007353 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007354 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007355 if (di->di_tv.v_type != VAR_NUMBER)
7356 return INVALID_QFIDX;
7357
7358 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007359 }
7360
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007361 return qf_idx;
7362}
7363
7364/*
7365 * Set the quickfix list title.
7366 */
7367 static int
7368qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7369{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007370 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007371
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007372 if (di->di_tv.v_type != VAR_STRING)
7373 return FAIL;
7374
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007375 vim_free(qfl->qf_title);
Bram Moolenaar8f667172018-12-14 15:38:31 +01007376 qfl->qf_title = dict_get_string(what, (char_u *)"title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007377 if (qf_idx == qi->qf_curlist)
7378 qf_update_win_titlevar(qi);
7379
7380 return OK;
7381}
7382
7383/*
7384 * Set quickfix list items/entries.
7385 */
7386 static int
7387qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7388{
7389 int retval = FAIL;
7390 char_u *title_save;
7391
7392 if (di->di_tv.v_type != VAR_LIST)
7393 return FAIL;
7394
7395 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7396 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7397 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007398 vim_free(title_save);
7399
7400 return retval;
7401}
7402
7403/*
7404 * Set quickfix list items/entries from a list of lines.
7405 */
7406 static int
7407qf_setprop_items_from_lines(
7408 qf_info_T *qi,
7409 int qf_idx,
7410 dict_T *what,
7411 dictitem_T *di,
7412 int action)
7413{
7414 char_u *errorformat = p_efm;
7415 dictitem_T *efm_di;
7416 int retval = FAIL;
7417
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007418 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007419 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7420 {
7421 if (efm_di->di_tv.v_type != VAR_STRING ||
7422 efm_di->di_tv.vval.v_string == NULL)
7423 return FAIL;
7424 errorformat = efm_di->di_tv.vval.v_string;
7425 }
7426
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007427 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007428 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7429 return FAIL;
7430
7431 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007432 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007433 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007434 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007435 retval = OK;
7436
7437 return retval;
7438}
7439
7440/*
7441 * Set quickfix list context.
7442 */
7443 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007444qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007445{
7446 typval_T *ctx;
7447
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007448 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007449 ctx = alloc_tv();
7450 if (ctx != NULL)
7451 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007452 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007453
7454 return OK;
7455}
7456
7457/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007458 * Set the current index in the specified quickfix list
7459 */
7460 static int
7461qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7462{
7463 int denote = FALSE;
7464 int newidx;
7465 int old_qfidx;
7466 qfline_T *qf_ptr;
7467
7468 // If the specified index is '$', then use the last entry
7469 if (di->di_tv.v_type == VAR_STRING
7470 && di->di_tv.vval.v_string != NULL
7471 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7472 newidx = qfl->qf_count;
7473 else
7474 {
7475 // Otherwise use the specified index
7476 newidx = tv_get_number_chk(&di->di_tv, &denote);
7477 if (denote)
7478 return FAIL;
7479 }
7480
7481 if (newidx < 1) // sanity check
7482 return FAIL;
7483 if (newidx > qfl->qf_count)
7484 newidx = qfl->qf_count;
7485
7486 old_qfidx = qfl->qf_index;
7487 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7488 if (qf_ptr == NULL)
7489 return FAIL;
7490 qfl->qf_ptr = qf_ptr;
7491 qfl->qf_index = newidx;
7492
7493 // If the current list is modified and it is displayed in the quickfix
7494 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007495 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007496 qf_win_pos_update(qi, old_qfidx);
7497
7498 return OK;
7499}
7500
7501/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007502 * Set the current index in the specified quickfix list
7503 */
7504 static int
7505qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7506{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007507 callback_T cb;
7508
7509 free_callback(&qfl->qftf_cb);
7510 cb = get_callback(&di->di_tv);
7511 if (cb.cb_name != NULL && *cb.cb_name != NUL)
7512 set_callback(&qfl->qftf_cb, &cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007513
7514 return OK;
7515}
7516
7517/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007518 * Set quickfix/location list properties (title, items, context).
7519 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007520 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007521 */
7522 static int
7523qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7524{
7525 dictitem_T *di;
7526 int retval = FAIL;
7527 int qf_idx;
7528 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007529 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007530
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007531 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007532 newlist = TRUE;
7533
7534 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007535 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007536 return FAIL;
7537
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007538 if (newlist)
7539 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007540 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007541 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007542 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007543 }
7544
Bram Moolenaar0398e002019-03-21 21:12:49 +01007545 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007546 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007547 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007548 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007549 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007550 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007551 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007552 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007553 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007554 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7555 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007556 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7557 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007558
Bram Moolenaar287153c2020-11-29 14:20:27 +01007559 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007560 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007561 if (newlist)
7562 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007563
Bram Moolenaard823fa92016-08-12 16:29:27 +02007564 return retval;
7565}
7566
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007567/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007568 * Free the entire quickfix/location list stack.
7569 * If the quickfix/location list window is open, then clear it.
7570 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007571 static void
7572qf_free_stack(win_T *wp, qf_info_T *qi)
7573{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007574 win_T *qfwin = qf_find_win(qi);
7575 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007576
7577 if (qfwin != NULL)
7578 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007579 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007580 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007581 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007582 qf_update_buffer(qi, NULL);
7583 }
7584
7585 if (wp != NULL && IS_LL_WINDOW(wp))
7586 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007587 // If in the location list window, then use the non-location list
7588 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007589 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007590 if (llwin != NULL)
7591 wp = llwin;
7592 }
7593
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007594 qf_free_all(wp);
7595 if (wp == NULL)
7596 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007597 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007598 qi->qf_curlist = 0;
7599 qi->qf_listcount = 0;
7600 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007601 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007602 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007603 // If the location list window is open, then create a new empty
7604 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007605 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007606
Bram Moolenaar95946f12019-03-31 15:31:59 +02007607 if (new_ll != NULL)
7608 {
7609 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007610
Bram Moolenaar95946f12019-03-31 15:31:59 +02007611 // first free the list reference in the location list window
7612 ll_free_all(&qfwin->w_llist_ref);
7613
7614 qfwin->w_llist_ref = new_ll;
7615 if (wp != qfwin)
7616 win_set_loclist(wp, new_ll);
7617 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007618 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007619}
7620
Bram Moolenaard823fa92016-08-12 16:29:27 +02007621/*
7622 * Populate the quickfix list with the items supplied in the list
7623 * of dictionaries. "title" will be copied to w:quickfix_title.
7624 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007625 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007626 */
7627 int
7628set_errorlist(
7629 win_T *wp,
7630 list_T *list,
7631 int action,
7632 char_u *title,
7633 dict_T *what)
7634{
7635 qf_info_T *qi = &ql_info;
7636 int retval = OK;
7637
7638 if (wp != NULL)
7639 {
7640 qi = ll_get_or_alloc_list(wp);
7641 if (qi == NULL)
7642 return FAIL;
7643 }
7644
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007645 if (action == 'f')
7646 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007647 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007648 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007649 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007650 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007651
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007652 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007653 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007654 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007655 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007656 _("cannot have both a list and a \"what\" argument"));
7657 return FAIL;
7658 }
7659
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007660 incr_quickfix_busy();
7661
7662 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007663 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007664 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007665 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007666 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007667 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007668 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007669 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007670
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007671 decr_quickfix_busy();
7672
Bram Moolenaard823fa92016-08-12 16:29:27 +02007673 return retval;
7674}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007675
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007676/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007677 * Mark the quickfix context and callback function as in use for all the lists
7678 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007679 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007680 static int
7681mark_quickfix_ctx(qf_info_T *qi, int copyID)
7682{
7683 int i;
7684 int abort = FALSE;
7685 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007686 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007687
7688 for (i = 0; i < LISTCOUNT && !abort; ++i)
7689 {
7690 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007691 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7692 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007693 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
7694
7695 cb = &qi->qf_lists[i].qftf_cb;
7696 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007697 }
7698
7699 return abort;
7700}
7701
7702/*
7703 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007704 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007705 */
7706 int
7707set_ref_in_quickfix(int copyID)
7708{
7709 int abort = FALSE;
7710 tabpage_T *tp;
7711 win_T *win;
7712
7713 abort = mark_quickfix_ctx(&ql_info, copyID);
7714 if (abort)
7715 return abort;
7716
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007717 abort = set_ref_in_callback(&qftf_cb, copyID);
7718 if (abort)
7719 return abort;
7720
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007721 FOR_ALL_TAB_WINDOWS(tp, win)
7722 {
7723 if (win->w_llist != NULL)
7724 {
7725 abort = mark_quickfix_ctx(win->w_llist, copyID);
7726 if (abort)
7727 return abort;
7728 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007729 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7730 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007731 // In a location list window and none of the other windows is
7732 // referring to this location list. Mark the location list
7733 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007734 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7735 if (abort)
7736 return abort;
7737 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007738 }
7739
7740 return abort;
7741}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007742#endif
7743
Bram Moolenaar81695252004-12-29 20:58:21 +00007744/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007745 * Return the autocmd name for the :cbuffer Ex commands
7746 */
7747 static char_u *
7748cbuffer_get_auname(cmdidx_T cmdidx)
7749{
7750 switch (cmdidx)
7751 {
7752 case CMD_cbuffer: return (char_u *)"cbuffer";
7753 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7754 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7755 case CMD_lbuffer: return (char_u *)"lbuffer";
7756 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7757 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7758 default: return NULL;
7759 }
7760}
7761
7762/*
7763 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7764 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7765 */
7766 static int
7767cbuffer_process_args(
7768 exarg_T *eap,
7769 buf_T **bufp,
7770 linenr_T *line1,
7771 linenr_T *line2)
7772{
7773 buf_T *buf = NULL;
7774
7775 if (*eap->arg == NUL)
7776 buf = curbuf;
7777 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7778 buf = buflist_findnr(atoi((char *)eap->arg));
7779
7780 if (buf == NULL)
7781 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007782 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007783 return FAIL;
7784 }
7785
7786 if (buf->b_ml.ml_mfp == NULL)
7787 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007788 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007789 return FAIL;
7790 }
7791
7792 if (eap->addr_count == 0)
7793 {
7794 eap->line1 = 1;
7795 eap->line2 = buf->b_ml.ml_line_count;
7796 }
7797
7798 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7799 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7800 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007801 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007802 return FAIL;
7803 }
7804
7805 *line1 = eap->line1;
7806 *line2 = eap->line2;
7807 *bufp = buf;
7808
7809 return OK;
7810}
7811
7812/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007813 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007814 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007815 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007816 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00007817 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00007818 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007819 */
7820 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007821ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007822{
7823 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007824 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007825 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01007826 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02007827 int_u save_qfid;
7828 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01007829 char_u *qf_title;
7830 linenr_T line1;
7831 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007832
Bram Moolenaara16123a2019-03-28 20:31:07 +01007833 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01007834 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01007835 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007836 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007837#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01007838 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007839 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007840#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007841 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02007842
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007843 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007844 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7845 if (qi == NULL)
7846 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01007847
Bram Moolenaara16123a2019-03-28 20:31:07 +01007848 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
7849 return;
7850
7851 qf_title = qf_cmdtitle(*eap->cmdlinep);
7852
7853 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007854 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01007855 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
7856 (char *)qf_title, (char *)buf->b_sfname);
7857 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007858 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007859
7860 incr_quickfix_busy();
7861
7862 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
7863 (eap->cmdidx != CMD_caddbuffer
7864 && eap->cmdidx != CMD_laddbuffer),
7865 line1, line2,
7866 qf_title, NULL);
7867 if (qf_stack_empty(qi))
7868 {
7869 decr_quickfix_busy();
7870 return;
7871 }
7872 if (res >= 0)
7873 qf_list_changed(qf_get_curlist(qi));
7874
7875 // Remember the current quickfix list identifier, so that we can
7876 // check for autocommands changing the current quickfix list.
7877 save_qfid = qf_get_curlist(qi)->qf_id;
7878 if (au_name != NULL)
7879 {
7880 buf_T *curbuf_old = curbuf;
7881
7882 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
7883 TRUE, curbuf);
7884 if (curbuf != curbuf_old)
7885 // Autocommands changed buffer, don't jump now, "qi" may
7886 // be invalid.
7887 res = 0;
7888 }
7889 // Jump to the first error for a new list and if autocmds didn't
7890 // free the list.
7891 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
7892 eap->cmdidx == CMD_lbuffer)
7893 && qflist_valid(wp, save_qfid))
7894 // display the first error
7895 qf_jump_first(qi, save_qfid, eap->forceit);
7896
7897 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00007898}
7899
Bram Moolenaar1e015462005-09-25 22:16:38 +00007900#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007901/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007902 * Return the autocmd name for the :cexpr Ex commands.
7903 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007904 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01007905cexpr_get_auname(cmdidx_T cmdidx)
7906{
7907 switch (cmdidx)
7908 {
7909 case CMD_cexpr: return (char_u *)"cexpr";
7910 case CMD_cgetexpr: return (char_u *)"cgetexpr";
7911 case CMD_caddexpr: return (char_u *)"caddexpr";
7912 case CMD_lexpr: return (char_u *)"lexpr";
7913 case CMD_lgetexpr: return (char_u *)"lgetexpr";
7914 case CMD_laddexpr: return (char_u *)"laddexpr";
7915 default: return NULL;
7916 }
7917}
7918
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007919 int
7920trigger_cexpr_autocmd(int cmdidx)
7921{
7922 char_u *au_name = cexpr_get_auname(cmdidx);
7923
7924 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
7925 curbuf->b_fname, TRUE, curbuf))
7926 {
7927 if (aborting())
7928 return FAIL;
7929 }
7930 return OK;
7931}
7932
7933 int
7934cexpr_core(exarg_T *eap, typval_T *tv)
7935{
7936 qf_info_T *qi;
7937 win_T *wp = NULL;
7938
7939 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
7940 if (qi == NULL)
7941 return FAIL;
7942
7943 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
7944 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
7945 {
7946 int res;
7947 int_u save_qfid;
7948 char_u *au_name = cexpr_get_auname(eap->cmdidx);
7949
7950 incr_quickfix_busy();
7951 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
7952 (eap->cmdidx != CMD_caddexpr
7953 && eap->cmdidx != CMD_laddexpr),
7954 (linenr_T)0, (linenr_T)0,
7955 qf_cmdtitle(*eap->cmdlinep), NULL);
7956 if (qf_stack_empty(qi))
7957 {
7958 decr_quickfix_busy();
7959 return FAIL;
7960 }
7961 if (res >= 0)
7962 qf_list_changed(qf_get_curlist(qi));
7963
7964 // Remember the current quickfix list identifier, so that we can
7965 // check for autocommands changing the current quickfix list.
7966 save_qfid = qf_get_curlist(qi)->qf_id;
7967 if (au_name != NULL)
7968 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
7969 curbuf->b_fname, TRUE, curbuf);
7970
7971 // Jump to the first error for a new list and if autocmds didn't
7972 // free the list.
7973 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
7974 && qflist_valid(wp, save_qfid))
7975 // display the first error
7976 qf_jump_first(qi, save_qfid, eap->forceit);
7977 decr_quickfix_busy();
7978 return OK;
7979 }
7980
7981 emsg(_("E777: String or List expected"));
7982 return FAIL;
7983}
7984
Bram Moolenaara16123a2019-03-28 20:31:07 +01007985/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00007986 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
7987 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007988 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007989 */
7990 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007991ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007992{
7993 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007994
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02007995 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02007996 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01007997
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007998 // Evaluate the expression. When the result is a string or a list we can
7999 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008000 tv = eval_expr(eap->arg, eap);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008001 if (tv != NULL)
8002 {
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008003 (void)cexpr_core(eap, tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008004 free_tv(tv);
8005 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008006}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008007#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008008
8009/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008010 * Get the location list for ":lhelpgrep"
8011 */
8012 static qf_info_T *
8013hgr_get_ll(int *new_ll)
8014{
8015 win_T *wp;
8016 qf_info_T *qi;
8017
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008018 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008019 if (bt_help(curwin->w_buffer))
8020 wp = curwin;
8021 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008022 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008023 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008024
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008025 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008026 qi = NULL;
8027 else
8028 qi = wp->w_llist;
8029
8030 if (qi == NULL)
8031 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008032 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008033 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008034 return NULL;
8035 *new_ll = TRUE;
8036 }
8037
8038 return qi;
8039}
8040
8041/*
8042 * Search for a pattern in a help file.
8043 */
8044 static void
8045hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008046 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008047 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008048 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008049 regmatch_T *p_regmatch)
8050{
8051 FILE *fd;
8052 long lnum;
8053
8054 fd = mch_fopen((char *)fname, "r");
8055 if (fd == NULL)
8056 return;
8057
8058 lnum = 1;
8059 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8060 {
8061 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008062
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008063 // Convert a line if 'encoding' is not utf-8 and
8064 // the line contains a non-ASCII character.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008065 if (p_vc->vc_type != CONV_NONE
8066 && has_non_ascii(IObuff))
8067 {
8068 line = string_convert(p_vc, IObuff, NULL);
8069 if (line == NULL)
8070 line = IObuff;
8071 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008072
8073 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8074 {
8075 int l = (int)STRLEN(line);
8076
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008077 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008078 while (l > 0 && line[l - 1] <= ' ')
8079 line[--l] = NUL;
8080
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008081 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008082 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008083 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008084 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008085 0,
8086 line,
8087 lnum,
thinca6864efa2021-06-19 20:45:20 +02008088 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008089 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008090 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008091 (int)(p_regmatch->endp[0] - line)
8092 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008093 FALSE, // vis_col
8094 NULL, // search pattern
8095 0, // nr
8096 1, // type
8097 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008098 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008099 {
8100 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008101 if (line != IObuff)
8102 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008103 break;
8104 }
8105 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008106 if (line != IObuff)
8107 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008108 ++lnum;
8109 line_breakcheck();
8110 }
8111 fclose(fd);
8112}
8113
8114/*
8115 * Search for a pattern in all the help files in the doc directory under
8116 * the given directory.
8117 */
8118 static void
8119hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008120 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008121 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008122 regmatch_T *p_regmatch,
8123 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008124#ifdef FEAT_MULTI_LANG
8125 , char_u *lang
8126#endif
8127 )
8128{
8129 int fcount;
8130 char_u **fnames;
8131 int fi;
8132
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008133 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008134 add_pathsep(dirname);
8135 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8136 if (gen_expand_wildcards(1, &dirname, &fcount,
8137 &fnames, EW_FILE|EW_SILENT) == OK
8138 && fcount > 0)
8139 {
8140 for (fi = 0; fi < fcount && !got_int; ++fi)
8141 {
8142#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008143 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008144 if (lang != NULL
8145 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008146 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008147 && !(STRNICMP(lang, "en", 2) == 0
8148 && STRNICMP("txt", fnames[fi]
8149 + STRLEN(fnames[fi]) - 3, 3) == 0))
8150 continue;
8151#endif
8152
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008153 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008154 }
8155 FreeWild(fcount, fnames);
8156 }
8157}
8158
8159/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008160 * Search for a pattern in all the help files in the 'runtimepath'
8161 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008162 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008163 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008164 */
8165 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008166hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008167{
8168 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008169
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008170 vimconv_T vc;
8171
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008172 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8173 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008174 vc.vc_type = CONV_NONE;
8175 if (!enc_utf8)
8176 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008177
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008178 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008179 p = p_rtp;
8180 while (*p != NUL && !got_int)
8181 {
8182 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8183
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008184 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008185#ifdef FEAT_MULTI_LANG
8186 , lang
8187#endif
8188 );
8189 }
8190
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008191 if (vc.vc_type != CONV_NONE)
8192 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008193}
8194
8195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 * ":helpgrep {pattern}"
8197 */
8198 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008199ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200{
8201 regmatch_T regmatch;
8202 char_u *save_cpo;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008203 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008204 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008205 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008206 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008207 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208
Bram Moolenaar73633f82012-01-20 13:39:07 +01008209 switch (eap->cmdidx)
8210 {
8211 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8212 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8213 default: break;
8214 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008215 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8216 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008217 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008218#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008219 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008220 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008221#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008222 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008223
Bram Moolenaar39665952018-08-15 20:59:48 +02008224 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008225 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008226 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008227 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008228 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008229 }
8230
Bram Moolenaara16123a2019-03-28 20:31:07 +01008231 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8232 save_cpo = p_cpo;
8233 p_cpo = empty_option;
8234
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008235 incr_quickfix_busy();
8236
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008237#ifdef FEAT_MULTI_LANG
8238 // Check for a specified language
8239 lang = check_help_lang(eap->arg);
8240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8242 regmatch.rm_ic = FALSE;
8243 if (regmatch.regprog != NULL)
8244 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008245 qf_list_T *qfl;
8246
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008247 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008248 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008249 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008251 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008252
Bram Moolenaar473de612013-06-08 18:19:48 +02008253 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008255 qfl->qf_nonevalid = FALSE;
8256 qfl->qf_ptr = qfl->qf_start;
8257 qfl->qf_index = 1;
8258 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008259 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 }
8261
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008262 if (p_cpo == empty_option)
8263 p_cpo = save_cpo;
8264 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008265 {
8266 // Darn, some plugin changed the value. If it's still empty it was
8267 // changed and restored, need to restore in the complicated way.
8268 if (*p_cpo == NUL)
8269 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008270 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008271 }
8272
8273 if (updated)
8274 // This may open a window and source scripts, do this after 'cpo' was
8275 // restored.
8276 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277
Bram Moolenaar73633f82012-01-20 13:39:07 +01008278 if (au_name != NULL)
8279 {
8280 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8281 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008282 // When adding a location list to an existing location list stack,
8283 // if the autocmd made the stack invalid, then just return.
8284 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008285 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008286 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008287 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008288 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008289 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008290
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008291 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008292 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008293 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008294 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008295 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008296
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008297 decr_quickfix_busy();
8298
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008299 if (eap->cmdidx == CMD_lhelpgrep)
8300 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008301 // If the help window is not opened or if it already points to the
8302 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008303 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008304 {
8305 if (new_qi)
8306 ll_free_all(&qi);
8307 }
8308 else if (curwin->w_llist == NULL)
8309 curwin->w_llist = qi;
8310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311}
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008312#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008313
8314#if defined(FEAT_EVAL) || defined(PROTO)
8315# ifdef FEAT_QUICKFIX
8316 static void
8317get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8318{
8319 if (what_arg->v_type == VAR_UNKNOWN)
8320 {
8321 if (rettv_list_alloc(rettv) == OK)
8322 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008323 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008324 }
8325 else
8326 {
8327 if (rettv_dict_alloc(rettv) == OK)
8328 if (is_qf || (wp != NULL))
8329 {
8330 if (what_arg->v_type == VAR_DICT)
8331 {
8332 dict_T *d = what_arg->vval.v_dict;
8333
8334 if (d != NULL)
8335 qf_get_properties(wp, d, rettv->vval.v_dict);
8336 }
8337 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008338 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008339 }
8340 }
8341}
8342# endif
8343
8344/*
8345 * "getloclist()" function
8346 */
8347 void
8348f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8349{
8350# ifdef FEAT_QUICKFIX
8351 win_T *wp;
8352
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008353 if (in_vim9script()
8354 && (check_for_number_arg(argvars, 0) == FAIL
8355 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8356 return;
8357
Bram Moolenaare677df82019-09-02 22:31:11 +02008358 wp = find_win_by_nr_or_id(&argvars[0]);
8359 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8360# endif
8361}
8362
8363/*
8364 * "getqflist()" function
8365 */
8366 void
8367f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8368{
8369# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008370 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8371 return;
8372
Bram Moolenaare677df82019-09-02 22:31:11 +02008373 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8374# endif
8375}
8376
8377/*
8378 * Used by "setqflist()" and "setloclist()" functions
8379 */
8380 static void
8381set_qf_ll_list(
8382 win_T *wp UNUSED,
8383 typval_T *list_arg UNUSED,
8384 typval_T *action_arg UNUSED,
8385 typval_T *what_arg UNUSED,
8386 typval_T *rettv)
8387{
8388# ifdef FEAT_QUICKFIX
8389 static char *e_invact = N_("E927: Invalid action: '%s'");
8390 char_u *act;
8391 int action = 0;
8392 static int recursive = 0;
8393# endif
8394
8395 rettv->vval.v_number = -1;
8396
8397# ifdef FEAT_QUICKFIX
8398 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008399 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008400 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008401 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008402 else
8403 {
8404 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008405 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008406 int valid_dict = TRUE;
8407
8408 if (action_arg->v_type == VAR_STRING)
8409 {
8410 act = tv_get_string_chk(action_arg);
8411 if (act == NULL)
8412 return; // type error; errmsg already given
8413 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8414 act[1] == NUL)
8415 action = *act;
8416 else
8417 semsg(_(e_invact), act);
8418 }
8419 else if (action_arg->v_type == VAR_UNKNOWN)
8420 action = ' ';
8421 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008422 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008423
8424 if (action_arg->v_type != VAR_UNKNOWN
8425 && what_arg->v_type != VAR_UNKNOWN)
8426 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008427 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8428 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008429 else
8430 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008431 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008432 valid_dict = FALSE;
8433 }
8434 }
8435
8436 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008437 if (l != NULL && action && valid_dict
8438 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008439 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008440 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008441 rettv->vval.v_number = 0;
8442 --recursive;
8443 }
8444# endif
8445}
8446
8447/*
8448 * "setloclist()" function
8449 */
8450 void
8451f_setloclist(typval_T *argvars, typval_T *rettv)
8452{
8453 win_T *win;
8454
8455 rettv->vval.v_number = -1;
8456
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008457 if (in_vim9script()
8458 && (check_for_number_arg(argvars, 0) == FAIL
8459 || check_for_list_arg(argvars, 1) == FAIL
8460 || check_for_opt_string_arg(argvars, 2) == FAIL
8461 || (argvars[2].v_type != VAR_UNKNOWN
8462 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8463 return;
8464
Bram Moolenaare677df82019-09-02 22:31:11 +02008465 win = find_win_by_nr_or_id(&argvars[0]);
8466 if (win != NULL)
8467 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8468}
8469
8470/*
8471 * "setqflist()" function
8472 */
8473 void
8474f_setqflist(typval_T *argvars, typval_T *rettv)
8475{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008476 if (in_vim9script()
8477 && (check_for_list_arg(argvars, 0) == FAIL
8478 || check_for_opt_string_arg(argvars, 1) == FAIL
8479 || (argvars[1].v_type != VAR_UNKNOWN
8480 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8481 return;
8482
Bram Moolenaare677df82019-09-02 22:31:11 +02008483 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8484}
8485#endif