blob: aa431ea10578ec162e9f286546e640aba5538cdb [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
Tom Praschanca6ac992023-08-11 23:26:12 +020046 typval_T qf_user_data; // custom user data associated with this item
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020047 char_u qf_valid; // valid error message detected
Bram Moolenaar071d4272004-06-13 20:20:40 +000048};
49
50/*
51 * There is a stack of error lists.
52 */
53#define LISTCOUNT 10
Bram Moolenaara2aa8a22018-04-24 13:55:00 +020054#define INVALID_QFIDX (-1)
Bram Moolenaaree8188f2019-02-05 21:23:04 +010055#define INVALID_QFBUFNR (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020057/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +010058 * Quickfix list type.
59 */
60typedef enum
61{
62 QFLT_QUICKFIX, // Quickfix list - global list
63 QFLT_LOCATION, // Location list - per window list
64 QFLT_INTERNAL // Internal - Temporary list used by getqflist()/getloclist()
65} qfltype_T;
66
67/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +020068 * Quickfix/Location list definition
69 * Contains a list of entries (qfline_T). qf_start points to the first entry
70 * and qf_last points to the last entry. qf_count contains the list size.
71 *
72 * Usually the list contains one or more entries. But an empty list can be
73 * created using setqflist()/setloclist() with a title and/or user context
74 * information and entries can be added later using setqflist()/setloclist().
75 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000076typedef struct qf_list_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000077{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020078 int_u qf_id; // Unique identifier for this list
Bram Moolenaar2d67d302018-11-16 18:46:02 +010079 qfltype_T qfl_type;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020080 qfline_T *qf_start; // pointer to the first error
81 qfline_T *qf_last; // pointer to the last error
82 qfline_T *qf_ptr; // pointer to the current error
83 int qf_count; // number of errors (0 means empty list)
84 int qf_index; // current index in the error list
85 int qf_nonevalid; // TRUE if not a single valid entry found
Tom Praschanca6ac992023-08-11 23:26:12 +020086 int qf_has_user_data; // TRUE if at least one item has user_data attached
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020087 char_u *qf_title; // title derived from the command that created
88 // the error list or set by setqflist
89 typval_T *qf_ctx; // context set by setqflist/setloclist
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +010090 callback_T qf_qftf_cb; // 'quickfixtextfunc' callback function
Bram Moolenaara7df8c72017-07-19 13:23:06 +020091
92 struct dir_stack_T *qf_dir_stack;
93 char_u *qf_directory;
94 struct dir_stack_T *qf_file_stack;
95 char_u *qf_currfile;
96 int qf_multiline;
97 int qf_multiignore;
98 int qf_multiscan;
Bram Moolenaarb254af32017-12-18 19:48:58 +010099 long qf_changedtick;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000100} qf_list_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
Bram Moolenaar6a8958d2017-06-22 21:33:20 +0200102/*
103 * Quickfix/Location list stack definition
104 * Contains a list of quickfix/location lists (qf_list_T)
105 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000106struct qf_info_S
107{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200108 // Count of references to this list. Used only for location lists.
109 // When a location list window reference this list, qf_refcount
110 // will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
111 // reaches 0, the list is freed.
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000112 int qf_refcount;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200113 int qf_listcount; // current number of lists
114 int qf_curlist; // current error list
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000115 qf_list_T qf_lists[LISTCOUNT];
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100116 qfltype_T qfl_type; // type of list
Bram Moolenaaree8188f2019-02-05 21:23:04 +0100117 int qf_bufnr; // quickfix window buffer number
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000118};
119
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200120static qf_info_T ql_info; // global quickfix list
121static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
haya14busae023d492022-02-08 18:09:29 +0000123#define FMT_PATTERNS 13 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124
125/*
126 * Structure used to hold the info of one part of 'errorformat'
127 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000128typedef struct efm_S efm_T;
129struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200131 regprog_T *prog; // pre-formatted part of 'errorformat'
132 efm_T *next; // pointer to next (NULL if last)
133 char_u addr[FMT_PATTERNS]; // indices of used % patterns
134 char_u prefix; // prefix of this format line:
135 // 'D' enter directory
136 // 'X' leave directory
137 // 'A' start of multi-line message
138 // 'E' error message
139 // 'W' warning message
140 // 'I' informational message
Bram Moolenaare9283662020-06-07 14:10:47 +0200141 // 'N' note message
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200142 // 'C' continuation line
143 // 'Z' end of multi-line message
144 // 'G' general, unspecific message
145 // 'P' push file (partial) message
146 // 'Q' pop/quit file (partial) message
147 // 'O' overread (partial) message
148 char_u flags; // additional flags given in prefix
149 // '-' do not include this line
150 // '+' include whole line in message
151 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152};
153
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200154// List of location lists to be deleted.
155// Used to delay the deletion of locations lists by autocmds.
156typedef struct qf_delq_S
157{
158 struct qf_delq_S *next;
159 qf_info_T *qi;
160} qf_delq_T;
161static qf_delq_T *qf_delq_head = NULL;
162
163// Counter to prevent autocmds from freeing up location lists when they are
164// still being used.
165static int quickfix_busy = 0;
166
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200167static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100168
Bram Moolenaard43906d2020-07-20 21:31:32 +0200169// callback function for 'quickfixtextfunc'
170static callback_T qftf_cb;
171
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100172static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Tom Praschanca6ac992023-08-11 23:26:12 +0200173static 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, typval_T *user_data, int valid);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200174static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100175static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100176static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200177static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100178static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200179static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200180static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, int newwin);
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +0100181static void qf_fmt_text(garray_T *gap, char_u *text);
182static void qf_range_text(garray_T *gap, qfline_T *qfp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100183static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100184static win_T *qf_find_win(qf_info_T *qi);
185static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200186static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +0200187static 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 +0100188static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
189static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
190static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
191static qf_info_T *ll_get_or_alloc_list(win_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200193// Quickfix window check helper macro
kylo252ae6f1d82022-02-16 19:24:07 +0000194#define IS_QF_WINDOW(wp) (bt_quickfix((wp)->w_buffer) && (wp)->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200195// Location list window check helper macro
kylo252ae6f1d82022-02-16 19:24:07 +0000196#define IS_LL_WINDOW(wp) (bt_quickfix((wp)->w_buffer) && (wp)->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200197
198// Quickfix and location list stack check helper macros
kylo252ae6f1d82022-02-16 19:24:07 +0000199#define IS_QF_STACK(qi) ((qi)->qfl_type == QFLT_QUICKFIX)
200#define IS_LL_STACK(qi) ((qi)->qfl_type == QFLT_LOCATION)
201#define IS_QF_LIST(qfl) ((qfl)->qfl_type == QFLT_QUICKFIX)
202#define IS_LL_LIST(qfl) ((qfl)->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200203
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000204/*
205 * Return location list for window 'wp'
206 * For location list window, return the referenced location list
207 */
kylo252ae6f1d82022-02-16 19:24:07 +0000208#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? (wp)->w_llist_ref : (wp)->w_llist)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000209
Bram Moolenaar95946f12019-03-31 15:31:59 +0200210// Macro to loop through all the items in a quickfix list
211// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100212#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
kylo252ae6f1d82022-02-16 19:24:07 +0000213 for ((i) = 1, (qfp) = (qfl)->qf_start; \
214 !got_int && (i) <= (qfl)->qf_count && (qfp) != NULL; \
215 ++(i), (qfp) = (qfp)->qf_next)
Bram Moolenaara16123a2019-03-28 20:31:07 +0100216
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200218 * Looking up a buffer can be slow if there are many. Remember the last one
219 * to make this a lot faster if there are multiple matches in the same file.
220 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200221static char_u *qf_last_bufname = NULL;
222static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200223
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +0100224static garray_T qfga;
225
226/*
227 * Get a growarray to buffer text in. Shared between various commands to avoid
228 * many alloc/free calls.
229 */
230 static garray_T *
231qfga_get(void)
232{
233 static int initialized = FALSE;
234
235 if (!initialized)
236 {
237 initialized = TRUE;
238 ga_init2(&qfga, 1, 256);
239 }
240
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +0100241 // Reset the length to zero. Retain ga_data from previous use to avoid
242 // many alloc/free calls.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +0100243 qfga.ga_len = 0;
244
245 return &qfga;
246}
247
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200248/*
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +0100249 * The "qfga" grow array buffer is reused across multiple quickfix commands as
250 * a temporary buffer to reduce the number of alloc/free calls. But if the
251 * buffer size is large, then to avoid holding on to that memory, clear the
252 * grow array. Otherwise just reset the grow array length.
253 */
254 static void
255qfga_clear(void)
256{
257 if (qfga.ga_maxlen > 1000)
258 ga_clear(&qfga);
259 else
260 qfga.ga_len = 0;
261}
262
263/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200264 * Maximum number of bytes allowed per line while reading a errorfile.
265 */
266#define LINE_MAXLEN 4096
267
haya14busae023d492022-02-08 18:09:29 +0000268/*
269 * Patterns used. Keep in sync with qf_parse_fmt[].
270 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200271static struct fmtpattern
272{
273 char_u convchar;
274 char *pattern;
275} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200276 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200277 {'f', ".\\+"}, // only used when at end
haya14busae023d492022-02-08 18:09:29 +0000278 {'n', "\\d\\+"}, // 1
279 {'l', "\\d\\+"}, // 2
280 {'e', "\\d\\+"}, // 3
281 {'c', "\\d\\+"}, // 4
282 {'k', "\\d\\+"}, // 5
283 {'t', "."}, // 6
284#define FMT_PATTERN_M 7
285 {'m', ".\\+"}, // 7
286#define FMT_PATTERN_R 8
287 {'r', ".*"}, // 8
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000288 {'p', "[- .]*"}, // 9
haya14busae023d492022-02-08 18:09:29 +0000289 {'v', "\\d\\+"}, // 10
290 {'s', ".\\+"}, // 11
291 {'o', ".\\+"} // 12
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200292 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200293
294/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200295 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200296 * See fmt_pat definition above for the list of supported patterns. The
297 * pattern specifier is supplied in "efmpat". The converted pattern is stored
298 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200299 */
300 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200301efmpat_to_regpat(
302 char_u *efmpat,
303 char_u *regpat,
304 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200305 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100306 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200307{
308 char_u *srcptr;
309
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200310 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200311 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200312 // Each errorformat pattern can occur only once
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000313 semsg(_(e_too_many_chr_in_format_string), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200314 return NULL;
315 }
haya14busae023d492022-02-08 18:09:29 +0000316 if ((idx && idx < FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200317 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
haya14busae023d492022-02-08 18:09:29 +0000318 || (idx == FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200319 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200320 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000321 semsg(_(e_unexpected_chr_in_format_str), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200322 return NULL;
323 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200324 efminfo->addr[idx] = (char_u)++round;
325 *regpat++ = '\\';
326 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200327#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200328 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200329 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200330 // Also match "c:" in the file name, even when
331 // checking for a colon next: "%f:".
332 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200333 STRCPY(regpat, "\\%(\\a:\\)\\=");
334 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200335 }
336#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200337 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200338 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200339 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200340 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200341 // A file name may contain spaces, but this isn't
342 // in "\f". For "%f:%l:%m" there may be a ":" in
343 // the file name. Use ".\{-1,}x" instead (x is
344 // the next character), the requirement that :999:
345 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200346 STRCPY(regpat, ".\\{-1,}");
347 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200348 }
349 else
350 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200351 // File name followed by '\\' or '%': include as
352 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200353 STRCPY(regpat, "\\f\\+");
354 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200355 }
356 }
357 else
358 {
359 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200360 while ((*regpat = *srcptr++) != NUL)
361 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200362 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200363 *regpat++ = '\\';
364 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200365
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200366 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200367}
368
369/*
370 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200371 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200372 */
373 static char_u *
374scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200375 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200376 char_u *efm,
377 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100378 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200379{
380 char_u *efmp = *pefmp;
381
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200382 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200383 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200384 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200385 {
386 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200387 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200388 if (efmp < efm + len)
389 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200390 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200391 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200392 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200393 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200394 if (efmp == efm + len)
395 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000396 emsg(_(e_missing_rsb_in_format_string));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200397 return NULL;
398 }
399 }
400 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200401 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200402 *regpat++ = *++efmp;
403 *regpat++ = '\\';
404 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200405 }
406 else
407 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200408 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000409 semsg(_(e_unsupported_chr_in_format_string), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200410 return NULL;
411 }
412
413 *pefmp = efmp;
414
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200415 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200416}
417
418/*
419 * Analyze/parse an errorformat prefix.
420 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200421 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100422efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200423{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200424 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200425 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200426 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200427 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200428 else
429 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000430 semsg(_(e_invalid_chr_in_format_string_prefix), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200431 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200432 }
433
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200434 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200435}
436
437/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200438 * Converts a 'errorformat' string part in 'efm' to a regular expression
439 * pattern. The resulting regex pattern is returned in "regpat". Additional
440 * information about the 'erroformat' pattern is returned in "fmt_ptr".
441 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200442 */
443 static int
444efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200445 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200446 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200447 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100448 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200449{
450 char_u *ptr;
451 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200452 int round;
453 int idx = 0;
454
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200455 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200456 ptr = regpat;
457 *ptr++ = '^';
458 round = 0;
459 for (efmp = efm; efmp < efm + len; ++efmp)
460 {
461 if (*efmp == '%')
462 {
463 ++efmp;
464 for (idx = 0; idx < FMT_PATTERNS; ++idx)
465 if (fmt_pat[idx].convchar == *efmp)
466 break;
467 if (idx < FMT_PATTERNS)
468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100469 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200470 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200471 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200472 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200473 }
474 else if (*efmp == '*')
475 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200476 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100477 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200478 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200479 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200480 }
481 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200482 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200483 else if (*efmp == '#')
484 *ptr++ = '*';
485 else if (*efmp == '>')
486 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200487 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200488 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200489 // prefix is allowed only at the beginning of the errorformat
490 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100491 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200492 if (efmp == NULL)
493 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200494 }
495 else
496 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000497 semsg(_(e_invalid_chr_in_format_string), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200498 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200499 }
500 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200501 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200502 {
503 if (*efmp == '\\' && efmp + 1 < efm + len)
504 ++efmp;
505 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200506 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200507 if (*efmp)
508 *ptr++ = *efmp;
509 }
510 }
511 *ptr++ = '$';
512 *ptr = NUL;
513
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200514 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200515}
516
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200517/*
518 * Free the 'errorformat' information list
519 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200520 static void
521free_efm_list(efm_T **efm_first)
522{
523 efm_T *efm_ptr;
524
525 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
526 {
527 *efm_first = efm_ptr->next;
528 vim_regfree(efm_ptr->prog);
529 vim_free(efm_ptr);
530 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100531 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200532}
533
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200534/*
535 * Compute the size of the buffer used to convert a 'errorformat' pattern into
536 * a regular expression pattern.
537 */
538 static int
539efm_regpat_bufsz(char_u *efm)
540{
541 int sz;
542 int i;
543
544 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
545 for (i = FMT_PATTERNS; i > 0; )
546 sz += (int)STRLEN(fmt_pat[--i].pattern);
547#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200548 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200549#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200550 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200551#endif
552
553 return sz;
554}
555
556/*
557 * Return the length of a 'errorformat' option part (separated by ",").
558 */
559 static int
560efm_option_part_len(char_u *efm)
561{
562 int len;
563
564 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
565 if (efm[len] == '\\' && efm[len + 1] != NUL)
566 ++len;
567
568 return len;
569}
570
571/*
572 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
573 * are parsed and converted to regular expressions. Returns information about
574 * the parsed 'errorformat' option.
575 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200576 static efm_T *
577parse_efm_option(char_u *efm)
578{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200579 efm_T *fmt_ptr = NULL;
580 efm_T *fmt_first = NULL;
581 efm_T *fmt_last = NULL;
582 char_u *fmtstr = NULL;
583 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200584 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200585
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200586 // Each part of the format string is copied and modified from errorformat
587 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200588
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200589 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200590 sz = efm_regpat_bufsz(efm);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000591 if ((fmtstr = alloc_id(sz, aid_qf_efm_fmtstr)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200592 goto parse_efm_error;
593
594 while (efm[0] != NUL)
595 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200596 // Allocate a new eformat structure and put it at the end of the list
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000597 fmt_ptr = ALLOC_CLEAR_ONE_ID(efm_T, aid_qf_efm_fmtpart);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200598 if (fmt_ptr == NULL)
599 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200600 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200601 fmt_first = fmt_ptr;
602 else
603 fmt_last->next = fmt_ptr;
604 fmt_last = fmt_ptr;
605
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200606 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200607 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200608
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100609 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200610 goto parse_efm_error;
611 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
612 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200613 // Advance to next part
614 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200615 }
616
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200617 if (fmt_first == NULL) // nothing found
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000618 emsg(_(e_errorformat_contains_no_pattern));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200619
620 goto parse_efm_end;
621
622parse_efm_error:
623 free_efm_list(&fmt_first);
624
625parse_efm_end:
626 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200627
628 return fmt_first;
629}
630
Bram Moolenaare0d37972016-07-15 22:36:01 +0200631enum {
632 QF_FAIL = 0,
633 QF_OK = 1,
634 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200635 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200636 QF_IGNORE_LINE = 4,
637 QF_MULTISCAN = 5,
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +0100638 QF_ABORT = 6
Bram Moolenaare0d37972016-07-15 22:36:01 +0200639};
640
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200641/*
642 * State information used to parse lines and add entries to a quickfix/location
643 * list.
644 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200645typedef struct {
646 char_u *linebuf;
647 int linelen;
648 char_u *growbuf;
649 int growbufsiz;
650 FILE *fd;
651 typval_T *tv;
652 char_u *p_str;
653 listitem_T *p_li;
654 buf_T *buf;
655 linenr_T buflnum;
656 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100657 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200658} qfstate_T;
659
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200660/*
661 * Allocate more memory for the line buffer used for parsing lines.
662 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200663 static char_u *
664qf_grow_linebuf(qfstate_T *state, int newsz)
665{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200666 char_u *p;
667
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200668 // If the line exceeds LINE_MAXLEN exclude the last
669 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200670 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
671 if (state->growbuf == NULL)
672 {
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000673 state->growbuf = alloc_id(state->linelen + 1, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200674 if (state->growbuf == NULL)
675 return NULL;
676 state->growbufsiz = state->linelen;
677 }
678 else if (state->linelen > state->growbufsiz)
679 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200680 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200681 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200682 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200683 state->growbufsiz = state->linelen;
684 }
685 return state->growbuf;
686}
687
688/*
689 * Get the next string (separated by newline) from state->p_str.
690 */
691 static int
692qf_get_next_str_line(qfstate_T *state)
693{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200694 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200695 char_u *p_str = state->p_str;
696 char_u *p;
697 int len;
698
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200699 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200700 return QF_END_OF_INPUT;
701
702 p = vim_strchr(p_str, '\n');
703 if (p != NULL)
704 len = (int)(p - p_str) + 1;
705 else
706 len = (int)STRLEN(p_str);
707
708 if (len > IOSIZE - 2)
709 {
710 state->linebuf = qf_grow_linebuf(state, len);
711 if (state->linebuf == NULL)
712 return QF_NOMEM;
713 }
714 else
715 {
716 state->linebuf = IObuff;
717 state->linelen = len;
718 }
719 vim_strncpy(state->linebuf, p_str, state->linelen);
720
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200721 // Increment using len in order to discard the rest of the
722 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200723 p_str += len;
724 state->p_str = p_str;
725
726 return QF_OK;
727}
728
729/*
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000730 * Get the next string from the List item state->p_li.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200731 */
732 static int
733qf_get_next_list_line(qfstate_T *state)
734{
735 listitem_T *p_li = state->p_li;
736 int len;
737
738 while (p_li != NULL
739 && (p_li->li_tv.v_type != VAR_STRING
740 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200741 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200742
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200743 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200744 {
745 state->p_li = NULL;
746 return QF_END_OF_INPUT;
747 }
748
749 len = (int)STRLEN(p_li->li_tv.vval.v_string);
750 if (len > IOSIZE - 2)
751 {
752 state->linebuf = qf_grow_linebuf(state, len);
753 if (state->linebuf == NULL)
754 return QF_NOMEM;
755 }
756 else
757 {
758 state->linebuf = IObuff;
759 state->linelen = len;
760 }
761
762 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
763
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200764 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200765 return QF_OK;
766}
767
768/*
769 * Get the next string from state->buf.
770 */
771 static int
772qf_get_next_buf_line(qfstate_T *state)
773{
774 char_u *p_buf = NULL;
775 int len;
776
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200777 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200778 if (state->buflnum > state->lnumlast)
779 return QF_END_OF_INPUT;
780
781 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
782 state->buflnum += 1;
783
784 len = (int)STRLEN(p_buf);
785 if (len > IOSIZE - 2)
786 {
787 state->linebuf = qf_grow_linebuf(state, len);
788 if (state->linebuf == NULL)
789 return QF_NOMEM;
790 }
791 else
792 {
793 state->linebuf = IObuff;
794 state->linelen = len;
795 }
796 vim_strncpy(state->linebuf, p_buf, state->linelen);
797
798 return QF_OK;
799}
800
801/*
802 * Get the next string from file state->fd.
803 */
804 static int
805qf_get_next_file_line(qfstate_T *state)
806{
807 int discard;
808 int growbuflen;
809
810 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
811 return QF_END_OF_INPUT;
812
813 discard = FALSE;
814 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200815 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200816 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200817 // The current line exceeds IObuff, continue reading using
818 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200819 if (state->growbuf == NULL)
820 {
821 state->growbufsiz = 2 * (IOSIZE - 1);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000822 state->growbuf = alloc_id(state->growbufsiz, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200823 if (state->growbuf == NULL)
824 return QF_NOMEM;
825 }
826
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200827 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200828 memcpy(state->growbuf, IObuff, IOSIZE - 1);
829 growbuflen = state->linelen;
830
831 for (;;)
832 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200833 char_u *p;
834
Bram Moolenaare0d37972016-07-15 22:36:01 +0200835 if (fgets((char *)state->growbuf + growbuflen,
836 state->growbufsiz - growbuflen, state->fd) == NULL)
837 break;
838 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
839 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200840 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200841 break;
842 if (state->growbufsiz == LINE_MAXLEN)
843 {
844 discard = TRUE;
845 break;
846 }
847
848 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
849 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200850 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200851 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200852 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200853 }
854
855 while (discard)
856 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200857 // The current line is longer than LINE_MAXLEN, continue
858 // reading but discard everything until EOL or EOF is
859 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200860 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
861 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar59941cb2020-09-05 17:03:40 +0200862 || IObuff[IOSIZE - 2] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200863 break;
864 }
865
866 state->linebuf = state->growbuf;
867 state->linelen = growbuflen;
868 }
869 else
870 state->linebuf = IObuff;
871
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200872 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200873 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
874 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100875 char_u *line;
876
877 line = string_convert(&state->vc, state->linebuf, &state->linelen);
878 if (line != NULL)
879 {
880 if (state->linelen < IOSIZE)
881 {
882 STRCPY(state->linebuf, line);
883 vim_free(line);
884 }
885 else
886 {
887 vim_free(state->growbuf);
888 state->linebuf = state->growbuf = line;
889 state->growbufsiz = state->linelen < LINE_MAXLEN
890 ? state->linelen : LINE_MAXLEN;
891 }
892 }
893 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100894
Bram Moolenaare0d37972016-07-15 22:36:01 +0200895 return QF_OK;
896}
897
898/*
899 * Get the next string from a file/buffer/list/string.
900 */
901 static int
902qf_get_nextline(qfstate_T *state)
903{
904 int status = QF_FAIL;
905
906 if (state->fd == NULL)
907 {
908 if (state->tv != NULL)
909 {
910 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200911 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200912 status = qf_get_next_str_line(state);
913 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200914 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200915 status = qf_get_next_list_line(state);
916 }
917 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200918 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200919 status = qf_get_next_buf_line(state);
920 }
921 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200922 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200923 status = qf_get_next_file_line(state);
924
925 if (status != QF_OK)
926 return status;
927
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200928 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200929 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200930 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200931 state->linebuf[state->linelen - 1] = NUL;
932#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200933 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
934 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200935#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200936 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200937
Bram Moolenaare0d37972016-07-15 22:36:01 +0200938 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200939
940 return QF_OK;
941}
942
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200943typedef struct {
944 char_u *namebuf;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200945 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200946 char_u *errmsg;
947 int errmsglen;
948 long lnum;
thinca6864efa2021-06-19 20:45:20 +0200949 long end_lnum;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200950 int col;
thinca6864efa2021-06-19 20:45:20 +0200951 int end_col;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200952 char_u use_viscol;
953 char_u *pattern;
954 int enr;
955 int type;
Tom Praschanca6ac992023-08-11 23:26:12 +0200956 typval_T *user_data;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200957 int valid;
958} qffields_T;
959
960/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200961 * Parse the match for filename ('%f') pattern in regmatch.
962 * Return the matched value in "fields->namebuf".
963 */
964 static int
965qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
966{
967 int c;
968
969 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
970 return QF_FAIL;
971
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200972 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200973 c = *rmp->endp[midx];
974 *rmp->endp[midx] = NUL;
975 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
976 *rmp->endp[midx] = c;
977
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200978 // For separate filename patterns (%O, %P and %Q), the specified file
979 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200980 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
981 && mch_getperm(fields->namebuf) == -1)
982 return QF_FAIL;
983
984 return QF_OK;
985}
986
987/*
988 * Parse the match for error number ('%n') pattern in regmatch.
989 * Return the matched value in "fields->enr".
990 */
991 static int
992qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
993{
994 if (rmp->startp[midx] == NULL)
995 return QF_FAIL;
996 fields->enr = (int)atol((char *)rmp->startp[midx]);
997 return QF_OK;
998}
999
1000/*
haya14busae023d492022-02-08 18:09:29 +00001001 * Parse the match for line number ('%l') pattern in regmatch.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001002 * Return the matched value in "fields->lnum".
1003 */
1004 static int
1005qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
1006{
1007 if (rmp->startp[midx] == NULL)
1008 return QF_FAIL;
1009 fields->lnum = atol((char *)rmp->startp[midx]);
1010 return QF_OK;
1011}
1012
1013/*
haya14busae023d492022-02-08 18:09:29 +00001014 * Parse the match for end line number ('%e') pattern in regmatch.
1015 * Return the matched value in "fields->end_lnum".
1016 */
1017 static int
1018qf_parse_fmt_e(regmatch_T *rmp, int midx, qffields_T *fields)
1019{
1020 if (rmp->startp[midx] == NULL)
1021 return QF_FAIL;
1022 fields->end_lnum = atol((char *)rmp->startp[midx]);
1023 return QF_OK;
1024}
1025
1026/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001027 * Parse the match for column number ('%c') pattern in regmatch.
1028 * Return the matched value in "fields->col".
1029 */
1030 static int
1031qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
1032{
1033 if (rmp->startp[midx] == NULL)
1034 return QF_FAIL;
1035 fields->col = (int)atol((char *)rmp->startp[midx]);
1036 return QF_OK;
1037}
1038
1039/*
haya14busae023d492022-02-08 18:09:29 +00001040 * Parse the match for end column number ('%k') pattern in regmatch.
1041 * Return the matched value in "fields->end_col".
1042 */
1043 static int
1044qf_parse_fmt_k(regmatch_T *rmp, int midx, qffields_T *fields)
1045{
1046 if (rmp->startp[midx] == NULL)
1047 return QF_FAIL;
1048 fields->end_col = (int)atol((char *)rmp->startp[midx]);
1049 return QF_OK;
1050}
1051
1052/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001053 * Parse the match for error type ('%t') pattern in regmatch.
1054 * Return the matched value in "fields->type".
1055 */
1056 static int
1057qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
1058{
1059 if (rmp->startp[midx] == NULL)
1060 return QF_FAIL;
1061 fields->type = *rmp->startp[midx];
1062 return QF_OK;
1063}
1064
1065/*
Bram Moolenaarf4140482020-02-15 23:06:45 +01001066 * Copy a non-error line into the error string. Return the matched line in
1067 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001068 */
1069 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +01001070copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001071{
1072 char_u *p;
1073
1074 if (linelen >= fields->errmsglen)
1075 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001076 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001077 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1078 return QF_NOMEM;
1079 fields->errmsg = p;
1080 fields->errmsglen = linelen + 1;
1081 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001082 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001083 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001084
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001085 return QF_OK;
1086}
1087
1088/*
1089 * Parse the match for error message ('%m') pattern in regmatch.
1090 * Return the matched value in "fields->errmsg".
1091 */
1092 static int
1093qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1094{
1095 char_u *p;
1096 int len;
1097
1098 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1099 return QF_FAIL;
1100 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1101 if (len >= fields->errmsglen)
1102 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001103 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001104 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1105 return QF_NOMEM;
1106 fields->errmsg = p;
1107 fields->errmsglen = len + 1;
1108 }
1109 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1110 return QF_OK;
1111}
1112
1113/*
1114 * Parse the match for rest of a single-line file message ('%r') pattern.
1115 * Return the matched value in "tail".
1116 */
1117 static int
1118qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1119{
1120 if (rmp->startp[midx] == NULL)
1121 return QF_FAIL;
1122 *tail = rmp->startp[midx];
1123 return QF_OK;
1124}
1125
1126/*
1127 * Parse the match for the pointer line ('%p') pattern in regmatch.
1128 * Return the matched value in "fields->col".
1129 */
1130 static int
1131qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1132{
1133 char_u *match_ptr;
1134
1135 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1136 return QF_FAIL;
1137 fields->col = 0;
1138 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1139 ++match_ptr)
1140 {
1141 ++fields->col;
1142 if (*match_ptr == TAB)
1143 {
1144 fields->col += 7;
1145 fields->col -= fields->col % 8;
1146 }
1147 }
1148 ++fields->col;
1149 fields->use_viscol = TRUE;
1150 return QF_OK;
1151}
1152
1153/*
1154 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1155 * Return the matched value in "fields->col".
1156 */
1157 static int
1158qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1159{
1160 if (rmp->startp[midx] == NULL)
1161 return QF_FAIL;
1162 fields->col = (int)atol((char *)rmp->startp[midx]);
1163 fields->use_viscol = TRUE;
1164 return QF_OK;
1165}
1166
1167/*
1168 * Parse the match for the search text ('%s') pattern in regmatch.
1169 * Return the matched value in "fields->pattern".
1170 */
1171 static int
1172qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1173{
1174 int len;
1175
1176 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1177 return QF_FAIL;
1178 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1179 if (len > CMDBUFFSIZE - 5)
1180 len = CMDBUFFSIZE - 5;
1181 STRCPY(fields->pattern, "^\\V");
1182 STRNCAT(fields->pattern, rmp->startp[midx], len);
1183 fields->pattern[len + 3] = '\\';
1184 fields->pattern[len + 4] = '$';
1185 fields->pattern[len + 5] = NUL;
1186 return QF_OK;
1187}
1188
1189/*
1190 * Parse the match for the module ('%o') pattern in regmatch.
1191 * Return the matched value in "fields->module".
1192 */
1193 static int
1194qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1195{
1196 int len;
1197
1198 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1199 return QF_FAIL;
1200 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1201 if (len > CMDBUFFSIZE)
1202 len = CMDBUFFSIZE;
1203 STRNCAT(fields->module, rmp->startp[midx], len);
1204 return QF_OK;
1205}
1206
1207/*
1208 * 'errorformat' format pattern parser functions.
1209 * The '%f' and '%r' formats are parsed differently from other formats.
1210 * See qf_parse_match() for details.
haya14busae023d492022-02-08 18:09:29 +00001211 * Keep in sync with fmt_pat[].
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001212 */
1213static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1214{
haya14busae023d492022-02-08 18:09:29 +00001215 NULL, // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001216 qf_parse_fmt_n,
1217 qf_parse_fmt_l,
haya14busae023d492022-02-08 18:09:29 +00001218 qf_parse_fmt_e,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001219 qf_parse_fmt_c,
haya14busae023d492022-02-08 18:09:29 +00001220 qf_parse_fmt_k,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001221 qf_parse_fmt_t,
1222 qf_parse_fmt_m,
haya14busae023d492022-02-08 18:09:29 +00001223 NULL, // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001224 qf_parse_fmt_p,
1225 qf_parse_fmt_v,
1226 qf_parse_fmt_s,
1227 qf_parse_fmt_o
1228};
1229
1230/*
1231 * Parse the error format pattern matches in "regmatch" and set the values in
1232 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1233 * match. Returns QF_OK if all the matches are successfully parsed. On
1234 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001235 */
1236 static int
1237qf_parse_match(
1238 char_u *linebuf,
1239 int linelen,
1240 efm_T *fmt_ptr,
1241 regmatch_T *regmatch,
1242 qffields_T *fields,
1243 int qf_multiline,
1244 int qf_multiscan,
1245 char_u **tail)
1246{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001247 int idx = fmt_ptr->prefix;
1248 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001249 int midx;
1250 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001251
1252 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1253 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001254 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001255 fields->type = idx;
1256 else
1257 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001258
1259 // Extract error message data from matched line.
1260 // We check for an actual submatch, because "\[" and "\]" in
1261 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001262 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001263 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001264 status = QF_OK;
1265 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001266 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001267 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
haya14busae023d492022-02-08 18:09:29 +00001268 else if (i == FMT_PATTERN_M)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001269 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001270 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001271 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001272 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001273 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001274 }
haya14busae023d492022-02-08 18:09:29 +00001275 else if (i == FMT_PATTERN_R && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001276 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001277 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001278 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001279
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001280 if (status != QF_OK)
1281 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001282 }
1283
1284 return QF_OK;
1285}
1286
1287/*
1288 * Parse an error line in 'linebuf' using a single error format string in
1289 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1290 * Returns QF_OK if the efm format matches completely and the fields are
1291 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1292 */
1293 static int
1294qf_parse_get_fields(
1295 char_u *linebuf,
1296 int linelen,
1297 efm_T *fmt_ptr,
1298 qffields_T *fields,
1299 int qf_multiline,
1300 int qf_multiscan,
1301 char_u **tail)
1302{
1303 regmatch_T regmatch;
1304 int status = QF_FAIL;
1305 int r;
1306
1307 if (qf_multiscan &&
1308 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1309 return QF_FAIL;
1310
1311 fields->namebuf[0] = NUL;
1312 fields->module[0] = NUL;
1313 fields->pattern[0] = NUL;
1314 if (!qf_multiscan)
1315 fields->errmsg[0] = NUL;
1316 fields->lnum = 0;
thinca6864efa2021-06-19 20:45:20 +02001317 fields->end_lnum = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001318 fields->col = 0;
thinca6864efa2021-06-19 20:45:20 +02001319 fields->end_col = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001320 fields->use_viscol = FALSE;
1321 fields->enr = -1;
1322 fields->type = 0;
1323 *tail = NULL;
1324
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001325 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001326 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001327 regmatch.regprog = fmt_ptr->prog;
1328 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1329 fmt_ptr->prog = regmatch.regprog;
1330 if (r)
1331 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1332 fields, qf_multiline, qf_multiscan, tail);
1333
1334 return status;
1335}
1336
1337/*
1338 * Parse directory error format prefixes (%D and %X).
1339 * Push and pop directories from the directory stack when scanning directory
1340 * names.
1341 */
1342 static int
1343qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1344{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001345 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001346 {
1347 if (*fields->namebuf == NUL)
1348 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001349 emsg(_(e_missing_or_empty_directory_name));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001350 return QF_FAIL;
1351 }
1352 qfl->qf_directory =
1353 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1354 if (qfl->qf_directory == NULL)
1355 return QF_FAIL;
1356 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001357 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001358 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1359
1360 return QF_OK;
1361}
1362
1363/*
1364 * Parse global file name error format prefixes (%O, %P and %Q).
1365 */
1366 static int
1367qf_parse_file_pfx(
1368 int idx,
1369 qffields_T *fields,
1370 qf_list_T *qfl,
1371 char_u *tail)
1372{
1373 fields->valid = FALSE;
1374 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1375 {
1376 if (*fields->namebuf && idx == 'P')
1377 qfl->qf_currfile =
1378 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1379 else if (idx == 'Q')
1380 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1381 *fields->namebuf = NUL;
1382 if (tail && *tail)
1383 {
1384 STRMOVE(IObuff, skipwhite(tail));
1385 qfl->qf_multiscan = TRUE;
1386 return QF_MULTISCAN;
1387 }
1388 }
1389
1390 return QF_OK;
1391}
1392
1393/*
1394 * Parse a non-error line (a line which doesn't match any of the error
1395 * format in 'efm').
1396 */
1397 static int
1398qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1399{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001400 fields->namebuf[0] = NUL; // no match found, remove file name
1401 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001402 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001403
Bram Moolenaarf4140482020-02-15 23:06:45 +01001404 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001405}
1406
1407/*
1408 * Parse multi-line error format prefixes (%C and %Z)
1409 */
1410 static int
1411qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001412 int idx,
1413 qf_list_T *qfl,
1414 qffields_T *fields)
1415{
1416 char_u *ptr;
1417 int len;
1418
1419 if (!qfl->qf_multiignore)
1420 {
1421 qfline_T *qfprev = qfl->qf_last;
1422
1423 if (qfprev == NULL)
1424 return QF_FAIL;
1425 if (*fields->errmsg && !qfl->qf_multiignore)
1426 {
1427 len = (int)STRLEN(qfprev->qf_text);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00001428 ptr = alloc_id(len + STRLEN(fields->errmsg) + 2,
1429 aid_qf_multiline_pfx);
1430 if (ptr == NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001431 return QF_FAIL;
1432 STRCPY(ptr, qfprev->qf_text);
1433 vim_free(qfprev->qf_text);
1434 qfprev->qf_text = ptr;
1435 *(ptr += len) = '\n';
1436 STRCPY(++ptr, fields->errmsg);
1437 }
1438 if (qfprev->qf_nr == -1)
1439 qfprev->qf_nr = fields->enr;
1440 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001441 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001442 qfprev->qf_type = fields->type;
1443
1444 if (!qfprev->qf_lnum)
1445 qfprev->qf_lnum = fields->lnum;
haya14busae023d492022-02-08 18:09:29 +00001446 if (!qfprev->qf_end_lnum)
1447 qfprev->qf_end_lnum = fields->end_lnum;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001448 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001449 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001450 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001451 qfprev->qf_viscol = fields->use_viscol;
1452 }
haya14busae023d492022-02-08 18:09:29 +00001453 if (!qfprev->qf_end_col)
1454 qfprev->qf_end_col = fields->end_col;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001455 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001456 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001457 qfl->qf_directory,
1458 *fields->namebuf || qfl->qf_directory != NULL
1459 ? fields->namebuf
1460 : qfl->qf_currfile != NULL && fields->valid
1461 ? qfl->qf_currfile : 0);
1462 }
1463 if (idx == 'Z')
1464 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1465 line_breakcheck();
1466
1467 return QF_IGNORE_LINE;
1468}
1469
1470/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001471 * Parse a line and get the quickfix fields.
1472 * Return the QF_ status.
1473 */
1474 static int
1475qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001476 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001477 char_u *linebuf,
1478 int linelen,
1479 efm_T *fmt_first,
1480 qffields_T *fields)
1481{
1482 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001483 int idx = 0;
1484 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001485 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001486
Bram Moolenaare333e792018-04-08 13:27:39 +02001487restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001488 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001489 if (fmt_start == NULL)
1490 fmt_ptr = fmt_first;
1491 else
1492 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001493 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001494 fmt_ptr = fmt_start;
1495 fmt_start = NULL;
1496 }
1497
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001498 // Try to match each part of 'errorformat' until we find a complete
1499 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001500 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001501 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1502 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001503 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001504 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1505 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1506 if (status == QF_NOMEM)
1507 return status;
1508 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001509 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001510 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001511 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001512
1513 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1514 {
1515 if (fmt_ptr != NULL)
1516 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001517 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001518 status = qf_parse_dir_pfx(idx, fields, qfl);
1519 if (status != QF_OK)
1520 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001521 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001522
1523 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1524 if (status != QF_OK)
1525 return status;
1526
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001527 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001528 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001529 }
1530 else if (fmt_ptr != NULL)
1531 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001532 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001533 if (fmt_ptr->conthere)
1534 fmt_start = fmt_ptr;
1535
Bram Moolenaare9283662020-06-07 14:10:47 +02001536 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001537 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001538 qfl->qf_multiline = TRUE; // start of a multi-line message
1539 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001540 }
1541 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001542 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001543 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001544 if (status != QF_OK)
1545 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001546 }
1547 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001548 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001549 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1550 if (status == QF_MULTISCAN)
1551 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001552 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001553 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001554 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001555 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001556 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001557 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001558 return QF_IGNORE_LINE;
1559 }
1560 }
1561
1562 return QF_OK;
1563}
1564
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001565/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001566 * Returns TRUE if the specified quickfix/location stack is empty
1567 */
1568 static int
1569qf_stack_empty(qf_info_T *qi)
1570{
1571 return qi == NULL || qi->qf_listcount <= 0;
1572}
1573
1574/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001575 * Returns TRUE if the specified quickfix/location list is empty.
1576 */
1577 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001578qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001579{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001580 return qfl == NULL || qfl->qf_count <= 0;
1581}
1582
1583/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001584 * Returns TRUE if the specified quickfix/location list is not empty and
1585 * has valid entries.
1586 */
1587 static int
1588qf_list_has_valid_entries(qf_list_T *qfl)
1589{
1590 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1591}
1592
1593/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001594 * Return a pointer to a list in the specified quickfix stack
1595 */
1596 static qf_list_T *
1597qf_get_list(qf_info_T *qi, int idx)
1598{
1599 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001600}
1601
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001602/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001603 * Allocate the fields used for parsing lines and populating a quickfix list.
1604 */
1605 static int
1606qf_alloc_fields(qffields_T *pfields)
1607{
1608 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1609 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1610 pfields->errmsglen = CMDBUFFSIZE + 1;
1611 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1612 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1613 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1614 || pfields->pattern == NULL || pfields->module == NULL)
1615 return FAIL;
1616
1617 return OK;
1618}
1619
1620/*
1621 * Free the fields used for parsing lines and populating a quickfix list.
1622 */
1623 static void
1624qf_free_fields(qffields_T *pfields)
1625{
1626 vim_free(pfields->namebuf);
1627 vim_free(pfields->module);
1628 vim_free(pfields->errmsg);
1629 vim_free(pfields->pattern);
1630}
1631
1632/*
1633 * Setup the state information used for parsing lines and populating a
1634 * quickfix list.
1635 */
1636 static int
1637qf_setup_state(
1638 qfstate_T *pstate,
1639 char_u *enc,
1640 char_u *efile,
1641 typval_T *tv,
1642 buf_T *buf,
1643 linenr_T lnumfirst,
1644 linenr_T lnumlast)
1645{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001646 pstate->vc.vc_type = CONV_NONE;
1647 if (enc != NULL && *enc != NUL)
1648 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001649
1650 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1651 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001652 semsg(_(e_cant_open_errorfile_str), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001653 return FAIL;
1654 }
1655
1656 if (tv != NULL)
1657 {
1658 if (tv->v_type == VAR_STRING)
1659 pstate->p_str = tv->vval.v_string;
1660 else if (tv->v_type == VAR_LIST)
1661 pstate->p_li = tv->vval.v_list->lv_first;
1662 pstate->tv = tv;
1663 }
1664 pstate->buf = buf;
1665 pstate->buflnum = lnumfirst;
1666 pstate->lnumlast = lnumlast;
1667
1668 return OK;
1669}
1670
1671/*
1672 * Cleanup the state information used for parsing lines and populating a
1673 * quickfix list.
1674 */
1675 static void
1676qf_cleanup_state(qfstate_T *pstate)
1677{
1678 if (pstate->fd != NULL)
1679 fclose(pstate->fd);
1680
1681 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001682 if (pstate->vc.vc_type != CONV_NONE)
1683 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001684}
1685
1686/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001687 * Process the next line from a file/buffer/list/string and add it
1688 * to the quickfix list 'qfl'.
1689 */
1690 static int
1691qf_init_process_nextline(
1692 qf_list_T *qfl,
1693 efm_T *fmt_first,
1694 qfstate_T *state,
1695 qffields_T *fields)
1696{
1697 int status;
1698
1699 // Get the next line from a file/buffer/list/string
1700 status = qf_get_nextline(state);
1701 if (status != QF_OK)
1702 return status;
1703
1704 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1705 fmt_first, fields);
1706 if (status != QF_OK)
1707 return status;
1708
1709 return qf_add_entry(qfl,
1710 qfl->qf_directory,
1711 (*fields->namebuf || qfl->qf_directory != NULL)
1712 ? fields->namebuf
1713 : ((qfl->qf_currfile != NULL && fields->valid)
1714 ? qfl->qf_currfile : (char_u *)NULL),
1715 fields->module,
1716 0,
1717 fields->errmsg,
1718 fields->lnum,
thinca6864efa2021-06-19 20:45:20 +02001719 fields->end_lnum,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001720 fields->col,
thinca6864efa2021-06-19 20:45:20 +02001721 fields->end_col,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001722 fields->use_viscol,
1723 fields->pattern,
1724 fields->enr,
1725 fields->type,
Tom Praschanca6ac992023-08-11 23:26:12 +02001726 fields->user_data,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001727 fields->valid);
1728}
1729
1730/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001731 * Read the errorfile "efile" into memory, line by line, building the error
1732 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001733 * Alternative: when "efile" is NULL read errors from buffer "buf".
1734 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001735 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001736 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1737 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001738 * Return -1 for error, number of errors for success.
1739 */
1740 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001741qf_init_ext(
1742 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001743 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001744 char_u *efile,
1745 buf_T *buf,
1746 typval_T *tv,
1747 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001748 int newlist, // TRUE: start a new error list
1749 linenr_T lnumfirst, // first line number to use
1750 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001751 char_u *qf_title,
1752 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001753{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001754 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001755 qfstate_T state;
1756 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001757 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001758 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001759 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001761 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001762 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001763 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001765 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001766 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001767
Bram Moolenaara80faa82020-04-12 19:37:17 +02001768 CLEAR_FIELD(state);
1769 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001770 if ((qf_alloc_fields(&fields) == FAIL) ||
1771 (qf_setup_state(&state, enc, efile, tv, buf,
1772 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 goto qf_init_end;
1774
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001775 if (newlist || qf_idx == qi->qf_listcount)
1776 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001777 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001778 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001779 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001780 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001781 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001782 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001783 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001784 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001785 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001786 qfl = qf_get_list(qi, qf_idx);
1787 if (!qf_list_empty(qfl))
1788 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001791 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001792 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001793 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 else
1795 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001797 // If the errorformat didn't change between calls, then reuse the
1798 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001799 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1800 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001801 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001802 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001803 free_efm_list(&fmt_first);
1804
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001805 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001806 fmt_first = parse_efm_option(efm);
1807 if (fmt_first != NULL)
1808 last_efm = vim_strsave(efm);
1809 }
1810
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001811 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001814 // got_int is reset here, because it was probably set when killing the
1815 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 got_int = FALSE;
1817
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001818 // Read the lines in the error file one by one.
1819 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001820 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001822 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001823 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001824 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001825 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001826 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001827 if (status == QF_FAIL)
1828 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 line_breakcheck();
1831 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001832 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001834 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001836 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001837 qfl->qf_ptr = qfl->qf_start;
1838 qfl->qf_index = 1;
1839 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 }
1841 else
1842 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001843 qfl->qf_nonevalid = FALSE;
1844 if (qfl->qf_ptr == NULL)
1845 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001847 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001848 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001849 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 }
Bram Moolenaard8e44472021-07-21 22:20:33 +02001851 emsg(_(e_error_while_reading_errorfile));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001853 if (!adding)
1854 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001855 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001856 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001857 qi->qf_listcount--;
1858 if (qi->qf_curlist > 0)
1859 --qi->qf_curlist;
1860 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001861qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001862 if (qf_idx == qi->qf_curlist)
1863 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001864 qf_cleanup_state(&state);
1865 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866
1867 return retval;
1868}
1869
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001870/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001871 * Read the errorfile "efile" into memory, line by line, building the error
1872 * list. Set the error list's title to qf_title.
1873 * Return -1 for error, number of errors for success.
1874 */
1875 int
1876qf_init(win_T *wp,
1877 char_u *efile,
1878 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001879 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001880 char_u *qf_title,
1881 char_u *enc)
1882{
1883 qf_info_T *qi = &ql_info;
1884
1885 if (wp != NULL)
1886 {
1887 qi = ll_get_or_alloc_list(wp);
1888 if (qi == NULL)
1889 return FAIL;
1890 }
1891
1892 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1893 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1894}
1895
1896/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001897 * Set the title of the specified quickfix list. Frees the previous title.
1898 * Prepends ':' to the title.
1899 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001900 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001901qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001902{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001903 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001904
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001905 if (title == NULL)
1906 return;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001907
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001908 char_u *p = alloc_id(STRLEN(title) + 2, aid_qf_title);
1909
1910 qfl->qf_title = p;
1911 if (p != NULL)
1912 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001913}
1914
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001916 * The title of a quickfix/location list is set, by default, to the command
1917 * that created the quickfix list with the ":" prefix.
1918 * Create a quickfix list title string by prepending ":" to a user command.
1919 * Returns a pointer to a static buffer with the title.
1920 */
1921 static char_u *
1922qf_cmdtitle(char_u *cmd)
1923{
1924 static char_u qftitle_str[IOSIZE];
1925
1926 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1927 return qftitle_str;
1928}
1929
1930/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001931 * Return a pointer to the current list in the specified quickfix stack
1932 */
1933 static qf_list_T *
1934qf_get_curlist(qf_info_T *qi)
1935{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001936 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001937}
1938
1939/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001940 * Prepare for adding a new quickfix list. If the current list is in the
1941 * middle of the stack, then all the following lists are freed and then
1942 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 */
1944 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001945qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946{
1947 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001948 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001950 // If the current entry is not the last entry, delete entries beyond
1951 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001952 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001953 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001954 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001956 // When the stack is full, remove to oldest entry
1957 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001958 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001960 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001962 qi->qf_lists[i - 1] = qi->qf_lists[i];
1963 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
1965 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001966 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001967 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001968 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001969 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001970 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001971 qfl->qf_id = ++last_qf_id;
Tom Praschanca6ac992023-08-11 23:26:12 +02001972 qfl->qf_has_user_data = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973}
1974
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001975/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001976 * Queue location list stack delete request.
1977 */
1978 static void
1979locstack_queue_delreq(qf_info_T *qi)
1980{
1981 qf_delq_T *q;
1982
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001983 q = ALLOC_ONE(qf_delq_T);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001984 if (q == NULL)
1985 return;
1986
1987 q->qi = qi;
1988 q->next = qf_delq_head;
1989 qf_delq_head = q;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001990}
1991
1992/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01001993 * Return the global quickfix stack window buffer number.
1994 */
1995 int
1996qf_stack_get_bufnr(void)
1997{
1998 return ql_info.qf_bufnr;
1999}
2000
2001/*
2002 * Wipe the quickfix window buffer (if present) for the specified
2003 * quickfix/location list.
2004 */
2005 static void
2006wipe_qf_buffer(qf_info_T *qi)
2007{
2008 buf_T *qfbuf;
2009
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002010 if (qi->qf_bufnr == INVALID_QFBUFNR)
2011 return;
2012
2013 qfbuf = buflist_findnr(qi->qf_bufnr);
2014 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002015 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002016 // If the quickfix buffer is not loaded in any window, then
2017 // wipe the buffer.
2018 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
2019 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002020 }
2021}
2022
2023/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002024 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002025 */
2026 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002027ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002028{
2029 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002030 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002031
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002032 qi = *pqi;
2033 if (qi == NULL)
2034 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002035 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002036
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002037 // If the location list is still in use, then queue the delete request
2038 // to be processed later.
2039 if (quickfix_busy > 0)
2040 {
2041 locstack_queue_delreq(qi);
2042 return;
2043 }
2044
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002045 qi->qf_refcount--;
2046 if (qi->qf_refcount < 1)
2047 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002048 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002049 // If the quickfix window buffer is loaded, then wipe it
2050 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002051
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002052 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002053 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002054 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002055 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002056}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002057
Bram Moolenaar18cebf42018-05-08 22:31:37 +02002058/*
2059 * Free all the quickfix/location lists in the stack.
2060 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002061 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002062qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002063{
2064 int i;
2065 qf_info_T *qi = &ql_info;
2066
2067 if (wp != NULL)
2068 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002069 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002070 ll_free_all(&wp->w_llist);
2071 ll_free_all(&wp->w_llist_ref);
2072 }
2073 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002074 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002075 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002076 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002077}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002078
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002080 * Delay freeing of location list stacks when the quickfix code is running.
2081 * Used to avoid problems with autocmds freeing location list stacks when the
2082 * quickfix code is still referencing the stack.
2083 * Must always call decr_quickfix_busy() exactly once after this.
2084 */
2085 static void
2086incr_quickfix_busy(void)
2087{
2088 quickfix_busy++;
2089}
2090
2091/*
2092 * Safe to free location list stacks. Process any delayed delete requests.
2093 */
2094 static void
2095decr_quickfix_busy(void)
2096{
2097 if (--quickfix_busy == 0)
2098 {
2099 // No longer referencing the location lists. Process all the pending
2100 // delete requests.
2101 while (qf_delq_head != NULL)
2102 {
2103 qf_delq_T *q = qf_delq_head;
2104
2105 qf_delq_head = q->next;
2106 ll_free_all(&q->qi);
2107 vim_free(q);
2108 }
2109 }
2110#ifdef ABORT_ON_INTERNAL_ERROR
2111 if (quickfix_busy < 0)
2112 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002113 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002114 abort();
2115 }
2116#endif
2117}
2118
2119#if defined(EXITFREE) || defined(PROTO)
2120 void
2121check_quickfix_busy(void)
2122{
2123 if (quickfix_busy != 0)
2124 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002125 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002126# ifdef ABORT_ON_INTERNAL_ERROR
2127 abort();
2128# endif
2129 }
2130}
2131#endif
2132
2133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 * Add an entry to the end of the list of errors.
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00002135 * Returns QF_OK on success or QF_FAIL on a memory allocation failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 */
2137 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002138qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002139 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002140 char_u *dir, // optional directory name
2141 char_u *fname, // file name or NULL
2142 char_u *module, // module name or NULL
2143 int bufnum, // buffer number or zero
2144 char_u *mesg, // message
2145 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002146 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002147 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002148 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002149 int vis_col, // using visual column
2150 char_u *pattern, // search pattern
2151 int nr, // error number
2152 int type, // type character
Tom Praschanca6ac992023-08-11 23:26:12 +02002153 typval_T *user_data, // custom user data or NULL
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002154 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002156 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002157 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002159 if ((qfp = ALLOC_ONE_ID(qfline_T, aid_qf_qfline)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002160 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002161 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002162 {
2163 buf_T *buf = buflist_findnr(bufnum);
2164
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002165 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002166 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002167 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002168 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002169 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002170 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002171 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2173 {
2174 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002175 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 }
2177 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002178 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002180 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002181 qfp->qf_viscol = vis_col;
Tom Praschanca6ac992023-08-11 23:26:12 +02002182 if (user_data == NULL || user_data->v_type == VAR_UNKNOWN)
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02002183 qfp->qf_user_data.v_type = VAR_UNKNOWN;
Tom Praschanca6ac992023-08-11 23:26:12 +02002184 else
2185 {
2186 copy_tv(user_data, &qfp->qf_user_data);
2187 qfl->qf_has_user_data = TRUE;
2188 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002189 if (pattern == NULL || *pattern == NUL)
2190 qfp->qf_pattern = NULL;
2191 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2192 {
2193 vim_free(qfp->qf_text);
2194 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002195 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002196 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002197 if (module == NULL || *module == NUL)
2198 qfp->qf_module = NULL;
2199 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2200 {
2201 vim_free(qfp->qf_text);
2202 vim_free(qfp->qf_pattern);
2203 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002204 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002207 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 type = 0;
2209 qfp->qf_type = type;
2210 qfp->qf_valid = valid;
2211
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002212 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002213 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002215 qfl->qf_start = qfp;
2216 qfl->qf_ptr = qfp;
2217 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002218 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 }
2220 else
2221 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002222 qfp->qf_prev = *lastp;
2223 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002225 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002227 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002228 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002229 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002231 qfl->qf_index = qfl->qf_count;
2232 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 }
2234
Bram Moolenaar95946f12019-03-31 15:31:59 +02002235 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236}
2237
2238/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002239 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002240 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002241 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002242qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002243{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002244 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002245
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002246 qi = ALLOC_CLEAR_ONE_ID(qf_info_T, aid_qf_qfinfo);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002247 if (qi == NULL)
2248 return NULL;
2249
2250 qi->qf_refcount++;
2251 qi->qfl_type = qfltype;
2252 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002253 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002254}
2255
2256/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002257 * Return the location list stack for window 'wp'.
2258 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002259 */
2260 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002261ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002262{
2263 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002264 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002265 return wp->w_llist_ref;
2266
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002267 // For a non-location list window, w_llist_ref should not point to a
2268 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002269 ll_free_all(&wp->w_llist_ref);
2270
2271 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002272 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002273 return wp->w_llist;
2274}
2275
2276/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002277 * Get the quickfix/location list stack to use for the specified Ex command.
2278 * For a location list command, returns the stack for the current window. If
2279 * the location list is not found, then returns NULL and prints an error
2280 * message if 'print_emsg' is TRUE.
2281 */
2282 static qf_info_T *
2283qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2284{
2285 qf_info_T *qi = &ql_info;
2286
2287 if (is_loclist_cmd(eap->cmdidx))
2288 {
2289 qi = GET_LOC_LIST(curwin);
2290 if (qi == NULL)
2291 {
2292 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002293 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002294 return NULL;
2295 }
2296 }
2297
2298 return qi;
2299}
2300
2301/*
2302 * Get the quickfix/location list stack to use for the specified Ex command.
2303 * For a location list command, returns the stack for the current window.
2304 * If the location list is not present, then allocates a new one.
2305 * Returns NULL if the allocation fails. For a location list command, sets
2306 * 'pwinp' to curwin.
2307 */
2308 static qf_info_T *
2309qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2310{
2311 qf_info_T *qi = &ql_info;
2312
2313 if (is_loclist_cmd(eap->cmdidx))
2314 {
2315 qi = ll_get_or_alloc_list(curwin);
2316 if (qi == NULL)
2317 return NULL;
2318 *pwinp = curwin;
2319 }
2320
2321 return qi;
2322}
2323
2324/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002325 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2326 */
2327 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002328copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002329{
2330 int i;
2331 qfline_T *from_qfp;
2332 qfline_T *prevp;
2333
2334 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002335 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002336 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002337 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002338 NULL,
2339 NULL,
2340 from_qfp->qf_module,
2341 0,
2342 from_qfp->qf_text,
2343 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002344 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002345 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002346 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002347 from_qfp->qf_viscol,
2348 from_qfp->qf_pattern,
2349 from_qfp->qf_nr,
2350 0,
Tom Praschanca6ac992023-08-11 23:26:12 +02002351 &from_qfp->qf_user_data,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002352 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002353 return FAIL;
2354
2355 // qf_add_entry() will not set the qf_num field, as the
2356 // directory and file names are not supplied. So the qf_fnum
2357 // field is copied here.
2358 prevp = to_qfl->qf_last;
2359 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2360 prevp->qf_type = from_qfp->qf_type; // error type
2361 if (from_qfl->qf_ptr == from_qfp)
2362 to_qfl->qf_ptr = prevp; // current location
2363 }
2364
2365 return OK;
2366}
2367
2368/*
2369 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2370 */
2371 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002372copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002373{
2374 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002375 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002376 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
Tom Praschanca6ac992023-08-11 23:26:12 +02002377 to_qfl->qf_has_user_data = from_qfl->qf_has_user_data;
Bram Moolenaar09037502018-09-25 22:08:14 +02002378 to_qfl->qf_count = 0;
2379 to_qfl->qf_index = 0;
2380 to_qfl->qf_start = NULL;
2381 to_qfl->qf_last = NULL;
2382 to_qfl->qf_ptr = NULL;
2383 if (from_qfl->qf_title != NULL)
2384 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2385 else
2386 to_qfl->qf_title = NULL;
2387 if (from_qfl->qf_ctx != NULL)
2388 {
2389 to_qfl->qf_ctx = alloc_tv();
2390 if (to_qfl->qf_ctx != NULL)
2391 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2392 }
2393 else
2394 to_qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002395 if (from_qfl->qf_qftf_cb.cb_name != NULL)
2396 copy_callback(&to_qfl->qf_qftf_cb, &from_qfl->qf_qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002397 else
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002398 to_qfl->qf_qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002399
2400 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002401 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002402 return FAIL;
2403
2404 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2405
2406 // Assign a new ID for the location list
2407 to_qfl->qf_id = ++last_qf_id;
2408 to_qfl->qf_changedtick = 0L;
2409
2410 // When no valid entries are present in the list, qf_ptr points to
2411 // the first item in the list
2412 if (to_qfl->qf_nonevalid)
2413 {
2414 to_qfl->qf_ptr = to_qfl->qf_start;
2415 to_qfl->qf_index = 1;
2416 }
2417
2418 return OK;
2419}
2420
2421/*
2422 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002423 */
2424 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002425copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002426{
2427 qf_info_T *qi;
2428 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002429
Bram Moolenaar09037502018-09-25 22:08:14 +02002430 // When copying from a location list window, copy the referenced
2431 // location list. For other windows, copy the location list for
2432 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002433 if (IS_LL_WINDOW(from))
2434 qi = from->w_llist_ref;
2435 else
2436 qi = from->w_llist;
2437
Bram Moolenaar09037502018-09-25 22:08:14 +02002438 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002439 return;
2440
Bram Moolenaar09037502018-09-25 22:08:14 +02002441 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002442 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002443 return;
2444
2445 to->w_llist->qf_listcount = qi->qf_listcount;
2446
Bram Moolenaar09037502018-09-25 22:08:14 +02002447 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002448 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002449 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002450 to->w_llist->qf_curlist = idx;
2451
Bram Moolenaar0398e002019-03-21 21:12:49 +01002452 if (copy_loclist(qf_get_list(qi, idx),
2453 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002454 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002455 qf_free_all(to);
2456 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002457 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002458 }
2459
Bram Moolenaar09037502018-09-25 22:08:14 +02002460 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002461}
2462
2463/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002464 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002465 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 */
2467 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002468qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
Bram Moolenaar82404332016-07-10 17:00:38 +02002470 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002471 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002472 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002473
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002474 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
Bram Moolenaare60acc12011-05-10 16:41:25 +02002477#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002478 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002479#endif
2480#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002481 if (directory != NULL)
2482 slash_adjust(directory);
2483 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002484#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002485 if (directory != NULL && !vim_isAbsName(fname)
2486 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2487 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002488 // Here we check if the file really exists.
2489 // This should normally be true, but if make works without
2490 // "leaving directory"-messages we might have missed a
2491 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002492 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002495 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002496 if (directory)
2497 ptr = concat_fnames(directory, fname, TRUE);
2498 else
2499 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002501 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002502 bufname = ptr;
2503 }
2504 else
2505 bufname = fname;
2506
2507 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002508 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002509 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002510 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002511 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002513 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002514 {
2515 vim_free(qf_last_bufname);
2516 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2517 if (bufname == ptr)
2518 qf_last_bufname = bufname;
2519 else
2520 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002521 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002522 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002523 if (buf == NULL)
2524 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002525
Bram Moolenaarc1542742016-07-20 21:44:37 +02002526 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002527 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002528 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529}
2530
2531/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002532 * Push dirbuf onto the directory stack and return pointer to actual dir or
2533 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 */
2535 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002536qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
2538 struct dir_stack_T *ds_new;
2539 struct dir_stack_T *ds_ptr;
2540
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002541 // allocate new stack element and hook it in
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002542 ds_new = ALLOC_ONE_ID(struct dir_stack_T, aid_qf_dirstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 if (ds_new == NULL)
2544 return NULL;
2545
2546 ds_new->next = *stackptr;
2547 *stackptr = ds_new;
2548
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002549 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 if (vim_isAbsName(dirbuf)
2551 || (*stackptr)->next == NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=dd410372022-05-15 13:59:11 +01002552 || is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 (*stackptr)->dirname = vim_strsave(dirbuf);
2554 else
2555 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002556 // Okay we don't have an absolute path.
2557 // dirbuf must be a subdir of one of the directories on the stack.
2558 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 ds_new = (*stackptr)->next;
2560 (*stackptr)->dirname = NULL;
2561 while (ds_new)
2562 {
2563 vim_free((*stackptr)->dirname);
2564 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2565 TRUE);
2566 if (mch_isdir((*stackptr)->dirname) == TRUE)
2567 break;
2568
2569 ds_new = ds_new->next;
2570 }
2571
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002572 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 while ((*stackptr)->next != ds_new)
2574 {
2575 ds_ptr = (*stackptr)->next;
2576 (*stackptr)->next = (*stackptr)->next->next;
2577 vim_free(ds_ptr->dirname);
2578 vim_free(ds_ptr);
2579 }
2580
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002581 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (ds_new == NULL)
2583 {
2584 vim_free((*stackptr)->dirname);
2585 (*stackptr)->dirname = vim_strsave(dirbuf);
2586 }
2587 }
2588
2589 if ((*stackptr)->dirname != NULL)
2590 return (*stackptr)->dirname;
2591 else
2592 {
2593 ds_ptr = *stackptr;
2594 *stackptr = (*stackptr)->next;
2595 vim_free(ds_ptr);
2596 return NULL;
2597 }
2598}
2599
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600/*
2601 * pop dirbuf from the directory stack and return previous directory or NULL if
2602 * stack is empty
2603 */
2604 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002605qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
2607 struct dir_stack_T *ds_ptr;
2608
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002609 // TODO: Should we check if dirbuf is the directory on top of the stack?
2610 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002612 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 if (*stackptr != NULL)
2614 {
2615 ds_ptr = *stackptr;
2616 *stackptr = (*stackptr)->next;
2617 vim_free(ds_ptr->dirname);
2618 vim_free(ds_ptr);
2619 }
2620
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002621 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 return *stackptr ? (*stackptr)->dirname : NULL;
2623}
2624
2625/*
2626 * clean up directory stack
2627 */
2628 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002629qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630{
2631 struct dir_stack_T *ds_ptr;
2632
2633 while ((ds_ptr = *stackptr) != NULL)
2634 {
2635 *stackptr = (*stackptr)->next;
2636 vim_free(ds_ptr->dirname);
2637 vim_free(ds_ptr);
2638 }
2639}
2640
2641/*
2642 * Check in which directory of the directory stack the given file can be
2643 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002644 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 * Cleans up intermediate directory entries.
2646 *
2647 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002648 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 * ./
2650 * ./aa
2651 * ./aa/bb
2652 * ./bb
2653 * ./bb/x.c
2654 * and make says:
2655 * making all in aa
2656 * making all in bb
2657 * x.c:9: Error
2658 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2659 * qf_guess_filepath will return NULL.
2660 */
2661 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002662qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663{
2664 struct dir_stack_T *ds_ptr;
2665 struct dir_stack_T *ds_tmp;
2666 char_u *fullname;
2667
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002668 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002669 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 return NULL;
2671
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002672 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 fullname = NULL;
2674 while (ds_ptr)
2675 {
2676 vim_free(fullname);
2677 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2678
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002679 // If concat_fnames failed, just go on. The worst thing that can happen
2680 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2682 break;
2683
2684 ds_ptr = ds_ptr->next;
2685 }
2686
2687 vim_free(fullname);
2688
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002689 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002690 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002692 ds_tmp = qfl->qf_dir_stack->next;
2693 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 vim_free(ds_tmp->dirname);
2695 vim_free(ds_tmp);
2696 }
2697
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002698 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699}
2700
2701/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002702 * Returns TRUE if a quickfix/location list with the given identifier exists.
2703 */
2704 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002705qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002706{
2707 qf_info_T *qi = &ql_info;
2708 int i;
2709
2710 if (wp != NULL)
2711 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002712 if (!win_valid(wp))
2713 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002714 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002715 if (qi == NULL)
2716 return FALSE;
2717 }
2718
2719 for (i = 0; i < qi->qf_listcount; ++i)
2720 if (qi->qf_lists[i].qf_id == qf_id)
2721 return TRUE;
2722
2723 return FALSE;
2724}
2725
2726/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002727 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002728 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002729 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002730 * Similar to location list.
2731 */
2732 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002733is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002734{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002735 qfline_T *qfp;
2736 int i;
2737
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002738 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002739 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2740 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002741 break;
2742
Bram Moolenaar95946f12019-03-31 15:31:59 +02002743 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002744 return FALSE;
2745
2746 return TRUE;
2747}
2748
2749/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002750 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002751 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002752 */
2753 static qfline_T *
2754get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002755 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002756 qfline_T *qf_ptr,
2757 int *qf_index,
2758 int dir)
2759{
2760 int idx;
2761 int old_qf_fnum;
2762
2763 idx = *qf_index;
2764 old_qf_fnum = qf_ptr->qf_fnum;
2765
2766 do
2767 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002768 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002769 return NULL;
2770 ++idx;
2771 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002772 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002773 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2774
2775 *qf_index = idx;
2776 return qf_ptr;
2777}
2778
2779/*
2780 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002781 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002782 */
2783 static qfline_T *
2784get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002785 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002786 qfline_T *qf_ptr,
2787 int *qf_index,
2788 int dir)
2789{
2790 int idx;
2791 int old_qf_fnum;
2792
2793 idx = *qf_index;
2794 old_qf_fnum = qf_ptr->qf_fnum;
2795
2796 do
2797 {
2798 if (idx == 1 || qf_ptr->qf_prev == NULL)
2799 return NULL;
2800 --idx;
2801 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002802 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002803 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2804
2805 *qf_index = idx;
2806 return qf_ptr;
2807}
2808
2809/*
2810 * Get the n'th (errornr) previous/next valid entry from the current entry in
2811 * the quickfix list.
2812 * dir == FORWARD or FORWARD_FILE: next valid entry
2813 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2814 */
2815 static qfline_T *
2816get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002817 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002818 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002819 int dir,
2820 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002821{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002822 qfline_T *qf_ptr = qfl->qf_ptr;
2823 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002824 qfline_T *prev_qf_ptr;
2825 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002826 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002827
2828 while (errornr--)
2829 {
2830 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002831 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002832
2833 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002834 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002835 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002836 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002837 if (qf_ptr == NULL)
2838 {
2839 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002840 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002841 if (err != NULL)
2842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002843 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002844 return NULL;
2845 }
2846 break;
2847 }
2848
2849 err = NULL;
2850 }
2851
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002852 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002853 return qf_ptr;
2854}
2855
2856/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002857 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2858 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002859 */
2860 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002861get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002862{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002863 qfline_T *qf_ptr = qfl->qf_ptr;
2864 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002865
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002866 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002867 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2868 {
2869 --qf_idx;
2870 qf_ptr = qf_ptr->qf_prev;
2871 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002872 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002873 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2874 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002875 {
2876 ++qf_idx;
2877 qf_ptr = qf_ptr->qf_next;
2878 }
2879
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002880 *new_qfidx = qf_idx;
2881 return qf_ptr;
2882}
2883
2884/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002885 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002886 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2887 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2888 * Returns a pointer to the entry and the index of the new entry is stored in
2889 * 'new_qfidx'.
2890 */
2891 static qfline_T *
2892qf_get_entry(
2893 qf_list_T *qfl,
2894 int errornr,
2895 int dir,
2896 int *new_qfidx)
2897{
2898 qfline_T *qf_ptr = qfl->qf_ptr;
2899 int qfidx = qfl->qf_index;
2900
2901 if (dir != 0) // next/prev valid entry
2902 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2903 else if (errornr != 0) // go to specified number
2904 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2905
2906 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002907 return qf_ptr;
2908}
2909
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002910/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002911 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002912 */
2913 static win_T *
2914qf_find_help_win(void)
2915{
2916 win_T *wp;
2917
2918 FOR_ALL_WINDOWS(wp)
2919 if (bt_help(wp->w_buffer))
2920 return wp;
2921
2922 return NULL;
2923}
2924
2925/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002926 * Set the location list for the specified window to 'qi'.
2927 */
2928 static void
2929win_set_loclist(win_T *wp, qf_info_T *qi)
2930{
2931 wp->w_llist = qi;
2932 qi->qf_refcount++;
2933}
2934
2935/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002936 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2937 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002938 */
2939 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002940jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002941{
2942 win_T *wp;
2943 int flags;
2944
Bram Moolenaare1004402020-10-24 20:49:43 +02002945 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002946 wp = NULL;
2947 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002948 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002949 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2950 win_enter(wp, TRUE);
2951 else
2952 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002953 // Split off help window; put it at far top if no position
2954 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002955 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02002956 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002957 && curwin->w_width < 80)
2958 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002959 // If the user asks to open a new window, then copy the location list.
2960 // Otherwise, don't copy the location list.
2961 if (IS_LL_STACK(qi) && !newwin)
2962 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002963
2964 if (win_split(0, flags) == FAIL)
2965 return FAIL;
2966
2967 *opened_window = TRUE;
2968
2969 if (curwin->w_height < p_hh)
2970 win_setheight((int)p_hh);
2971
Bram Moolenaarb2443732018-11-11 22:50:27 +01002972 // When using location list, the new window should use the supplied
2973 // location list. If the user asks to open a new window, then the new
2974 // window will get a copy of the location list.
2975 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002976 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002977 }
2978
2979 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002980 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002981
2982 return OK;
2983}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002984
2985/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002986 * Find a non-quickfix window using the given location list stack in the
2987 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002988 * Returns NULL if a matching window is not found.
2989 */
2990 static win_T *
2991qf_find_win_with_loclist(qf_info_T *ll)
2992{
2993 win_T *wp;
2994
2995 FOR_ALL_WINDOWS(wp)
2996 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
2997 return wp;
2998
2999 return NULL;
3000}
3001
3002/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003003 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003004 */
3005 static win_T *
3006qf_find_win_with_normal_buf(void)
3007{
3008 win_T *wp;
3009
3010 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02003011 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003012 return wp;
3013
3014 return NULL;
3015}
3016
3017/*
3018 * Go to a window in any tabpage containing the specified file. Returns TRUE
3019 * if successfully jumped to the window. Otherwise returns FALSE.
3020 */
3021 static int
3022qf_goto_tabwin_with_file(int fnum)
3023{
3024 tabpage_T *tp;
3025 win_T *wp;
3026
3027 FOR_ALL_TAB_WINDOWS(tp, wp)
3028 if (wp->w_buffer->b_fnum == fnum)
3029 {
3030 goto_tabpage_win(tp, wp);
3031 return TRUE;
3032 }
3033
3034 return FALSE;
3035}
3036
3037/*
3038 * Create a new window to show a file above the quickfix window. Called when
3039 * only the quickfix window is present.
3040 */
3041 static int
3042qf_open_new_file_win(qf_info_T *ll_ref)
3043{
3044 int flags;
3045
3046 flags = WSP_ABOVE;
3047 if (ll_ref != NULL)
3048 flags |= WSP_NEWLOC;
3049 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003050 return FAIL; // not enough room for window
3051 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003052 swb_flags = 0;
3053 RESET_BINDING(curwin);
3054 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003055 // The new window should use the location list from the
3056 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003057 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003058 return OK;
3059}
3060
3061/*
3062 * Go to a window that shows the right buffer. If the window is not found, go
3063 * to the window just above the location list window. This is used for opening
3064 * a file from a location window and not from a quickfix window. If some usable
3065 * window is previously found, then it is supplied in 'use_win'.
3066 */
3067 static void
3068qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
3069{
3070 win_T *win = use_win;
3071
3072 if (win == NULL)
3073 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003074 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003075 FOR_ALL_WINDOWS(win)
3076 if (win->w_buffer->b_fnum == qf_fnum)
3077 break;
3078 if (win == NULL)
3079 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003080 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003081 win = curwin;
3082 do
3083 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02003084 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003085 break;
3086 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003087 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003088 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003089 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003090 } while (win != curwin);
3091 }
3092 }
3093 win_goto(win);
3094
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003095 // If the location list for the window is not set, then set it
3096 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003097 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003098 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003099}
3100
3101/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003102 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3103 * not found, then go to the window just above the quickfix window. This is
3104 * used for opening a file from a quickfix window and not from a location
3105 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003106 */
3107 static void
3108qf_goto_win_with_qfl_file(int qf_fnum)
3109{
3110 win_T *win;
3111 win_T *altwin;
3112
3113 win = curwin;
3114 altwin = NULL;
3115 for (;;)
3116 {
3117 if (win->w_buffer->b_fnum == qf_fnum)
3118 break;
3119 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003120 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003121 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003122 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003123
3124 if (IS_QF_WINDOW(win))
3125 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003126 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003127 // window, unless 'switchbuf' contains 'uselast': in this case we
3128 // try to jump to the previously used window first.
3129 if ((swb_flags & SWB_USELAST) && win_valid(prevwin))
3130 win = prevwin;
3131 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003132 win = altwin;
3133 else if (curwin->w_prev != NULL)
3134 win = curwin->w_prev;
3135 else
3136 win = curwin->w_next;
3137 break;
3138 }
3139
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003140 // Remember a usable window.
Bram Moolenaar91335e52018-08-01 17:53:12 +02003141 if (altwin == NULL && !win->w_p_pvw && bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003142 altwin = win;
3143 }
3144
3145 win_goto(win);
3146}
3147
3148/*
3149 * Find a suitable window for opening a file (qf_fnum) from the
3150 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003151 * window, jump to it. Otherwise open a new window to display the file. If
3152 * 'newwin' is TRUE, then always open a new window. This is called from either
3153 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003154 */
3155 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003156qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003157{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003158 win_T *usable_wp = NULL;
3159 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003160 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003161
Bram Moolenaarb2443732018-11-11 22:50:27 +01003162 // If opening a new window, then don't use the location list referred by
3163 // the current window. Otherwise two windows will refer to the same
3164 // location list.
3165 if (!newwin)
3166 ll_ref = curwin->w_llist_ref;
3167
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003168 if (ll_ref != NULL)
3169 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003170 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003171 usable_wp = qf_find_win_with_loclist(ll_ref);
3172 if (usable_wp != NULL)
3173 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003174 }
3175
3176 if (!usable_win)
3177 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003178 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003179 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003180 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003181 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003182 }
3183
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003184 // If no usable window is found and 'switchbuf' contains "usetab"
3185 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003186 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003187 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003188
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003189 // If there is only one window and it is the quickfix window, create a
3190 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003191 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003192 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003193 if (qf_open_new_file_win(ll_ref) != OK)
3194 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003195 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003196 }
3197 else
3198 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003199 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003200 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003201 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003202 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003203 }
3204
3205 return OK;
3206}
3207
3208/*
3209 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003210 * Returns OK if successfully edited the file, FAIL on failing to open the
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003211 * buffer and QF_ABORT if the quickfix/location list was freed by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003212 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003213 */
3214 static int
3215qf_jump_edit_buffer(
3216 qf_info_T *qi,
3217 qfline_T *qf_ptr,
3218 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003219 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003220 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003221{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003222 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003223 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003224 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003225 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003226 int old_qf_curlist = qi->qf_curlist;
3227 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003228
3229 if (qf_ptr->qf_type == 1)
3230 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003231 // Open help file (do_ecmd() will set b_help flag, readfile() will
3232 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003233 if (!can_abandon(curbuf, forceit))
3234 {
3235 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003236 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003237 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003238
3239 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3240 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003241 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003242 }
3243 else
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003244 retval = buflist_getfile(qf_ptr->qf_fnum,
3245 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
Bram Moolenaar3c097222017-12-21 20:54:49 +01003246
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003247 // If a location list, check whether the associated window is still
3248 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003249 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003250 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003251 win_T *wp = win_id2wp(prev_winid);
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003252
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003253 if (wp == NULL && curwin->w_llist != qi)
3254 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003255 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003256 *opened_window = FALSE;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003257 return QF_ABORT;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003258 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003259 }
3260
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003261 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003262 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003263 emsg(_(e_current_quickfix_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003264 return QF_ABORT;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003265 }
3266
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003267 // Check if the list was changed. The pointers may happen to be identical,
3268 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003269 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003270 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003271 || !is_qf_entry_present(qfl, qf_ptr))
3272 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003273 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003274 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003275 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003276 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003277 return QF_ABORT;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003278 }
3279
3280 return retval;
3281}
3282
3283/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003284 * Go to the error line in the current file using either line/column number or
3285 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003286 */
3287 static void
3288qf_jump_goto_line(
3289 linenr_T qf_lnum,
3290 int qf_col,
3291 char_u qf_viscol,
3292 char_u *qf_pattern)
3293{
3294 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003295
3296 if (qf_pattern == NULL)
3297 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003298 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003299 i = qf_lnum;
3300 if (i > 0)
3301 {
3302 if (i > curbuf->b_ml.ml_line_count)
3303 i = curbuf->b_ml.ml_line_count;
3304 curwin->w_cursor.lnum = i;
3305 }
3306 if (qf_col > 0)
3307 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003308 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003309 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003310 coladvance(qf_col - 1);
3311 else
3312 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003313 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003314 check_cursor();
3315 }
3316 else
3317 beginline(BL_WHITE | BL_FIX);
3318 }
3319 else
3320 {
3321 pos_T save_cursor;
3322
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003323 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003324 save_cursor = curwin->w_cursor;
3325 curwin->w_cursor.lnum = 0;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003326 if (!do_search(NULL, '/', '/', qf_pattern, (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003327 curwin->w_cursor = save_cursor;
3328 }
3329}
3330
3331/*
3332 * Display quickfix list index and size message
3333 */
3334 static void
3335qf_jump_print_msg(
3336 qf_info_T *qi,
3337 int qf_index,
3338 qfline_T *qf_ptr,
3339 buf_T *old_curbuf,
3340 linenr_T old_lnum)
3341{
3342 linenr_T i;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003343 garray_T *gap;
3344
3345 gap = qfga_get();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003346
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003347 // Update the screen before showing the message, unless the screen
3348 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003349 if (!msg_scrolled)
3350 update_topline_redraw();
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003351 vim_snprintf((char *)IObuff, IOSIZE, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003352 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003353 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3354 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003355 // Add the message, skipping leading whitespace and newlines.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003356 ga_concat(gap, IObuff);
3357 qf_fmt_text(gap, skipwhite(qf_ptr->qf_text));
Shane Harper5bf04282023-06-07 19:09:57 +01003358 ga_append(gap, NUL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003359
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003360 // Output the message. Overwrite to avoid scrolling when the 'O'
3361 // flag is present in 'shortmess'; But when not jumping, print the
3362 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003363 i = msg_scroll;
3364 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3365 msg_scroll = TRUE;
3366 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3367 msg_scroll = FALSE;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003368 msg_attr_keep((char *)gap->ga_data, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003369 msg_scroll = i;
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01003370
3371 qfga_clear();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003372}
3373
3374/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003375 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003376 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3377 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003378 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3379 * able to jump/open a window. Returns NOTDONE if a file is not associated
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003380 * with the entry. Returns QF_ABORT if the quickfix/location list was modified
3381 * by an autocmd.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003382 */
3383 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003384qf_jump_open_window(
3385 qf_info_T *qi,
3386 qfline_T *qf_ptr,
3387 int newwin,
3388 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003389{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003390 qf_list_T *qfl = qf_get_curlist(qi);
3391 int old_changedtick = qfl->qf_changedtick;
3392 int old_qf_curlist = qi->qf_curlist;
3393 qfltype_T qfl_type = qfl->qfl_type;
3394
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003395 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003396 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3397 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003398 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003399 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003400 if (old_qf_curlist != qi->qf_curlist
3401 || old_changedtick != qfl->qf_changedtick
3402 || !is_qf_entry_present(qfl, qf_ptr))
3403 {
3404 if (qfl_type == QFLT_QUICKFIX)
3405 emsg(_(e_current_quickfix_list_was_changed));
3406 else
3407 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003408 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003409 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003410
3411 // If currently in the quickfix window, find another window to show the
3412 // file in.
3413 if (bt_quickfix(curbuf) && !*opened_window)
3414 {
3415 // If there is no file specified, we don't know where to go.
3416 // But do advance, otherwise ":cn" gets stuck.
3417 if (qf_ptr->qf_fnum == 0)
3418 return NOTDONE;
3419
Bram Moolenaarb2443732018-11-11 22:50:27 +01003420 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3421 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003422 return FAIL;
3423 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003424 if (old_qf_curlist != qi->qf_curlist
3425 || old_changedtick != qfl->qf_changedtick
3426 || !is_qf_entry_present(qfl, qf_ptr))
3427 {
3428 if (qfl_type == QFLT_QUICKFIX)
3429 emsg(_(e_current_quickfix_list_was_changed));
3430 else
3431 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003432 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003433 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003434
3435 return OK;
3436}
3437
3438/*
3439 * Edit a selected file from the quickfix/location list and jump to a
3440 * particular line/column, adjust the folds and display a message about the
3441 * jump.
3442 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003443 * QF_ABORT if the quickfix/location list is freed by an autocmd when opening
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003444 * the file.
3445 */
3446 static int
3447qf_jump_to_buffer(
3448 qf_info_T *qi,
3449 int qf_index,
3450 qfline_T *qf_ptr,
3451 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003452 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003453 int *opened_window,
3454 int openfold,
3455 int print_message)
3456{
3457 buf_T *old_curbuf;
3458 linenr_T old_lnum;
3459 int retval = OK;
3460
3461 // If there is a file name, read the wanted file if needed, and check
3462 // autowrite etc.
3463 old_curbuf = curbuf;
3464 old_lnum = curwin->w_cursor.lnum;
3465
3466 if (qf_ptr->qf_fnum != 0)
3467 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003468 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003469 opened_window);
3470 if (retval != OK)
3471 return retval;
3472 }
3473
3474 // When not switched to another buffer, still need to set pc mark
3475 if (curbuf == old_curbuf)
3476 setpcmark();
3477
3478 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3479 qf_ptr->qf_pattern);
3480
3481#ifdef FEAT_FOLDING
3482 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3483 foldOpenCursor();
3484#endif
3485 if (print_message)
3486 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3487
3488 return retval;
3489}
3490
3491/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003492 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 */
3494 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003495qf_jump(qf_info_T *qi,
3496 int dir,
3497 int errornr,
3498 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003500 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3501}
3502
3503/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003504 * Jump to a quickfix line.
3505 * If dir == 0 go to entry "errornr".
3506 * If dir == FORWARD go "errornr" valid entries forward.
3507 * If dir == BACKWARD go "errornr" valid entries backward.
3508 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3509 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3510 * else if "errornr" is zero, redisplay the same line
3511 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003512 * If 'newwin' is TRUE, then open the file in a new window.
3513 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003514 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003515qf_jump_newwin(qf_info_T *qi,
3516 int dir,
3517 int errornr,
3518 int forceit,
3519 int newwin)
3520{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003521 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003522 qfline_T *qf_ptr;
3523 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003526 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003527 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003528 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003531 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003532 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003534 if (qi == NULL)
3535 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003536
Bram Moolenaar0398e002019-03-21 21:12:49 +01003537 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003539 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 return;
3541 }
3542
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003543 incr_quickfix_busy();
3544
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003545 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003546
3547 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003549 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003551
3552 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3553 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003555 qf_ptr = old_qf_ptr;
3556 qf_index = old_qf_index;
3557 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003560 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003561 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003562 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003563 // No need to print the error message if it's visible in the error
3564 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 print_message = FALSE;
3566
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003567 prev_winid = curwin->w_id;
3568
Bram Moolenaarb2443732018-11-11 22:50:27 +01003569 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003570 if (retval == FAIL)
3571 goto failed;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003572 if (retval == QF_ABORT)
3573 {
3574 qi = NULL;
3575 qf_ptr = NULL;
3576 goto theend;
3577 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003578 if (retval == NOTDONE)
3579 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003580
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003581 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003582 &opened_window, old_KeyTyped, print_message);
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003583 if (retval == QF_ABORT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003585 // Quickfix/location list was modified by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003586 qi = NULL;
3587 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003590 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003593 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003594 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003596 // Couldn't open file, so put index back where it was. This could
3597 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 qf_ptr = old_qf_ptr;
3600 qf_index = old_qf_index;
3601 }
3602 }
3603theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003604 if (qi != NULL)
3605 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003606 qfl->qf_ptr = qf_ptr;
3607 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003608 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003609 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003611 // Restore old 'switchbuf' value, but not when an autocommand or
3612 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003613 p_swb = old_swb;
3614 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003616 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617}
3618
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003619// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003620static int qfFileAttr;
3621static int qfSepAttr;
3622static int qfLineAttr;
3623
3624/*
3625 * Display information about a single entry from the quickfix/location list.
3626 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003627 * 'cursel' will be set to TRUE for the currently selected entry in the
3628 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003629 */
3630 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003631qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003632{
3633 char_u *fname;
3634 buf_T *buf;
3635 int filter_entry;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003636 garray_T *gap;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003637
3638 fname = NULL;
3639 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3640 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3641 (char *)qfp->qf_module);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00003642 else
3643 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003644 if (qfp->qf_fnum != 0
3645 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3646 {
3647 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003648 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003649 fname = gettail(fname);
3650 }
3651 if (fname == NULL)
3652 sprintf((char *)IObuff, "%2d", qf_idx);
3653 else
3654 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3655 qf_idx, (char *)fname);
3656 }
3657
3658 // Support for filtering entries using :filter /pat/ clist
3659 // Match against the module name, file name, search pattern and
3660 // text of the entry.
3661 filter_entry = TRUE;
3662 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3663 filter_entry &= message_filtered(qfp->qf_module);
3664 if (filter_entry && fname != NULL)
3665 filter_entry &= message_filtered(fname);
3666 if (filter_entry && qfp->qf_pattern != NULL)
3667 filter_entry &= message_filtered(qfp->qf_pattern);
3668 if (filter_entry)
3669 filter_entry &= message_filtered(qfp->qf_text);
3670 if (filter_entry)
3671 return;
3672
3673 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003674 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003675
3676 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003677 msg_puts_attr(":", qfSepAttr);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003678 gap = qfga_get();
Shane Harper5bf04282023-06-07 19:09:57 +01003679 if (qfp->qf_lnum != 0)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003680 qf_range_text(gap, qfp);
3681 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
3682 ga_append(gap, NUL);
3683 msg_puts_attr((char *)gap->ga_data, qfLineAttr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003684 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003685 if (qfp->qf_pattern != NULL)
3686 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003687 gap = qfga_get();
3688 qf_fmt_text(gap, qfp->qf_pattern);
Shane Harper5bf04282023-06-07 19:09:57 +01003689 ga_append(gap, NUL);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003690 msg_puts((char *)gap->ga_data);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003691 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003692 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003693 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003694
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003695 // Remove newlines and leading whitespace from the text. For an
3696 // unrecognized line keep the indent, the compiler may mark a word
3697 // with ^^^^.
3698 gap = qfga_get();
3699 qf_fmt_text(gap, (fname != NULL || qfp->qf_lnum != 0)
Shane Harper5bf04282023-06-07 19:09:57 +01003700 ? skipwhite(qfp->qf_text) : qfp->qf_text);
3701 ga_append(gap, NUL);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003702 msg_prt_line((char_u *)gap->ga_data, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003703 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003704}
3705
3706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003708 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 */
3710 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003711qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003713 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003714 qfline_T *qfp;
3715 int i;
3716 int idx1 = 1;
3717 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003718 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003719 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003720 int all = eap->forceit; // if not :cl!, only show
3721 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003722 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003724 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3725 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003726
Bram Moolenaar0398e002019-03-21 21:12:49 +01003727 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003729 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 return;
3731 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003732 if (*arg == '+')
3733 {
3734 ++arg;
3735 plus = TRUE;
3736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3738 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003739 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 return;
3741 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003742 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003743 if (plus)
3744 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003745 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003746 idx2 = i + idx1;
3747 idx1 = i;
3748 }
3749 else
3750 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003751 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003752 if (idx1 < 0)
3753 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3754 if (idx2 < 0)
3755 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003758 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003759 shorten_fnames(FALSE);
3760
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003761 // Get the attributes for the different quickfix highlight items. Note
3762 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003763 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3764 if (qfFileAttr == 0)
3765 qfFileAttr = HL_ATTR(HLF_D);
3766 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3767 if (qfSepAttr == 0)
3768 qfSepAttr = HL_ATTR(HLF_D);
3769 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3770 if (qfLineAttr == 0)
3771 qfLineAttr = HL_ATTR(HLF_N);
3772
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003773 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003775 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 {
3777 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003778 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003779
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 ui_breakcheck();
3781 }
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01003782 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783}
3784
3785/*
3786 * Remove newlines and leading whitespace from an error message.
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003787 * Add the result to the grow array "gap".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 */
3789 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003790qf_fmt_text(garray_T *gap, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 char_u *p = text;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003793 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
3795 if (*p == '\n')
3796 {
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003797 ga_append(gap, ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003799 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 break;
3801 }
3802 else
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003803 ga_append(gap, *p++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805}
3806
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003807/*
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003808 * Add the range information from the lnum, col, end_lnum, and end_col values
3809 * of a quickfix entry to the grow array "gap".
thinca6864efa2021-06-19 20:45:20 +02003810 */
3811 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003812qf_range_text(garray_T *gap, qfline_T *qfp)
thinca6864efa2021-06-19 20:45:20 +02003813{
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003814 char_u *buf = IObuff;
3815 int bufsize = IOSIZE;
thinca6864efa2021-06-19 20:45:20 +02003816 int len;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003817
thinca6864efa2021-06-19 20:45:20 +02003818 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3819 len = (int)STRLEN(buf);
3820
3821 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3822 {
Shane Harper5bf04282023-06-07 19:09:57 +01003823 vim_snprintf((char *)buf + len, bufsize - len, "-%ld",
3824 qfp->qf_end_lnum);
thinca6864efa2021-06-19 20:45:20 +02003825 len += (int)STRLEN(buf + len);
3826 }
3827 if (qfp->qf_col > 0)
3828 {
3829 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3830 len += (int)STRLEN(buf + len);
3831 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3832 {
Shane Harper5bf04282023-06-07 19:09:57 +01003833 vim_snprintf((char *)buf + len, bufsize - len, "-%d",
3834 qfp->qf_end_col);
thinca6864efa2021-06-19 20:45:20 +02003835 len += (int)STRLEN(buf + len);
3836 }
3837 }
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003838
3839 ga_concat_len(gap, buf, len);
thinca6864efa2021-06-19 20:45:20 +02003840}
3841
3842/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003843 * Display information (list number, list size and the title) about a
3844 * quickfix/location list.
3845 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003846 static void
3847qf_msg(qf_info_T *qi, int which, char *lead)
3848{
3849 char *title = (char *)qi->qf_lists[which].qf_title;
3850 int count = qi->qf_lists[which].qf_count;
3851 char_u buf[IOSIZE];
3852
3853 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3854 lead,
3855 which + 1,
3856 qi->qf_listcount,
3857 count);
3858
3859 if (title != NULL)
3860 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003861 size_t len = STRLEN(buf);
3862
3863 if (len < 34)
3864 {
3865 vim_memset(buf + len, ' ', 34 - len);
3866 buf[34] = NUL;
3867 }
3868 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003869 }
3870 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003871 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003872}
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874/*
3875 * ":colder [count]": Up in the quickfix stack.
3876 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003877 * ":lolder [count]": Up in the location list stack.
3878 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 */
3880 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003881qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003883 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 int count;
3885
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003886 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3887 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (eap->addr_count != 0)
3890 count = eap->line2;
3891 else
3892 count = 1;
3893 while (count--)
3894 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003895 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003897 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003899 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003900 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003902 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
3904 else
3905 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003906 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003908 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003909 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003911 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
3913 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003914 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003915 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916}
3917
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003918/*
3919 * Display the information about all the quickfix/location lists in the stack
3920 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003921 void
3922qf_history(exarg_T *eap)
3923{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003924 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003925 int i;
3926
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003927 if (eap->addr_count > 0)
3928 {
3929 if (qi == NULL)
3930 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003931 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003932 return;
3933 }
3934
3935 // Jump to the specified quickfix list
3936 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
3937 {
3938 qi->qf_curlist = eap->line2 - 1;
3939 qf_msg(qi, qi->qf_curlist, "");
3940 qf_update_buffer(qi, NULL);
3941 }
3942 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003943 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003944
3945 return;
3946 }
3947
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003948 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01003949 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003950 else
3951 for (i = 0; i < qi->qf_listcount; ++i)
3952 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
3953}
3954
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003956 * Free all the entries in the error list "idx". Note that other information
3957 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 */
3959 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003960qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003962 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003963 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01003964 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003966 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003968 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003969 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003970 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003971 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02003972 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003973 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003974 vim_free(qfp->qf_pattern);
Tom Praschanca6ac992023-08-11 23:26:12 +02003975 clear_tv(&qfp->qf_user_data);
Bram Moolenaard76ce852018-05-01 15:02:04 +02003976 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02003977 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01003978 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003979 // Somehow qf_count may have an incorrect value, set it to 1
3980 // to avoid crashing when it's wrong.
3981 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003982 qfl->qf_count = 1;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01003983 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003984 qfl->qf_start = qfpnext;
3985 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02003987
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003988 qfl->qf_index = 0;
3989 qfl->qf_start = NULL;
3990 qfl->qf_last = NULL;
3991 qfl->qf_ptr = NULL;
3992 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02003993
Bram Moolenaara7df8c72017-07-19 13:23:06 +02003994 qf_clean_dir_stack(&qfl->qf_dir_stack);
3995 qfl->qf_directory = NULL;
3996 qf_clean_dir_stack(&qfl->qf_file_stack);
3997 qfl->qf_currfile = NULL;
3998 qfl->qf_multiline = FALSE;
3999 qfl->qf_multiignore = FALSE;
4000 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001}
4002
4003/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004004 * Free error list "idx". Frees all the entries in the quickfix list,
4005 * associated context information and the title.
4006 */
4007 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004008qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004009{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004010 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004011
Bram Moolenaard23a8232018-02-10 18:45:26 +01004012 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004013 free_tv(qfl->qf_ctx);
4014 qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004015 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004016 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004017 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004018}
4019
4020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 * qf_mark_adjust: adjust marks
4022 */
4023 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004024qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02004025 win_T *wp,
4026 linenr_T line1,
4027 linenr_T line2,
4028 long amount,
4029 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004031 int i;
4032 qfline_T *qfp;
4033 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004034 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004035 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02004036 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037
Bram Moolenaarc1542742016-07-20 21:44:37 +02004038 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004039 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004040 if (wp != NULL)
4041 {
4042 if (wp->w_llist == NULL)
4043 return;
4044 qi = wp->w_llist;
4045 }
4046
4047 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01004048 {
4049 qf_list_T *qfl = qf_get_list(qi, idx);
4050
4051 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01004052 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 if (qfp->qf_fnum == curbuf->b_fnum)
4054 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004055 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
4057 {
4058 if (amount == MAXLNUM)
4059 qfp->qf_cleared = TRUE;
4060 else
4061 qfp->qf_lnum += amount;
4062 }
4063 else if (amount_after && qfp->qf_lnum > line2)
4064 qfp->qf_lnum += amount_after;
4065 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01004066 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004067
4068 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02004069 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070}
4071
4072/*
4073 * Make a nice message out of the error character and the error number:
4074 * char number message
4075 * e or E 0 " error"
4076 * w or W 0 " warning"
4077 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004078 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 * 0 0 ""
4080 * other 0 " c"
4081 * e or E n " error n"
4082 * w or W n " warning n"
4083 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004084 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 * 0 n " error n"
4086 * other n " c n"
4087 * 1 x "" :helpgrep
4088 */
4089 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004090qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
4092 static char_u buf[20];
4093 static char_u cc[3];
4094 char_u *p;
4095
4096 if (c == 'W' || c == 'w')
4097 p = (char_u *)" warning";
4098 else if (c == 'I' || c == 'i')
4099 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004100 else if (c == 'N' || c == 'n')
4101 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4103 p = (char_u *)" error";
4104 else if (c == 0 || c == 1)
4105 p = (char_u *)"";
4106 else
4107 {
4108 cc[0] = ' ';
4109 cc[1] = c;
4110 cc[2] = NUL;
4111 p = cc;
4112 }
4113
4114 if (nr <= 0)
4115 return p;
4116
4117 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4118 return buf;
4119}
4120
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004122 * When "split" is FALSE: Open the entry/result under the cursor.
4123 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4124 */
4125 void
4126qf_view_result(int split)
4127{
4128 qf_info_T *qi = &ql_info;
4129
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004130 if (IS_LL_WINDOW(curwin))
4131 qi = GET_LOC_LIST(curwin);
4132
Bram Moolenaar0398e002019-03-21 21:12:49 +01004133 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004134 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004135 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004136 return;
4137 }
4138
4139 if (split)
4140 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004141 // Open the selected entry in a new window
4142 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4143 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004144 return;
4145 }
4146
4147 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4148}
4149
4150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 * ":cwindow": open the quickfix window if we have errors to display,
4152 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004153 * ":lwindow": open the location list window if we have locations to display,
4154 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 */
4156 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004157ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004159 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004160 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 win_T *win;
4162
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004163 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4164 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004165
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004166 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004167
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004168 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004169 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004171 // If a quickfix window is open but we have no errors to display,
4172 // close the window. If a quickfix window is not open, then open
4173 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004174 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004175 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004176 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
4178 if (win != NULL)
4179 ex_cclose(eap);
4180 }
4181 else if (win == NULL)
4182 ex_copen(eap);
4183}
4184
4185/*
4186 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004187 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004190ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004192 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004193 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004195 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4196 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004198 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004199 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 if (win != NULL)
4201 win_close(win, FALSE);
4202}
4203
4204/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004205 * Set "w:quickfix_title" if "qi" has a title.
4206 */
4207 static void
4208qf_set_title_var(qf_list_T *qfl)
4209{
4210 if (qfl->qf_title != NULL)
4211 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4212}
4213
4214/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004215 * Goto a quickfix or location list window (if present).
4216 * Returns OK if the window is found, FAIL otherwise.
4217 */
4218 static int
4219qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4220{
4221 win_T *win;
4222
4223 win = qf_find_win(qi);
4224 if (win == NULL)
4225 return FAIL;
4226
4227 win_goto(win);
4228 if (resize)
4229 {
4230 if (vertsplit)
4231 {
4232 if (sz != win->w_width)
4233 win_setwidth(sz);
4234 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004235 else if (sz != win->w_height && win->w_height
4236 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004237 win_setheight(sz);
4238 }
4239
4240 return OK;
4241}
4242
4243/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004244 * Set options for the buffer in the quickfix or location list window.
4245 */
4246 static void
4247qf_set_cwindow_options(void)
4248{
4249 // switch off 'swapfile'
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004250 set_option_value_give_err((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4251 set_option_value_give_err((char_u *)"bt",
4252 0L, (char_u *)"quickfix", OPT_LOCAL);
4253 set_option_value_give_err((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004254 RESET_BINDING(curwin);
4255#ifdef FEAT_DIFF
4256 curwin->w_p_diff = FALSE;
4257#endif
4258#ifdef FEAT_FOLDING
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004259 set_option_value_give_err((char_u *)"fdm", 0L, (char_u *)"manual",
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004260 OPT_LOCAL);
4261#endif
4262}
4263
4264/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004265 * Open a new quickfix or location list window, load the quickfix buffer and
4266 * set the appropriate options for the window.
4267 * Returns FAIL if the window could not be opened.
4268 */
4269 static int
4270qf_open_new_cwindow(qf_info_T *qi, int height)
4271{
4272 buf_T *qf_buf;
4273 win_T *oldwin = curwin;
4274 tabpage_T *prevtab = curtab;
4275 int flags = 0;
4276 win_T *win;
4277
4278 qf_buf = qf_find_buf(qi);
4279
4280 // The current window becomes the previous window afterwards.
4281 win = curwin;
4282
Bram Moolenaare1004402020-10-24 20:49:43 +02004283 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004284 // Create the new quickfix window at the very bottom, except when
4285 // :belowright or :aboveleft is used.
4286 win_goto(lastwin);
4287 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004288 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004289 flags = WSP_BELOW;
4290 flags |= WSP_NEWLOC;
4291 if (win_split(height, flags) == FAIL)
4292 return FAIL; // not enough room for window
4293 RESET_BINDING(curwin);
4294
4295 if (IS_LL_STACK(qi))
4296 {
4297 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004298 // location list stack from the window 'win'.
4299 curwin->w_llist_ref = qi;
4300 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004301 }
4302
4303 if (oldwin != curwin)
4304 oldwin = NULL; // don't store info when in another window
4305 if (qf_buf != NULL)
4306 {
4307 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004308 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004309 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004310 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004311 }
4312 else
4313 {
4314 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004315 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4316 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004317 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004318
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004319 // save the number of the new buffer
4320 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004321 }
4322
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004323 // Set the options for the quickfix buffer/window (if not already done)
4324 // Do this even if the quickfix buffer was already present, as an autocmd
4325 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004326 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004327 qf_set_cwindow_options();
4328
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004329 // Only set the height when still in the same tab page and there is no
4330 // window to the side.
4331 if (curtab == prevtab && curwin->w_width == Columns)
4332 win_setheight(height);
4333 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4334 if (win_valid(win))
4335 prevwin = win;
4336
4337 return OK;
4338}
4339
4340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004342 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 */
4344 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004345ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004347 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004348 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004350 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004351 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004353 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4354 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004355
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004356 incr_quickfix_busy();
4357
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 if (eap->addr_count != 0)
4359 height = eap->line2;
4360 else
4361 height = QF_WINHEIGHT;
4362
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004363 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364#ifdef FEAT_GUI
4365 need_mouse_correct = TRUE;
4366#endif
4367
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004368 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004369 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004370 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004371 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004372 if (status == FAIL)
4373 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004374 {
4375 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004376 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004379 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004380 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004381 // Save the current index here, as updating the quickfix buffer may free
4382 // the quickfix list
4383 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004384
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004385 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004386 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004388 decr_quickfix_busy();
4389
4390 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 curwin->w_cursor.col = 0;
4392 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004393 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394}
4395
4396/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004397 * Move the cursor in the quickfix window to "lnum".
4398 */
4399 static void
4400qf_win_goto(win_T *win, linenr_T lnum)
4401{
4402 win_T *old_curwin = curwin;
4403
4404 curwin = win;
4405 curbuf = win->w_buffer;
4406 curwin->w_cursor.lnum = lnum;
4407 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004408 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004409 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004410 update_topline(); // scroll to show the line
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004411 redraw_later(UPD_VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004412 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004413 curwin = old_curwin;
4414 curbuf = curwin->w_buffer;
4415}
4416
4417/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004418 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004419 */
4420 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004421ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004422{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004423 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004424 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004425
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004426 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4427 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004428
4429 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004430 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4431 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4432}
4433
4434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 * Return the number of the current entry (line number in the quickfix
4436 * window).
4437 */
4438 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004439qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004441 qf_info_T *qi = &ql_info;
4442
4443 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004444 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004445 qi = wp->w_llist_ref;
4446
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004447 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448}
4449
4450/*
4451 * Update the cursor position in the quickfix window to the current error.
4452 * Return TRUE if there is a quickfix window.
4453 */
4454 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004455qf_win_pos_update(
4456 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004457 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458{
4459 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004460 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004462 // Put the cursor on the current error in the quickfix window, so that
4463 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004464 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 if (win != NULL
4466 && qf_index <= win->w_buffer->b_ml.ml_line_count
4467 && old_qf_index != qf_index)
4468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 if (qf_index > old_qf_index)
4470 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004471 win->w_redraw_top = old_qf_index;
4472 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 else
4475 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004476 win->w_redraw_top = qf_index;
4477 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004479 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 }
4481 return win != NULL;
4482}
4483
4484/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004485 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004486 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004487 */
4488 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004489is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004490{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004491 // A window displaying the quickfix buffer will have the w_llist_ref field
4492 // set to NULL.
4493 // A window displaying a location list buffer will have the w_llist_ref
4494 // pointing to the location list.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004495 if (bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004496 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4497 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004498 return TRUE;
4499
4500 return FALSE;
4501}
4502
4503/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004504 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4505 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004506 */
4507 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004508qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004509{
4510 win_T *win;
4511
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004512 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004513 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004514 return win;
4515 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004516}
4517
4518/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004519 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004520 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 */
4522 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004523qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004525 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004526 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004528 if (qi->qf_bufnr != INVALID_QFBUFNR)
4529 {
4530 buf_T *qfbuf;
4531 qfbuf = buflist_findnr(qi->qf_bufnr);
4532 if (qfbuf != NULL)
4533 return qfbuf;
4534 // buffer is no longer present
4535 qi->qf_bufnr = INVALID_QFBUFNR;
4536 }
4537
Bram Moolenaar9c102382006-05-03 21:26:49 +00004538 FOR_ALL_TAB_WINDOWS(tp, win)
4539 if (is_qf_win(win, qi))
4540 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004541
Bram Moolenaar9c102382006-05-03 21:26:49 +00004542 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543}
4544
4545/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004546 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004547 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004548 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004549 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004550did_set_quickfixtextfunc(optset_T *args UNUSED)
Bram Moolenaard43906d2020-07-20 21:31:32 +02004551{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004552 if (option_set_callback_func(p_qftf, &qftf_cb) == FAIL)
4553 return e_invalid_argument;
4554
4555 return NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004556}
4557
4558/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004559 * Update the w:quickfix_title variable in the quickfix/location list window in
4560 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004561 */
4562 static void
4563qf_update_win_titlevar(qf_info_T *qi)
4564{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004565 qf_list_T *qfl = qf_get_curlist(qi);
4566 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004567 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004568 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004569
Bram Moolenaar530bed92020-12-16 21:02:56 +01004570 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004571 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004572 if (is_qf_win(win, qi))
4573 {
4574 curwin = win;
4575 qf_set_title_var(qfl);
4576 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004577 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004578 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004579}
4580
4581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 * Find the quickfix buffer. If it exists, update the contents.
4583 */
4584 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004585qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586{
4587 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004588 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004591 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004592 buf = qf_find_buf(qi);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004593 if (buf == NULL)
4594 return;
4595
4596 linenr_T old_line_count = buf->b_ml.ml_line_count;
4597 int qf_winid = 0;
4598
4599 if (IS_LL_STACK(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004601 if (curwin->w_llist == qi)
4602 win = curwin;
4603 else
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004604 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004605 // Find the file window (non-quickfix) with this location list
4606 win = qf_find_win_with_loclist(qi);
4607 if (win == NULL)
4608 // File window is not found. Find the location list window.
4609 win = qf_find_win(qi);
4610 if (win == NULL)
4611 return;
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004612 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004613 qf_winid = win->w_id;
4614 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004615
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004616 // autocommands may cause trouble
4617 incr_quickfix_busy();
Bram Moolenaard0fab102022-10-20 16:03:33 +01004618
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004619 int do_fill = TRUE;
4620 if (old_last == NULL)
4621 {
4622 // set curwin/curbuf to buf and save a few things
4623 aucmd_prepbuf(&aco, buf);
4624 if (curbuf != buf)
4625 do_fill = FALSE; // failed to find a window for "buf"
4626 }
4627
4628 if (do_fill)
4629 {
4630 qf_update_win_titlevar(qi);
4631
4632 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
4633 ++CHANGEDTICK(buf);
4634
Bram Moolenaar864293a2016-06-02 13:40:04 +02004635 if (old_last == NULL)
4636 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004637 (void)qf_win_pos_update(qi, 0);
4638
4639 // restore curwin/curbuf and a few other things
4640 aucmd_restbuf(&aco);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004643
4644 // Only redraw when added lines are visible. This avoids flickering
4645 // when the added lines are not visible.
4646 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4647 redraw_buf_later(buf, UPD_NOT_VALID);
4648
4649 // always called after incr_quickfix_busy()
4650 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651}
4652
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004653/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004654 * Add an error line to the quickfix buffer.
4655 */
4656 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004657qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004658 buf_T *buf, // quickfix window buffer
4659 linenr_T lnum,
4660 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004661 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004662 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004663 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004664{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004665 buf_T *errbuf;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004666 garray_T *gap;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004667
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004668 gap = qfga_get();
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004669
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004670 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004671 // for this entry, then use it.
4672 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004673 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004674 ga_concat(gap, qftf_str);
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004675 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004676 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004677 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004678 if (qfp->qf_module != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004679 ga_concat(gap, qfp->qf_module);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004680 else if (qfp->qf_fnum != 0
4681 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4682 && errbuf->b_fname != NULL)
4683 {
4684 if (qfp->qf_type == 1) // :helpgrep
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004685 ga_concat(gap, gettail(errbuf->b_fname));
Bram Moolenaar858ba062020-05-31 23:11:59 +02004686 else
4687 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004688 // Shorten the file name if not done already.
4689 // For optimization, do this only for the first entry in a
4690 // buffer.
4691 if (first_bufline && (errbuf->b_sfname == NULL
4692 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004693 {
4694 if (*dirname == NUL)
4695 mch_dirname(dirname, MAXPATHL);
4696 shorten_buf_fname(errbuf, dirname, FALSE);
4697 }
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004698 ga_concat(gap, errbuf->b_fname);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004699 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004700 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004701
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004702 ga_append(gap, '|');
Bram Moolenaar858ba062020-05-31 23:11:59 +02004703
4704 if (qfp->qf_lnum > 0)
4705 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004706 qf_range_text(gap, qfp);
4707 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004708 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004709 else if (qfp->qf_pattern != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004710 qf_fmt_text(gap, qfp->qf_pattern);
4711 ga_append(gap, '|');
4712 ga_append(gap, ' ');
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004713
Bram Moolenaar858ba062020-05-31 23:11:59 +02004714 // Remove newlines and leading whitespace from the text.
4715 // For an unrecognized line keep the indent, the compiler may
4716 // mark a word with ^^^^.
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004717 qf_fmt_text(gap, gap->ga_len > 3 ? skipwhite(qfp->qf_text)
4718 : qfp->qf_text);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004719 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004720
Shane Harper5bf04282023-06-07 19:09:57 +01004721 ga_append(gap, NUL);
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004722 if (ml_append_buf(buf, lnum, gap->ga_data, gap->ga_len, FALSE) == FAIL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004723 return FAIL;
4724
4725 return OK;
4726}
4727
Bram Moolenaard43906d2020-07-20 21:31:32 +02004728/*
4729 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4730 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4731 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004732 static list_T *
4733call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4734{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004735 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004736 list_T *qftf_list = NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004737 static int recursive = FALSE;
4738
4739 if (recursive)
4740 return NULL; // this doesn't work properly recursively
4741 recursive = TRUE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004742
4743 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4744 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4745 // set.
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004746 if (qfl->qf_qftf_cb.cb_name != NULL)
4747 cb = &qfl->qf_qftf_cb;
4748 if (cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004749 {
4750 typval_T args[1];
4751 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004752 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004753
4754 // create the dict argument
4755 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
Bram Moolenaard6c67622022-08-24 20:07:22 +01004756 {
4757 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004758 return NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004759 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004760 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4761 dict_add_number(d, "winid", (long)qf_winid);
4762 dict_add_number(d, "id", (long)qfl->qf_id);
4763 dict_add_number(d, "start_idx", start_idx);
4764 dict_add_number(d, "end_idx", end_idx);
4765 ++d->dv_refcount;
4766 args[0].v_type = VAR_DICT;
4767 args[0].vval.v_dict = d;
4768
Bram Moolenaard43906d2020-07-20 21:31:32 +02004769 qftf_list = NULL;
4770 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4771 {
4772 if (rettv.v_type == VAR_LIST)
4773 {
4774 qftf_list = rettv.vval.v_list;
4775 qftf_list->lv_refcount++;
4776 }
4777 clear_tv(&rettv);
4778 }
4779 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004780 }
4781
Bram Moolenaard6c67622022-08-24 20:07:22 +01004782 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004783 return qftf_list;
4784}
4785
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 * Fill current buffer with quickfix errors, replacing any previous contents.
4788 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004789 * If "old_last" is not NULL append the items after this one.
4790 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4791 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 */
4793 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004794qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004796 linenr_T lnum;
4797 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004798 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004799 list_T *qftf_list = NULL;
4800 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
Bram Moolenaar864293a2016-06-02 13:40:04 +02004802 if (old_last == NULL)
4803 {
4804 if (buf != curbuf)
4805 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004806 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004807 return;
4808 }
4809
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004810 // delete all existing lines
Bram Moolenaar864293a2016-06-02 13:40:04 +02004811 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004812 (void)ml_delete((linenr_T)1);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004815 // Check if there is anything to display
Bram Moolenaar4f1b0832022-08-29 20:45:16 +01004816 if (qfl != NULL && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004818 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004819 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004820 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004821
4822 *dirname = NUL;
4823
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004824 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004825 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004826 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004827 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004828 lnum = 0;
4829 }
4830 else
4831 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004832 if (old_last->qf_next != NULL)
4833 qfp = old_last->qf_next;
4834 else
4835 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004836 lnum = buf->b_ml.ml_line_count;
4837 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004838
4839 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4840 (long)qfl->qf_count);
4841 if (qftf_list != NULL)
4842 qftf_li = qftf_list->lv_first;
4843
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004844 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004846 char_u *qftf_str = NULL;
4847
Bram Moolenaard43906d2020-07-20 21:31:32 +02004848 // Use the text supplied by the user defined function (if any).
4849 // If the returned value is not string, then ignore the rest
4850 // of the returned values and use the default.
4851 if (qftf_li != NULL && !invalid_val)
4852 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004853 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004854 if (qftf_str == NULL)
4855 invalid_val = TRUE;
4856 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004857
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004858 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4859 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004861
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004862 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004863 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004865 if (qfp == NULL)
4866 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004867
4868 if (qftf_li != NULL)
4869 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004871
4872 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004873 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004874 (void)ml_delete(lnum + 1);
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01004875
4876 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004879 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 check_lnums(TRUE);
4881
Bram Moolenaar864293a2016-06-02 13:40:04 +02004882 if (old_last == NULL)
4883 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004884 // Set the 'filetype' to "qf" each time after filling the buffer.
4885 // This resembles reading a file into a buffer, it's more logical when
4886 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004887 ++curbuf_lock;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004888 set_option_value_give_err((char_u *)"ft",
4889 0L, (char_u *)"qf", OPT_LOCAL);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004890 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004892 keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004893 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004895 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004897 keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004898 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004899
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004900 // make sure it will be redrawn
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004901 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004904 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 KeyTyped = old_KeyTyped;
4906}
4907
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004908/*
4909 * For every change made to the quickfix list, update the changed tick.
4910 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004911 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004912qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004913{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004914 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004915}
4916
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004918 * Return the quickfix/location list number with the given identifier.
4919 * Returns -1 if list is not found.
4920 */
4921 static int
4922qf_id2nr(qf_info_T *qi, int_u qfid)
4923{
4924 int qf_idx;
4925
4926 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4927 if (qi->qf_lists[qf_idx].qf_id == qfid)
4928 return qf_idx;
4929 return INVALID_QFIDX;
4930}
4931
4932/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004933 * If the current list is not "save_qfid" and we can find the list with that ID
4934 * then make it the current list.
4935 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004936 * Returns OK if successfully restored the list. Returns FAIL if the list with
4937 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004938 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004939 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004940qf_restore_list(qf_info_T *qi, int_u save_qfid)
4941{
4942 int curlist;
4943
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004944 if (qf_get_curlist(qi)->qf_id == save_qfid)
4945 return OK;
4946
4947 curlist = qf_id2nr(qi, save_qfid);
4948 if (curlist < 0)
4949 // list is not present
4950 return FAIL;
4951 qi->qf_curlist = curlist;
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004952 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02004953}
4954
4955/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004956 * Jump to the first entry if there is one.
4957 */
4958 static void
4959qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
4960{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004961 if (qf_restore_list(qi, save_qfid) == FAIL)
4962 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004963
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02004964 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01004965 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02004966 qf_jump(qi, 0, 0, forceit);
4967}
4968
4969/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00004970 * Return TRUE when using ":vimgrep" for ":grep".
4971 */
4972 int
Bram Moolenaar05540972016-01-30 20:31:25 +01004973grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004974{
Bram Moolenaar754b5602006-02-09 23:53:20 +00004975 return ((cmdidx == CMD_grep
4976 || cmdidx == CMD_lgrep
4977 || cmdidx == CMD_grepadd
4978 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004979 && STRCMP("internal",
4980 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
4981}
4982
4983/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004984 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004986 static char_u *
4987make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004989 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004990 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02004991 case CMD_make: return (char_u *)"make";
4992 case CMD_lmake: return (char_u *)"lmake";
4993 case CMD_grep: return (char_u *)"grep";
4994 case CMD_lgrep: return (char_u *)"lgrep";
4995 case CMD_grepadd: return (char_u *)"grepadd";
4996 case CMD_lgrepadd: return (char_u *)"lgrepadd";
4997 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02004998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999}
5000
5001/*
5002 * Return the name for the errorfile, in allocated memory.
5003 * Find a new unique name when 'makeef' contains "##".
5004 * Returns NULL for error.
5005 */
5006 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01005007get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008{
5009 char_u *p;
5010 char_u *name;
5011 static int start = -1;
5012 static int off = 0;
5013#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02005014 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015#endif
5016
5017 if (*p_mef == NUL)
5018 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005019 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005021 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 return name;
5023 }
5024
5025 for (p = p_mef; *p; ++p)
5026 if (p[0] == '#' && p[1] == '#')
5027 break;
5028
5029 if (*p == NUL)
5030 return vim_strsave(p_mef);
5031
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005032 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 for (;;)
5034 {
5035 if (start == -1)
5036 start = mch_get_pid();
5037 else
5038 off += 19;
5039
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005040 name = alloc_id(STRLEN(p_mef) + 30, aid_qf_mef_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 if (name == NULL)
5042 break;
5043 STRCPY(name, p_mef);
5044 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
5045 STRCAT(name, p + 2);
5046 if (mch_getperm(name) < 0
5047#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005048 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 && mch_lstat((char *)name, &sb) < 0
5050#endif
5051 )
5052 break;
5053 vim_free(name);
5054 }
5055 return name;
5056}
5057
5058/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005059 * Form the complete command line to invoke 'make'/'grep'. Quote the command
5060 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
5061 */
5062 static char_u *
5063make_get_fullcmd(char_u *makecmd, char_u *fname)
5064{
5065 char_u *cmd;
5066 unsigned len;
5067
5068 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
5069 if (*p_sp != NUL)
5070 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005071 cmd = alloc_id(len, aid_qf_makecmd);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005072 if (cmd == NULL)
5073 return NULL;
5074 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
5075 (char *)p_shq);
5076
5077 // If 'shellpipe' empty: don't redirect to 'errorfile'.
5078 if (*p_sp != NUL)
5079 append_redir(cmd, len, p_sp, fname);
5080
5081 // Display the fully formed command. Output a newline if there's something
5082 // else than the :make command that was typed (in which case the cursor is
5083 // in column 0).
5084 if (msg_col == 0)
5085 msg_didout = FALSE;
5086 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005087 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005088 msg_outtrans(cmd); // show what we are doing
5089
5090 return cmd;
5091}
5092
5093/*
5094 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5095 */
5096 void
5097ex_make(exarg_T *eap)
5098{
5099 char_u *fname;
5100 char_u *cmd;
5101 char_u *enc = NULL;
5102 win_T *wp = NULL;
5103 qf_info_T *qi = &ql_info;
5104 int res;
5105 char_u *au_name = NULL;
5106 int_u save_qfid;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005107 char_u *errorformat = p_efm;
5108 int newlist = TRUE;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005109
5110 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5111 if (grep_internal(eap->cmdidx))
5112 {
5113 ex_vimgrep(eap);
5114 return;
5115 }
5116
5117 au_name = make_get_auname(eap->cmdidx);
5118 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5119 curbuf->b_fname, TRUE, curbuf))
5120 {
5121#ifdef FEAT_EVAL
5122 if (aborting())
5123 return;
5124#endif
5125 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005126 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005127
5128 if (is_loclist_cmd(eap->cmdidx))
5129 wp = curwin;
5130
5131 autowrite_all();
5132 fname = get_mef_name();
5133 if (fname == NULL)
5134 return;
5135 mch_remove(fname); // in case it's not unique
5136
5137 cmd = make_get_fullcmd(eap->arg, fname);
5138 if (cmd == NULL)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005139 {
5140 vim_free(fname);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005141 return;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005142 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005143
5144 // let the shell know if we are redirecting output or not
5145 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5146
5147#ifdef AMIGA
5148 out_flush();
5149 // read window status report and redraw before message
5150 (void)char_avail();
5151#endif
5152
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005153 incr_quickfix_busy();
5154
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005155 if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake)
5156 errorformat = p_gefm;
5157 if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
5158 newlist = FALSE;
5159
5160 res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep),
5161 enc);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005162 if (wp != NULL)
5163 {
5164 qi = GET_LOC_LIST(wp);
5165 if (qi == NULL)
5166 goto cleanup;
5167 }
5168 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005169 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005170
5171 // Remember the current quickfix list identifier, so that we can
5172 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005173 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005174 if (au_name != NULL)
5175 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5176 curbuf->b_fname, TRUE, curbuf);
5177 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5178 // display the first error
5179 qf_jump_first(qi, save_qfid, FALSE);
5180
5181cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005182 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005183 mch_remove(fname);
5184 vim_free(fname);
5185 vim_free(cmd);
5186}
5187
5188/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005189 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005190 */
5191 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005192qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005193{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005194 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005195
5196 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5197 return 0;
5198 return qf_get_curlist(qi)->qf_count;
5199}
5200
5201/*
5202 * Returns the number of valid entries in the current quickfix/location list.
5203 */
5204 int
5205qf_get_valid_size(exarg_T *eap)
5206{
5207 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005208 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005209 qfline_T *qfp;
5210 int i, sz = 0;
5211 int prev_fnum = 0;
5212
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005213 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5214 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005215
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005216 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005217 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005218 {
5219 if (qfp->qf_valid)
5220 {
5221 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005222 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005223 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5224 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005225 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005226 sz++;
5227 prev_fnum = qfp->qf_fnum;
5228 }
5229 }
5230 }
5231
5232 return sz;
5233}
5234
5235/*
5236 * Returns the current index of the quickfix/location list.
5237 * Returns 0 if there is an error.
5238 */
5239 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005240qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005241{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005242 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005243
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005244 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5245 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005246
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005247 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005248}
5249
5250/*
5251 * Returns the current index in the quickfix/location list (counting only valid
5252 * entries). If no valid entries are in the list, then returns 1.
5253 */
5254 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005255qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005256{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005257 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005258 qf_list_T *qfl;
5259 qfline_T *qfp;
5260 int i, eidx = 0;
5261 int prev_fnum = 0;
5262
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005263 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5264 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005265
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005266 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005267 qfp = qfl->qf_start;
5268
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005269 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005270 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005271 return 1;
5272
5273 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5274 {
5275 if (qfp->qf_valid)
5276 {
5277 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5278 {
5279 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5280 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005281 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005282 eidx++;
5283 prev_fnum = qfp->qf_fnum;
5284 }
5285 }
5286 else
5287 eidx++;
5288 }
5289 }
5290
5291 return eidx ? eidx : 1;
5292}
5293
5294/*
5295 * Get the 'n'th valid error entry in the quickfix or location list.
5296 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5297 * For :cdo and :ldo returns the 'n'th valid error entry.
5298 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5299 */
5300 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005301qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005302{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005303 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005304 int i, eidx;
5305 int prev_fnum = 0;
5306
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005307 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005308 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005309 return 1;
5310
Bram Moolenaar95946f12019-03-31 15:31:59 +02005311 eidx = 0;
5312 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005313 {
5314 if (qfp->qf_valid)
5315 {
5316 if (fdo)
5317 {
5318 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5319 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005320 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005321 eidx++;
5322 prev_fnum = qfp->qf_fnum;
5323 }
5324 }
5325 else
5326 eidx++;
5327 }
5328
5329 if (eidx == n)
5330 break;
5331 }
5332
5333 if (i <= qfl->qf_count)
5334 return i;
5335 else
5336 return 1;
5337}
5338
5339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005341 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005342 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 */
5344 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005345ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005347 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005348 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005349
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005350 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5351 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005352
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005353 if (eap->addr_count > 0)
5354 errornr = (int)eap->line2;
5355 else
5356 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005357 switch (eap->cmdidx)
5358 {
5359 case CMD_cc: case CMD_ll:
5360 errornr = 0;
5361 break;
5362 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5363 case CMD_lfirst:
5364 errornr = 1;
5365 break;
5366 default:
5367 errornr = 32767;
5368 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005369 }
5370
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005371 // For cdo and ldo commands, jump to the nth valid error.
5372 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005373 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5374 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005375 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005376 eap->addr_count > 0 ? (int)eap->line1 : 1,
5377 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5378
5379 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380}
5381
5382/*
5383 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005384 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005385 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 */
5387 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005388ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005390 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005391 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005392 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005393
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005394 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5395 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005396
Bram Moolenaar55b69262017-08-13 13:42:01 +02005397 if (eap->addr_count > 0
5398 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5399 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005400 errornr = (int)eap->line2;
5401 else
5402 errornr = 1;
5403
Bram Moolenaar39665952018-08-15 20:59:48 +02005404 // Depending on the command jump to either next or previous entry/file.
5405 switch (eap->cmdidx)
5406 {
5407 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5408 dir = FORWARD;
5409 break;
5410 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5411 case CMD_lNext:
5412 dir = BACKWARD;
5413 break;
5414 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5415 dir = FORWARD_FILE;
5416 break;
5417 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5418 dir = BACKWARD_FILE;
5419 break;
5420 default:
5421 dir = FORWARD;
5422 break;
5423 }
5424
5425 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426}
5427
5428/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005429 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5430 * The index of the entry is stored in 'errornr'.
5431 * Returns NULL if an entry is not found.
5432 */
5433 static qfline_T *
5434qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5435{
5436 qfline_T *qfp = NULL;
5437 int idx = 0;
5438
5439 // Find the first entry in this file
5440 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5441 if (qfp->qf_fnum == bnr)
5442 break;
5443
5444 *errornr = idx;
5445 return qfp;
5446}
5447
5448/*
5449 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5450 * with the error number for the first entry. Assumes the entries are sorted in
5451 * the quickfix list by line number.
5452 */
5453 static qfline_T *
5454qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5455{
5456 while (!got_int
5457 && entry->qf_prev != NULL
5458 && entry->qf_fnum == entry->qf_prev->qf_fnum
5459 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5460 {
5461 entry = entry->qf_prev;
5462 --*errornr;
5463 }
5464
5465 return entry;
5466}
5467
5468/*
5469 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5470 * with the error number for the last entry. Assumes the entries are sorted in
5471 * the quickfix list by line number.
5472 */
5473 static qfline_T *
5474qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5475{
5476 while (!got_int &&
5477 entry->qf_next != NULL
5478 && entry->qf_fnum == entry->qf_next->qf_fnum
5479 && entry->qf_lnum == entry->qf_next->qf_lnum)
5480 {
5481 entry = entry->qf_next;
5482 ++*errornr;
5483 }
5484
5485 return entry;
5486}
5487
5488/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005489 * Returns TRUE if the specified quickfix entry is
5490 * after the given line (linewise is TRUE)
5491 * or after the line and column.
5492 */
5493 static int
5494qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5495{
5496 if (linewise)
5497 return qfp->qf_lnum > pos->lnum;
5498 else
5499 return (qfp->qf_lnum > pos->lnum ||
5500 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5501}
5502
5503/*
5504 * Returns TRUE if the specified quickfix entry is
5505 * before the given line (linewise is TRUE)
5506 * or before the line and column.
5507 */
5508 static int
5509qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5510{
5511 if (linewise)
5512 return qfp->qf_lnum < pos->lnum;
5513 else
5514 return (qfp->qf_lnum < pos->lnum ||
5515 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5516}
5517
5518/*
5519 * Returns TRUE if the specified quickfix entry is
5520 * on or after the given line (linewise is TRUE)
5521 * or on or after the line and column.
5522 */
5523 static int
5524qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5525{
5526 if (linewise)
5527 return qfp->qf_lnum >= pos->lnum;
5528 else
5529 return (qfp->qf_lnum > pos->lnum ||
5530 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5531}
5532
5533/*
5534 * Returns TRUE if the specified quickfix entry is
5535 * on or before the given line (linewise is TRUE)
5536 * or on or before the line and column.
5537 */
5538 static int
5539qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5540{
5541 if (linewise)
5542 return qfp->qf_lnum <= pos->lnum;
5543 else
5544 return (qfp->qf_lnum < pos->lnum ||
5545 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5546}
5547
5548/*
5549 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5550 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5551 * multiple entries on a single line as one. Otherwise returns the entry after
5552 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005553 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5554 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005555 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005556 */
5557 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005558qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005559 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005560 pos_T *pos,
5561 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005562 qfline_T *qfp,
5563 int *errornr)
5564{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005565 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005566 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005567 return qfp;
5568
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005569 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005570 while (qfp->qf_next != NULL
5571 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005572 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005573 {
5574 qfp = qfp->qf_next;
5575 ++*errornr;
5576 }
5577
5578 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005579 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005580 return NULL;
5581
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005582 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005583 qfp = qfp->qf_next;
5584 ++*errornr;
5585
5586 return qfp;
5587}
5588
5589/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005590 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5591 * If 'linewise' is TRUE, returns the entry before the specified line and
5592 * treats multiple entries on a single line as one. Otherwise returns the entry
5593 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005594 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5595 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005596 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005597 */
5598 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005599qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005600 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005601 pos_T *pos,
5602 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005603 qfline_T *qfp,
5604 int *errornr)
5605{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005606 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005607 while (qfp->qf_next != NULL
5608 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005609 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005610 {
5611 qfp = qfp->qf_next;
5612 ++*errornr;
5613 }
5614
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005615 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005616 return NULL;
5617
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005618 if (linewise)
5619 // If multiple entries are on the same line, then use the first entry
5620 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005621
5622 return qfp;
5623}
5624
5625/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005626 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005627 * the direction 'dir'.
5628 */
5629 static qfline_T *
5630qf_find_closest_entry(
5631 qf_list_T *qfl,
5632 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005633 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005634 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005635 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005636 int *errornr)
5637{
5638 qfline_T *qfp;
5639
5640 *errornr = 0;
5641
5642 // Find the first entry in this file
5643 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5644 if (qfp == NULL)
5645 return NULL; // no entry in this file
5646
5647 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005648 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005649 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005650 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005651
5652 return qfp;
5653}
5654
5655/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005656 * Get the nth quickfix entry below the specified entry. Searches forward in
5657 * the list. If linewise is TRUE, then treat multiple entries on a single line
5658 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005659 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005660 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005661qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005662{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005663 qfline_T *entry = entry_arg;
5664
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005665 while (n-- > 0 && !got_int)
5666 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005667 int first_errornr = *errornr;
5668
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005669 if (linewise)
5670 // Treat all the entries on the same line in this file as one
5671 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005672
5673 if (entry->qf_next == NULL
5674 || entry->qf_next->qf_fnum != entry->qf_fnum)
5675 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005676 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005677 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005678 break;
5679 }
5680
5681 entry = entry->qf_next;
5682 ++*errornr;
5683 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005684}
5685
5686/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005687 * Get the nth quickfix entry above the specified entry. Searches backwards in
5688 * the list. If linewise is TRUE, then treat multiple entries on a single line
5689 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005690 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005691 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005692qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005693{
5694 while (n-- > 0 && !got_int)
5695 {
5696 if (entry->qf_prev == NULL
5697 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5698 break;
5699
5700 entry = entry->qf_prev;
5701 --*errornr;
5702
5703 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005704 if (linewise)
5705 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005706 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005707}
5708
5709/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005710 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5711 * the specified direction. Returns the error number in the quickfix list or 0
5712 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005713 */
5714 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005715qf_find_nth_adj_entry(
5716 qf_list_T *qfl,
5717 int bnr,
5718 pos_T *pos,
5719 int n,
5720 int dir,
5721 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005722{
5723 qfline_T *adj_entry;
5724 int errornr;
5725
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005726 // Find an entry closest to the specified position
5727 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005728 if (adj_entry == NULL)
5729 return 0;
5730
5731 if (--n > 0)
5732 {
5733 // Go to the n'th entry in the current buffer
5734 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005735 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005736 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005737 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005738 }
5739
5740 return errornr;
5741}
5742
5743/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005744 * Jump to a quickfix entry in the current file nearest to the current line or
5745 * current line/col.
5746 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5747 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005748 */
5749 void
5750ex_cbelow(exarg_T *eap)
5751{
5752 qf_info_T *qi;
5753 qf_list_T *qfl;
5754 int dir;
5755 int buf_has_flag;
5756 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005757 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005758
5759 if (eap->addr_count > 0 && eap->line2 <= 0)
5760 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005761 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005762 return;
5763 }
5764
5765 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005766 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5767 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005768 buf_has_flag = BUF_HAS_QF_ENTRY;
5769 else
5770 buf_has_flag = BUF_HAS_LL_ENTRY;
5771 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5772 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005773 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005774 return;
5775 }
5776
5777 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5778 return;
5779
5780 qfl = qf_get_curlist(qi);
5781 // check if the list has valid errors
5782 if (!qf_list_has_valid_entries(qfl))
5783 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005784 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005785 return;
5786 }
5787
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005788 if (eap->cmdidx == CMD_cbelow
5789 || eap->cmdidx == CMD_lbelow
5790 || eap->cmdidx == CMD_cafter
5791 || eap->cmdidx == CMD_lafter)
5792 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005793 dir = FORWARD;
5794 else
5795 dir = BACKWARD;
5796
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005797 pos = curwin->w_cursor;
5798 // A quickfix entry column number is 1 based whereas cursor column
5799 // number is 0 based. Adjust the column number.
5800 pos.col++;
5801 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5802 eap->addr_count > 0 ? eap->line2 : 0, dir,
5803 eap->cmdidx == CMD_cbelow
5804 || eap->cmdidx == CMD_lbelow
5805 || eap->cmdidx == CMD_cabove
5806 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005807
5808 if (errornr > 0)
5809 qf_jump(qi, 0, errornr, FALSE);
5810 else
5811 emsg(_(e_no_more_items));
5812}
5813
5814/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005815 * Return the autocmd name for the :cfile Ex commands
5816 */
5817 static char_u *
5818cfile_get_auname(cmdidx_T cmdidx)
5819{
5820 switch (cmdidx)
5821 {
5822 case CMD_cfile: return (char_u *)"cfile";
5823 case CMD_cgetfile: return (char_u *)"cgetfile";
5824 case CMD_caddfile: return (char_u *)"caddfile";
5825 case CMD_lfile: return (char_u *)"lfile";
5826 case CMD_lgetfile: return (char_u *)"lgetfile";
5827 case CMD_laddfile: return (char_u *)"laddfile";
5828 default: return NULL;
5829 }
5830}
5831
5832/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005833 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005834 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 */
5836 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005837ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005839 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005840 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005841 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005842 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005843 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005844 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005845
Bram Moolenaara16123a2019-03-28 20:31:07 +01005846 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005847 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5848 NULL, FALSE, curbuf))
5849 {
5850#ifdef FEAT_EVAL
5851 if (aborting())
5852 return;
5853#endif
5854 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005855
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005856 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005857#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005858 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005859 {
5860 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005861 NULL, NULL,
5862 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005863 if (browse_file == NULL)
5864 return;
5865 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5866 vim_free(browse_file);
5867 }
5868 else
5869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005871 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005872
Bram Moolenaar39665952018-08-15 20:59:48 +02005873 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005874 wp = curwin;
5875
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005876 incr_quickfix_busy();
5877
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005878 // This function is used by the :cfile, :cgetfile and :caddfile
5879 // commands.
5880 // :cfile always creates a new quickfix list and jumps to the
5881 // first error.
5882 // :cgetfile creates a new quickfix list but doesn't jump to the
5883 // first error.
5884 // :caddfile adds to an existing quickfix list. If there is no
5885 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005886 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005887 && eap->cmdidx != CMD_laddfile),
5888 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005889 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005890 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005891 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005892 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005893 {
5894 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005895 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005896 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005897 }
5898 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005899 qf_list_changed(qf_get_curlist(qi));
5900 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005901 if (au_name != NULL)
5902 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005903
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005904 // Jump to the first error for a new list and if autocmds didn't
5905 // free the list.
5906 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5907 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005908 // display the first error
5909 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005910
5911 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005912}
5913
5914/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005915 * Return the vimgrep autocmd name.
5916 */
5917 static char_u *
5918vgr_get_auname(cmdidx_T cmdidx)
5919{
5920 switch (cmdidx)
5921 {
5922 case CMD_vimgrep: return (char_u *)"vimgrep";
5923 case CMD_lvimgrep: return (char_u *)"lvimgrep";
5924 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
5925 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
5926 case CMD_grep: return (char_u *)"grep";
5927 case CMD_lgrep: return (char_u *)"lgrep";
5928 case CMD_grepadd: return (char_u *)"grepadd";
5929 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5930 default: return NULL;
5931 }
5932}
5933
5934/*
5935 * Initialize the regmatch used by vimgrep for pattern "s".
5936 */
5937 static void
5938vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
5939{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005940 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005941 regmatch->regprog = NULL;
5942
5943 if (s == NULL || *s == NUL)
5944 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005945 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005946 if (last_search_pat() == NULL)
5947 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005948 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005949 return;
5950 }
5951 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
5952 }
5953 else
5954 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
5955
5956 regmatch->rmm_ic = p_ic;
5957 regmatch->rmm_maxcol = 0;
5958}
5959
5960/*
5961 * Display a file name when vimgrep is running.
5962 */
5963 static void
5964vgr_display_fname(char_u *fname)
5965{
5966 char_u *p;
5967
5968 msg_start();
5969 p = msg_strtrunc(fname, TRUE);
5970 if (p == NULL)
5971 msg_outtrans(fname);
5972 else
5973 {
5974 msg_outtrans(p);
5975 vim_free(p);
5976 }
5977 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005978 msg_didout = FALSE; // overwrite this message
5979 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01005980 msg_col = 0;
5981 out_flush();
5982}
5983
5984/*
5985 * Load a dummy buffer to search for a pattern using vimgrep.
5986 */
5987 static buf_T *
5988vgr_load_dummy_buf(
5989 char_u *fname,
5990 char_u *dirname_start,
5991 char_u *dirname_now)
5992{
5993 int save_mls;
5994#if defined(FEAT_SYN_HL)
5995 char_u *save_ei = NULL;
5996#endif
5997 buf_T *buf;
5998
5999#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006000 // Don't do Filetype autocommands to avoid loading syntax and
6001 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006002 save_ei = au_event_disable(",Filetype");
6003#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006004 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006005 save_mls = p_mls;
6006 p_mls = 0;
6007
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006008 // Load file into a buffer, so that 'fileencoding' is detected,
6009 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006010 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
6011
6012 p_mls = save_mls;
6013#if defined(FEAT_SYN_HL)
6014 au_event_restore(save_ei);
6015#endif
6016
6017 return buf;
6018}
6019
6020/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006021 * Check whether a quickfix/location list is valid. Autocmds may remove or
6022 * change a quickfix list when vimgrep is running. If the list is not found,
6023 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006024 */
6025 static int
6026vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006027 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006028 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006029 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006030 char_u *title)
6031{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006032 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006033 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006034 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006035 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006036 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006037 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02006038 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006039 return FALSE;
6040 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006041 else
6042 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006043 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006044 qf_new_list(qi, title);
6045 return TRUE;
6046 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006047 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006048
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02006049 if (qf_restore_list(qi, qfid) == FAIL)
6050 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006051
6052 return TRUE;
6053}
6054
6055/*
6056 * Search for a pattern in all the lines in a buffer and add the matching lines
6057 * to a quickfix list.
6058 */
6059 static int
6060vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006061 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006062 char_u *fname,
6063 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006064 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006065 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01006066 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006067 int duplicate_name,
6068 int flags)
6069{
6070 int found_match = FALSE;
6071 long lnum;
6072 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02006073 int pat_len = (int)STRLEN(spat);
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006074 if (pat_len > MAX_FUZZY_MATCHES)
6075 pat_len = MAX_FUZZY_MATCHES;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006076
Bram Moolenaar1c299432018-10-28 14:36:09 +01006077 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006078 {
6079 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006080 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006081 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006082 // Regular expression match
6083 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006084 col, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006085 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006086 // Pass the buffer number so that it gets used even for a
6087 // dummy buffer, unless duplicate_name is set, then the
6088 // buffer will be wiped out below.
6089 if (qf_add_entry(qfl,
6090 NULL, // dir
6091 fname,
6092 NULL,
6093 duplicate_name ? 0 : buf->b_fnum,
6094 ml_get_buf(buf,
6095 regmatch->startpos[0].lnum + lnum, FALSE),
6096 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02006097 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006098 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02006099 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006100 FALSE, // vis_col
6101 NULL, // search pattern
6102 0, // nr
6103 0, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02006104 NULL, // user_data
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006105 TRUE // valid
6106 ) == QF_FAIL)
6107 {
6108 got_int = TRUE;
6109 break;
6110 }
6111 found_match = TRUE;
6112 if (--*tomatch == 0)
6113 break;
6114 if ((flags & VGR_GLOBAL) == 0
6115 || regmatch->endpos[0].lnum > 0)
6116 break;
6117 col = regmatch->endpos[0].col
6118 + (col == regmatch->endpos[0].col);
6119 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE)))
6120 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006121 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006122 }
6123 else
6124 {
6125 char_u *str = ml_get_buf(buf, lnum, FALSE);
6126 int score;
6127 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006128 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006129
6130 // Fuzzy string match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006131 CLEAR_FIELD(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006132 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
6133 {
6134 // Pass the buffer number so that it gets used even for a
6135 // dummy buffer, unless duplicate_name is set, then the
6136 // buffer will be wiped out below.
6137 if (qf_add_entry(qfl,
6138 NULL, // dir
6139 fname,
6140 NULL,
6141 duplicate_name ? 0 : buf->b_fnum,
6142 str,
6143 lnum,
thinca6864efa2021-06-19 20:45:20 +02006144 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006145 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006146 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006147 FALSE, // vis_col
6148 NULL, // search pattern
6149 0, // nr
6150 0, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02006151 NULL, // user_data
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006152 TRUE // valid
6153 ) == QF_FAIL)
6154 {
6155 got_int = TRUE;
6156 break;
6157 }
6158 found_match = TRUE;
6159 if (--*tomatch == 0)
6160 break;
6161 if ((flags & VGR_GLOBAL) == 0)
6162 break;
6163 col = matches[pat_len - 1] + col + 1;
6164 if (col > (colnr_T)STRLEN(str))
6165 break;
6166 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006167 }
6168 line_breakcheck();
6169 if (got_int)
6170 break;
6171 }
6172
6173 return found_match;
6174}
6175
6176/*
6177 * Jump to the first match and update the directory.
6178 */
6179 static void
6180vgr_jump_to_match(
6181 qf_info_T *qi,
6182 int forceit,
6183 int *redraw_for_dummy,
6184 buf_T *first_match_buf,
6185 char_u *target_dir)
6186{
6187 buf_T *buf;
6188
6189 buf = curbuf;
6190 qf_jump(qi, 0, 0, forceit);
6191 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006192 // If we jumped to another buffer redrawing will already be
6193 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006194 *redraw_for_dummy = FALSE;
6195
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006196 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006197 if (curbuf == first_match_buf && target_dir != NULL)
6198 {
6199 exarg_T ea;
6200
Bram Moolenaara80faa82020-04-12 19:37:17 +02006201 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006202 ea.arg = target_dir;
6203 ea.cmdidx = CMD_lcd;
6204 ex_cd(&ea);
6205 }
6206}
6207
6208/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006209 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006210 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006211typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006212{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006213 long tomatch; // maximum number of matches to find
6214 char_u *spat; // search pattern
6215 int flags; // search modifier
6216 char_u **fnames; // list of files to search
6217 int fcount; // number of files
6218 regmmatch_T regmatch; // compiled search pattern
6219 char_u *qf_title; // quickfix list title
6220} vgr_args_T;
6221
6222/*
6223 * Process :vimgrep command arguments. The command syntax is:
6224 *
6225 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6226 */
6227 static int
6228vgr_process_args(
6229 exarg_T *eap,
6230 vgr_args_T *args)
6231{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006232 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006233
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00006234 CLEAR_POINTER(args);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006235
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006236 args->regmatch.regprog = NULL;
6237 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006238
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006239 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006240 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006241 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006242 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006243
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006244 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006245 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006246 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006247 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006248 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006249 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006250 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006251
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006252 vgr_init_regmatch(&args->regmatch, args->spat);
6253 if (args->regmatch.regprog == NULL)
6254 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006255
6256 p = skipwhite(p);
6257 if (*p == NUL)
6258 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006259 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006260 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006261 }
6262
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006263 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006264 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6265 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006266 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006267 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006268 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006269 }
6270
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006271 return OK;
6272}
6273
6274/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006275 * Return TRUE if "buf" had an existing swap file, the current swap file does
6276 * not end in ".swp".
6277 */
6278 static int
6279existing_swapfile(buf_T *buf)
6280{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006281 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006282 {
6283 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6284 size_t len = STRLEN(fname);
6285
6286 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6287 }
6288 return FALSE;
6289}
6290
6291/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006292 * Search for a pattern in a list of files and populate the quickfix list with
6293 * the matches.
6294 */
6295 static int
6296vgr_process_files(
6297 win_T *wp,
6298 qf_info_T *qi,
6299 vgr_args_T *cmd_args,
6300 int *redraw_for_dummy,
6301 buf_T **first_match_buf,
6302 char_u **target_dir)
6303{
6304 int status = FAIL;
6305 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6306 time_t seconds = 0;
6307 char_u *fname;
6308 int fi;
6309 buf_T *buf;
6310 int duplicate_name = FALSE;
6311 int using_dummy;
6312 char_u *dirname_start = NULL;
6313 char_u *dirname_now = NULL;
6314 int found_match;
6315 aco_save_T aco;
6316
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006317 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6318 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006319 if (dirname_start == NULL || dirname_now == NULL)
6320 goto theend;
6321
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006322 // Remember the current directory, because a BufRead autocommand that does
6323 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006324 mch_dirname(dirname_start, MAXPATHL);
6325
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006326 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006327 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6328 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006329 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006330 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006331 if (time(NULL) > seconds)
6332 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006333 // Display the file name every second or so, show the user we are
6334 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006335 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006336 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006337 }
6338
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006339 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006340 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6341 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006342 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006343 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006344 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006345 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006346
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006347 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006348 }
6349 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006350 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006351 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006352
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006353 // Check whether the quickfix list is still valid. When loading a
6354 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006355 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006356 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006357
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006358 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006359
Bram Moolenaar81695252004-12-29 20:58:21 +00006360 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006361 {
6362 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006363 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006364 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006365 else
6366 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006367 // Try for a match in all lines of the buffer.
6368 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006369 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006370 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006371 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006372
Bram Moolenaar81695252004-12-29 20:58:21 +00006373 if (using_dummy)
6374 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006375 if (found_match && *first_match_buf == NULL)
6376 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006377 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006378 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006379 // Never keep a dummy buffer if there is another buffer
6380 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006381 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006382 buf = NULL;
6383 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006384 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006385 || buf->b_p_bh[0] == 'u' // "unload"
6386 || buf->b_p_bh[0] == 'w' // "wipe"
6387 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006388 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006389 // When no match was found we don't need to remember the
6390 // buffer, wipe it out. If there was a match and it
6391 // wasn't the first one or we won't jump there: only
6392 // unload the buffer.
6393 // Ignore 'hidden' here, because it may lead to having too
6394 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006395 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006396 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006397 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006398 buf = NULL;
6399 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006400 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006401 || (cmd_args->flags & VGR_NOJUMP)
6402 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006403 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006404 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006405 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006406 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006407 buf = NULL;
6408 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006409 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006410
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006411 if (buf != NULL)
6412 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006413 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006414 buf->b_flags &= ~BF_DUMMY;
6415
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006416 // If the buffer is still loaded we need to use the
6417 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006418 if (buf == *first_match_buf
6419 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006420 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006421 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006422
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006423 // The buffer is still loaded, the Filetype autocommands
6424 // need to be done now, in that buffer. And the modelines
6425 // need to be done (again). But not the window-local
6426 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006427 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006428 if (curbuf == buf)
6429 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006430#if defined(FEAT_SYN_HL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00006431 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006432 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006433#endif
Bram Moolenaare76062c2022-11-28 18:51:43 +00006434 do_modelines(OPT_NOWIN);
6435 aucmd_restbuf(&aco);
6436 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006437 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006438 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006439 }
6440 }
6441
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006442 status = OK;
6443
6444theend:
6445 vim_free(dirname_now);
6446 vim_free(dirname_start);
6447 return status;
6448}
6449
6450/*
6451 * ":vimgrep {pattern} file(s)"
6452 * ":vimgrepadd {pattern} file(s)"
6453 * ":lvimgrep {pattern} file(s)"
6454 * ":lvimgrepadd {pattern} file(s)"
6455 */
6456 void
6457ex_vimgrep(exarg_T *eap)
6458{
6459 vgr_args_T args;
6460 qf_info_T *qi;
6461 qf_list_T *qfl;
6462 int_u save_qfid;
6463 win_T *wp = NULL;
6464 int redraw_for_dummy = FALSE;
6465 buf_T *first_match_buf = NULL;
6466 char_u *target_dir = NULL;
6467 char_u *au_name = NULL;
6468 int status;
6469
6470 au_name = vgr_get_auname(eap->cmdidx);
6471 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6472 curbuf->b_fname, TRUE, curbuf))
6473 {
6474#ifdef FEAT_EVAL
6475 if (aborting())
6476 return;
6477#endif
6478 }
6479
6480 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6481 if (qi == NULL)
6482 return;
6483
6484 if (vgr_process_args(eap, &args) == FAIL)
6485 goto theend;
6486
6487 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6488 && eap->cmdidx != CMD_vimgrepadd
6489 && eap->cmdidx != CMD_lvimgrepadd)
6490 || qf_stack_empty(qi))
6491 // make place for a new list
6492 qf_new_list(qi, args.qf_title);
6493
6494 incr_quickfix_busy();
6495
6496 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6497 &first_match_buf, &target_dir);
6498 if (status != OK)
6499 {
6500 FreeWild(args.fcount, args.fnames);
6501 decr_quickfix_busy();
6502 goto theend;
6503 }
6504
6505 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006506
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006507 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006508 qfl->qf_nonevalid = FALSE;
6509 qfl->qf_ptr = qfl->qf_start;
6510 qfl->qf_index = 1;
6511 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006512
Bram Moolenaar864293a2016-06-02 13:40:04 +02006513 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006514
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006515 // Remember the current quickfix list identifier, so that we can check for
6516 // autocommands changing the current quickfix list.
6517 save_qfid = qf_get_curlist(qi)->qf_id;
6518
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006519 if (au_name != NULL)
6520 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6521 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006522 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6523 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006524 if (!qflist_valid(wp, save_qfid)
6525 || qf_restore_list(qi, save_qfid) == FAIL)
6526 {
6527 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006528 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006529 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006530
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02006531 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006532 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006533 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006534 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006535 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6536 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006537 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006538 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006539 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006540
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006541 decr_quickfix_busy();
6542
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006543 // If we loaded a dummy buffer into the current window, the autocommands
6544 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006545 if (redraw_for_dummy)
6546 {
6547#ifdef FEAT_FOLDING
6548 foldUpdateAll(curwin);
6549#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006550 redraw_later(UPD_NOT_VALID);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006551#endif
6552 }
6553
Bram Moolenaar86b68352004-12-27 21:59:20 +00006554theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006555 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006556 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006557 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006558}
6559
6560/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006561 * Restore current working directory to "dirname_start" if they differ, taking
6562 * into account whether it is set locally or globally.
6563 */
6564 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006565restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006566{
6567 char_u *dirname_now = alloc(MAXPATHL);
6568
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006569 if (dirname_now == NULL)
6570 return;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006571
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006572 mch_dirname(dirname_now, MAXPATHL);
6573 if (STRCMP(dirname_start, dirname_now) != 0)
6574 {
6575 // If the directory has changed, change it back by building up an
6576 // appropriate ex command and executing it.
6577 exarg_T ea;
6578
6579 CLEAR_FIELD(ea);
6580 ea.arg = dirname_start;
6581 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6582 ex_cd(&ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006583 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006584 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006585}
6586
6587/*
6588 * Load file "fname" into a dummy buffer and return the buffer pointer,
6589 * placing the directory resulting from the buffer load into the
6590 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6591 * prior to calling this function. Restores directory to "dirname_start" prior
6592 * to returning, if autocmds or the 'autochdir' option have changed it.
6593 *
6594 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6595 * or wipe_dummy_buffer() later!
6596 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006597 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006598 */
6599 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006600load_dummy_buffer(
6601 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006602 char_u *dirname_start, // in: old directory
6603 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006604{
6605 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006606 bufref_T newbufref;
6607 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006608 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006609 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006610 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006611
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006612 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006613 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6614 if (newbuf == NULL)
6615 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006616 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006617
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006618 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006619 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6620
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006621 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006622 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006623 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006624 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006625 ++newbuf->b_locked;
6626
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006627 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006628 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006629 if (curbuf == newbuf)
Bram Moolenaar81695252004-12-29 20:58:21 +00006630 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00006631 // Need to set the filename for autocommands.
6632 (void)setfname(curbuf, fname, NULL, FALSE);
6633
6634 // Create swap file now to avoid the ATTENTION message.
6635 check_need_swap(TRUE);
6636
6637 // Remove the "dummy" flag, otherwise autocommands may not
6638 // work.
6639 curbuf->b_flags &= ~BF_DUMMY;
6640
6641 newbuf_to_wipe.br_buf = NULL;
6642 readfile_result = readfile(fname, NULL,
6643 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6644 NULL, READ_NEW | READ_DUMMY);
6645 --newbuf->b_locked;
6646 if (readfile_result == OK
6647 && !got_int
6648 && !(curbuf->b_flags & BF_NEW))
Bram Moolenaar81695252004-12-29 20:58:21 +00006649 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00006650 failed = FALSE;
6651 if (curbuf != newbuf)
6652 {
6653 // Bloody autocommands changed the buffer! Can happen when
6654 // using netrw and editing a remote file. Use the current
6655 // buffer instead, delete the dummy one after restoring the
6656 // window stuff.
6657 set_bufref(&newbuf_to_wipe, newbuf);
6658 newbuf = curbuf;
6659 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006660 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00006661
6662 // restore curwin/curbuf and a few other things
6663 aucmd_restbuf(&aco);
Bram Moolenaar81695252004-12-29 20:58:21 +00006664
Bram Moolenaar84497cd2022-11-28 20:34:52 +00006665 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6666 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
6667 }
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006668
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006669 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6670 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006671 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006672 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006673
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006674 // When autocommands/'autochdir' option changed directory: go back.
6675 // Let the caller know what the resulting dir was first, in case it is
6676 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006677 mch_dirname(resulting_dir, MAXPATHL);
6678 restore_start_dir(dirname_start);
6679
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006680 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006681 return NULL;
6682 if (failed)
6683 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006684 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006685 return NULL;
6686 }
6687 return newbuf;
6688}
6689
6690/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006691 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6692 * directory to "dirname_start" prior to returning, if autocmds or the
6693 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006694 */
6695 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006696wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006697{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006698 // If any autocommand opened a window on the dummy buffer, close that
6699 // window. If we can't close them all then give up.
6700 while (buf->b_nwindows > 0)
6701 {
6702 int did_one = FALSE;
6703 win_T *wp;
6704
6705 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006706 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006707 if (wp->w_buffer == buf)
6708 {
6709 if (win_close(wp, FALSE) == OK)
6710 did_one = TRUE;
6711 break;
6712 }
6713 if (!did_one)
6714 return;
6715 }
6716
6717 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006718 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006719#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006720 cleanup_T cs;
6721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006722 // Reset the error/interrupt/exception state here so that aborting()
6723 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6724 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006725 enter_cleanup(&cs);
6726#endif
6727
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006728 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006729
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006730#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006731 // Restore the error/interrupt/exception state if not discarded by a
6732 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006733 leave_cleanup(&cs);
6734#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006735 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006736 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006737 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006738}
6739
6740/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006741 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6742 * directory to "dirname_start" prior to returning, if autocmds or the
6743 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006744 */
6745 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006746unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006747{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006748 if (curbuf == buf) // safety check
6749 return;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006750
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006751 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
6752
6753 // When autocommands/'autochdir' option changed directory: go back.
6754 restore_start_dir(dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006755}
6756
Bram Moolenaar05159a02005-02-26 23:04:13 +00006757#if defined(FEAT_EVAL) || defined(PROTO)
6758/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006759 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006760 * to 'list'. Returns OK on success.
6761 */
6762 static int
6763get_qfline_items(qfline_T *qfp, list_T *list)
6764{
6765 int bufnum;
6766 dict_T *dict;
6767 char_u buf[2];
6768
6769 // Handle entries with a non-existing buffer number.
6770 bufnum = qfp->qf_fnum;
6771 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6772 bufnum = 0;
6773
6774 if ((dict = dict_alloc()) == NULL)
6775 return FAIL;
6776 if (list_append_dict(list, dict) == FAIL)
6777 return FAIL;
6778
6779 buf[0] = qfp->qf_type;
6780 buf[1] = NUL;
6781 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006782 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6783 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6784 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6785 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6786 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6787 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006788 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6789 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6790 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6791 || dict_add_string(dict, "type", buf) == FAIL
Tom Praschanca6ac992023-08-11 23:26:12 +02006792 || (qfp->qf_user_data.v_type != VAR_UNKNOWN
6793 && dict_add_tv(dict, "user_data", &qfp->qf_user_data) == FAIL )
Bram Moolenaara16123a2019-03-28 20:31:07 +01006794 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6795 return FAIL;
6796
6797 return OK;
6798}
6799
6800/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006801 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006802 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006803 * If eidx is not 0, then return only the specified entry. Otherwise return
6804 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006805 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006806 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006807get_errorlist(
6808 qf_info_T *qi_arg,
6809 win_T *wp,
6810 int qf_idx,
6811 int eidx,
6812 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006813{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006814 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006815 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006816 qfline_T *qfp;
6817 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006818
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006819 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006820 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006821 qi = &ql_info;
6822 if (wp != NULL)
6823 {
6824 qi = GET_LOC_LIST(wp);
6825 if (qi == NULL)
6826 return FAIL;
6827 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006828 }
6829
Bram Moolenaar858ba062020-05-31 23:11:59 +02006830 if (eidx < 0)
6831 return OK;
6832
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006833 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006834 qf_idx = qi->qf_curlist;
6835
Bram Moolenaar0398e002019-03-21 21:12:49 +01006836 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006837 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006838
Bram Moolenaar0398e002019-03-21 21:12:49 +01006839 qfl = qf_get_list(qi, qf_idx);
6840 if (qf_list_empty(qfl))
6841 return FAIL;
6842
Bram Moolenaara16123a2019-03-28 20:31:07 +01006843 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006844 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006845 if (eidx > 0)
6846 {
6847 if (eidx == i)
6848 return get_qfline_items(qfp, list);
6849 }
6850 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006851 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006852 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006853
Bram Moolenaar05159a02005-02-26 23:04:13 +00006854 return OK;
6855}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006856
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006857// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006858enum {
6859 QF_GETLIST_NONE = 0x0,
6860 QF_GETLIST_TITLE = 0x1,
6861 QF_GETLIST_ITEMS = 0x2,
6862 QF_GETLIST_NR = 0x4,
6863 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006864 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006865 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006866 QF_GETLIST_IDX = 0x40,
6867 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006868 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006869 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006870 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006871 QF_GETLIST_QFTF = 0x800,
6872 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006873};
6874
6875/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006876 * Parse text from 'di' and return the quickfix list items.
6877 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006878 */
6879 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006880qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006881{
6882 int status = FAIL;
6883 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006884 char_u *errorformat = p_efm;
6885 dictitem_T *efm_di;
6886 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006887
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006888 // Only a List value is supported
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006889 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6890 return FAIL;
6891
6892 // If errorformat is supplied then use it, otherwise use the 'efm'
6893 // option setting
6894 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006895 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006896 if (efm_di->di_tv.v_type != VAR_STRING ||
6897 efm_di->di_tv.vval.v_string == NULL)
Bram Moolenaarda732532017-08-31 20:58:02 +02006898 return FAIL;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006899 errorformat = efm_di->di_tv.vval.v_string;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006900 }
6901
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006902 l = list_alloc();
6903 if (l == NULL)
6904 return FAIL;
6905
6906 qi = qf_alloc_stack(QFLT_INTERNAL);
6907 if (qi != NULL)
6908 {
6909 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
6910 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
6911 {
6912 (void)get_errorlist(qi, NULL, 0, 0, l);
6913 qf_free(&qi->qf_lists[0]);
6914 }
6915 free(qi);
6916 }
6917 dict_add_list(retdict, "items", l);
6918 status = OK;
6919
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006920 return status;
6921}
6922
6923/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006924 * Return the quickfix/location list window identifier in the current tabpage.
6925 */
6926 static int
6927qf_winid(qf_info_T *qi)
6928{
6929 win_T *win;
6930
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006931 // The quickfix window can be opened even if the quickfix list is not set
6932 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01006933 if (qi == NULL)
6934 return 0;
6935 win = qf_find_win(qi);
6936 if (win != NULL)
6937 return win->w_id;
6938 return 0;
6939}
6940
6941/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006942 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006943 * window. If there is no buffer associated with the list or the buffer is
6944 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006945 */
6946 static int
6947qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
6948{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00006949 int bufnum = 0;
6950
6951 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
6952 bufnum = qi->qf_bufnr;
6953
6954 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006955}
6956
6957/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006958 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006959 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006960 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006961qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006962{
Bram Moolenaard823fa92016-08-12 16:29:27 +02006963 int flags = QF_GETLIST_NONE;
6964
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006965 if (dict_has_key(what, "all"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006966 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006967 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006968 if (!loclist)
6969 // File window ID is applicable only to location list windows
6970 flags &= ~ QF_GETLIST_FILEWINID;
6971 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02006972
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006973 if (dict_has_key(what, "title"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006974 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006975
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006976 if (dict_has_key(what, "nr"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01006977 flags |= QF_GETLIST_NR;
6978
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006979 if (dict_has_key(what, "winid"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006980 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02006981
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006982 if (dict_has_key(what, "context"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006983 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02006984
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006985 if (dict_has_key(what, "id"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01006986 flags |= QF_GETLIST_ID;
6987
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006988 if (dict_has_key(what, "items"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006989 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006990
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006991 if (dict_has_key(what, "idx"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006992 flags |= QF_GETLIST_IDX;
6993
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006994 if (dict_has_key(what, "size"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006995 flags |= QF_GETLIST_SIZE;
6996
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01006997 if (dict_has_key(what, "changedtick"))
Bram Moolenaarb254af32017-12-18 19:48:58 +01006998 flags |= QF_GETLIST_TICK;
6999
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007000 if (loclist && dict_has_key(what, "filewinid"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007001 flags |= QF_GETLIST_FILEWINID;
7002
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007003 if (dict_has_key(what, "qfbufnr"))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007004 flags |= QF_GETLIST_QFBUFNR;
7005
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007006 if (dict_has_key(what, "quickfixtextfunc"))
Bram Moolenaard43906d2020-07-20 21:31:32 +02007007 flags |= QF_GETLIST_QFTF;
7008
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007009 return flags;
7010}
Bram Moolenaara6d48492017-12-12 22:45:31 +01007011
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007012/*
7013 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
7014 * If 'nr' and 'id' are not present in 'what' then return the current
7015 * quickfix list index.
7016 * If 'nr' is zero then return the current quickfix list index.
7017 * If 'nr' is '$' then return the last quickfix list index.
7018 * If 'id' is present then return the index of the quickfix list with that id.
7019 * If 'id' is zero then return the quickfix list index specified by 'nr'.
7020 * Return -1, if quickfix list is not present or if the stack is empty.
7021 */
7022 static int
7023qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
7024{
7025 int qf_idx;
7026 dictitem_T *di;
7027
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007028 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007029 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7030 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007031 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007032 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01007033 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007034 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007035 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01007036 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007037 qf_idx = di->di_tv.vval.v_number - 1;
7038 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007039 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007040 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01007041 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007042 else if (di->di_tv.v_type == VAR_STRING
7043 && di->di_tv.vval.v_string != NULL
7044 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007045 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007046 qf_idx = qi->qf_listcount - 1;
7047 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007048 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007049 }
7050
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007051 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
7052 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007053 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007054 if (di->di_tv.v_type == VAR_NUMBER)
7055 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007056 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007057 if (di->di_tv.vval.v_number != 0)
7058 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
7059 }
7060 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007061 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007062 }
7063
7064 return qf_idx;
7065}
7066
7067/*
7068 * Return default values for quickfix list properties in retdict.
7069 */
7070 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007071qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007072{
7073 int status = OK;
7074
7075 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007076 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007077 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
7078 {
7079 list_T *l = list_alloc();
7080 if (l != NULL)
7081 status = dict_add_list(retdict, "items", l);
7082 else
7083 status = FAIL;
7084 }
7085 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007086 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007087 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007088 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007089 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007090 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007091 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007092 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007093 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007094 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007095 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007096 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007097 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007098 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007099 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007100 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007101 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7102 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007103 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7104 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007105
7106 return status;
7107}
7108
7109/*
7110 * Return the quickfix list title as 'title' in retdict
7111 */
7112 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007113qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007114{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007115 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007116}
7117
7118/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007119 * Returns the identifier of the window used to display files from a location
7120 * list. If there is no associated window, then returns 0. Useful only when
7121 * called from a location list window.
7122 */
7123 static int
7124qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7125{
7126 int winid = 0;
7127
7128 if (wp != NULL && IS_LL_WINDOW(wp))
7129 {
7130 win_T *ll_wp = qf_find_win_with_loclist(qi);
7131 if (ll_wp != NULL)
7132 winid = ll_wp->w_id;
7133 }
7134
7135 return dict_add_number(retdict, "filewinid", winid);
7136}
7137
7138/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007139 * Return the quickfix list items/entries as 'items' in retdict.
7140 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007141 */
7142 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007143qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007144{
7145 int status = OK;
7146 list_T *l = list_alloc();
7147 if (l != NULL)
7148 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007149 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007150 dict_add_list(retdict, "items", l);
7151 }
7152 else
7153 status = FAIL;
7154
7155 return status;
7156}
7157
7158/*
7159 * Return the quickfix list context (if any) as 'context' in retdict.
7160 */
7161 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007162qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007163{
7164 int status;
7165 dictitem_T *di;
7166
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007167 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007168 {
7169 di = dictitem_alloc((char_u *)"context");
7170 if (di != NULL)
7171 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007172 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007173 status = dict_add(retdict, di);
7174 if (status == FAIL)
7175 dictitem_free(di);
7176 }
7177 else
7178 status = FAIL;
7179 }
7180 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007181 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007182
7183 return status;
7184}
7185
7186/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007187 * Return the current quickfix list index as 'idx' in retdict.
7188 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007189 */
7190 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007191qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007192{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007193 if (eidx == 0)
7194 {
7195 eidx = qfl->qf_index;
7196 if (qf_list_empty(qfl))
7197 // For empty lists, current index is set to 0
7198 eidx = 0;
7199 }
7200 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007201}
7202
7203/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007204 * Return the 'quickfixtextfunc' function of a quickfix/location list
7205 */
7206 static int
7207qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7208{
7209 int status;
7210
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007211 if (qfl->qf_qftf_cb.cb_name != NULL)
Bram Moolenaard43906d2020-07-20 21:31:32 +02007212 {
7213 typval_T tv;
7214
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007215 put_callback(&qfl->qf_qftf_cb, &tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007216 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7217 clear_tv(&tv);
7218 }
7219 else
7220 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7221
7222 return status;
7223}
7224
7225/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007226 * Return quickfix/location list details (title) as a
7227 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7228 * then current list is used. Otherwise the specified list is used.
7229 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007230 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007231qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7232{
7233 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007234 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007235 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007236 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007237 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007238 dictitem_T *di;
7239 int flags = QF_GETLIST_NONE;
7240
7241 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7242 return qf_get_list_from_lines(what, di, retdict);
7243
7244 if (wp != NULL)
7245 qi = GET_LOC_LIST(wp);
7246
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007247 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007248
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007249 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007250 qf_idx = qf_getprop_qfidx(qi, what);
7251
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007252 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007253 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007254 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007255
Bram Moolenaar0398e002019-03-21 21:12:49 +01007256 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007257
Bram Moolenaar858ba062020-05-31 23:11:59 +02007258 // If an entry index is specified, use that
7259 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7260 {
7261 if (di->di_tv.v_type != VAR_NUMBER)
7262 return FAIL;
7263 eidx = di->di_tv.vval.v_number;
7264 }
7265
Bram Moolenaard823fa92016-08-12 16:29:27 +02007266 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007267 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007268 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007269 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007270 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007271 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007272 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007273 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007274 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007275 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007276 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007277 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007278 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007279 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007280 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007281 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007282 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007283 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007284 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7285 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007286 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7287 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007288 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7289 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007290
Bram Moolenaard823fa92016-08-12 16:29:27 +02007291 return status;
7292}
7293
7294/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007295 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007296 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7297 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007298 */
7299 static int
7300qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007301 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007302 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007303 int first_entry,
7304 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007305{
7306 static int did_bufnr_emsg;
7307 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007308 int bufnum, valid, status, col, end_col, vcol, nr;
7309 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007310
7311 if (first_entry)
7312 did_bufnr_emsg = FALSE;
7313
Bram Moolenaard61efa52022-07-23 09:52:04 +01007314 filename = dict_get_string(d, "filename", TRUE);
7315 module = dict_get_string(d, "module", TRUE);
7316 bufnum = (int)dict_get_number(d, "bufnr");
7317 lnum = (int)dict_get_number(d, "lnum");
7318 end_lnum = (int)dict_get_number(d, "end_lnum");
7319 col = (int)dict_get_number(d, "col");
7320 end_col = (int)dict_get_number(d, "end_col");
7321 vcol = (int)dict_get_number(d, "vcol");
7322 nr = (int)dict_get_number(d, "nr");
7323 type = dict_get_string(d, "type", TRUE);
7324 pattern = dict_get_string(d, "pattern", TRUE);
7325 text = dict_get_string(d, "text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007326 if (text == NULL)
7327 text = vim_strsave((char_u *)"");
Tom Praschanca6ac992023-08-11 23:26:12 +02007328 typval_T user_data;
7329 user_data.v_type = VAR_UNKNOWN;
7330 dict_get_tv(d, "user_data", &user_data);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007331
7332 valid = TRUE;
7333 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7334 valid = FALSE;
7335
7336 // Mark entries with non-existing buffer number as not valid. Give the
7337 // error message only once.
7338 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7339 {
7340 if (!did_bufnr_emsg)
7341 {
7342 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007343 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007344 }
7345 valid = FALSE;
7346 bufnum = 0;
7347 }
7348
7349 // If the 'valid' field is present it overrules the detected value.
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007350 if (dict_has_key(d, "valid"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01007351 valid = (int)dict_get_bool(d, "valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007352
Bram Moolenaar0398e002019-03-21 21:12:49 +01007353 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007354 NULL, // dir
7355 filename,
7356 module,
7357 bufnum,
7358 text,
7359 lnum,
thinca6864efa2021-06-19 20:45:20 +02007360 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007361 col,
thinca6864efa2021-06-19 20:45:20 +02007362 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007363 vcol, // vis_col
7364 pattern, // search pattern
7365 nr,
7366 type == NULL ? NUL : *type,
Tom Praschanca6ac992023-08-11 23:26:12 +02007367 &user_data,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007368 valid);
7369
7370 vim_free(filename);
7371 vim_free(module);
7372 vim_free(pattern);
7373 vim_free(text);
7374 vim_free(type);
Tom Praschanca6ac992023-08-11 23:26:12 +02007375 clear_tv(&user_data);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007376
Bram Moolenaar9752c722018-12-22 16:49:34 +01007377 if (valid)
7378 *valid_entry = TRUE;
7379
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007380 return status;
7381}
7382
7383/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007384 * Add list of entries to quickfix/location list. Each list entry is
7385 * a dictionary with item information.
7386 */
7387 static int
7388qf_add_entries(
7389 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007390 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007391 list_T *list,
7392 char_u *title,
7393 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007394{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007395 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007396 listitem_T *li;
7397 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007398 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007399 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007400 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007401
Bram Moolenaara3921f42017-06-04 15:30:34 +02007402 if (action == ' ' || qf_idx == qi->qf_listcount)
7403 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007404 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007405 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007406 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007407 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007408 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007409 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007410 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007411 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007412 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007413 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007414 qf_free_items(qfl);
7415 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007416 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007417
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007418 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007419 {
7420 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007421 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007422
7423 d = li->li_tv.vval.v_dict;
7424 if (d == NULL)
7425 continue;
7426
Bram Moolenaar0398e002019-03-21 21:12:49 +01007427 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007428 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007429 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007430 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007431 }
7432
Bram Moolenaar9752c722018-12-22 16:49:34 +01007433 // Check if any valid error entries are added to the list.
7434 if (valid_entry)
7435 qfl->qf_nonevalid = FALSE;
7436 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007437 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007438 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007439
7440 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007441 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007442 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007443
7444 // Update the current error index if not appending to the list or if the
7445 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007446 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007447 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007448
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007449 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007450 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007451
7452 return retval;
7453}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007454
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007455/*
7456 * Get the quickfix list index from 'nr' or 'id'
7457 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007458 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007459qf_setprop_get_qfidx(
7460 qf_info_T *qi,
7461 dict_T *what,
7462 int action,
7463 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007464{
7465 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007466 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007467
Bram Moolenaard823fa92016-08-12 16:29:27 +02007468 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7469 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007470 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007471 if (di->di_tv.v_type == VAR_NUMBER)
7472 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007473 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007474 if (di->di_tv.vval.v_number != 0)
7475 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007476
Bram Moolenaar55b69262017-08-13 13:42:01 +02007477 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7478 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007479 // When creating a new list, accept qf_idx pointing to the next
7480 // non-available list and add the new list at the end of the
7481 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007482 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007483 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007484 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007485 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007486 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007487 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007488 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007489 }
7490 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007491 && di->di_tv.vval.v_string != NULL
7492 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007493 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007494 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007495 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007496 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007497 qf_idx = 0;
7498 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007499 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007500 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007501 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007502 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007503 }
7504
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007505 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007506 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007507 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007508 if (di->di_tv.v_type != VAR_NUMBER)
7509 return INVALID_QFIDX;
7510
7511 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007512 }
7513
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007514 return qf_idx;
7515}
7516
7517/*
7518 * Set the quickfix list title.
7519 */
7520 static int
7521qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7522{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007523 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007524
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007525 if (di->di_tv.v_type != VAR_STRING)
7526 return FAIL;
7527
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007528 vim_free(qfl->qf_title);
Bram Moolenaard61efa52022-07-23 09:52:04 +01007529 qfl->qf_title = dict_get_string(what, "title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007530 if (qf_idx == qi->qf_curlist)
7531 qf_update_win_titlevar(qi);
7532
7533 return OK;
7534}
7535
7536/*
7537 * Set quickfix list items/entries.
7538 */
7539 static int
7540qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7541{
7542 int retval = FAIL;
7543 char_u *title_save;
7544
7545 if (di->di_tv.v_type != VAR_LIST)
7546 return FAIL;
7547
7548 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7549 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7550 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007551 vim_free(title_save);
7552
7553 return retval;
7554}
7555
7556/*
7557 * Set quickfix list items/entries from a list of lines.
7558 */
7559 static int
7560qf_setprop_items_from_lines(
7561 qf_info_T *qi,
7562 int qf_idx,
7563 dict_T *what,
7564 dictitem_T *di,
7565 int action)
7566{
7567 char_u *errorformat = p_efm;
7568 dictitem_T *efm_di;
7569 int retval = FAIL;
7570
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007571 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007572 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7573 {
7574 if (efm_di->di_tv.v_type != VAR_STRING ||
7575 efm_di->di_tv.vval.v_string == NULL)
7576 return FAIL;
7577 errorformat = efm_di->di_tv.vval.v_string;
7578 }
7579
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007580 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007581 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7582 return FAIL;
7583
7584 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007585 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007586 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007587 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007588 retval = OK;
7589
7590 return retval;
7591}
7592
7593/*
7594 * Set quickfix list context.
7595 */
7596 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007597qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007598{
7599 typval_T *ctx;
7600
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007601 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007602 ctx = alloc_tv();
7603 if (ctx != NULL)
7604 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007605 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007606
7607 return OK;
7608}
7609
7610/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007611 * Set the current index in the specified quickfix list
7612 */
7613 static int
7614qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7615{
7616 int denote = FALSE;
7617 int newidx;
7618 int old_qfidx;
7619 qfline_T *qf_ptr;
7620
7621 // If the specified index is '$', then use the last entry
7622 if (di->di_tv.v_type == VAR_STRING
7623 && di->di_tv.vval.v_string != NULL
7624 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7625 newidx = qfl->qf_count;
7626 else
7627 {
7628 // Otherwise use the specified index
7629 newidx = tv_get_number_chk(&di->di_tv, &denote);
7630 if (denote)
7631 return FAIL;
7632 }
7633
7634 if (newidx < 1) // sanity check
7635 return FAIL;
7636 if (newidx > qfl->qf_count)
7637 newidx = qfl->qf_count;
7638
7639 old_qfidx = qfl->qf_index;
7640 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7641 if (qf_ptr == NULL)
7642 return FAIL;
7643 qfl->qf_ptr = qf_ptr;
7644 qfl->qf_index = newidx;
7645
7646 // If the current list is modified and it is displayed in the quickfix
7647 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007648 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007649 qf_win_pos_update(qi, old_qfidx);
7650
7651 return OK;
7652}
7653
7654/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007655 * Set the current index in the specified quickfix list
7656 */
7657 static int
7658qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7659{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007660 callback_T cb;
7661
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007662 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007663 cb = get_callback(&di->di_tv);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007664 if (cb.cb_name == NULL || *cb.cb_name == NUL)
7665 return OK;
7666
7667 set_callback(&qfl->qf_qftf_cb, &cb);
7668 if (cb.cb_free_name)
7669 vim_free(cb.cb_name);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007670
7671 return OK;
7672}
7673
7674/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007675 * Set quickfix/location list properties (title, items, context).
7676 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007677 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007678 */
7679 static int
7680qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7681{
7682 dictitem_T *di;
7683 int retval = FAIL;
7684 int qf_idx;
7685 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007686 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007687
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007688 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007689 newlist = TRUE;
7690
7691 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007692 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007693 return FAIL;
7694
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007695 if (newlist)
7696 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007697 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007698 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007699 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007700 }
7701
Bram Moolenaar0398e002019-03-21 21:12:49 +01007702 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007703 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007704 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007705 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007706 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007707 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007708 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007709 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007710 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007711 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7712 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007713 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7714 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007715
Bram Moolenaar287153c2020-11-29 14:20:27 +01007716 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007717 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007718 if (newlist)
7719 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007720
Bram Moolenaard823fa92016-08-12 16:29:27 +02007721 return retval;
7722}
7723
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007724/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007725 * Free the entire quickfix/location list stack.
7726 * If the quickfix/location list window is open, then clear it.
7727 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007728 static void
7729qf_free_stack(win_T *wp, qf_info_T *qi)
7730{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007731 win_T *qfwin = qf_find_win(qi);
7732 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007733
7734 if (qfwin != NULL)
7735 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007736 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007737 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007738 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007739 qf_update_buffer(qi, NULL);
7740 }
7741
7742 if (wp != NULL && IS_LL_WINDOW(wp))
7743 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007744 // If in the location list window, then use the non-location list
7745 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007746 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007747 if (llwin != NULL)
7748 wp = llwin;
7749 }
7750
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007751 qf_free_all(wp);
7752 if (wp == NULL)
7753 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007754 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007755 qi->qf_curlist = 0;
7756 qi->qf_listcount = 0;
7757 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007758 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007759 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007760 // If the location list window is open, then create a new empty
7761 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007762 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007763
Bram Moolenaar95946f12019-03-31 15:31:59 +02007764 if (new_ll != NULL)
7765 {
7766 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007767
Bram Moolenaar95946f12019-03-31 15:31:59 +02007768 // first free the list reference in the location list window
7769 ll_free_all(&qfwin->w_llist_ref);
7770
7771 qfwin->w_llist_ref = new_ll;
7772 if (wp != qfwin)
7773 win_set_loclist(wp, new_ll);
7774 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007775 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007776}
7777
Bram Moolenaard823fa92016-08-12 16:29:27 +02007778/*
7779 * Populate the quickfix list with the items supplied in the list
7780 * of dictionaries. "title" will be copied to w:quickfix_title.
7781 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007782 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007783 */
7784 int
7785set_errorlist(
7786 win_T *wp,
7787 list_T *list,
7788 int action,
7789 char_u *title,
7790 dict_T *what)
7791{
7792 qf_info_T *qi = &ql_info;
7793 int retval = OK;
7794
7795 if (wp != NULL)
7796 {
7797 qi = ll_get_or_alloc_list(wp);
7798 if (qi == NULL)
7799 return FAIL;
7800 }
7801
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007802 if (action == 'f')
7803 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007804 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007805 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007806 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007807 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007808
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007809 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007810 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007811 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007812 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007813 _("cannot have both a list and a \"what\" argument"));
7814 return FAIL;
7815 }
7816
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007817 incr_quickfix_busy();
7818
7819 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007820 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007821 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007822 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007823 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007824 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007825 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007826 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007827
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007828 decr_quickfix_busy();
7829
Bram Moolenaard823fa92016-08-12 16:29:27 +02007830 return retval;
7831}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007832
Tom Praschanca6ac992023-08-11 23:26:12 +02007833static int mark_quickfix_user_data(qf_info_T *qi, int copyID)
7834{
7835 int abort = FALSE;
7836 for (int i = 0; i < LISTCOUNT && !abort; ++i)
7837 {
7838 qf_list_T *qfl = &qi->qf_lists[i];
7839 if (!qfl->qf_has_user_data)
7840 continue;
7841 qfline_T *qfp;
7842 int j;
7843 FOR_ALL_QFL_ITEMS(qfl, qfp, j)
7844 {
7845 typval_T* user_data = &qfp->qf_user_data;
7846 if (user_data != NULL && user_data->v_type != VAR_NUMBER
7847 && user_data->v_type != VAR_STRING && user_data->v_type != VAR_FLOAT)
7848 abort = abort || set_ref_in_item(user_data, copyID, NULL, NULL);
7849 }
7850 }
7851 return abort;
7852}
7853
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007854/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007855 * Mark the quickfix context and callback function as in use for all the lists
7856 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007857 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007858 static int
7859mark_quickfix_ctx(qf_info_T *qi, int copyID)
7860{
7861 int i;
7862 int abort = FALSE;
7863 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007864 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007865
7866 for (i = 0; i < LISTCOUNT && !abort; ++i)
7867 {
7868 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007869 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7870 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007871 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
7872
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007873 cb = &qi->qf_lists[i].qf_qftf_cb;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007874 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007875 }
7876
7877 return abort;
7878}
7879
7880/*
7881 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007882 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007883 */
7884 int
7885set_ref_in_quickfix(int copyID)
7886{
7887 int abort = FALSE;
7888 tabpage_T *tp;
7889 win_T *win;
7890
7891 abort = mark_quickfix_ctx(&ql_info, copyID);
7892 if (abort)
7893 return abort;
7894
Tom Praschanca6ac992023-08-11 23:26:12 +02007895 abort = mark_quickfix_user_data(&ql_info, copyID);
7896 if (abort)
7897 return abort;
7898
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007899 abort = set_ref_in_callback(&qftf_cb, copyID);
7900 if (abort)
7901 return abort;
7902
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007903 FOR_ALL_TAB_WINDOWS(tp, win)
7904 {
7905 if (win->w_llist != NULL)
7906 {
7907 abort = mark_quickfix_ctx(win->w_llist, copyID);
7908 if (abort)
7909 return abort;
Tom Praschanca6ac992023-08-11 23:26:12 +02007910
7911 abort = mark_quickfix_user_data(win->w_llist, copyID);
7912 if (abort)
7913 return abort;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007914 }
Bram Moolenaar12237442017-12-19 12:38:52 +01007915 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
7916 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007917 // In a location list window and none of the other windows is
7918 // referring to this location list. Mark the location list
7919 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01007920 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
7921 if (abort)
7922 return abort;
7923 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007924 }
7925
7926 return abort;
7927}
Bram Moolenaar05159a02005-02-26 23:04:13 +00007928#endif
7929
Bram Moolenaar81695252004-12-29 20:58:21 +00007930/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01007931 * Return the autocmd name for the :cbuffer Ex commands
7932 */
7933 static char_u *
7934cbuffer_get_auname(cmdidx_T cmdidx)
7935{
7936 switch (cmdidx)
7937 {
7938 case CMD_cbuffer: return (char_u *)"cbuffer";
7939 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
7940 case CMD_caddbuffer: return (char_u *)"caddbuffer";
7941 case CMD_lbuffer: return (char_u *)"lbuffer";
7942 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
7943 case CMD_laddbuffer: return (char_u *)"laddbuffer";
7944 default: return NULL;
7945 }
7946}
7947
7948/*
7949 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
7950 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
7951 */
7952 static int
7953cbuffer_process_args(
7954 exarg_T *eap,
7955 buf_T **bufp,
7956 linenr_T *line1,
7957 linenr_T *line2)
7958{
7959 buf_T *buf = NULL;
7960
7961 if (*eap->arg == NUL)
7962 buf = curbuf;
7963 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
7964 buf = buflist_findnr(atoi((char *)eap->arg));
7965
7966 if (buf == NULL)
7967 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007968 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007969 return FAIL;
7970 }
7971
7972 if (buf->b_ml.ml_mfp == NULL)
7973 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007974 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007975 return FAIL;
7976 }
7977
7978 if (eap->addr_count == 0)
7979 {
7980 eap->line1 = 1;
7981 eap->line2 = buf->b_ml.ml_line_count;
7982 }
7983
7984 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
7985 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
7986 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007987 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01007988 return FAIL;
7989 }
7990
7991 *line1 = eap->line1;
7992 *line2 = eap->line2;
7993 *bufp = buf;
7994
7995 return OK;
7996}
7997
7998/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00007999 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00008000 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00008001 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008002 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00008003 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00008004 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008005 */
8006 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008007ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008008{
8009 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008010 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008011 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01008012 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02008013 int_u save_qfid;
8014 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01008015 char_u *qf_title;
8016 linenr_T line1;
8017 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008018
Bram Moolenaara16123a2019-03-28 20:31:07 +01008019 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01008020 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01008021 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008022 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008023#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008024 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008025 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008026#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008027 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008028
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008029 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008030 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8031 if (qi == NULL)
8032 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01008033
Bram Moolenaara16123a2019-03-28 20:31:07 +01008034 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
8035 return;
8036
8037 qf_title = qf_cmdtitle(*eap->cmdlinep);
8038
8039 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008040 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01008041 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
8042 (char *)qf_title, (char *)buf->b_sfname);
8043 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008044 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01008045
8046 incr_quickfix_busy();
8047
8048 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
8049 (eap->cmdidx != CMD_caddbuffer
8050 && eap->cmdidx != CMD_laddbuffer),
8051 line1, line2,
8052 qf_title, NULL);
8053 if (qf_stack_empty(qi))
8054 {
8055 decr_quickfix_busy();
8056 return;
8057 }
8058 if (res >= 0)
8059 qf_list_changed(qf_get_curlist(qi));
8060
8061 // Remember the current quickfix list identifier, so that we can
8062 // check for autocommands changing the current quickfix list.
8063 save_qfid = qf_get_curlist(qi)->qf_id;
8064 if (au_name != NULL)
8065 {
8066 buf_T *curbuf_old = curbuf;
8067
8068 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
8069 TRUE, curbuf);
8070 if (curbuf != curbuf_old)
8071 // Autocommands changed buffer, don't jump now, "qi" may
8072 // be invalid.
8073 res = 0;
8074 }
8075 // Jump to the first error for a new list and if autocmds didn't
8076 // free the list.
8077 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
8078 eap->cmdidx == CMD_lbuffer)
8079 && qflist_valid(wp, save_qfid))
8080 // display the first error
8081 qf_jump_first(qi, save_qfid, eap->forceit);
8082
8083 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00008084}
8085
Bram Moolenaar1e015462005-09-25 22:16:38 +00008086#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008087/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01008088 * Return the autocmd name for the :cexpr Ex commands.
8089 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008090 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01008091cexpr_get_auname(cmdidx_T cmdidx)
8092{
8093 switch (cmdidx)
8094 {
8095 case CMD_cexpr: return (char_u *)"cexpr";
8096 case CMD_cgetexpr: return (char_u *)"cgetexpr";
8097 case CMD_caddexpr: return (char_u *)"caddexpr";
8098 case CMD_lexpr: return (char_u *)"lexpr";
8099 case CMD_lgetexpr: return (char_u *)"lgetexpr";
8100 case CMD_laddexpr: return (char_u *)"laddexpr";
8101 default: return NULL;
8102 }
8103}
8104
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008105 int
8106trigger_cexpr_autocmd(int cmdidx)
8107{
8108 char_u *au_name = cexpr_get_auname(cmdidx);
8109
8110 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8111 curbuf->b_fname, TRUE, curbuf))
8112 {
8113 if (aborting())
8114 return FAIL;
8115 }
8116 return OK;
8117}
8118
8119 int
8120cexpr_core(exarg_T *eap, typval_T *tv)
8121{
8122 qf_info_T *qi;
8123 win_T *wp = NULL;
8124
8125 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8126 if (qi == NULL)
8127 return FAIL;
8128
8129 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
8130 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
8131 {
8132 int res;
8133 int_u save_qfid;
8134 char_u *au_name = cexpr_get_auname(eap->cmdidx);
8135
8136 incr_quickfix_busy();
8137 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
8138 (eap->cmdidx != CMD_caddexpr
8139 && eap->cmdidx != CMD_laddexpr),
8140 (linenr_T)0, (linenr_T)0,
8141 qf_cmdtitle(*eap->cmdlinep), NULL);
8142 if (qf_stack_empty(qi))
8143 {
8144 decr_quickfix_busy();
8145 return FAIL;
8146 }
8147 if (res >= 0)
8148 qf_list_changed(qf_get_curlist(qi));
8149
8150 // Remember the current quickfix list identifier, so that we can
8151 // check for autocommands changing the current quickfix list.
8152 save_qfid = qf_get_curlist(qi)->qf_id;
8153 if (au_name != NULL)
8154 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8155 curbuf->b_fname, TRUE, curbuf);
8156
8157 // Jump to the first error for a new list and if autocmds didn't
8158 // free the list.
8159 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8160 && qflist_valid(wp, save_qfid))
8161 // display the first error
8162 qf_jump_first(qi, save_qfid, eap->forceit);
8163 decr_quickfix_busy();
8164 return OK;
8165 }
8166
Bram Moolenaar677658a2022-01-05 16:09:06 +00008167 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008168 return FAIL;
8169}
8170
Bram Moolenaara16123a2019-03-28 20:31:07 +01008171/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008172 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8173 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008174 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008175 */
8176 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008177ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008178{
8179 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008180
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008181 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008182 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008183
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008184 // Evaluate the expression. When the result is a string or a list we can
8185 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008186 tv = eval_expr(eap->arg, eap);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00008187 if (tv == NULL)
8188 return;
8189
8190 (void)cexpr_core(eap, tv);
8191 free_tv(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008192}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008193#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008194
8195/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008196 * Get the location list for ":lhelpgrep"
8197 */
8198 static qf_info_T *
8199hgr_get_ll(int *new_ll)
8200{
8201 win_T *wp;
8202 qf_info_T *qi;
8203
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008204 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008205 if (bt_help(curwin->w_buffer))
8206 wp = curwin;
8207 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008208 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008209 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008210
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008211 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008212 qi = NULL;
8213 else
8214 qi = wp->w_llist;
8215
8216 if (qi == NULL)
8217 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008218 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008219 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008220 return NULL;
8221 *new_ll = TRUE;
8222 }
8223
8224 return qi;
8225}
8226
8227/*
8228 * Search for a pattern in a help file.
8229 */
8230 static void
8231hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008232 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008233 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008234 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008235 regmatch_T *p_regmatch)
8236{
8237 FILE *fd;
8238 long lnum;
8239
8240 fd = mch_fopen((char *)fname, "r");
8241 if (fd == NULL)
8242 return;
8243
8244 lnum = 1;
8245 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8246 {
8247 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008248
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008249 // Convert a line if 'encoding' is not utf-8 and
8250 // the line contains a non-ASCII character.
Yegappan Lakshmanandf1bbea2022-03-05 14:35:12 +00008251 if (p_vc->vc_type != CONV_NONE && has_non_ascii(IObuff))
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008252 {
8253 line = string_convert(p_vc, IObuff, NULL);
8254 if (line == NULL)
8255 line = IObuff;
8256 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008257
8258 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8259 {
8260 int l = (int)STRLEN(line);
8261
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008262 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008263 while (l > 0 && line[l - 1] <= ' ')
8264 line[--l] = NUL;
8265
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008266 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008267 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008268 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008269 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008270 0,
8271 line,
8272 lnum,
thinca6864efa2021-06-19 20:45:20 +02008273 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008274 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008275 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008276 (int)(p_regmatch->endp[0] - line)
8277 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008278 FALSE, // vis_col
8279 NULL, // search pattern
8280 0, // nr
8281 1, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02008282 NULL, // user_data
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008283 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008284 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008285 {
8286 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008287 if (line != IObuff)
8288 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008289 break;
8290 }
8291 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008292 if (line != IObuff)
8293 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008294 ++lnum;
8295 line_breakcheck();
8296 }
8297 fclose(fd);
8298}
8299
8300/*
8301 * Search for a pattern in all the help files in the doc directory under
8302 * the given directory.
8303 */
8304 static void
8305hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008306 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008307 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008308 regmatch_T *p_regmatch,
8309 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008310#ifdef FEAT_MULTI_LANG
8311 , char_u *lang
8312#endif
8313 )
8314{
8315 int fcount;
8316 char_u **fnames;
8317 int fi;
8318
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008319 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008320 add_pathsep(dirname);
8321 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8322 if (gen_expand_wildcards(1, &dirname, &fcount,
8323 &fnames, EW_FILE|EW_SILENT) == OK
8324 && fcount > 0)
8325 {
8326 for (fi = 0; fi < fcount && !got_int; ++fi)
8327 {
8328#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008329 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008330 if (lang != NULL
8331 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008332 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008333 && !(STRNICMP(lang, "en", 2) == 0
8334 && STRNICMP("txt", fnames[fi]
8335 + STRLEN(fnames[fi]) - 3, 3) == 0))
8336 continue;
8337#endif
8338
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008339 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008340 }
8341 FreeWild(fcount, fnames);
8342 }
8343}
8344
8345/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008346 * Search for a pattern in all the help files in the 'runtimepath'
8347 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008348 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008349 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008350 */
8351 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008352hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008353{
8354 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008355
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008356 vimconv_T vc;
8357
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008358 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8359 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008360 vc.vc_type = CONV_NONE;
8361 if (!enc_utf8)
8362 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008363
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008364 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008365 p = p_rtp;
8366 while (*p != NUL && !got_int)
8367 {
8368 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8369
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008370 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008371#ifdef FEAT_MULTI_LANG
8372 , lang
8373#endif
8374 );
8375 }
8376
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008377 if (vc.vc_type != CONV_NONE)
8378 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008379}
8380
8381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 * ":helpgrep {pattern}"
8383 */
8384 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008385ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386{
8387 regmatch_T regmatch;
8388 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008389 int save_cpo_allocated;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008390 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008391 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008392 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008393 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008394 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395
Bram Moolenaar73633f82012-01-20 13:39:07 +01008396 switch (eap->cmdidx)
8397 {
8398 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8399 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8400 default: break;
8401 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008402 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8403 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008404 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008405#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008406 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008407 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008408#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008409 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008410
Bram Moolenaar39665952018-08-15 20:59:48 +02008411 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008412 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008413 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008414 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008415 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008416 }
8417
Bram Moolenaara16123a2019-03-28 20:31:07 +01008418 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8419 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008420 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008421 p_cpo = empty_option;
8422
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008423 incr_quickfix_busy();
8424
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008425#ifdef FEAT_MULTI_LANG
8426 // Check for a specified language
8427 lang = check_help_lang(eap->arg);
8428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8430 regmatch.rm_ic = FALSE;
8431 if (regmatch.regprog != NULL)
8432 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008433 qf_list_T *qfl;
8434
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008435 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008436 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008437 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008439 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008440
Bram Moolenaar473de612013-06-08 18:19:48 +02008441 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008443 qfl->qf_nonevalid = FALSE;
8444 qfl->qf_ptr = qfl->qf_start;
8445 qfl->qf_index = 1;
8446 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008447 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 }
8449
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008450 if (p_cpo == empty_option)
8451 p_cpo = save_cpo;
8452 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008453 {
8454 // Darn, some plugin changed the value. If it's still empty it was
8455 // changed and restored, need to restore in the complicated way.
8456 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008457 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008458 if (save_cpo_allocated)
8459 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008460 }
8461
8462 if (updated)
8463 // This may open a window and source scripts, do this after 'cpo' was
8464 // restored.
8465 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466
Bram Moolenaar73633f82012-01-20 13:39:07 +01008467 if (au_name != NULL)
8468 {
8469 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8470 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008471 // When adding a location list to an existing location list stack,
8472 // if the autocmd made the stack invalid, then just return.
8473 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008474 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008475 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008476 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008477 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008478 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008479
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008480 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008481 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008482 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008483 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008484 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008485
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008486 decr_quickfix_busy();
8487
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008488 if (eap->cmdidx == CMD_lhelpgrep)
8489 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008490 // If the help window is not opened or if it already points to the
8491 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008492 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008493 {
8494 if (new_qi)
8495 ll_free_all(&qi);
8496 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008497 else if (curwin->w_llist == NULL && new_qi)
8498 // current window didn't have a location list associated with it
8499 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008500 curwin->w_llist = qi;
8501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502}
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008503
8504# if defined(EXITFREE) || defined(PROTO)
8505 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00008506free_quickfix(void)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008507{
8508 win_T *win;
8509 tabpage_T *tab;
8510
8511 qf_free_all(NULL);
8512 // Free all location lists
8513 FOR_ALL_TAB_WINDOWS(tab, win)
8514 qf_free_all(win);
8515
8516 ga_clear(&qfga);
8517}
8518# endif
8519
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008520#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008521
8522#if defined(FEAT_EVAL) || defined(PROTO)
8523# ifdef FEAT_QUICKFIX
8524 static void
8525get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8526{
8527 if (what_arg->v_type == VAR_UNKNOWN)
8528 {
8529 if (rettv_list_alloc(rettv) == OK)
8530 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008531 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008532 }
8533 else
8534 {
8535 if (rettv_dict_alloc(rettv) == OK)
8536 if (is_qf || (wp != NULL))
8537 {
8538 if (what_arg->v_type == VAR_DICT)
8539 {
8540 dict_T *d = what_arg->vval.v_dict;
8541
8542 if (d != NULL)
8543 qf_get_properties(wp, d, rettv->vval.v_dict);
8544 }
8545 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008546 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008547 }
8548 }
8549}
8550# endif
8551
8552/*
8553 * "getloclist()" function
8554 */
8555 void
8556f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8557{
8558# ifdef FEAT_QUICKFIX
8559 win_T *wp;
8560
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008561 if (in_vim9script()
8562 && (check_for_number_arg(argvars, 0) == FAIL
8563 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8564 return;
8565
Bram Moolenaare677df82019-09-02 22:31:11 +02008566 wp = find_win_by_nr_or_id(&argvars[0]);
8567 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8568# endif
8569}
8570
8571/*
8572 * "getqflist()" function
8573 */
8574 void
8575f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8576{
8577# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008578 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8579 return;
8580
Bram Moolenaare677df82019-09-02 22:31:11 +02008581 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8582# endif
8583}
8584
8585/*
8586 * Used by "setqflist()" and "setloclist()" functions
8587 */
8588 static void
8589set_qf_ll_list(
8590 win_T *wp UNUSED,
8591 typval_T *list_arg UNUSED,
8592 typval_T *action_arg UNUSED,
8593 typval_T *what_arg UNUSED,
8594 typval_T *rettv)
8595{
8596# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008597 char_u *act;
8598 int action = 0;
8599 static int recursive = 0;
8600# endif
8601
8602 rettv->vval.v_number = -1;
8603
8604# ifdef FEAT_QUICKFIX
8605 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008606 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008607 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008608 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008609 else
8610 {
8611 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008612 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008613 int valid_dict = TRUE;
8614
8615 if (action_arg->v_type == VAR_STRING)
8616 {
8617 act = tv_get_string_chk(action_arg);
8618 if (act == NULL)
8619 return; // type error; errmsg already given
8620 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8621 act[1] == NUL)
8622 action = *act;
8623 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008624 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02008625 }
8626 else if (action_arg->v_type == VAR_UNKNOWN)
8627 action = ' ';
8628 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008629 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008630
8631 if (action_arg->v_type != VAR_UNKNOWN
8632 && what_arg->v_type != VAR_UNKNOWN)
8633 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008634 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8635 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008636 else
8637 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008638 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008639 valid_dict = FALSE;
8640 }
8641 }
8642
8643 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008644 if (l != NULL && action && valid_dict
8645 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008646 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008647 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008648 rettv->vval.v_number = 0;
8649 --recursive;
8650 }
8651# endif
8652}
8653
8654/*
8655 * "setloclist()" function
8656 */
8657 void
8658f_setloclist(typval_T *argvars, typval_T *rettv)
8659{
8660 win_T *win;
8661
8662 rettv->vval.v_number = -1;
8663
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008664 if (in_vim9script()
8665 && (check_for_number_arg(argvars, 0) == FAIL
8666 || check_for_list_arg(argvars, 1) == FAIL
8667 || check_for_opt_string_arg(argvars, 2) == FAIL
8668 || (argvars[2].v_type != VAR_UNKNOWN
8669 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8670 return;
8671
Bram Moolenaare677df82019-09-02 22:31:11 +02008672 win = find_win_by_nr_or_id(&argvars[0]);
8673 if (win != NULL)
8674 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8675}
8676
8677/*
8678 * "setqflist()" function
8679 */
8680 void
8681f_setqflist(typval_T *argvars, typval_T *rettv)
8682{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008683 if (in_vim9script()
8684 && (check_for_list_arg(argvars, 0) == FAIL
8685 || check_for_opt_string_arg(argvars, 1) == FAIL
8686 || (argvars[1].v_type != VAR_UNKNOWN
8687 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8688 return;
8689
Bram Moolenaare677df82019-09-02 22:31:11 +02008690 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8691}
8692#endif