blob: 6f7d3a58b9a904d9cd1c03ef9458ec3e6ef694e8 [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
Yegappan Lakshmananb7318002023-10-25 20:50:28 +0200123#define FMT_PATTERNS 14 // 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
Yegappan Lakshmananb7318002023-10-25 20:50:28 +0200278 {'b', "\\d\\+"}, // 1
279 {'n', "\\d\\+"}, // 2
280 {'l', "\\d\\+"}, // 3
281 {'e', "\\d\\+"}, // 4
282 {'c', "\\d\\+"}, // 5
283 {'k', "\\d\\+"}, // 6
284 {'t', "."}, // 7
285#define FMT_PATTERN_M 8
286 {'m', ".\\+"}, // 8
287#define FMT_PATTERN_R 9
288 {'r', ".*"}, // 9
289 {'p', "[- .]*"}, // 10
290 {'v', "\\d\\+"}, // 11
291 {'s', ".\\+"}, // 12
292 {'o', ".\\+"} // 13
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200293 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200294
295/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200296 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200297 * See fmt_pat definition above for the list of supported patterns. The
298 * pattern specifier is supplied in "efmpat". The converted pattern is stored
299 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200300 */
301 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200302efmpat_to_regpat(
303 char_u *efmpat,
304 char_u *regpat,
305 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200306 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100307 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200308{
309 char_u *srcptr;
310
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200311 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200312 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200313 // Each errorformat pattern can occur only once
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000314 semsg(_(e_too_many_chr_in_format_string), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200315 return NULL;
316 }
haya14busae023d492022-02-08 18:09:29 +0000317 if ((idx && idx < FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200318 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
haya14busae023d492022-02-08 18:09:29 +0000319 || (idx == FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200320 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200321 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000322 semsg(_(e_unexpected_chr_in_format_str), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200323 return NULL;
324 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200325 efminfo->addr[idx] = (char_u)++round;
326 *regpat++ = '\\';
327 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200328#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200329 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200330 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200331 // Also match "c:" in the file name, even when
332 // checking for a colon next: "%f:".
333 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200334 STRCPY(regpat, "\\%(\\a:\\)\\=");
335 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200336 }
337#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200338 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200339 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200340 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200341 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200342 // A file name may contain spaces, but this isn't
343 // in "\f". For "%f:%l:%m" there may be a ":" in
344 // the file name. Use ".\{-1,}x" instead (x is
345 // the next character), the requirement that :999:
346 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200347 STRCPY(regpat, ".\\{-1,}");
348 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200349 }
350 else
351 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200352 // File name followed by '\\' or '%': include as
353 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200354 STRCPY(regpat, "\\f\\+");
355 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200356 }
357 }
358 else
359 {
360 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200361 while ((*regpat = *srcptr++) != NUL)
362 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200363 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200364 *regpat++ = '\\';
365 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200366
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200367 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200368}
369
370/*
371 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200372 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200373 */
374 static char_u *
375scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200376 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200377 char_u *efm,
378 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100379 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200380{
381 char_u *efmp = *pefmp;
382
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200383 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200384 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200385 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200386 {
387 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200388 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200389 if (efmp < efm + len)
390 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200391 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200392 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200393 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200394 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200395 if (efmp == efm + len)
396 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000397 emsg(_(e_missing_rsb_in_format_string));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200398 return NULL;
399 }
400 }
401 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200402 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200403 *regpat++ = *++efmp;
404 *regpat++ = '\\';
405 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200406 }
407 else
408 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200409 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000410 semsg(_(e_unsupported_chr_in_format_string), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200411 return NULL;
412 }
413
414 *pefmp = efmp;
415
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200416 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200417}
418
419/*
420 * Analyze/parse an errorformat prefix.
421 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200422 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100423efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200424{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200425 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200426 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200427 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200428 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200429 else
430 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000431 semsg(_(e_invalid_chr_in_format_string_prefix), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200432 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200433 }
434
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200435 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200436}
437
438/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200439 * Converts a 'errorformat' string part in 'efm' to a regular expression
440 * pattern. The resulting regex pattern is returned in "regpat". Additional
441 * information about the 'erroformat' pattern is returned in "fmt_ptr".
442 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200443 */
444 static int
445efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200446 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200447 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200448 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100449 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200450{
451 char_u *ptr;
452 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200453 int round;
454 int idx = 0;
455
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200456 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200457 ptr = regpat;
458 *ptr++ = '^';
459 round = 0;
460 for (efmp = efm; efmp < efm + len; ++efmp)
461 {
462 if (*efmp == '%')
463 {
464 ++efmp;
465 for (idx = 0; idx < FMT_PATTERNS; ++idx)
466 if (fmt_pat[idx].convchar == *efmp)
467 break;
468 if (idx < FMT_PATTERNS)
469 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100470 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200471 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200472 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200473 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200474 }
475 else if (*efmp == '*')
476 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200477 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100478 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200479 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200480 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200481 }
482 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200483 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200484 else if (*efmp == '#')
485 *ptr++ = '*';
486 else if (*efmp == '>')
487 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200488 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200489 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200490 // prefix is allowed only at the beginning of the errorformat
491 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100492 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200493 if (efmp == NULL)
494 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200495 }
496 else
497 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000498 semsg(_(e_invalid_chr_in_format_string), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200499 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200500 }
501 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200502 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200503 {
504 if (*efmp == '\\' && efmp + 1 < efm + len)
505 ++efmp;
506 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200507 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200508 if (*efmp)
509 *ptr++ = *efmp;
510 }
511 }
512 *ptr++ = '$';
513 *ptr = NUL;
514
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200515 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200516}
517
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200518/*
519 * Free the 'errorformat' information list
520 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200521 static void
522free_efm_list(efm_T **efm_first)
523{
524 efm_T *efm_ptr;
525
526 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
527 {
528 *efm_first = efm_ptr->next;
529 vim_regfree(efm_ptr->prog);
530 vim_free(efm_ptr);
531 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100532 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200533}
534
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200535/*
536 * Compute the size of the buffer used to convert a 'errorformat' pattern into
537 * a regular expression pattern.
538 */
539 static int
540efm_regpat_bufsz(char_u *efm)
541{
542 int sz;
543 int i;
544
545 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
546 for (i = FMT_PATTERNS; i > 0; )
547 sz += (int)STRLEN(fmt_pat[--i].pattern);
548#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200549 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200550#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200551 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200552#endif
553
554 return sz;
555}
556
557/*
558 * Return the length of a 'errorformat' option part (separated by ",").
559 */
560 static int
561efm_option_part_len(char_u *efm)
562{
563 int len;
564
565 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
566 if (efm[len] == '\\' && efm[len + 1] != NUL)
567 ++len;
568
569 return len;
570}
571
572/*
573 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
574 * are parsed and converted to regular expressions. Returns information about
575 * the parsed 'errorformat' option.
576 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200577 static efm_T *
578parse_efm_option(char_u *efm)
579{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200580 efm_T *fmt_ptr = NULL;
581 efm_T *fmt_first = NULL;
582 efm_T *fmt_last = NULL;
583 char_u *fmtstr = NULL;
584 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200585 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200586
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200587 // Each part of the format string is copied and modified from errorformat
588 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200589
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200590 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200591 sz = efm_regpat_bufsz(efm);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000592 if ((fmtstr = alloc_id(sz, aid_qf_efm_fmtstr)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200593 goto parse_efm_error;
594
595 while (efm[0] != NUL)
596 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200597 // Allocate a new eformat structure and put it at the end of the list
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000598 fmt_ptr = ALLOC_CLEAR_ONE_ID(efm_T, aid_qf_efm_fmtpart);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200599 if (fmt_ptr == NULL)
600 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200601 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200602 fmt_first = fmt_ptr;
603 else
604 fmt_last->next = fmt_ptr;
605 fmt_last = fmt_ptr;
606
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200607 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200608 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200609
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100610 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200611 goto parse_efm_error;
612 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
613 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200614 // Advance to next part
615 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200616 }
617
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200618 if (fmt_first == NULL) // nothing found
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000619 emsg(_(e_errorformat_contains_no_pattern));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200620
621 goto parse_efm_end;
622
623parse_efm_error:
624 free_efm_list(&fmt_first);
625
626parse_efm_end:
627 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200628
629 return fmt_first;
630}
631
Bram Moolenaare0d37972016-07-15 22:36:01 +0200632enum {
633 QF_FAIL = 0,
634 QF_OK = 1,
635 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200636 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200637 QF_IGNORE_LINE = 4,
638 QF_MULTISCAN = 5,
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +0100639 QF_ABORT = 6
Bram Moolenaare0d37972016-07-15 22:36:01 +0200640};
641
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200642/*
643 * State information used to parse lines and add entries to a quickfix/location
644 * list.
645 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200646typedef struct {
647 char_u *linebuf;
648 int linelen;
649 char_u *growbuf;
650 int growbufsiz;
651 FILE *fd;
652 typval_T *tv;
653 char_u *p_str;
654 listitem_T *p_li;
655 buf_T *buf;
656 linenr_T buflnum;
657 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100658 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200659} qfstate_T;
660
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200661/*
662 * Allocate more memory for the line buffer used for parsing lines.
663 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200664 static char_u *
665qf_grow_linebuf(qfstate_T *state, int newsz)
666{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200667 char_u *p;
668
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200669 // If the line exceeds LINE_MAXLEN exclude the last
670 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200671 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
672 if (state->growbuf == NULL)
673 {
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000674 state->growbuf = alloc_id(state->linelen + 1, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200675 if (state->growbuf == NULL)
676 return NULL;
677 state->growbufsiz = state->linelen;
678 }
679 else if (state->linelen > state->growbufsiz)
680 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200681 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200682 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200683 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200684 state->growbufsiz = state->linelen;
685 }
686 return state->growbuf;
687}
688
689/*
690 * Get the next string (separated by newline) from state->p_str.
691 */
692 static int
693qf_get_next_str_line(qfstate_T *state)
694{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200695 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200696 char_u *p_str = state->p_str;
697 char_u *p;
698 int len;
699
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200700 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200701 return QF_END_OF_INPUT;
702
703 p = vim_strchr(p_str, '\n');
704 if (p != NULL)
705 len = (int)(p - p_str) + 1;
706 else
707 len = (int)STRLEN(p_str);
708
709 if (len > IOSIZE - 2)
710 {
711 state->linebuf = qf_grow_linebuf(state, len);
712 if (state->linebuf == NULL)
713 return QF_NOMEM;
714 }
715 else
716 {
717 state->linebuf = IObuff;
718 state->linelen = len;
719 }
720 vim_strncpy(state->linebuf, p_str, state->linelen);
721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200722 // Increment using len in order to discard the rest of the
723 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200724 p_str += len;
725 state->p_str = p_str;
726
727 return QF_OK;
728}
729
730/*
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000731 * Get the next string from the List item state->p_li.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200732 */
733 static int
734qf_get_next_list_line(qfstate_T *state)
735{
736 listitem_T *p_li = state->p_li;
737 int len;
738
739 while (p_li != NULL
740 && (p_li->li_tv.v_type != VAR_STRING
741 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200742 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200743
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200744 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200745 {
746 state->p_li = NULL;
747 return QF_END_OF_INPUT;
748 }
749
750 len = (int)STRLEN(p_li->li_tv.vval.v_string);
751 if (len > IOSIZE - 2)
752 {
753 state->linebuf = qf_grow_linebuf(state, len);
754 if (state->linebuf == NULL)
755 return QF_NOMEM;
756 }
757 else
758 {
759 state->linebuf = IObuff;
760 state->linelen = len;
761 }
762
763 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
764
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200765 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200766 return QF_OK;
767}
768
769/*
770 * Get the next string from state->buf.
771 */
772 static int
773qf_get_next_buf_line(qfstate_T *state)
774{
775 char_u *p_buf = NULL;
776 int len;
777
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200778 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200779 if (state->buflnum > state->lnumlast)
780 return QF_END_OF_INPUT;
781
782 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +0100783 len = ml_get_buf_len(state->buf, state->buflnum);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200784 state->buflnum += 1;
785
Bram Moolenaare0d37972016-07-15 22:36:01 +0200786 if (len > IOSIZE - 2)
787 {
788 state->linebuf = qf_grow_linebuf(state, len);
789 if (state->linebuf == NULL)
790 return QF_NOMEM;
791 }
792 else
793 {
794 state->linebuf = IObuff;
795 state->linelen = len;
796 }
797 vim_strncpy(state->linebuf, p_buf, state->linelen);
798
799 return QF_OK;
800}
801
802/*
803 * Get the next string from file state->fd.
804 */
805 static int
806qf_get_next_file_line(qfstate_T *state)
807{
808 int discard;
809 int growbuflen;
810
811 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
812 return QF_END_OF_INPUT;
813
814 discard = FALSE;
815 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200816 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200817 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200818 // The current line exceeds IObuff, continue reading using
819 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200820 if (state->growbuf == NULL)
821 {
822 state->growbufsiz = 2 * (IOSIZE - 1);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000823 state->growbuf = alloc_id(state->growbufsiz, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200824 if (state->growbuf == NULL)
825 return QF_NOMEM;
826 }
827
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200828 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200829 memcpy(state->growbuf, IObuff, IOSIZE - 1);
830 growbuflen = state->linelen;
831
832 for (;;)
833 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200834 char_u *p;
835
Bram Moolenaare0d37972016-07-15 22:36:01 +0200836 if (fgets((char *)state->growbuf + growbuflen,
837 state->growbufsiz - growbuflen, state->fd) == NULL)
838 break;
839 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
840 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200841 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200842 break;
843 if (state->growbufsiz == LINE_MAXLEN)
844 {
845 discard = TRUE;
846 break;
847 }
848
849 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
850 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200851 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200852 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200853 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200854 }
855
856 while (discard)
857 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200858 // The current line is longer than LINE_MAXLEN, continue
859 // reading but discard everything until EOL or EOF is
860 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200861 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
862 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar59941cb2020-09-05 17:03:40 +0200863 || IObuff[IOSIZE - 2] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200864 break;
865 }
866
867 state->linebuf = state->growbuf;
868 state->linelen = growbuflen;
869 }
870 else
871 state->linebuf = IObuff;
872
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200873 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200874 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
875 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100876 char_u *line;
877
878 line = string_convert(&state->vc, state->linebuf, &state->linelen);
879 if (line != NULL)
880 {
881 if (state->linelen < IOSIZE)
882 {
883 STRCPY(state->linebuf, line);
884 vim_free(line);
885 }
886 else
887 {
888 vim_free(state->growbuf);
889 state->linebuf = state->growbuf = line;
890 state->growbufsiz = state->linelen < LINE_MAXLEN
891 ? state->linelen : LINE_MAXLEN;
892 }
893 }
894 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100895
Bram Moolenaare0d37972016-07-15 22:36:01 +0200896 return QF_OK;
897}
898
899/*
900 * Get the next string from a file/buffer/list/string.
901 */
902 static int
903qf_get_nextline(qfstate_T *state)
904{
905 int status = QF_FAIL;
906
907 if (state->fd == NULL)
908 {
909 if (state->tv != NULL)
910 {
911 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200912 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200913 status = qf_get_next_str_line(state);
914 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200915 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200916 status = qf_get_next_list_line(state);
917 }
918 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200919 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200920 status = qf_get_next_buf_line(state);
921 }
922 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200923 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200924 status = qf_get_next_file_line(state);
925
926 if (status != QF_OK)
927 return status;
928
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200929 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200930 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200931 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200932 state->linebuf[state->linelen - 1] = NUL;
933#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200934 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
935 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200936#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200937 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200938
Bram Moolenaare0d37972016-07-15 22:36:01 +0200939 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200940
941 return QF_OK;
942}
943
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200944typedef struct {
945 char_u *namebuf;
Yegappan Lakshmananb7318002023-10-25 20:50:28 +0200946 int bnr;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200947 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200948 char_u *errmsg;
949 int errmsglen;
950 long lnum;
thinca6864efa2021-06-19 20:45:20 +0200951 long end_lnum;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200952 int col;
thinca6864efa2021-06-19 20:45:20 +0200953 int end_col;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200954 char_u use_viscol;
955 char_u *pattern;
956 int enr;
957 int type;
Tom Praschanca6ac992023-08-11 23:26:12 +0200958 typval_T *user_data;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200959 int valid;
960} qffields_T;
961
962/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200963 * Parse the match for filename ('%f') pattern in regmatch.
964 * Return the matched value in "fields->namebuf".
965 */
966 static int
967qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
968{
969 int c;
970
971 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
972 return QF_FAIL;
973
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200974 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200975 c = *rmp->endp[midx];
976 *rmp->endp[midx] = NUL;
977 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
978 *rmp->endp[midx] = c;
979
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200980 // For separate filename patterns (%O, %P and %Q), the specified file
981 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200982 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
983 && mch_getperm(fields->namebuf) == -1)
984 return QF_FAIL;
985
986 return QF_OK;
987}
988
989/*
zeertzjqb0221812023-10-26 23:15:44 +0200990 * Parse the match for buffer number ('%b') pattern in regmatch.
991 * Return the matched value in "fields->bnr".
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200992 */
993 static int
Yegappan Lakshmananb7318002023-10-25 20:50:28 +0200994qf_parse_fmt_b(regmatch_T *rmp, int midx, qffields_T *fields)
995{
996 if (rmp->startp[midx] == NULL)
997 return QF_FAIL;
998 int bnr = (int)atol((char *)rmp->startp[midx]);
999 if (buflist_findnr(bnr) == NULL)
1000 return QF_FAIL;
1001 fields->bnr = bnr;
1002 return QF_OK;
1003}
1004
1005/*
1006 * Parse the match for error number ('%n') pattern in regmatch.
1007 * Return the matched value in "fields->enr".
1008 */
1009 static int
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001010qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
1011{
1012 if (rmp->startp[midx] == NULL)
1013 return QF_FAIL;
1014 fields->enr = (int)atol((char *)rmp->startp[midx]);
1015 return QF_OK;
1016}
1017
1018/*
haya14busae023d492022-02-08 18:09:29 +00001019 * Parse the match for line number ('%l') pattern in regmatch.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001020 * Return the matched value in "fields->lnum".
1021 */
1022 static int
1023qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
1024{
1025 if (rmp->startp[midx] == NULL)
1026 return QF_FAIL;
1027 fields->lnum = atol((char *)rmp->startp[midx]);
1028 return QF_OK;
1029}
1030
1031/*
haya14busae023d492022-02-08 18:09:29 +00001032 * Parse the match for end line number ('%e') pattern in regmatch.
1033 * Return the matched value in "fields->end_lnum".
1034 */
1035 static int
1036qf_parse_fmt_e(regmatch_T *rmp, int midx, qffields_T *fields)
1037{
1038 if (rmp->startp[midx] == NULL)
1039 return QF_FAIL;
1040 fields->end_lnum = atol((char *)rmp->startp[midx]);
1041 return QF_OK;
1042}
1043
1044/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001045 * Parse the match for column number ('%c') pattern in regmatch.
1046 * Return the matched value in "fields->col".
1047 */
1048 static int
1049qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
1050{
1051 if (rmp->startp[midx] == NULL)
1052 return QF_FAIL;
1053 fields->col = (int)atol((char *)rmp->startp[midx]);
1054 return QF_OK;
1055}
1056
1057/*
haya14busae023d492022-02-08 18:09:29 +00001058 * Parse the match for end column number ('%k') pattern in regmatch.
1059 * Return the matched value in "fields->end_col".
1060 */
1061 static int
1062qf_parse_fmt_k(regmatch_T *rmp, int midx, qffields_T *fields)
1063{
1064 if (rmp->startp[midx] == NULL)
1065 return QF_FAIL;
1066 fields->end_col = (int)atol((char *)rmp->startp[midx]);
1067 return QF_OK;
1068}
1069
1070/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001071 * Parse the match for error type ('%t') pattern in regmatch.
1072 * Return the matched value in "fields->type".
1073 */
1074 static int
1075qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
1076{
1077 if (rmp->startp[midx] == NULL)
1078 return QF_FAIL;
1079 fields->type = *rmp->startp[midx];
1080 return QF_OK;
1081}
1082
1083/*
Bram Moolenaarf4140482020-02-15 23:06:45 +01001084 * Copy a non-error line into the error string. Return the matched line in
1085 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001086 */
1087 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +01001088copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001089{
1090 char_u *p;
1091
1092 if (linelen >= fields->errmsglen)
1093 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001094 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001095 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1096 return QF_NOMEM;
1097 fields->errmsg = p;
1098 fields->errmsglen = linelen + 1;
1099 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001100 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001101 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001102
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001103 return QF_OK;
1104}
1105
1106/*
1107 * Parse the match for error message ('%m') pattern in regmatch.
1108 * Return the matched value in "fields->errmsg".
1109 */
1110 static int
1111qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1112{
1113 char_u *p;
1114 int len;
1115
1116 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1117 return QF_FAIL;
1118 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1119 if (len >= fields->errmsglen)
1120 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001121 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001122 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1123 return QF_NOMEM;
1124 fields->errmsg = p;
1125 fields->errmsglen = len + 1;
1126 }
1127 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1128 return QF_OK;
1129}
1130
1131/*
1132 * Parse the match for rest of a single-line file message ('%r') pattern.
1133 * Return the matched value in "tail".
1134 */
1135 static int
1136qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1137{
1138 if (rmp->startp[midx] == NULL)
1139 return QF_FAIL;
1140 *tail = rmp->startp[midx];
1141 return QF_OK;
1142}
1143
1144/*
1145 * Parse the match for the pointer line ('%p') pattern in regmatch.
1146 * Return the matched value in "fields->col".
1147 */
1148 static int
1149qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1150{
1151 char_u *match_ptr;
1152
1153 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1154 return QF_FAIL;
1155 fields->col = 0;
1156 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1157 ++match_ptr)
1158 {
1159 ++fields->col;
1160 if (*match_ptr == TAB)
1161 {
1162 fields->col += 7;
1163 fields->col -= fields->col % 8;
1164 }
1165 }
1166 ++fields->col;
1167 fields->use_viscol = TRUE;
1168 return QF_OK;
1169}
1170
1171/*
1172 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1173 * Return the matched value in "fields->col".
1174 */
1175 static int
1176qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1177{
1178 if (rmp->startp[midx] == NULL)
1179 return QF_FAIL;
1180 fields->col = (int)atol((char *)rmp->startp[midx]);
1181 fields->use_viscol = TRUE;
1182 return QF_OK;
1183}
1184
1185/*
1186 * Parse the match for the search text ('%s') pattern in regmatch.
1187 * Return the matched value in "fields->pattern".
1188 */
1189 static int
1190qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1191{
1192 int len;
1193
1194 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1195 return QF_FAIL;
1196 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1197 if (len > CMDBUFFSIZE - 5)
1198 len = CMDBUFFSIZE - 5;
1199 STRCPY(fields->pattern, "^\\V");
1200 STRNCAT(fields->pattern, rmp->startp[midx], len);
1201 fields->pattern[len + 3] = '\\';
1202 fields->pattern[len + 4] = '$';
1203 fields->pattern[len + 5] = NUL;
1204 return QF_OK;
1205}
1206
1207/*
1208 * Parse the match for the module ('%o') pattern in regmatch.
1209 * Return the matched value in "fields->module".
1210 */
1211 static int
1212qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1213{
1214 int len;
1215
1216 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1217 return QF_FAIL;
1218 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1219 if (len > CMDBUFFSIZE)
1220 len = CMDBUFFSIZE;
1221 STRNCAT(fields->module, rmp->startp[midx], len);
1222 return QF_OK;
1223}
1224
1225/*
1226 * 'errorformat' format pattern parser functions.
1227 * The '%f' and '%r' formats are parsed differently from other formats.
1228 * See qf_parse_match() for details.
haya14busae023d492022-02-08 18:09:29 +00001229 * Keep in sync with fmt_pat[].
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001230 */
1231static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1232{
haya14busae023d492022-02-08 18:09:29 +00001233 NULL, // %f
Yegappan Lakshmananb7318002023-10-25 20:50:28 +02001234 qf_parse_fmt_b,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001235 qf_parse_fmt_n,
1236 qf_parse_fmt_l,
haya14busae023d492022-02-08 18:09:29 +00001237 qf_parse_fmt_e,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001238 qf_parse_fmt_c,
haya14busae023d492022-02-08 18:09:29 +00001239 qf_parse_fmt_k,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001240 qf_parse_fmt_t,
1241 qf_parse_fmt_m,
haya14busae023d492022-02-08 18:09:29 +00001242 NULL, // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001243 qf_parse_fmt_p,
1244 qf_parse_fmt_v,
1245 qf_parse_fmt_s,
1246 qf_parse_fmt_o
1247};
1248
1249/*
1250 * Parse the error format pattern matches in "regmatch" and set the values in
1251 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1252 * match. Returns QF_OK if all the matches are successfully parsed. On
1253 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001254 */
1255 static int
1256qf_parse_match(
1257 char_u *linebuf,
1258 int linelen,
1259 efm_T *fmt_ptr,
1260 regmatch_T *regmatch,
1261 qffields_T *fields,
1262 int qf_multiline,
1263 int qf_multiscan,
1264 char_u **tail)
1265{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001266 int idx = fmt_ptr->prefix;
1267 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001268 int midx;
1269 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001270
1271 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1272 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001273 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001274 fields->type = idx;
1275 else
1276 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001277
1278 // Extract error message data from matched line.
1279 // We check for an actual submatch, because "\[" and "\]" in
1280 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001281 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001282 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001283 status = QF_OK;
1284 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001285 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001286 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
haya14busae023d492022-02-08 18:09:29 +00001287 else if (i == FMT_PATTERN_M)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001288 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001289 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001290 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001291 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001292 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001293 }
haya14busae023d492022-02-08 18:09:29 +00001294 else if (i == FMT_PATTERN_R && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001295 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001296 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001297 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001298
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001299 if (status != QF_OK)
1300 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001301 }
1302
1303 return QF_OK;
1304}
1305
1306/*
1307 * Parse an error line in 'linebuf' using a single error format string in
1308 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1309 * Returns QF_OK if the efm format matches completely and the fields are
1310 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1311 */
1312 static int
1313qf_parse_get_fields(
1314 char_u *linebuf,
1315 int linelen,
1316 efm_T *fmt_ptr,
1317 qffields_T *fields,
1318 int qf_multiline,
1319 int qf_multiscan,
1320 char_u **tail)
1321{
1322 regmatch_T regmatch;
1323 int status = QF_FAIL;
1324 int r;
1325
1326 if (qf_multiscan &&
1327 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1328 return QF_FAIL;
1329
1330 fields->namebuf[0] = NUL;
Yegappan Lakshmananb7318002023-10-25 20:50:28 +02001331 fields->bnr = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001332 fields->module[0] = NUL;
1333 fields->pattern[0] = NUL;
1334 if (!qf_multiscan)
1335 fields->errmsg[0] = NUL;
1336 fields->lnum = 0;
thinca6864efa2021-06-19 20:45:20 +02001337 fields->end_lnum = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001338 fields->col = 0;
thinca6864efa2021-06-19 20:45:20 +02001339 fields->end_col = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001340 fields->use_viscol = FALSE;
1341 fields->enr = -1;
1342 fields->type = 0;
1343 *tail = NULL;
1344
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001345 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001346 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001347 regmatch.regprog = fmt_ptr->prog;
1348 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1349 fmt_ptr->prog = regmatch.regprog;
1350 if (r)
1351 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1352 fields, qf_multiline, qf_multiscan, tail);
1353
1354 return status;
1355}
1356
1357/*
1358 * Parse directory error format prefixes (%D and %X).
1359 * Push and pop directories from the directory stack when scanning directory
1360 * names.
1361 */
1362 static int
1363qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1364{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001365 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001366 {
1367 if (*fields->namebuf == NUL)
1368 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001369 emsg(_(e_missing_or_empty_directory_name));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001370 return QF_FAIL;
1371 }
1372 qfl->qf_directory =
1373 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1374 if (qfl->qf_directory == NULL)
1375 return QF_FAIL;
1376 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001377 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001378 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1379
1380 return QF_OK;
1381}
1382
1383/*
1384 * Parse global file name error format prefixes (%O, %P and %Q).
1385 */
1386 static int
1387qf_parse_file_pfx(
1388 int idx,
1389 qffields_T *fields,
1390 qf_list_T *qfl,
1391 char_u *tail)
1392{
1393 fields->valid = FALSE;
1394 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1395 {
1396 if (*fields->namebuf && idx == 'P')
1397 qfl->qf_currfile =
1398 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1399 else if (idx == 'Q')
1400 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1401 *fields->namebuf = NUL;
1402 if (tail && *tail)
1403 {
1404 STRMOVE(IObuff, skipwhite(tail));
1405 qfl->qf_multiscan = TRUE;
1406 return QF_MULTISCAN;
1407 }
1408 }
1409
1410 return QF_OK;
1411}
1412
1413/*
1414 * Parse a non-error line (a line which doesn't match any of the error
1415 * format in 'efm').
1416 */
1417 static int
1418qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1419{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001420 fields->namebuf[0] = NUL; // no match found, remove file name
1421 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001422 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001423
Bram Moolenaarf4140482020-02-15 23:06:45 +01001424 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001425}
1426
1427/*
1428 * Parse multi-line error format prefixes (%C and %Z)
1429 */
1430 static int
1431qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001432 int idx,
1433 qf_list_T *qfl,
1434 qffields_T *fields)
1435{
1436 char_u *ptr;
1437 int len;
1438
1439 if (!qfl->qf_multiignore)
1440 {
1441 qfline_T *qfprev = qfl->qf_last;
1442
1443 if (qfprev == NULL)
1444 return QF_FAIL;
1445 if (*fields->errmsg && !qfl->qf_multiignore)
1446 {
1447 len = (int)STRLEN(qfprev->qf_text);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00001448 ptr = alloc_id(len + STRLEN(fields->errmsg) + 2,
1449 aid_qf_multiline_pfx);
1450 if (ptr == NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001451 return QF_FAIL;
1452 STRCPY(ptr, qfprev->qf_text);
1453 vim_free(qfprev->qf_text);
1454 qfprev->qf_text = ptr;
1455 *(ptr += len) = '\n';
1456 STRCPY(++ptr, fields->errmsg);
1457 }
1458 if (qfprev->qf_nr == -1)
1459 qfprev->qf_nr = fields->enr;
1460 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001461 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001462 qfprev->qf_type = fields->type;
1463
1464 if (!qfprev->qf_lnum)
1465 qfprev->qf_lnum = fields->lnum;
haya14busae023d492022-02-08 18:09:29 +00001466 if (!qfprev->qf_end_lnum)
1467 qfprev->qf_end_lnum = fields->end_lnum;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001468 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001469 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001470 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001471 qfprev->qf_viscol = fields->use_viscol;
1472 }
haya14busae023d492022-02-08 18:09:29 +00001473 if (!qfprev->qf_end_col)
1474 qfprev->qf_end_col = fields->end_col;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001475 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001476 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001477 qfl->qf_directory,
1478 *fields->namebuf || qfl->qf_directory != NULL
1479 ? fields->namebuf
1480 : qfl->qf_currfile != NULL && fields->valid
1481 ? qfl->qf_currfile : 0);
1482 }
1483 if (idx == 'Z')
1484 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1485 line_breakcheck();
1486
1487 return QF_IGNORE_LINE;
1488}
1489
1490/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001491 * Parse a line and get the quickfix fields.
1492 * Return the QF_ status.
1493 */
1494 static int
1495qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001496 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001497 char_u *linebuf,
1498 int linelen,
1499 efm_T *fmt_first,
1500 qffields_T *fields)
1501{
1502 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001503 int idx = 0;
1504 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001505 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001506
Bram Moolenaare333e792018-04-08 13:27:39 +02001507restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001508 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001509 if (fmt_start == NULL)
1510 fmt_ptr = fmt_first;
1511 else
1512 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001513 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001514 fmt_ptr = fmt_start;
1515 fmt_start = NULL;
1516 }
1517
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001518 // Try to match each part of 'errorformat' until we find a complete
1519 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001520 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001521 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1522 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001523 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001524 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1525 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1526 if (status == QF_NOMEM)
1527 return status;
1528 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001529 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001530 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001531 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001532
1533 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1534 {
1535 if (fmt_ptr != NULL)
1536 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001537 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001538 status = qf_parse_dir_pfx(idx, fields, qfl);
1539 if (status != QF_OK)
1540 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001541 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001542
1543 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1544 if (status != QF_OK)
1545 return status;
1546
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001547 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001548 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001549 }
1550 else if (fmt_ptr != NULL)
1551 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001552 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001553 if (fmt_ptr->conthere)
1554 fmt_start = fmt_ptr;
1555
Bram Moolenaare9283662020-06-07 14:10:47 +02001556 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001557 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001558 qfl->qf_multiline = TRUE; // start of a multi-line message
1559 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001560 }
1561 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001562 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001563 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001564 if (status != QF_OK)
1565 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001566 }
1567 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001568 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001569 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1570 if (status == QF_MULTISCAN)
1571 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001572 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001573 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001574 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001575 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001576 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001577 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001578 return QF_IGNORE_LINE;
1579 }
1580 }
1581
1582 return QF_OK;
1583}
1584
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001585/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001586 * Returns TRUE if the specified quickfix/location stack is empty
1587 */
1588 static int
1589qf_stack_empty(qf_info_T *qi)
1590{
1591 return qi == NULL || qi->qf_listcount <= 0;
1592}
1593
1594/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001595 * Returns TRUE if the specified quickfix/location list is empty.
1596 */
1597 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001598qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001599{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001600 return qfl == NULL || qfl->qf_count <= 0;
1601}
1602
1603/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001604 * Returns TRUE if the specified quickfix/location list is not empty and
1605 * has valid entries.
1606 */
1607 static int
1608qf_list_has_valid_entries(qf_list_T *qfl)
1609{
1610 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1611}
1612
1613/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001614 * Return a pointer to a list in the specified quickfix stack
1615 */
1616 static qf_list_T *
1617qf_get_list(qf_info_T *qi, int idx)
1618{
1619 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001620}
1621
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001622/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001623 * Allocate the fields used for parsing lines and populating a quickfix list.
1624 */
1625 static int
1626qf_alloc_fields(qffields_T *pfields)
1627{
1628 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1629 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1630 pfields->errmsglen = CMDBUFFSIZE + 1;
1631 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1632 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1633 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1634 || pfields->pattern == NULL || pfields->module == NULL)
1635 return FAIL;
1636
1637 return OK;
1638}
1639
1640/*
1641 * Free the fields used for parsing lines and populating a quickfix list.
1642 */
1643 static void
1644qf_free_fields(qffields_T *pfields)
1645{
1646 vim_free(pfields->namebuf);
1647 vim_free(pfields->module);
1648 vim_free(pfields->errmsg);
1649 vim_free(pfields->pattern);
1650}
1651
1652/*
1653 * Setup the state information used for parsing lines and populating a
1654 * quickfix list.
1655 */
1656 static int
1657qf_setup_state(
1658 qfstate_T *pstate,
1659 char_u *enc,
1660 char_u *efile,
1661 typval_T *tv,
1662 buf_T *buf,
1663 linenr_T lnumfirst,
1664 linenr_T lnumlast)
1665{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001666 pstate->vc.vc_type = CONV_NONE;
1667 if (enc != NULL && *enc != NUL)
1668 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001669
1670 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1671 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001672 semsg(_(e_cant_open_errorfile_str), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001673 return FAIL;
1674 }
1675
1676 if (tv != NULL)
1677 {
1678 if (tv->v_type == VAR_STRING)
1679 pstate->p_str = tv->vval.v_string;
1680 else if (tv->v_type == VAR_LIST)
1681 pstate->p_li = tv->vval.v_list->lv_first;
1682 pstate->tv = tv;
1683 }
1684 pstate->buf = buf;
1685 pstate->buflnum = lnumfirst;
1686 pstate->lnumlast = lnumlast;
1687
1688 return OK;
1689}
1690
1691/*
1692 * Cleanup the state information used for parsing lines and populating a
1693 * quickfix list.
1694 */
1695 static void
1696qf_cleanup_state(qfstate_T *pstate)
1697{
1698 if (pstate->fd != NULL)
1699 fclose(pstate->fd);
1700
1701 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001702 if (pstate->vc.vc_type != CONV_NONE)
1703 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001704}
1705
1706/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001707 * Process the next line from a file/buffer/list/string and add it
1708 * to the quickfix list 'qfl'.
1709 */
1710 static int
1711qf_init_process_nextline(
1712 qf_list_T *qfl,
1713 efm_T *fmt_first,
1714 qfstate_T *state,
1715 qffields_T *fields)
1716{
1717 int status;
1718
1719 // Get the next line from a file/buffer/list/string
1720 status = qf_get_nextline(state);
1721 if (status != QF_OK)
1722 return status;
1723
1724 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1725 fmt_first, fields);
1726 if (status != QF_OK)
1727 return status;
1728
1729 return qf_add_entry(qfl,
1730 qfl->qf_directory,
1731 (*fields->namebuf || qfl->qf_directory != NULL)
1732 ? fields->namebuf
1733 : ((qfl->qf_currfile != NULL && fields->valid)
1734 ? qfl->qf_currfile : (char_u *)NULL),
1735 fields->module,
Yegappan Lakshmananb7318002023-10-25 20:50:28 +02001736 fields->bnr,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001737 fields->errmsg,
1738 fields->lnum,
thinca6864efa2021-06-19 20:45:20 +02001739 fields->end_lnum,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001740 fields->col,
thinca6864efa2021-06-19 20:45:20 +02001741 fields->end_col,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001742 fields->use_viscol,
1743 fields->pattern,
1744 fields->enr,
1745 fields->type,
Tom Praschanca6ac992023-08-11 23:26:12 +02001746 fields->user_data,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001747 fields->valid);
1748}
1749
1750/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001751 * Read the errorfile "efile" into memory, line by line, building the error
1752 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001753 * Alternative: when "efile" is NULL read errors from buffer "buf".
1754 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001755 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001756 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1757 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001758 * Return -1 for error, number of errors for success.
1759 */
1760 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001761qf_init_ext(
1762 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001763 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001764 char_u *efile,
1765 buf_T *buf,
1766 typval_T *tv,
1767 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001768 int newlist, // TRUE: start a new error list
1769 linenr_T lnumfirst, // first line number to use
1770 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001771 char_u *qf_title,
1772 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001773{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001774 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001775 qfstate_T state;
1776 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001777 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001778 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001779 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001781 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001782 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001783 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001785 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001786 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001787
Bram Moolenaara80faa82020-04-12 19:37:17 +02001788 CLEAR_FIELD(state);
1789 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001790 if ((qf_alloc_fields(&fields) == FAIL) ||
1791 (qf_setup_state(&state, enc, efile, tv, buf,
1792 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 goto qf_init_end;
1794
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001795 if (newlist || qf_idx == qi->qf_listcount)
1796 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001797 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001798 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001799 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001800 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001801 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001802 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001803 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001804 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001805 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001806 qfl = qf_get_list(qi, qf_idx);
1807 if (!qf_list_empty(qfl))
1808 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001811 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001812 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001813 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 else
1815 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001817 // If the errorformat didn't change between calls, then reuse the
1818 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001819 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1820 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001821 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001822 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001823 free_efm_list(&fmt_first);
1824
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001825 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001826 fmt_first = parse_efm_option(efm);
1827 if (fmt_first != NULL)
1828 last_efm = vim_strsave(efm);
1829 }
1830
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001831 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001834 // got_int is reset here, because it was probably set when killing the
1835 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 got_int = FALSE;
1837
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001838 // Read the lines in the error file one by one.
1839 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001840 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001842 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001843 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001844 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001845 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001846 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001847 if (status == QF_FAIL)
1848 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 line_breakcheck();
1851 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001852 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001854 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001856 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001857 qfl->qf_ptr = qfl->qf_start;
1858 qfl->qf_index = 1;
1859 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 }
1861 else
1862 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001863 qfl->qf_nonevalid = FALSE;
1864 if (qfl->qf_ptr == NULL)
1865 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001867 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001868 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001869 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 }
Bram Moolenaard8e44472021-07-21 22:20:33 +02001871 emsg(_(e_error_while_reading_errorfile));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001873 if (!adding)
1874 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001875 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001876 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001877 qi->qf_listcount--;
1878 if (qi->qf_curlist > 0)
1879 --qi->qf_curlist;
1880 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001881qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001882 if (qf_idx == qi->qf_curlist)
1883 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001884 qf_cleanup_state(&state);
1885 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886
1887 return retval;
1888}
1889
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001890/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001891 * Read the errorfile "efile" into memory, line by line, building the error
1892 * list. Set the error list's title to qf_title.
1893 * Return -1 for error, number of errors for success.
1894 */
1895 int
1896qf_init(win_T *wp,
1897 char_u *efile,
1898 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001899 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001900 char_u *qf_title,
1901 char_u *enc)
1902{
1903 qf_info_T *qi = &ql_info;
1904
1905 if (wp != NULL)
1906 {
1907 qi = ll_get_or_alloc_list(wp);
1908 if (qi == NULL)
1909 return FAIL;
1910 }
1911
1912 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1913 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1914}
1915
1916/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001917 * Set the title of the specified quickfix list. Frees the previous title.
1918 * Prepends ':' to the title.
1919 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001920 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001921qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001922{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001923 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001924
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001925 if (title == NULL)
1926 return;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001927
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001928 char_u *p = alloc_id(STRLEN(title) + 2, aid_qf_title);
1929
1930 qfl->qf_title = p;
1931 if (p != NULL)
1932 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001933}
1934
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001936 * The title of a quickfix/location list is set, by default, to the command
1937 * that created the quickfix list with the ":" prefix.
1938 * Create a quickfix list title string by prepending ":" to a user command.
1939 * Returns a pointer to a static buffer with the title.
1940 */
1941 static char_u *
1942qf_cmdtitle(char_u *cmd)
1943{
1944 static char_u qftitle_str[IOSIZE];
1945
1946 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1947 return qftitle_str;
1948}
1949
1950/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001951 * Return a pointer to the current list in the specified quickfix stack
1952 */
1953 static qf_list_T *
1954qf_get_curlist(qf_info_T *qi)
1955{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001956 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001957}
1958
1959/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001960 * Prepare for adding a new quickfix list. If the current list is in the
1961 * middle of the stack, then all the following lists are freed and then
1962 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 */
1964 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001965qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967 int i;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001968 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001970 // If the current entry is not the last entry, delete entries beyond
1971 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001972 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001973 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001974 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001976 // When the stack is full, remove to oldest entry
1977 // Otherwise, add a new entry.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001978 if (qi->qf_listcount == LISTCOUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001980 qf_free(&qi->qf_lists[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 for (i = 1; i < LISTCOUNT; ++i)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001982 qi->qf_lists[i - 1] = qi->qf_lists[i];
1983 qi->qf_curlist = LISTCOUNT - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
1985 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001986 qi->qf_curlist = qi->qf_listcount++;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001987 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001988 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001989 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01001990 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001991 qfl->qf_id = ++last_qf_id;
Tom Praschanca6ac992023-08-11 23:26:12 +02001992 qfl->qf_has_user_data = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993}
1994
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001995/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001996 * Queue location list stack delete request.
1997 */
1998 static void
1999locstack_queue_delreq(qf_info_T *qi)
2000{
2001 qf_delq_T *q;
2002
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002003 q = ALLOC_ONE(qf_delq_T);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002004 if (q == NULL)
2005 return;
2006
2007 q->qi = qi;
2008 q->next = qf_delq_head;
2009 qf_delq_head = q;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002010}
2011
2012/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002013 * Return the global quickfix stack window buffer number.
2014 */
2015 int
2016qf_stack_get_bufnr(void)
2017{
2018 return ql_info.qf_bufnr;
2019}
2020
2021/*
2022 * Wipe the quickfix window buffer (if present) for the specified
2023 * quickfix/location list.
2024 */
2025 static void
2026wipe_qf_buffer(qf_info_T *qi)
2027{
2028 buf_T *qfbuf;
2029
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002030 if (qi->qf_bufnr == INVALID_QFBUFNR)
2031 return;
2032
2033 qfbuf = buflist_findnr(qi->qf_bufnr);
2034 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002035 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002036 // If the quickfix buffer is not loaded in any window, then
2037 // wipe the buffer.
2038 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
2039 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002040 }
2041}
2042
2043/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002044 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002045 */
2046 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002047ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002048{
2049 int i;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002050 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002051
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002052 qi = *pqi;
2053 if (qi == NULL)
2054 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002055 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002056
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002057 // If the location list is still in use, then queue the delete request
2058 // to be processed later.
2059 if (quickfix_busy > 0)
2060 {
2061 locstack_queue_delreq(qi);
2062 return;
2063 }
2064
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002065 qi->qf_refcount--;
2066 if (qi->qf_refcount < 1)
2067 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002068 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002069 // If the quickfix window buffer is loaded, then wipe it
2070 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002071
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002072 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002073 qf_free(qf_get_list(qi, i));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002074 vim_free(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002075 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002076}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002077
Bram Moolenaar18cebf42018-05-08 22:31:37 +02002078/*
2079 * Free all the quickfix/location lists in the stack.
2080 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002081 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002082qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002083{
2084 int i;
2085 qf_info_T *qi = &ql_info;
2086
2087 if (wp != NULL)
2088 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002089 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002090 ll_free_all(&wp->w_llist);
2091 ll_free_all(&wp->w_llist_ref);
2092 }
2093 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002094 // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002095 for (i = 0; i < qi->qf_listcount; ++i)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002096 qf_free(qf_get_list(qi, i));
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002097}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002098
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002100 * Delay freeing of location list stacks when the quickfix code is running.
2101 * Used to avoid problems with autocmds freeing location list stacks when the
2102 * quickfix code is still referencing the stack.
2103 * Must always call decr_quickfix_busy() exactly once after this.
2104 */
2105 static void
2106incr_quickfix_busy(void)
2107{
2108 quickfix_busy++;
2109}
2110
2111/*
2112 * Safe to free location list stacks. Process any delayed delete requests.
2113 */
2114 static void
2115decr_quickfix_busy(void)
2116{
2117 if (--quickfix_busy == 0)
2118 {
2119 // No longer referencing the location lists. Process all the pending
2120 // delete requests.
2121 while (qf_delq_head != NULL)
2122 {
2123 qf_delq_T *q = qf_delq_head;
2124
2125 qf_delq_head = q->next;
2126 ll_free_all(&q->qi);
2127 vim_free(q);
2128 }
2129 }
2130#ifdef ABORT_ON_INTERNAL_ERROR
2131 if (quickfix_busy < 0)
2132 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002133 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002134 abort();
2135 }
2136#endif
2137}
2138
2139#if defined(EXITFREE) || defined(PROTO)
2140 void
2141check_quickfix_busy(void)
2142{
2143 if (quickfix_busy != 0)
2144 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002145 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002146# ifdef ABORT_ON_INTERNAL_ERROR
2147 abort();
2148# endif
2149 }
2150}
2151#endif
2152
2153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 * Add an entry to the end of the list of errors.
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00002155 * Returns QF_OK on success or QF_FAIL on a memory allocation failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 */
2157 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002158qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002159 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002160 char_u *dir, // optional directory name
2161 char_u *fname, // file name or NULL
2162 char_u *module, // module name or NULL
2163 int bufnum, // buffer number or zero
2164 char_u *mesg, // message
2165 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002166 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002167 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002168 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002169 int vis_col, // using visual column
2170 char_u *pattern, // search pattern
2171 int nr, // error number
2172 int type, // type character
Tom Praschanca6ac992023-08-11 23:26:12 +02002173 typval_T *user_data, // custom user data or NULL
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002174 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002176 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002177 qfline_T **lastp; // pointer to qf_last or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002179 if ((qfp = ALLOC_ONE_ID(qfline_T, aid_qf_qfline)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002180 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002181 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002182 {
2183 buf_T *buf = buflist_findnr(bufnum);
2184
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002185 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002186 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002187 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002188 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002189 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002190 else
Bram Moolenaar0398e002019-03-21 21:12:49 +01002191 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2193 {
2194 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002195 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 }
2197 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002198 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002200 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002201 qfp->qf_viscol = vis_col;
Tom Praschanca6ac992023-08-11 23:26:12 +02002202 if (user_data == NULL || user_data->v_type == VAR_UNKNOWN)
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02002203 qfp->qf_user_data.v_type = VAR_UNKNOWN;
Tom Praschanca6ac992023-08-11 23:26:12 +02002204 else
2205 {
2206 copy_tv(user_data, &qfp->qf_user_data);
2207 qfl->qf_has_user_data = TRUE;
2208 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002209 if (pattern == NULL || *pattern == NUL)
2210 qfp->qf_pattern = NULL;
2211 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2212 {
2213 vim_free(qfp->qf_text);
2214 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002215 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002216 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002217 if (module == NULL || *module == NUL)
2218 qfp->qf_module = NULL;
2219 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2220 {
2221 vim_free(qfp->qf_text);
2222 vim_free(qfp->qf_pattern);
2223 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002224 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002227 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 type = 0;
2229 qfp->qf_type = type;
2230 qfp->qf_valid = valid;
2231
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002232 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002233 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002235 qfl->qf_start = qfp;
2236 qfl->qf_ptr = qfp;
2237 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002238 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 }
2240 else
2241 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002242 qfp->qf_prev = *lastp;
2243 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002245 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002247 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002248 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002249 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002251 qfl->qf_index = qfl->qf_count;
2252 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
2254
Bram Moolenaar95946f12019-03-31 15:31:59 +02002255 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256}
2257
2258/*
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002259 * Allocate a new quickfix/location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002260 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002261 static qf_info_T *
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002262qf_alloc_stack(qfltype_T qfltype)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002263{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002264 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002265
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002266 qi = ALLOC_CLEAR_ONE_ID(qf_info_T, aid_qf_qfinfo);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002267 if (qi == NULL)
2268 return NULL;
2269
2270 qi->qf_refcount++;
2271 qi->qfl_type = qfltype;
2272 qi->qf_bufnr = INVALID_QFBUFNR;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002273 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002274}
2275
2276/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002277 * Return the location list stack for window 'wp'.
2278 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002279 */
2280 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002281ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002282{
2283 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002284 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002285 return wp->w_llist_ref;
2286
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002287 // For a non-location list window, w_llist_ref should not point to a
2288 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002289 ll_free_all(&wp->w_llist_ref);
2290
2291 if (wp->w_llist == NULL)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002292 wp->w_llist = qf_alloc_stack(QFLT_LOCATION); // new location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002293 return wp->w_llist;
2294}
2295
2296/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002297 * Get the quickfix/location list stack to use for the specified Ex command.
2298 * For a location list command, returns the stack for the current window. If
2299 * the location list is not found, then returns NULL and prints an error
2300 * message if 'print_emsg' is TRUE.
2301 */
2302 static qf_info_T *
2303qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2304{
2305 qf_info_T *qi = &ql_info;
2306
2307 if (is_loclist_cmd(eap->cmdidx))
2308 {
2309 qi = GET_LOC_LIST(curwin);
2310 if (qi == NULL)
2311 {
2312 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002313 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002314 return NULL;
2315 }
2316 }
2317
2318 return qi;
2319}
2320
2321/*
2322 * Get the quickfix/location list stack to use for the specified Ex command.
2323 * For a location list command, returns the stack for the current window.
2324 * If the location list is not present, then allocates a new one.
2325 * Returns NULL if the allocation fails. For a location list command, sets
2326 * 'pwinp' to curwin.
2327 */
2328 static qf_info_T *
2329qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2330{
2331 qf_info_T *qi = &ql_info;
2332
2333 if (is_loclist_cmd(eap->cmdidx))
2334 {
2335 qi = ll_get_or_alloc_list(curwin);
2336 if (qi == NULL)
2337 return NULL;
2338 *pwinp = curwin;
2339 }
2340
2341 return qi;
2342}
2343
2344/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002345 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2346 */
2347 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002348copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002349{
2350 int i;
2351 qfline_T *from_qfp;
2352 qfline_T *prevp;
2353
2354 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002355 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002356 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002357 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002358 NULL,
2359 NULL,
2360 from_qfp->qf_module,
2361 0,
2362 from_qfp->qf_text,
2363 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002364 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002365 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002366 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002367 from_qfp->qf_viscol,
2368 from_qfp->qf_pattern,
2369 from_qfp->qf_nr,
2370 0,
Tom Praschanca6ac992023-08-11 23:26:12 +02002371 &from_qfp->qf_user_data,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002372 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002373 return FAIL;
2374
2375 // qf_add_entry() will not set the qf_num field, as the
2376 // directory and file names are not supplied. So the qf_fnum
2377 // field is copied here.
2378 prevp = to_qfl->qf_last;
2379 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2380 prevp->qf_type = from_qfp->qf_type; // error type
2381 if (from_qfl->qf_ptr == from_qfp)
2382 to_qfl->qf_ptr = prevp; // current location
2383 }
2384
2385 return OK;
2386}
2387
2388/*
2389 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2390 */
2391 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002392copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002393{
2394 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002395 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002396 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
Tom Praschanca6ac992023-08-11 23:26:12 +02002397 to_qfl->qf_has_user_data = from_qfl->qf_has_user_data;
Bram Moolenaar09037502018-09-25 22:08:14 +02002398 to_qfl->qf_count = 0;
2399 to_qfl->qf_index = 0;
2400 to_qfl->qf_start = NULL;
2401 to_qfl->qf_last = NULL;
2402 to_qfl->qf_ptr = NULL;
2403 if (from_qfl->qf_title != NULL)
2404 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2405 else
2406 to_qfl->qf_title = NULL;
2407 if (from_qfl->qf_ctx != NULL)
2408 {
2409 to_qfl->qf_ctx = alloc_tv();
2410 if (to_qfl->qf_ctx != NULL)
2411 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2412 }
2413 else
2414 to_qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002415 if (from_qfl->qf_qftf_cb.cb_name != NULL)
2416 copy_callback(&to_qfl->qf_qftf_cb, &from_qfl->qf_qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002417 else
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002418 to_qfl->qf_qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002419
2420 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002421 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002422 return FAIL;
2423
2424 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2425
2426 // Assign a new ID for the location list
2427 to_qfl->qf_id = ++last_qf_id;
2428 to_qfl->qf_changedtick = 0L;
2429
2430 // When no valid entries are present in the list, qf_ptr points to
2431 // the first item in the list
2432 if (to_qfl->qf_nonevalid)
2433 {
2434 to_qfl->qf_ptr = to_qfl->qf_start;
2435 to_qfl->qf_index = 1;
2436 }
2437
2438 return OK;
2439}
2440
2441/*
2442 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002443 */
2444 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002445copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002446{
2447 qf_info_T *qi;
2448 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002449
Bram Moolenaar09037502018-09-25 22:08:14 +02002450 // When copying from a location list window, copy the referenced
2451 // location list. For other windows, copy the location list for
2452 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002453 if (IS_LL_WINDOW(from))
2454 qi = from->w_llist_ref;
2455 else
2456 qi = from->w_llist;
2457
Bram Moolenaar09037502018-09-25 22:08:14 +02002458 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002459 return;
2460
Bram Moolenaar09037502018-09-25 22:08:14 +02002461 // allocate a new location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002462 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002463 return;
2464
2465 to->w_llist->qf_listcount = qi->qf_listcount;
2466
Bram Moolenaar09037502018-09-25 22:08:14 +02002467 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002468 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002469 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002470 to->w_llist->qf_curlist = idx;
2471
Bram Moolenaar0398e002019-03-21 21:12:49 +01002472 if (copy_loclist(qf_get_list(qi, idx),
2473 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002474 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002475 qf_free_all(to);
2476 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002477 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002478 }
2479
Bram Moolenaar09037502018-09-25 22:08:14 +02002480 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002481}
2482
2483/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002484 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002485 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 */
2487 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002488qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489{
Bram Moolenaar82404332016-07-10 17:00:38 +02002490 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002491 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002492 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002493
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002494 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
Bram Moolenaare60acc12011-05-10 16:41:25 +02002497#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002498 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002499#endif
2500#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002501 if (directory != NULL)
2502 slash_adjust(directory);
2503 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002504#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002505 if (directory != NULL && !vim_isAbsName(fname)
2506 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2507 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002508 // Here we check if the file really exists.
2509 // This should normally be true, but if make works without
2510 // "leaving directory"-messages we might have missed a
2511 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002512 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002515 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002516 if (directory)
2517 ptr = concat_fnames(directory, fname, TRUE);
2518 else
2519 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002521 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002522 bufname = ptr;
2523 }
2524 else
2525 bufname = fname;
2526
2527 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002528 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002529 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002530 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002531 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002533 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002534 {
2535 vim_free(qf_last_bufname);
2536 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2537 if (bufname == ptr)
2538 qf_last_bufname = bufname;
2539 else
2540 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002541 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002542 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002543 if (buf == NULL)
2544 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002545
Bram Moolenaarc1542742016-07-20 21:44:37 +02002546 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002547 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002548 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549}
2550
2551/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002552 * Push dirbuf onto the directory stack and return pointer to actual dir or
2553 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 */
2555 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002556qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
2558 struct dir_stack_T *ds_new;
2559 struct dir_stack_T *ds_ptr;
2560
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002561 // allocate new stack element and hook it in
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002562 ds_new = ALLOC_ONE_ID(struct dir_stack_T, aid_qf_dirstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (ds_new == NULL)
2564 return NULL;
2565
2566 ds_new->next = *stackptr;
2567 *stackptr = ds_new;
2568
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002569 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 if (vim_isAbsName(dirbuf)
2571 || (*stackptr)->next == NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=dd410372022-05-15 13:59:11 +01002572 || is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 (*stackptr)->dirname = vim_strsave(dirbuf);
2574 else
2575 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002576 // Okay we don't have an absolute path.
2577 // dirbuf must be a subdir of one of the directories on the stack.
2578 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 ds_new = (*stackptr)->next;
2580 (*stackptr)->dirname = NULL;
2581 while (ds_new)
2582 {
2583 vim_free((*stackptr)->dirname);
2584 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2585 TRUE);
2586 if (mch_isdir((*stackptr)->dirname) == TRUE)
2587 break;
2588
2589 ds_new = ds_new->next;
2590 }
2591
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002592 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 while ((*stackptr)->next != ds_new)
2594 {
2595 ds_ptr = (*stackptr)->next;
2596 (*stackptr)->next = (*stackptr)->next->next;
2597 vim_free(ds_ptr->dirname);
2598 vim_free(ds_ptr);
2599 }
2600
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002601 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 if (ds_new == NULL)
2603 {
2604 vim_free((*stackptr)->dirname);
2605 (*stackptr)->dirname = vim_strsave(dirbuf);
2606 }
2607 }
2608
2609 if ((*stackptr)->dirname != NULL)
2610 return (*stackptr)->dirname;
2611 else
2612 {
2613 ds_ptr = *stackptr;
2614 *stackptr = (*stackptr)->next;
2615 vim_free(ds_ptr);
2616 return NULL;
2617 }
2618}
2619
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620/*
2621 * pop dirbuf from the directory stack and return previous directory or NULL if
2622 * stack is empty
2623 */
2624 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002625qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
2627 struct dir_stack_T *ds_ptr;
2628
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002629 // TODO: Should we check if dirbuf is the directory on top of the stack?
2630 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002632 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 if (*stackptr != NULL)
2634 {
2635 ds_ptr = *stackptr;
2636 *stackptr = (*stackptr)->next;
2637 vim_free(ds_ptr->dirname);
2638 vim_free(ds_ptr);
2639 }
2640
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002641 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 return *stackptr ? (*stackptr)->dirname : NULL;
2643}
2644
2645/*
2646 * clean up directory stack
2647 */
2648 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002649qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
2651 struct dir_stack_T *ds_ptr;
2652
2653 while ((ds_ptr = *stackptr) != NULL)
2654 {
2655 *stackptr = (*stackptr)->next;
2656 vim_free(ds_ptr->dirname);
2657 vim_free(ds_ptr);
2658 }
2659}
2660
2661/*
2662 * Check in which directory of the directory stack the given file can be
2663 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002664 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 * Cleans up intermediate directory entries.
2666 *
2667 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002668 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 * ./
2670 * ./aa
2671 * ./aa/bb
2672 * ./bb
2673 * ./bb/x.c
2674 * and make says:
2675 * making all in aa
2676 * making all in bb
2677 * x.c:9: Error
2678 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2679 * qf_guess_filepath will return NULL.
2680 */
2681 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002682qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
2684 struct dir_stack_T *ds_ptr;
2685 struct dir_stack_T *ds_tmp;
2686 char_u *fullname;
2687
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002688 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002689 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 return NULL;
2691
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002692 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 fullname = NULL;
2694 while (ds_ptr)
2695 {
2696 vim_free(fullname);
2697 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2698
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002699 // If concat_fnames failed, just go on. The worst thing that can happen
2700 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2702 break;
2703
2704 ds_ptr = ds_ptr->next;
2705 }
2706
2707 vim_free(fullname);
2708
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002709 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002710 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002712 ds_tmp = qfl->qf_dir_stack->next;
2713 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 vim_free(ds_tmp->dirname);
2715 vim_free(ds_tmp);
2716 }
2717
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002718 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719}
2720
2721/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002722 * Returns TRUE if a quickfix/location list with the given identifier exists.
2723 */
2724 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002725qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002726{
2727 qf_info_T *qi = &ql_info;
2728 int i;
2729
2730 if (wp != NULL)
2731 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002732 if (!win_valid(wp))
2733 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002734 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002735 if (qi == NULL)
2736 return FALSE;
2737 }
2738
2739 for (i = 0; i < qi->qf_listcount; ++i)
2740 if (qi->qf_lists[i].qf_id == qf_id)
2741 return TRUE;
2742
2743 return FALSE;
2744}
2745
2746/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002747 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002748 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002749 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002750 * Similar to location list.
2751 */
2752 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002753is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002754{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002755 qfline_T *qfp;
2756 int i;
2757
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002758 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002759 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
2760 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002761 break;
2762
Bram Moolenaar95946f12019-03-31 15:31:59 +02002763 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002764 return FALSE;
2765
2766 return TRUE;
2767}
2768
2769/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002770 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002771 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002772 */
2773 static qfline_T *
2774get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002775 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002776 qfline_T *qf_ptr,
2777 int *qf_index,
2778 int dir)
2779{
2780 int idx;
2781 int old_qf_fnum;
2782
2783 idx = *qf_index;
2784 old_qf_fnum = qf_ptr->qf_fnum;
2785
2786 do
2787 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002788 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002789 return NULL;
2790 ++idx;
2791 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002792 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002793 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2794
2795 *qf_index = idx;
2796 return qf_ptr;
2797}
2798
2799/*
2800 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02002801 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002802 */
2803 static qfline_T *
2804get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002805 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002806 qfline_T *qf_ptr,
2807 int *qf_index,
2808 int dir)
2809{
2810 int idx;
2811 int old_qf_fnum;
2812
2813 idx = *qf_index;
2814 old_qf_fnum = qf_ptr->qf_fnum;
2815
2816 do
2817 {
2818 if (idx == 1 || qf_ptr->qf_prev == NULL)
2819 return NULL;
2820 --idx;
2821 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002822 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002823 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
2824
2825 *qf_index = idx;
2826 return qf_ptr;
2827}
2828
2829/*
2830 * Get the n'th (errornr) previous/next valid entry from the current entry in
2831 * the quickfix list.
2832 * dir == FORWARD or FORWARD_FILE: next valid entry
2833 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
2834 */
2835 static qfline_T *
2836get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002837 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002838 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002839 int dir,
2840 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002841{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002842 qfline_T *qf_ptr = qfl->qf_ptr;
2843 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002844 qfline_T *prev_qf_ptr;
2845 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002846 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002847
2848 while (errornr--)
2849 {
2850 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002851 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002852
2853 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002854 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002855 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002856 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002857 if (qf_ptr == NULL)
2858 {
2859 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002860 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002861 if (err != NULL)
2862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002863 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002864 return NULL;
2865 }
2866 break;
2867 }
2868
2869 err = NULL;
2870 }
2871
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002872 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002873 return qf_ptr;
2874}
2875
2876/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002877 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
2878 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002879 */
2880 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002881get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002882{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002883 qfline_T *qf_ptr = qfl->qf_ptr;
2884 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002885
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002886 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002887 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
2888 {
2889 --qf_idx;
2890 qf_ptr = qf_ptr->qf_prev;
2891 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002892 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002893 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
2894 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002895 {
2896 ++qf_idx;
2897 qf_ptr = qf_ptr->qf_next;
2898 }
2899
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002900 *new_qfidx = qf_idx;
2901 return qf_ptr;
2902}
2903
2904/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002905 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02002906 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
2907 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
2908 * Returns a pointer to the entry and the index of the new entry is stored in
2909 * 'new_qfidx'.
2910 */
2911 static qfline_T *
2912qf_get_entry(
2913 qf_list_T *qfl,
2914 int errornr,
2915 int dir,
2916 int *new_qfidx)
2917{
2918 qfline_T *qf_ptr = qfl->qf_ptr;
2919 int qfidx = qfl->qf_index;
2920
2921 if (dir != 0) // next/prev valid entry
2922 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
2923 else if (errornr != 0) // go to specified number
2924 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
2925
2926 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002927 return qf_ptr;
2928}
2929
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002930/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00002931 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002932 */
2933 static win_T *
2934qf_find_help_win(void)
2935{
2936 win_T *wp;
2937
2938 FOR_ALL_WINDOWS(wp)
2939 if (bt_help(wp->w_buffer))
2940 return wp;
2941
2942 return NULL;
2943}
2944
2945/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002946 * Set the location list for the specified window to 'qi'.
2947 */
2948 static void
2949win_set_loclist(win_T *wp, qf_info_T *qi)
2950{
2951 wp->w_llist = qi;
2952 qi->qf_refcount++;
2953}
2954
2955/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01002956 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
2957 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002958 */
2959 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01002960jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002961{
2962 win_T *wp;
2963 int flags;
2964
Bram Moolenaare1004402020-10-24 20:49:43 +02002965 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002966 wp = NULL;
2967 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002968 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002969 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
2970 win_enter(wp, TRUE);
2971 else
2972 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002973 // Split off help window; put it at far top if no position
2974 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002975 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02002976 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002977 && curwin->w_width < 80)
2978 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01002979 // If the user asks to open a new window, then copy the location list.
2980 // Otherwise, don't copy the location list.
2981 if (IS_LL_STACK(qi) && !newwin)
2982 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002983
2984 if (win_split(0, flags) == FAIL)
2985 return FAIL;
2986
2987 *opened_window = TRUE;
2988
2989 if (curwin->w_height < p_hh)
2990 win_setheight((int)p_hh);
2991
Bram Moolenaarb2443732018-11-11 22:50:27 +01002992 // When using location list, the new window should use the supplied
2993 // location list. If the user asks to open a new window, then the new
2994 // window will get a copy of the location list.
2995 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002996 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02002997 }
2998
2999 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003000 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003001
3002 return OK;
3003}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003004
3005/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003006 * Find a non-quickfix window using the given location list stack in the
3007 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003008 * Returns NULL if a matching window is not found.
3009 */
3010 static win_T *
3011qf_find_win_with_loclist(qf_info_T *ll)
3012{
3013 win_T *wp;
3014
3015 FOR_ALL_WINDOWS(wp)
3016 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
3017 return wp;
3018
3019 return NULL;
3020}
3021
3022/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003023 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003024 */
3025 static win_T *
3026qf_find_win_with_normal_buf(void)
3027{
3028 win_T *wp;
3029
3030 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02003031 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003032 return wp;
3033
3034 return NULL;
3035}
3036
3037/*
3038 * Go to a window in any tabpage containing the specified file. Returns TRUE
3039 * if successfully jumped to the window. Otherwise returns FALSE.
3040 */
3041 static int
3042qf_goto_tabwin_with_file(int fnum)
3043{
3044 tabpage_T *tp;
3045 win_T *wp;
3046
3047 FOR_ALL_TAB_WINDOWS(tp, wp)
3048 if (wp->w_buffer->b_fnum == fnum)
3049 {
3050 goto_tabpage_win(tp, wp);
3051 return TRUE;
3052 }
3053
3054 return FALSE;
3055}
3056
3057/*
3058 * Create a new window to show a file above the quickfix window. Called when
3059 * only the quickfix window is present.
3060 */
3061 static int
3062qf_open_new_file_win(qf_info_T *ll_ref)
3063{
3064 int flags;
3065
3066 flags = WSP_ABOVE;
3067 if (ll_ref != NULL)
3068 flags |= WSP_NEWLOC;
3069 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003070 return FAIL; // not enough room for window
3071 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003072 swb_flags = 0;
3073 RESET_BINDING(curwin);
3074 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003075 // The new window should use the location list from the
3076 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003077 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003078 return OK;
3079}
3080
3081/*
3082 * Go to a window that shows the right buffer. If the window is not found, go
3083 * to the window just above the location list window. This is used for opening
3084 * a file from a location window and not from a quickfix window. If some usable
3085 * window is previously found, then it is supplied in 'use_win'.
3086 */
3087 static void
3088qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
3089{
3090 win_T *win = use_win;
3091
3092 if (win == NULL)
3093 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003094 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003095 FOR_ALL_WINDOWS(win)
3096 if (win->w_buffer->b_fnum == qf_fnum)
3097 break;
3098 if (win == NULL)
3099 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003100 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003101 win = curwin;
3102 do
3103 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02003104 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003105 break;
3106 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003107 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003108 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003109 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003110 } while (win != curwin);
3111 }
3112 }
3113 win_goto(win);
3114
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003115 // If the location list for the window is not set, then set it
3116 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003117 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003118 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003119}
3120
3121/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003122 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3123 * not found, then go to the window just above the quickfix window. This is
3124 * used for opening a file from a quickfix window and not from a location
3125 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003126 */
3127 static void
3128qf_goto_win_with_qfl_file(int qf_fnum)
3129{
3130 win_T *win;
3131 win_T *altwin;
3132
3133 win = curwin;
3134 altwin = NULL;
3135 for (;;)
3136 {
3137 if (win->w_buffer->b_fnum == qf_fnum)
3138 break;
3139 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003140 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003141 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003142 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003143
3144 if (IS_QF_WINDOW(win))
3145 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003146 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003147 // window, unless 'switchbuf' contains 'uselast': in this case we
3148 // try to jump to the previously used window first.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003149 if ((swb_flags & SWB_USELAST) && win_valid(prevwin) &&
3150 !prevwin->w_p_wfb)
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003151 win = prevwin;
3152 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003153 win = altwin;
3154 else if (curwin->w_prev != NULL)
3155 win = curwin->w_prev;
3156 else
3157 win = curwin->w_next;
3158 break;
3159 }
3160
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003161 // Remember a usable window.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003162 if (altwin == NULL && !win->w_p_pvw && !win->w_p_wfb &&
3163 bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003164 altwin = win;
3165 }
3166
3167 win_goto(win);
3168}
3169
3170/*
3171 * Find a suitable window for opening a file (qf_fnum) from the
3172 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003173 * window, jump to it. Otherwise open a new window to display the file. If
3174 * 'newwin' is TRUE, then always open a new window. This is called from either
3175 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003176 */
3177 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003178qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003179{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003180 win_T *usable_wp = NULL;
3181 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003182 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003183
Bram Moolenaarb2443732018-11-11 22:50:27 +01003184 // If opening a new window, then don't use the location list referred by
3185 // the current window. Otherwise two windows will refer to the same
3186 // location list.
3187 if (!newwin)
3188 ll_ref = curwin->w_llist_ref;
3189
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003190 if (ll_ref != NULL)
3191 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003192 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003193 usable_wp = qf_find_win_with_loclist(ll_ref);
3194 if (usable_wp != NULL)
3195 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003196 }
3197
3198 if (!usable_win)
3199 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003200 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003201 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003202 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003203 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003204 }
3205
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003206 // If no usable window is found and 'switchbuf' contains "usetab"
3207 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003208 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003209 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003210
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003211 // If there is only one window and it is the quickfix window, create a
3212 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003213 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003214 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003215 if (qf_open_new_file_win(ll_ref) != OK)
3216 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003217 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003218 }
3219 else
3220 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003221 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003222 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003223 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003224 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003225 }
3226
3227 return OK;
3228}
3229
3230/*
3231 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003232 * Returns OK if successfully edited the file, FAIL on failing to open the
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003233 * buffer and QF_ABORT if the quickfix/location list was freed by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003234 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003235 */
3236 static int
3237qf_jump_edit_buffer(
3238 qf_info_T *qi,
3239 qfline_T *qf_ptr,
3240 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003241 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003242 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003243{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003244 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003245 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003246 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003247 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003248 int old_qf_curlist = qi->qf_curlist;
3249 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003250
3251 if (qf_ptr->qf_type == 1)
3252 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003253 // Open help file (do_ecmd() will set b_help flag, readfile() will
3254 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003255 if (!can_abandon(curbuf, forceit))
3256 {
3257 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003258 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003259 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003260
3261 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3262 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003263 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003264 }
3265 else
Colin Kennedy21570352024-03-03 16:16:47 +01003266 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003267 int fnum = qf_ptr->qf_fnum;
3268
3269 if (!forceit && curwin->w_p_wfb && curbuf->b_fnum != fnum)
Colin Kennedy21570352024-03-03 16:16:47 +01003270 {
3271 if (qi->qfl_type == QFLT_LOCATION)
3272 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003273 // Location lists cannot split or reassign their window
3274 // so 'winfixbuf' windows must fail
3275 emsg(_(e_winfixbuf_cannot_go_to_buffer));
3276 return FAIL;
Colin Kennedy21570352024-03-03 16:16:47 +01003277 }
3278
Sean Dewar4bb505e2024-03-05 20:39:07 +01003279 if (win_valid(prevwin) && !prevwin->w_p_wfb &&
3280 !bt_quickfix(prevwin->w_buffer))
Colin Kennedy21570352024-03-03 16:16:47 +01003281 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003282 // 'winfixbuf' is set; attempt to change to a window without it
3283 // that isn't a quickfix/location list window.
3284 win_goto(prevwin);
3285 }
3286 if (curwin->w_p_wfb)
3287 {
3288 // Split the window, which will be 'nowinfixbuf', and set curwin
3289 // to that
3290 if (win_split(0, 0) == OK)
3291 *opened_window = TRUE;
3292
3293 if (curwin->w_p_wfb)
3294 {
3295 // Autocommands set 'winfixbuf' or sent us to another window
Sean Dewar769eb2d2024-03-07 21:37:50 +01003296 // with it set, or we failed to split the window. Give up,
3297 // but don't return immediately, as they may have messed
3298 // with the list.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003299 emsg(_(e_winfixbuf_cannot_go_to_buffer));
3300 retval = FAIL;
3301 }
Colin Kennedy21570352024-03-03 16:16:47 +01003302 }
3303 }
3304
Sean Dewar4bb505e2024-03-05 20:39:07 +01003305 if (retval == OK)
3306 {
3307 retval = buflist_getfile(fnum,
3308 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
3309 }
Colin Kennedy21570352024-03-03 16:16:47 +01003310 }
Bram Moolenaar3c097222017-12-21 20:54:49 +01003311
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003312 // If a location list, check whether the associated window is still
3313 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003314 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003315 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003316 win_T *wp = win_id2wp(prev_winid);
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003317
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003318 if (wp == NULL && curwin->w_llist != qi)
3319 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003320 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003321 *opened_window = FALSE;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003322 return QF_ABORT;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003323 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003324 }
3325
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003326 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003327 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003328 emsg(_(e_current_quickfix_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003329 return QF_ABORT;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003330 }
3331
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003332 // Check if the list was changed. The pointers may happen to be identical,
3333 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003334 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003335 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003336 || !is_qf_entry_present(qfl, qf_ptr))
3337 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003338 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003339 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003340 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003341 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003342 return QF_ABORT;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003343 }
3344
3345 return retval;
3346}
3347
3348/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003349 * Go to the error line in the current file using either line/column number or
3350 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003351 */
3352 static void
3353qf_jump_goto_line(
3354 linenr_T qf_lnum,
3355 int qf_col,
3356 char_u qf_viscol,
3357 char_u *qf_pattern)
3358{
3359 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003360
3361 if (qf_pattern == NULL)
3362 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003363 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003364 i = qf_lnum;
3365 if (i > 0)
3366 {
3367 if (i > curbuf->b_ml.ml_line_count)
3368 i = curbuf->b_ml.ml_line_count;
3369 curwin->w_cursor.lnum = i;
3370 }
3371 if (qf_col > 0)
3372 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003373 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003374 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003375 coladvance(qf_col - 1);
3376 else
3377 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003378 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003379 check_cursor();
3380 }
3381 else
3382 beginline(BL_WHITE | BL_FIX);
3383 }
3384 else
3385 {
3386 pos_T save_cursor;
3387
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003388 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003389 save_cursor = curwin->w_cursor;
3390 curwin->w_cursor.lnum = 0;
John Marriott8c85a2a2024-05-20 19:18:26 +02003391 if (!do_search(NULL, '/', '/', qf_pattern, STRLEN(qf_pattern), (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003392 curwin->w_cursor = save_cursor;
3393 }
3394}
3395
3396/*
3397 * Display quickfix list index and size message
3398 */
3399 static void
3400qf_jump_print_msg(
3401 qf_info_T *qi,
3402 int qf_index,
3403 qfline_T *qf_ptr,
3404 buf_T *old_curbuf,
3405 linenr_T old_lnum)
3406{
3407 linenr_T i;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003408 garray_T *gap;
3409
3410 gap = qfga_get();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003411
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003412 // Update the screen before showing the message, unless the screen
3413 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003414 if (!msg_scrolled)
3415 update_topline_redraw();
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003416 vim_snprintf((char *)IObuff, IOSIZE, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003417 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003418 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3419 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003420 // Add the message, skipping leading whitespace and newlines.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003421 ga_concat(gap, IObuff);
3422 qf_fmt_text(gap, skipwhite(qf_ptr->qf_text));
Shane Harper5bf04282023-06-07 19:09:57 +01003423 ga_append(gap, NUL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003424
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003425 // Output the message. Overwrite to avoid scrolling when the 'O'
3426 // flag is present in 'shortmess'; But when not jumping, print the
3427 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003428 i = msg_scroll;
3429 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3430 msg_scroll = TRUE;
3431 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3432 msg_scroll = FALSE;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003433 msg_attr_keep((char *)gap->ga_data, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003434 msg_scroll = i;
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01003435
3436 qfga_clear();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003437}
3438
3439/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003440 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003441 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3442 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003443 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3444 * able to jump/open a window. Returns NOTDONE if a file is not associated
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003445 * with the entry. Returns QF_ABORT if the quickfix/location list was modified
3446 * by an autocmd.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003447 */
3448 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003449qf_jump_open_window(
3450 qf_info_T *qi,
3451 qfline_T *qf_ptr,
3452 int newwin,
3453 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003454{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003455 qf_list_T *qfl = qf_get_curlist(qi);
3456 int old_changedtick = qfl->qf_changedtick;
3457 int old_qf_curlist = qi->qf_curlist;
3458 qfltype_T qfl_type = qfl->qfl_type;
3459
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003460 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003461 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3462 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003463 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003464 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003465 if (old_qf_curlist != qi->qf_curlist
3466 || old_changedtick != qfl->qf_changedtick
3467 || !is_qf_entry_present(qfl, qf_ptr))
3468 {
3469 if (qfl_type == QFLT_QUICKFIX)
3470 emsg(_(e_current_quickfix_list_was_changed));
3471 else
3472 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003473 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003474 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003475
3476 // If currently in the quickfix window, find another window to show the
3477 // file in.
3478 if (bt_quickfix(curbuf) && !*opened_window)
3479 {
3480 // If there is no file specified, we don't know where to go.
3481 // But do advance, otherwise ":cn" gets stuck.
3482 if (qf_ptr->qf_fnum == 0)
3483 return NOTDONE;
3484
Bram Moolenaarb2443732018-11-11 22:50:27 +01003485 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3486 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003487 return FAIL;
3488 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003489 if (old_qf_curlist != qi->qf_curlist
3490 || old_changedtick != qfl->qf_changedtick
3491 || !is_qf_entry_present(qfl, qf_ptr))
3492 {
3493 if (qfl_type == QFLT_QUICKFIX)
3494 emsg(_(e_current_quickfix_list_was_changed));
3495 else
3496 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003497 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003498 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003499
3500 return OK;
3501}
3502
3503/*
3504 * Edit a selected file from the quickfix/location list and jump to a
3505 * particular line/column, adjust the folds and display a message about the
3506 * jump.
3507 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003508 * QF_ABORT if the quickfix/location list is freed by an autocmd when opening
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003509 * the file.
3510 */
3511 static int
3512qf_jump_to_buffer(
3513 qf_info_T *qi,
3514 int qf_index,
3515 qfline_T *qf_ptr,
3516 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003517 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003518 int *opened_window,
3519 int openfold,
3520 int print_message)
3521{
3522 buf_T *old_curbuf;
3523 linenr_T old_lnum;
3524 int retval = OK;
3525
3526 // If there is a file name, read the wanted file if needed, and check
3527 // autowrite etc.
3528 old_curbuf = curbuf;
3529 old_lnum = curwin->w_cursor.lnum;
3530
3531 if (qf_ptr->qf_fnum != 0)
3532 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003533 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003534 opened_window);
3535 if (retval != OK)
3536 return retval;
3537 }
3538
3539 // When not switched to another buffer, still need to set pc mark
3540 if (curbuf == old_curbuf)
3541 setpcmark();
3542
3543 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3544 qf_ptr->qf_pattern);
3545
3546#ifdef FEAT_FOLDING
3547 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3548 foldOpenCursor();
3549#endif
3550 if (print_message)
3551 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3552
3553 return retval;
3554}
3555
3556/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003557 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 */
3559 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003560qf_jump(qf_info_T *qi,
3561 int dir,
3562 int errornr,
3563 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003565 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3566}
3567
3568/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003569 * Jump to a quickfix line.
3570 * If dir == 0 go to entry "errornr".
3571 * If dir == FORWARD go "errornr" valid entries forward.
3572 * If dir == BACKWARD go "errornr" valid entries backward.
3573 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3574 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3575 * else if "errornr" is zero, redisplay the same line
3576 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003577 * If 'newwin' is TRUE, then open the file in a new window.
3578 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003579 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003580qf_jump_newwin(qf_info_T *qi,
3581 int dir,
3582 int errornr,
3583 int forceit,
3584 int newwin)
3585{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003586 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003587 qfline_T *qf_ptr;
3588 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003591 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003592 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003593 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003596 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003597 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003599 if (qi == NULL)
3600 qi = &ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003601
Bram Moolenaar0398e002019-03-21 21:12:49 +01003602 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003604 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 return;
3606 }
3607
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003608 incr_quickfix_busy();
3609
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003610 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003611
3612 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003614 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003616
3617 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3618 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003620 qf_ptr = old_qf_ptr;
3621 qf_index = old_qf_index;
3622 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003625 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003626 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003627 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003628 // No need to print the error message if it's visible in the error
3629 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 print_message = FALSE;
3631
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003632 prev_winid = curwin->w_id;
3633
Bram Moolenaarb2443732018-11-11 22:50:27 +01003634 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003635 if (retval == FAIL)
3636 goto failed;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003637 if (retval == QF_ABORT)
3638 {
3639 qi = NULL;
3640 qf_ptr = NULL;
3641 goto theend;
3642 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003643 if (retval == NOTDONE)
3644 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003645
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003646 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003647 &opened_window, old_KeyTyped, print_message);
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003648 if (retval == QF_ABORT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003650 // Quickfix/location list was modified by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003651 qi = NULL;
3652 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003655 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003658 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003659 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003661 // Couldn't open file, so put index back where it was. This could
3662 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 qf_ptr = old_qf_ptr;
3665 qf_index = old_qf_index;
3666 }
3667 }
3668theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003669 if (qi != NULL)
3670 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003671 qfl->qf_ptr = qf_ptr;
3672 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003673 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003674 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003676 // Restore old 'switchbuf' value, but not when an autocommand or
3677 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003678 p_swb = old_swb;
3679 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003681 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682}
3683
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003684// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003685static int qfFileAttr;
3686static int qfSepAttr;
3687static int qfLineAttr;
3688
3689/*
3690 * Display information about a single entry from the quickfix/location list.
3691 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003692 * 'cursel' will be set to TRUE for the currently selected entry in the
3693 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003694 */
3695 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003696qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003697{
3698 char_u *fname;
3699 buf_T *buf;
3700 int filter_entry;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003701 garray_T *gap;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003702
3703 fname = NULL;
3704 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3705 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3706 (char *)qfp->qf_module);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00003707 else
3708 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003709 if (qfp->qf_fnum != 0
3710 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3711 {
3712 fname = buf->b_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003713 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003714 fname = gettail(fname);
3715 }
3716 if (fname == NULL)
3717 sprintf((char *)IObuff, "%2d", qf_idx);
3718 else
3719 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3720 qf_idx, (char *)fname);
3721 }
3722
3723 // Support for filtering entries using :filter /pat/ clist
3724 // Match against the module name, file name, search pattern and
3725 // text of the entry.
3726 filter_entry = TRUE;
3727 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3728 filter_entry &= message_filtered(qfp->qf_module);
3729 if (filter_entry && fname != NULL)
3730 filter_entry &= message_filtered(fname);
3731 if (filter_entry && qfp->qf_pattern != NULL)
3732 filter_entry &= message_filtered(qfp->qf_pattern);
3733 if (filter_entry)
3734 filter_entry &= message_filtered(qfp->qf_text);
3735 if (filter_entry)
3736 return;
3737
3738 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003739 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003740
3741 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003742 msg_puts_attr(":", qfSepAttr);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003743 gap = qfga_get();
Shane Harper5bf04282023-06-07 19:09:57 +01003744 if (qfp->qf_lnum != 0)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003745 qf_range_text(gap, qfp);
3746 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
3747 ga_append(gap, NUL);
3748 msg_puts_attr((char *)gap->ga_data, qfLineAttr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003749 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003750 if (qfp->qf_pattern != NULL)
3751 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003752 gap = qfga_get();
3753 qf_fmt_text(gap, qfp->qf_pattern);
Shane Harper5bf04282023-06-07 19:09:57 +01003754 ga_append(gap, NUL);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003755 msg_puts((char *)gap->ga_data);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003756 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003757 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01003758 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003759
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003760 // Remove newlines and leading whitespace from the text. For an
3761 // unrecognized line keep the indent, the compiler may mark a word
3762 // with ^^^^.
3763 gap = qfga_get();
3764 qf_fmt_text(gap, (fname != NULL || qfp->qf_lnum != 0)
Shane Harper5bf04282023-06-07 19:09:57 +01003765 ? skipwhite(qfp->qf_text) : qfp->qf_text);
3766 ga_append(gap, NUL);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003767 msg_prt_line((char_u *)gap->ga_data, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003768 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003769}
3770
3771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003773 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 */
3775 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003776qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003778 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003779 qfline_T *qfp;
3780 int i;
3781 int idx1 = 1;
3782 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003783 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003784 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003785 int all = eap->forceit; // if not :cl!, only show
3786 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003787 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003789 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3790 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003791
Bram Moolenaar0398e002019-03-21 21:12:49 +01003792 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003794 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 return;
3796 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02003797 if (*arg == '+')
3798 {
3799 ++arg;
3800 plus = TRUE;
3801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
3803 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003804 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 return;
3806 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003807 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02003808 if (plus)
3809 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003810 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003811 idx2 = i + idx1;
3812 idx1 = i;
3813 }
3814 else
3815 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003816 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02003817 if (idx1 < 0)
3818 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
3819 if (idx2 < 0)
3820 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
3821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003823 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02003824 shorten_fnames(FALSE);
3825
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003826 // Get the attributes for the different quickfix highlight items. Note
3827 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01003828 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
3829 if (qfFileAttr == 0)
3830 qfFileAttr = HL_ATTR(HLF_D);
3831 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
3832 if (qfSepAttr == 0)
3833 qfSepAttr = HL_ATTR(HLF_D);
3834 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
3835 if (qfLineAttr == 0)
3836 qfLineAttr = HL_ATTR(HLF_N);
3837
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003838 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02003840 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
3842 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003843 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003844
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 ui_breakcheck();
3846 }
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01003847 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848}
3849
3850/*
3851 * Remove newlines and leading whitespace from an error message.
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003852 * Add the result to the grow array "gap".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 */
3854 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003855qf_fmt_text(garray_T *gap, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 char_u *p = text;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003858 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
3860 if (*p == '\n')
3861 {
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003862 ga_append(gap, ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01003864 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 break;
3866 }
3867 else
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003868 ga_append(gap, *p++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870}
3871
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003872/*
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003873 * Add the range information from the lnum, col, end_lnum, and end_col values
3874 * of a quickfix entry to the grow array "gap".
thinca6864efa2021-06-19 20:45:20 +02003875 */
3876 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003877qf_range_text(garray_T *gap, qfline_T *qfp)
thinca6864efa2021-06-19 20:45:20 +02003878{
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003879 char_u *buf = IObuff;
3880 int bufsize = IOSIZE;
thinca6864efa2021-06-19 20:45:20 +02003881 int len;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003882
thinca6864efa2021-06-19 20:45:20 +02003883 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
3884 len = (int)STRLEN(buf);
3885
3886 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
3887 {
Shane Harper5bf04282023-06-07 19:09:57 +01003888 vim_snprintf((char *)buf + len, bufsize - len, "-%ld",
3889 qfp->qf_end_lnum);
thinca6864efa2021-06-19 20:45:20 +02003890 len += (int)STRLEN(buf + len);
3891 }
3892 if (qfp->qf_col > 0)
3893 {
3894 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
3895 len += (int)STRLEN(buf + len);
3896 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
3897 {
Shane Harper5bf04282023-06-07 19:09:57 +01003898 vim_snprintf((char *)buf + len, bufsize - len, "-%d",
3899 qfp->qf_end_col);
thinca6864efa2021-06-19 20:45:20 +02003900 len += (int)STRLEN(buf + len);
3901 }
3902 }
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003903
3904 ga_concat_len(gap, buf, len);
thinca6864efa2021-06-19 20:45:20 +02003905}
3906
3907/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003908 * Display information (list number, list size and the title) about a
3909 * quickfix/location list.
3910 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003911 static void
3912qf_msg(qf_info_T *qi, int which, char *lead)
3913{
3914 char *title = (char *)qi->qf_lists[which].qf_title;
3915 int count = qi->qf_lists[which].qf_count;
3916 char_u buf[IOSIZE];
3917
3918 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
3919 lead,
3920 which + 1,
3921 qi->qf_listcount,
3922 count);
3923
3924 if (title != NULL)
3925 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02003926 size_t len = STRLEN(buf);
3927
3928 if (len < 34)
3929 {
3930 vim_memset(buf + len, ' ', 34 - len);
3931 buf[34] = NUL;
3932 }
3933 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003934 }
3935 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003936 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003937}
3938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939/*
3940 * ":colder [count]": Up in the quickfix stack.
3941 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003942 * ":lolder [count]": Up in the location list stack.
3943 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 */
3945 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003946qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003948 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 int count;
3950
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003951 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
3952 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003953
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 if (eap->addr_count != 0)
3955 count = eap->line2;
3956 else
3957 count = 1;
3958 while (count--)
3959 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003960 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003962 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003964 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003965 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003967 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 else
3970 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003971 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003973 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02003974 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003976 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003979 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02003980 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981}
3982
Bram Moolenaar18cebf42018-05-08 22:31:37 +02003983/*
3984 * Display the information about all the quickfix/location lists in the stack
3985 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003986 void
3987qf_history(exarg_T *eap)
3988{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02003989 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02003990 int i;
3991
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003992 if (eap->addr_count > 0)
3993 {
3994 if (qi == NULL)
3995 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003996 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02003997 return;
3998 }
3999
4000 // Jump to the specified quickfix list
4001 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
4002 {
4003 qi->qf_curlist = eap->line2 - 1;
4004 qf_msg(qi, qi->qf_curlist, "");
4005 qf_update_buffer(qi, NULL);
4006 }
4007 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004008 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004009
4010 return;
4011 }
4012
Bram Moolenaar5b69c222019-01-11 14:50:06 +01004013 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004014 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004015 else
4016 for (i = 0; i < qi->qf_listcount; ++i)
4017 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
4018}
4019
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004021 * Free all the entries in the error list "idx". Note that other information
4022 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 */
4024 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004025qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004027 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004028 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01004029 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004031 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004033 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004034 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004035 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01004036 {
Bram Moolenaard76ce852018-05-01 15:02:04 +02004037 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004038 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004039 vim_free(qfp->qf_pattern);
Tom Praschanca6ac992023-08-11 23:26:12 +02004040 clear_tv(&qfp->qf_user_data);
Bram Moolenaard76ce852018-05-01 15:02:04 +02004041 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004042 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01004043 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004044 // Somehow qf_count may have an incorrect value, set it to 1
4045 // to avoid crashing when it's wrong.
4046 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004047 qfl->qf_count = 1;
Christian Brabandt567cae22023-11-19 16:19:27 +01004048 else
4049 qfl->qf_start = qfpnext;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01004050 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004051 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004053
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004054 qfl->qf_index = 0;
4055 qfl->qf_start = NULL;
4056 qfl->qf_last = NULL;
4057 qfl->qf_ptr = NULL;
4058 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02004059
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004060 qf_clean_dir_stack(&qfl->qf_dir_stack);
4061 qfl->qf_directory = NULL;
4062 qf_clean_dir_stack(&qfl->qf_file_stack);
4063 qfl->qf_currfile = NULL;
4064 qfl->qf_multiline = FALSE;
4065 qfl->qf_multiignore = FALSE;
4066 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067}
4068
4069/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004070 * Free error list "idx". Frees all the entries in the quickfix list,
4071 * associated context information and the title.
4072 */
4073 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004074qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004075{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004076 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004077
Bram Moolenaard23a8232018-02-10 18:45:26 +01004078 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004079 free_tv(qfl->qf_ctx);
4080 qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004081 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004082 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004083 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004084}
4085
4086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 * qf_mark_adjust: adjust marks
4088 */
4089 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004090qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02004091 win_T *wp,
4092 linenr_T line1,
4093 linenr_T line2,
4094 long amount,
4095 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004097 int i;
4098 qfline_T *qfp;
4099 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004100 qf_info_T *qi = &ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004101 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02004102 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103
Bram Moolenaarc1542742016-07-20 21:44:37 +02004104 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004105 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004106 if (wp != NULL)
4107 {
4108 if (wp->w_llist == NULL)
4109 return;
4110 qi = wp->w_llist;
4111 }
4112
4113 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01004114 {
4115 qf_list_T *qfl = qf_get_list(qi, idx);
4116
4117 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01004118 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 if (qfp->qf_fnum == curbuf->b_fnum)
4120 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004121 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
4123 {
4124 if (amount == MAXLNUM)
4125 qfp->qf_cleared = TRUE;
4126 else
4127 qfp->qf_lnum += amount;
4128 }
4129 else if (amount_after && qfp->qf_lnum > line2)
4130 qfp->qf_lnum += amount_after;
4131 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01004132 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004133
4134 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02004135 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136}
4137
4138/*
4139 * Make a nice message out of the error character and the error number:
4140 * char number message
4141 * e or E 0 " error"
4142 * w or W 0 " warning"
4143 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004144 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 * 0 0 ""
4146 * other 0 " c"
4147 * e or E n " error n"
4148 * w or W n " warning n"
4149 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004150 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 * 0 n " error n"
4152 * other n " c n"
4153 * 1 x "" :helpgrep
4154 */
4155 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004156qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157{
4158 static char_u buf[20];
4159 static char_u cc[3];
4160 char_u *p;
4161
4162 if (c == 'W' || c == 'w')
4163 p = (char_u *)" warning";
4164 else if (c == 'I' || c == 'i')
4165 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004166 else if (c == 'N' || c == 'n')
4167 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4169 p = (char_u *)" error";
4170 else if (c == 0 || c == 1)
4171 p = (char_u *)"";
4172 else
4173 {
4174 cc[0] = ' ';
4175 cc[1] = c;
4176 cc[2] = NUL;
4177 p = cc;
4178 }
4179
4180 if (nr <= 0)
4181 return p;
4182
4183 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4184 return buf;
4185}
4186
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004188 * When "split" is FALSE: Open the entry/result under the cursor.
4189 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4190 */
4191 void
4192qf_view_result(int split)
4193{
4194 qf_info_T *qi = &ql_info;
4195
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004196 if (IS_LL_WINDOW(curwin))
4197 qi = GET_LOC_LIST(curwin);
4198
Bram Moolenaar0398e002019-03-21 21:12:49 +01004199 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004200 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004201 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004202 return;
4203 }
4204
4205 if (split)
4206 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004207 // Open the selected entry in a new window
4208 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4209 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004210 return;
4211 }
4212
4213 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4214}
4215
4216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 * ":cwindow": open the quickfix window if we have errors to display,
4218 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004219 * ":lwindow": open the location list window if we have locations to display,
4220 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 */
4222 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004223ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004225 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004226 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 win_T *win;
4228
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004229 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4230 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004231
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004232 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004233
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004234 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004235 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004237 // If a quickfix window is open but we have no errors to display,
4238 // close the window. If a quickfix window is not open, then open
4239 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004240 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004241 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004242 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
4244 if (win != NULL)
4245 ex_cclose(eap);
4246 }
4247 else if (win == NULL)
4248 ex_copen(eap);
4249}
4250
4251/*
4252 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004253 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004256ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004258 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004259 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004261 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4262 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004264 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004265 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 if (win != NULL)
4267 win_close(win, FALSE);
4268}
4269
4270/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004271 * Set "w:quickfix_title" if "qi" has a title.
4272 */
4273 static void
4274qf_set_title_var(qf_list_T *qfl)
4275{
4276 if (qfl->qf_title != NULL)
4277 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4278}
4279
4280/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004281 * Goto a quickfix or location list window (if present).
4282 * Returns OK if the window is found, FAIL otherwise.
4283 */
4284 static int
4285qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4286{
4287 win_T *win;
4288
4289 win = qf_find_win(qi);
4290 if (win == NULL)
4291 return FAIL;
4292
4293 win_goto(win);
4294 if (resize)
4295 {
4296 if (vertsplit)
4297 {
4298 if (sz != win->w_width)
4299 win_setwidth(sz);
4300 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004301 else if (sz != win->w_height && win->w_height
4302 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004303 win_setheight(sz);
4304 }
4305
4306 return OK;
4307}
4308
4309/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004310 * Set options for the buffer in the quickfix or location list window.
4311 */
4312 static void
4313qf_set_cwindow_options(void)
4314{
4315 // switch off 'swapfile'
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004316 set_option_value_give_err((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4317 set_option_value_give_err((char_u *)"bt",
4318 0L, (char_u *)"quickfix", OPT_LOCAL);
4319 set_option_value_give_err((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004320 RESET_BINDING(curwin);
4321#ifdef FEAT_DIFF
4322 curwin->w_p_diff = FALSE;
4323#endif
4324#ifdef FEAT_FOLDING
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004325 set_option_value_give_err((char_u *)"fdm", 0L, (char_u *)"manual",
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004326 OPT_LOCAL);
4327#endif
4328}
4329
4330/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004331 * Open a new quickfix or location list window, load the quickfix buffer and
4332 * set the appropriate options for the window.
4333 * Returns FAIL if the window could not be opened.
4334 */
4335 static int
4336qf_open_new_cwindow(qf_info_T *qi, int height)
4337{
4338 buf_T *qf_buf;
4339 win_T *oldwin = curwin;
4340 tabpage_T *prevtab = curtab;
4341 int flags = 0;
4342 win_T *win;
4343
4344 qf_buf = qf_find_buf(qi);
4345
4346 // The current window becomes the previous window afterwards.
4347 win = curwin;
4348
Bram Moolenaare1004402020-10-24 20:49:43 +02004349 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004350 // Create the new quickfix window at the very bottom, except when
4351 // :belowright or :aboveleft is used.
4352 win_goto(lastwin);
4353 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004354 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004355 flags = WSP_BELOW;
4356 flags |= WSP_NEWLOC;
4357 if (win_split(height, flags) == FAIL)
4358 return FAIL; // not enough room for window
4359 RESET_BINDING(curwin);
4360
4361 if (IS_LL_STACK(qi))
4362 {
4363 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004364 // location list stack from the window 'win'.
4365 curwin->w_llist_ref = qi;
4366 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004367 }
4368
4369 if (oldwin != curwin)
4370 oldwin = NULL; // don't store info when in another window
4371 if (qf_buf != NULL)
4372 {
4373 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004374 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004375 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004376 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004377 }
4378 else
4379 {
4380 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004381 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4382 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004383 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004384
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004385 // save the number of the new buffer
4386 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004387 }
4388
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004389 // Set the options for the quickfix buffer/window (if not already done)
4390 // Do this even if the quickfix buffer was already present, as an autocmd
4391 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004392 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004393 qf_set_cwindow_options();
4394
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004395 // Only set the height when still in the same tab page and there is no
4396 // window to the side.
4397 if (curtab == prevtab && curwin->w_width == Columns)
4398 win_setheight(height);
4399 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4400 if (win_valid(win))
4401 prevwin = win;
4402
4403 return OK;
4404}
4405
4406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004408 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 */
4410 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004411ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004413 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004414 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004416 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004417 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004419 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4420 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004421
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004422 incr_quickfix_busy();
4423
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 if (eap->addr_count != 0)
4425 height = eap->line2;
4426 else
4427 height = QF_WINHEIGHT;
4428
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004429 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430#ifdef FEAT_GUI
4431 need_mouse_correct = TRUE;
4432#endif
4433
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004434 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004435 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004436 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004437 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004438 if (status == FAIL)
4439 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004440 {
4441 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004442 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004445 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004446 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004447 // Save the current index here, as updating the quickfix buffer may free
4448 // the quickfix list
4449 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004450
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004451 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004452 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004454 decr_quickfix_busy();
4455
4456 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 curwin->w_cursor.col = 0;
4458 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004459 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460}
4461
4462/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004463 * Move the cursor in the quickfix window to "lnum".
4464 */
4465 static void
4466qf_win_goto(win_T *win, linenr_T lnum)
4467{
4468 win_T *old_curwin = curwin;
4469
4470 curwin = win;
4471 curbuf = win->w_buffer;
4472 curwin->w_cursor.lnum = lnum;
4473 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004474 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004475 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004476 update_topline(); // scroll to show the line
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004477 redraw_later(UPD_VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004478 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004479 curwin = old_curwin;
4480 curbuf = curwin->w_buffer;
4481}
4482
4483/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004484 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004485 */
4486 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004487ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004488{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004489 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004490 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004491
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004492 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4493 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004494
4495 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004496 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4497 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4498}
4499
4500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 * Return the number of the current entry (line number in the quickfix
4502 * window).
4503 */
4504 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004505qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004507 qf_info_T *qi = &ql_info;
4508
4509 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004510 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004511 qi = wp->w_llist_ref;
4512
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004513 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514}
4515
4516/*
4517 * Update the cursor position in the quickfix window to the current error.
4518 * Return TRUE if there is a quickfix window.
4519 */
4520 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004521qf_win_pos_update(
4522 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004523 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
4525 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004526 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004528 // Put the cursor on the current error in the quickfix window, so that
4529 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004530 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 if (win != NULL
4532 && qf_index <= win->w_buffer->b_ml.ml_line_count
4533 && old_qf_index != qf_index)
4534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (qf_index > old_qf_index)
4536 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004537 win->w_redraw_top = old_qf_index;
4538 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
4540 else
4541 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004542 win->w_redraw_top = qf_index;
4543 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004545 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 }
4547 return win != NULL;
4548}
4549
4550/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004551 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004552 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004553 */
4554 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004555is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004556{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004557 // A window displaying the quickfix buffer will have the w_llist_ref field
4558 // set to NULL.
4559 // A window displaying a location list buffer will have the w_llist_ref
4560 // pointing to the location list.
Christian Brabandtfc682992023-09-03 20:20:52 +02004561 if (buf_valid(win->w_buffer) && bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004562 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4563 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004564 return TRUE;
4565
4566 return FALSE;
4567}
4568
4569/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004570 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4571 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004572 */
4573 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004574qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004575{
4576 win_T *win;
4577
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004578 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004579 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004580 return win;
4581 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004582}
4583
4584/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004585 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004586 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 */
4588 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004589qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004591 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004592 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004594 if (qi->qf_bufnr != INVALID_QFBUFNR)
4595 {
4596 buf_T *qfbuf;
4597 qfbuf = buflist_findnr(qi->qf_bufnr);
4598 if (qfbuf != NULL)
4599 return qfbuf;
4600 // buffer is no longer present
4601 qi->qf_bufnr = INVALID_QFBUFNR;
4602 }
4603
Bram Moolenaar9c102382006-05-03 21:26:49 +00004604 FOR_ALL_TAB_WINDOWS(tp, win)
4605 if (is_qf_win(win, qi))
4606 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004607
Bram Moolenaar9c102382006-05-03 21:26:49 +00004608 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609}
4610
4611/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004612 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004613 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004614 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004615 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004616did_set_quickfixtextfunc(optset_T *args UNUSED)
Bram Moolenaard43906d2020-07-20 21:31:32 +02004617{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004618 if (option_set_callback_func(p_qftf, &qftf_cb) == FAIL)
4619 return e_invalid_argument;
4620
4621 return NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004622}
4623
4624/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004625 * Update the w:quickfix_title variable in the quickfix/location list window in
4626 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004627 */
4628 static void
4629qf_update_win_titlevar(qf_info_T *qi)
4630{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004631 qf_list_T *qfl = qf_get_curlist(qi);
4632 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004633 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004634 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004635
Bram Moolenaar530bed92020-12-16 21:02:56 +01004636 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004637 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004638 if (is_qf_win(win, qi))
4639 {
4640 curwin = win;
4641 qf_set_title_var(qfl);
4642 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004643 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004644 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004645}
4646
4647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 * Find the quickfix buffer. If it exists, update the contents.
4649 */
4650 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004651qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652{
4653 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004654 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004657 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004658 buf = qf_find_buf(qi);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004659 if (buf == NULL)
4660 return;
4661
4662 linenr_T old_line_count = buf->b_ml.ml_line_count;
4663 int qf_winid = 0;
4664
4665 if (IS_LL_STACK(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004667 if (curwin->w_llist == qi)
4668 win = curwin;
4669 else
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004670 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004671 // Find the file window (non-quickfix) with this location list
4672 win = qf_find_win_with_loclist(qi);
4673 if (win == NULL)
4674 // File window is not found. Find the location list window.
4675 win = qf_find_win(qi);
4676 if (win == NULL)
4677 return;
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004678 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004679 qf_winid = win->w_id;
4680 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004681
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004682 // autocommands may cause trouble
4683 incr_quickfix_busy();
Bram Moolenaard0fab102022-10-20 16:03:33 +01004684
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004685 int do_fill = TRUE;
4686 if (old_last == NULL)
4687 {
4688 // set curwin/curbuf to buf and save a few things
4689 aucmd_prepbuf(&aco, buf);
4690 if (curbuf != buf)
4691 do_fill = FALSE; // failed to find a window for "buf"
4692 }
4693
4694 if (do_fill)
4695 {
4696 qf_update_win_titlevar(qi);
4697
4698 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
4699 ++CHANGEDTICK(buf);
4700
Bram Moolenaar864293a2016-06-02 13:40:04 +02004701 if (old_last == NULL)
4702 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004703 (void)qf_win_pos_update(qi, 0);
4704
4705 // restore curwin/curbuf and a few other things
4706 aucmd_restbuf(&aco);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004709
4710 // Only redraw when added lines are visible. This avoids flickering
4711 // when the added lines are not visible.
4712 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4713 redraw_buf_later(buf, UPD_NOT_VALID);
4714
4715 // always called after incr_quickfix_busy()
4716 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717}
4718
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004719/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004720 * Add an error line to the quickfix buffer.
4721 */
4722 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004723qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004724 buf_T *buf, // quickfix window buffer
4725 linenr_T lnum,
4726 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004727 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004728 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004729 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004730{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004731 buf_T *errbuf;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004732 garray_T *gap;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004733
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004734 gap = qfga_get();
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004735
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004736 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004737 // for this entry, then use it.
4738 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004739 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004740 ga_concat(gap, qftf_str);
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004741 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004742 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004743 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02004744 if (qfp->qf_module != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004745 ga_concat(gap, qfp->qf_module);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004746 else if (qfp->qf_fnum != 0
4747 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
4748 && errbuf->b_fname != NULL)
4749 {
4750 if (qfp->qf_type == 1) // :helpgrep
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004751 ga_concat(gap, gettail(errbuf->b_fname));
Bram Moolenaar858ba062020-05-31 23:11:59 +02004752 else
4753 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004754 // Shorten the file name if not done already.
4755 // For optimization, do this only for the first entry in a
4756 // buffer.
4757 if (first_bufline && (errbuf->b_sfname == NULL
4758 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02004759 {
4760 if (*dirname == NUL)
4761 mch_dirname(dirname, MAXPATHL);
4762 shorten_buf_fname(errbuf, dirname, FALSE);
4763 }
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004764 ga_concat(gap, errbuf->b_fname);
Bram Moolenaar858ba062020-05-31 23:11:59 +02004765 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004766 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004767
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004768 ga_append(gap, '|');
Bram Moolenaar858ba062020-05-31 23:11:59 +02004769
4770 if (qfp->qf_lnum > 0)
4771 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004772 qf_range_text(gap, qfp);
4773 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004774 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02004775 else if (qfp->qf_pattern != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004776 qf_fmt_text(gap, qfp->qf_pattern);
4777 ga_append(gap, '|');
4778 ga_append(gap, ' ');
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004779
Bram Moolenaar858ba062020-05-31 23:11:59 +02004780 // Remove newlines and leading whitespace from the text.
4781 // For an unrecognized line keep the indent, the compiler may
4782 // mark a word with ^^^^.
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004783 qf_fmt_text(gap, gap->ga_len > 3 ? skipwhite(qfp->qf_text)
4784 : qfp->qf_text);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004785 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004786
Shane Harper5bf04282023-06-07 19:09:57 +01004787 ga_append(gap, NUL);
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004788 if (ml_append_buf(buf, lnum, gap->ga_data, gap->ga_len, FALSE) == FAIL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004789 return FAIL;
4790
4791 return OK;
4792}
4793
Bram Moolenaard43906d2020-07-20 21:31:32 +02004794/*
4795 * Call the 'quickfixtextfunc' function to get the list of lines to display in
4796 * the quickfix window for the entries 'start_idx' to 'end_idx'.
4797 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004798 static list_T *
4799call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
4800{
Bram Moolenaard43906d2020-07-20 21:31:32 +02004801 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004802 list_T *qftf_list = NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004803 static int recursive = FALSE;
4804
4805 if (recursive)
4806 return NULL; // this doesn't work properly recursively
4807 recursive = TRUE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004808
4809 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
4810 // the text to display. Use the local value of 'quickfixtextfunc' if it is
4811 // set.
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004812 if (qfl->qf_qftf_cb.cb_name != NULL)
4813 cb = &qfl->qf_qftf_cb;
4814 if (cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004815 {
4816 typval_T args[1];
4817 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004818 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004819
4820 // create the dict argument
4821 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
Bram Moolenaard6c67622022-08-24 20:07:22 +01004822 {
4823 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004824 return NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01004825 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004826 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
4827 dict_add_number(d, "winid", (long)qf_winid);
4828 dict_add_number(d, "id", (long)qfl->qf_id);
4829 dict_add_number(d, "start_idx", start_idx);
4830 dict_add_number(d, "end_idx", end_idx);
4831 ++d->dv_refcount;
4832 args[0].v_type = VAR_DICT;
4833 args[0].vval.v_dict = d;
4834
Bram Moolenaard43906d2020-07-20 21:31:32 +02004835 qftf_list = NULL;
4836 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
4837 {
4838 if (rettv.v_type == VAR_LIST)
4839 {
4840 qftf_list = rettv.vval.v_list;
4841 qftf_list->lv_refcount++;
4842 }
4843 clear_tv(&rettv);
4844 }
4845 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004846 }
4847
Bram Moolenaard6c67622022-08-24 20:07:22 +01004848 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004849 return qftf_list;
4850}
4851
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 * Fill current buffer with quickfix errors, replacing any previous contents.
4854 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02004855 * If "old_last" is not NULL append the items after this one.
4856 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
4857 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 */
4859 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004860qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004862 linenr_T lnum;
4863 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004864 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004865 list_T *qftf_list = NULL;
4866 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
Bram Moolenaar864293a2016-06-02 13:40:04 +02004868 if (old_last == NULL)
4869 {
zeertzjqc7a8eb52024-05-08 20:22:40 +02004870 win_T *wp;
4871 tabpage_T *tp;
4872
Bram Moolenaar864293a2016-06-02 13:40:04 +02004873 if (buf != curbuf)
4874 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01004875 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004876 return;
4877 }
4878
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004879 // delete all existing lines
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01004880 //
4881 // Note: we cannot store undo information, because
4882 // qf buffer is usually not allowed to be modified.
4883 //
4884 // So we need to clean up undo information
4885 // otherwise autocommands may invalidate the undo stack
Bram Moolenaar864293a2016-06-02 13:40:04 +02004886 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02004887 (void)ml_delete((linenr_T)1);
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01004888
zeertzjqc7a8eb52024-05-08 20:22:40 +02004889 FOR_ALL_TAB_WINDOWS(tp, wp)
4890 if (wp->w_buffer == curbuf)
4891 wp->w_skipcol = 0;
4892
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01004893 // Remove all undo information
Christian Brabandt9071ed82024-02-15 20:17:37 +01004894 u_clearallandblockfree(curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004897 // Check if there is anything to display
Bram Moolenaar4f1b0832022-08-29 20:45:16 +01004898 if (qfl != NULL && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004900 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02004901 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004902 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02004903
4904 *dirname = NUL;
4905
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004906 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004907 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02004908 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004909 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004910 lnum = 0;
4911 }
4912 else
4913 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01004914 if (old_last->qf_next != NULL)
4915 qfp = old_last->qf_next;
4916 else
4917 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004918 lnum = buf->b_ml.ml_line_count;
4919 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004920
4921 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
4922 (long)qfl->qf_count);
4923 if (qftf_list != NULL)
4924 qftf_li = qftf_list->lv_first;
4925
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004926 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004928 char_u *qftf_str = NULL;
4929
Bram Moolenaard43906d2020-07-20 21:31:32 +02004930 // Use the text supplied by the user defined function (if any).
4931 // If the returned value is not string, then ignore the rest
4932 // of the returned values and use the default.
4933 if (qftf_li != NULL && !invalid_val)
4934 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004935 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02004936 if (qftf_str == NULL)
4937 invalid_val = TRUE;
4938 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004939
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004940 if (qf_buf_add_line(buf, lnum, qfp, dirname,
4941 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004943
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004944 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02004945 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004947 if (qfp == NULL)
4948 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004949
4950 if (qftf_li != NULL)
4951 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004953
4954 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004955 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02004956 (void)ml_delete(lnum + 1);
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01004957
4958 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 }
4960
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004961 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 check_lnums(TRUE);
4963
Bram Moolenaar864293a2016-06-02 13:40:04 +02004964 if (old_last == NULL)
4965 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004966 // Set the 'filetype' to "qf" each time after filling the buffer.
4967 // This resembles reading a file into a buffer, it's more logical when
4968 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02004969 ++curbuf_lock;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004970 set_option_value_give_err((char_u *)"ft",
4971 0L, (char_u *)"qf", OPT_LOCAL);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004972 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
zeertzjq5bf6c212024-03-31 18:41:27 +02004974 curbuf->b_keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02004975 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004977 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 FALSE, curbuf);
zeertzjq5bf6c212024-03-31 18:41:27 +02004979 curbuf->b_keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02004980 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004981
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004982 // make sure it will be redrawn
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004983 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar864293a2016-06-02 13:40:04 +02004984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004986 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 KeyTyped = old_KeyTyped;
4988}
4989
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004990/*
4991 * For every change made to the quickfix list, update the changed tick.
4992 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01004993 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004994qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004995{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004996 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004997}
4998
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005000 * Return the quickfix/location list number with the given identifier.
5001 * Returns -1 if list is not found.
5002 */
5003 static int
5004qf_id2nr(qf_info_T *qi, int_u qfid)
5005{
5006 int qf_idx;
5007
5008 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
5009 if (qi->qf_lists[qf_idx].qf_id == qfid)
5010 return qf_idx;
5011 return INVALID_QFIDX;
5012}
5013
5014/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005015 * If the current list is not "save_qfid" and we can find the list with that ID
5016 * then make it the current list.
5017 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005018 * Returns OK if successfully restored the list. Returns FAIL if the list with
5019 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005020 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005021 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005022qf_restore_list(qf_info_T *qi, int_u save_qfid)
5023{
5024 int curlist;
5025
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00005026 if (qf_get_curlist(qi)->qf_id == save_qfid)
5027 return OK;
5028
5029 curlist = qf_id2nr(qi, save_qfid);
5030 if (curlist < 0)
5031 // list is not present
5032 return FAIL;
5033 qi->qf_curlist = curlist;
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005034 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005035}
5036
5037/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005038 * Jump to the first entry if there is one.
5039 */
5040 static void
5041qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
5042{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005043 if (qf_restore_list(qi, save_qfid) == FAIL)
5044 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005045
Colin Kennedy21570352024-03-03 16:16:47 +01005046
5047 if (!check_can_set_curbuf_forceit(forceit))
5048 return;
5049
5050
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005051 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01005052 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005053 qf_jump(qi, 0, 0, forceit);
5054}
5055
5056/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005057 * Return TRUE when using ":vimgrep" for ":grep".
5058 */
5059 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005060grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005061{
Bram Moolenaar754b5602006-02-09 23:53:20 +00005062 return ((cmdidx == CMD_grep
5063 || cmdidx == CMD_lgrep
5064 || cmdidx == CMD_grepadd
5065 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005066 && STRCMP("internal",
5067 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
5068}
5069
5070/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005071 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005073 static char_u *
5074make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005076 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02005077 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005078 case CMD_make: return (char_u *)"make";
5079 case CMD_lmake: return (char_u *)"lmake";
5080 case CMD_grep: return (char_u *)"grep";
5081 case CMD_lgrep: return (char_u *)"lgrep";
5082 case CMD_grepadd: return (char_u *)"grepadd";
5083 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5084 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02005085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086}
5087
5088/*
5089 * Return the name for the errorfile, in allocated memory.
5090 * Find a new unique name when 'makeef' contains "##".
5091 * Returns NULL for error.
5092 */
5093 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01005094get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095{
5096 char_u *p;
5097 char_u *name;
5098 static int start = -1;
5099 static int off = 0;
5100#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02005101 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102#endif
5103
5104 if (*p_mef == NUL)
5105 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005106 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005108 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 return name;
5110 }
5111
5112 for (p = p_mef; *p; ++p)
5113 if (p[0] == '#' && p[1] == '#')
5114 break;
5115
5116 if (*p == NUL)
5117 return vim_strsave(p_mef);
5118
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005119 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 for (;;)
5121 {
5122 if (start == -1)
5123 start = mch_get_pid();
5124 else
5125 off += 19;
5126
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005127 name = alloc_id(STRLEN(p_mef) + 30, aid_qf_mef_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (name == NULL)
5129 break;
5130 STRCPY(name, p_mef);
5131 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
5132 STRCAT(name, p + 2);
5133 if (mch_getperm(name) < 0
5134#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005135 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 && mch_lstat((char *)name, &sb) < 0
5137#endif
5138 )
5139 break;
5140 vim_free(name);
5141 }
5142 return name;
5143}
5144
5145/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005146 * Form the complete command line to invoke 'make'/'grep'. Quote the command
5147 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
5148 */
5149 static char_u *
5150make_get_fullcmd(char_u *makecmd, char_u *fname)
5151{
5152 char_u *cmd;
5153 unsigned len;
5154
5155 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
5156 if (*p_sp != NUL)
5157 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005158 cmd = alloc_id(len, aid_qf_makecmd);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005159 if (cmd == NULL)
5160 return NULL;
5161 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
5162 (char *)p_shq);
5163
5164 // If 'shellpipe' empty: don't redirect to 'errorfile'.
5165 if (*p_sp != NUL)
5166 append_redir(cmd, len, p_sp, fname);
5167
5168 // Display the fully formed command. Output a newline if there's something
5169 // else than the :make command that was typed (in which case the cursor is
5170 // in column 0).
5171 if (msg_col == 0)
5172 msg_didout = FALSE;
5173 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005174 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005175 msg_outtrans(cmd); // show what we are doing
5176
5177 return cmd;
5178}
5179
5180/*
5181 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5182 */
5183 void
5184ex_make(exarg_T *eap)
5185{
5186 char_u *fname;
5187 char_u *cmd;
5188 char_u *enc = NULL;
5189 win_T *wp = NULL;
5190 qf_info_T *qi = &ql_info;
5191 int res;
5192 char_u *au_name = NULL;
5193 int_u save_qfid;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005194 char_u *errorformat = p_efm;
5195 int newlist = TRUE;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005196
5197 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5198 if (grep_internal(eap->cmdidx))
5199 {
5200 ex_vimgrep(eap);
5201 return;
5202 }
5203
5204 au_name = make_get_auname(eap->cmdidx);
5205 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5206 curbuf->b_fname, TRUE, curbuf))
5207 {
5208#ifdef FEAT_EVAL
5209 if (aborting())
5210 return;
5211#endif
5212 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005213 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005214
5215 if (is_loclist_cmd(eap->cmdidx))
5216 wp = curwin;
5217
5218 autowrite_all();
5219 fname = get_mef_name();
5220 if (fname == NULL)
5221 return;
5222 mch_remove(fname); // in case it's not unique
5223
5224 cmd = make_get_fullcmd(eap->arg, fname);
5225 if (cmd == NULL)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005226 {
5227 vim_free(fname);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005228 return;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005229 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005230
5231 // let the shell know if we are redirecting output or not
5232 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5233
5234#ifdef AMIGA
5235 out_flush();
5236 // read window status report and redraw before message
5237 (void)char_avail();
5238#endif
5239
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005240 incr_quickfix_busy();
5241
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005242 if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake)
5243 errorformat = p_gefm;
5244 if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
5245 newlist = FALSE;
5246
5247 res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep),
5248 enc);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005249 if (wp != NULL)
5250 {
5251 qi = GET_LOC_LIST(wp);
5252 if (qi == NULL)
5253 goto cleanup;
5254 }
5255 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005256 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005257
5258 // Remember the current quickfix list identifier, so that we can
5259 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005260 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005261 if (au_name != NULL)
5262 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5263 curbuf->b_fname, TRUE, curbuf);
5264 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5265 // display the first error
5266 qf_jump_first(qi, save_qfid, FALSE);
5267
5268cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005269 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005270 mch_remove(fname);
5271 vim_free(fname);
5272 vim_free(cmd);
5273}
5274
5275/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005276 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005277 */
5278 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005279qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005280{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005281 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005282
5283 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5284 return 0;
5285 return qf_get_curlist(qi)->qf_count;
5286}
5287
5288/*
5289 * Returns the number of valid entries in the current quickfix/location list.
5290 */
5291 int
5292qf_get_valid_size(exarg_T *eap)
5293{
5294 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005295 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005296 qfline_T *qfp;
5297 int i, sz = 0;
5298 int prev_fnum = 0;
5299
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005300 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5301 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005302
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005303 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005304 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005305 {
5306 if (qfp->qf_valid)
5307 {
5308 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005309 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005310 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5311 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005312 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005313 sz++;
5314 prev_fnum = qfp->qf_fnum;
5315 }
5316 }
5317 }
5318
5319 return sz;
5320}
5321
5322/*
5323 * Returns the current index of the quickfix/location list.
5324 * Returns 0 if there is an error.
5325 */
5326 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005327qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005328{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005329 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005330
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005331 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5332 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005333
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005334 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005335}
5336
5337/*
5338 * Returns the current index in the quickfix/location list (counting only valid
5339 * entries). If no valid entries are in the list, then returns 1.
5340 */
5341 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005342qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005343{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005344 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005345 qf_list_T *qfl;
5346 qfline_T *qfp;
5347 int i, eidx = 0;
5348 int prev_fnum = 0;
5349
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005350 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5351 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005352
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005353 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005354 qfp = qfl->qf_start;
5355
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005356 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005357 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005358 return 1;
5359
5360 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5361 {
5362 if (qfp->qf_valid)
5363 {
5364 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5365 {
5366 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5367 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005368 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005369 eidx++;
5370 prev_fnum = qfp->qf_fnum;
5371 }
5372 }
5373 else
5374 eidx++;
5375 }
5376 }
5377
5378 return eidx ? eidx : 1;
5379}
5380
5381/*
5382 * Get the 'n'th valid error entry in the quickfix or location list.
5383 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5384 * For :cdo and :ldo returns the 'n'th valid error entry.
5385 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5386 */
5387 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005388qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005389{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005390 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005391 int i, eidx;
5392 int prev_fnum = 0;
5393
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005394 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005395 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005396 return 1;
5397
Bram Moolenaar95946f12019-03-31 15:31:59 +02005398 eidx = 0;
5399 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005400 {
5401 if (qfp->qf_valid)
5402 {
5403 if (fdo)
5404 {
5405 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5406 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005407 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005408 eidx++;
5409 prev_fnum = qfp->qf_fnum;
5410 }
5411 }
5412 else
5413 eidx++;
5414 }
5415
5416 if (eidx == n)
5417 break;
5418 }
5419
5420 if (i <= qfl->qf_count)
5421 return i;
5422 else
5423 return 1;
5424}
5425
5426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005428 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005429 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 */
5431 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005432ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005434 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005435 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005436
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005437 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5438 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005439
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005440 if (eap->addr_count > 0)
5441 errornr = (int)eap->line2;
5442 else
5443 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005444 switch (eap->cmdidx)
5445 {
5446 case CMD_cc: case CMD_ll:
5447 errornr = 0;
5448 break;
5449 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5450 case CMD_lfirst:
5451 errornr = 1;
5452 break;
5453 default:
5454 errornr = 32767;
5455 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005456 }
5457
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005458 // For cdo and ldo commands, jump to the nth valid error.
5459 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005460 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5461 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005462 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005463 eap->addr_count > 0 ? (int)eap->line1 : 1,
5464 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5465
5466 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467}
5468
5469/*
5470 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005471 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005472 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 */
5474 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005475ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005477 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005478 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005479 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005480
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005481 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5482 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005483
Bram Moolenaar55b69262017-08-13 13:42:01 +02005484 if (eap->addr_count > 0
5485 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5486 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005487 errornr = (int)eap->line2;
5488 else
5489 errornr = 1;
5490
Bram Moolenaar39665952018-08-15 20:59:48 +02005491 // Depending on the command jump to either next or previous entry/file.
5492 switch (eap->cmdidx)
5493 {
5494 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5495 dir = FORWARD;
5496 break;
5497 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5498 case CMD_lNext:
5499 dir = BACKWARD;
5500 break;
5501 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5502 dir = FORWARD_FILE;
5503 break;
5504 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5505 dir = BACKWARD_FILE;
5506 break;
5507 default:
5508 dir = FORWARD;
5509 break;
5510 }
5511
5512 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513}
5514
5515/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005516 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5517 * The index of the entry is stored in 'errornr'.
5518 * Returns NULL if an entry is not found.
5519 */
5520 static qfline_T *
5521qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5522{
5523 qfline_T *qfp = NULL;
5524 int idx = 0;
5525
5526 // Find the first entry in this file
5527 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5528 if (qfp->qf_fnum == bnr)
5529 break;
5530
5531 *errornr = idx;
5532 return qfp;
5533}
5534
5535/*
5536 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5537 * with the error number for the first entry. Assumes the entries are sorted in
5538 * the quickfix list by line number.
5539 */
5540 static qfline_T *
5541qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5542{
5543 while (!got_int
5544 && entry->qf_prev != NULL
5545 && entry->qf_fnum == entry->qf_prev->qf_fnum
5546 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5547 {
5548 entry = entry->qf_prev;
5549 --*errornr;
5550 }
5551
5552 return entry;
5553}
5554
5555/*
5556 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5557 * with the error number for the last entry. Assumes the entries are sorted in
5558 * the quickfix list by line number.
5559 */
5560 static qfline_T *
5561qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5562{
5563 while (!got_int &&
5564 entry->qf_next != NULL
5565 && entry->qf_fnum == entry->qf_next->qf_fnum
5566 && entry->qf_lnum == entry->qf_next->qf_lnum)
5567 {
5568 entry = entry->qf_next;
5569 ++*errornr;
5570 }
5571
5572 return entry;
5573}
5574
5575/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005576 * Returns TRUE if the specified quickfix entry is
5577 * after the given line (linewise is TRUE)
5578 * or after the line and column.
5579 */
5580 static int
5581qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5582{
5583 if (linewise)
5584 return qfp->qf_lnum > pos->lnum;
5585 else
5586 return (qfp->qf_lnum > pos->lnum ||
5587 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5588}
5589
5590/*
5591 * Returns TRUE if the specified quickfix entry is
5592 * before the given line (linewise is TRUE)
5593 * or before the line and column.
5594 */
5595 static int
5596qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5597{
5598 if (linewise)
5599 return qfp->qf_lnum < pos->lnum;
5600 else
5601 return (qfp->qf_lnum < pos->lnum ||
5602 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5603}
5604
5605/*
5606 * Returns TRUE if the specified quickfix entry is
5607 * on or after the given line (linewise is TRUE)
5608 * or on or after the line and column.
5609 */
5610 static int
5611qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5612{
5613 if (linewise)
5614 return qfp->qf_lnum >= pos->lnum;
5615 else
5616 return (qfp->qf_lnum > pos->lnum ||
5617 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5618}
5619
5620/*
5621 * Returns TRUE if the specified quickfix entry is
5622 * on or before the given line (linewise is TRUE)
5623 * or on or before the line and column.
5624 */
5625 static int
5626qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5627{
5628 if (linewise)
5629 return qfp->qf_lnum <= pos->lnum;
5630 else
5631 return (qfp->qf_lnum < pos->lnum ||
5632 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5633}
5634
5635/*
5636 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5637 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5638 * multiple entries on a single line as one. Otherwise returns the entry after
5639 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005640 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5641 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005642 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005643 */
5644 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005645qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005646 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005647 pos_T *pos,
5648 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005649 qfline_T *qfp,
5650 int *errornr)
5651{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005652 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005653 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005654 return qfp;
5655
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005656 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005657 while (qfp->qf_next != NULL
5658 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005659 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005660 {
5661 qfp = qfp->qf_next;
5662 ++*errornr;
5663 }
5664
5665 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005666 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005667 return NULL;
5668
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005669 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005670 qfp = qfp->qf_next;
5671 ++*errornr;
5672
5673 return qfp;
5674}
5675
5676/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005677 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5678 * If 'linewise' is TRUE, returns the entry before the specified line and
5679 * treats multiple entries on a single line as one. Otherwise returns the entry
5680 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005681 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5682 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005683 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005684 */
5685 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005686qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005687 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005688 pos_T *pos,
5689 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005690 qfline_T *qfp,
5691 int *errornr)
5692{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005693 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005694 while (qfp->qf_next != NULL
5695 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005696 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005697 {
5698 qfp = qfp->qf_next;
5699 ++*errornr;
5700 }
5701
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005702 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005703 return NULL;
5704
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005705 if (linewise)
5706 // If multiple entries are on the same line, then use the first entry
5707 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005708
5709 return qfp;
5710}
5711
5712/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005713 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005714 * the direction 'dir'.
5715 */
5716 static qfline_T *
5717qf_find_closest_entry(
5718 qf_list_T *qfl,
5719 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005720 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005721 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005722 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005723 int *errornr)
5724{
5725 qfline_T *qfp;
5726
5727 *errornr = 0;
5728
5729 // Find the first entry in this file
5730 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5731 if (qfp == NULL)
5732 return NULL; // no entry in this file
5733
5734 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005735 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005736 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005737 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005738
5739 return qfp;
5740}
5741
5742/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005743 * Get the nth quickfix entry below the specified entry. Searches forward in
5744 * the list. If linewise is TRUE, then treat multiple entries on a single line
5745 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005746 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005747 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005748qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005749{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01005750 qfline_T *entry = entry_arg;
5751
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005752 while (n-- > 0 && !got_int)
5753 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005754 int first_errornr = *errornr;
5755
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005756 if (linewise)
5757 // Treat all the entries on the same line in this file as one
5758 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005759
5760 if (entry->qf_next == NULL
5761 || entry->qf_next->qf_fnum != entry->qf_fnum)
5762 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005763 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005764 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005765 break;
5766 }
5767
5768 entry = entry->qf_next;
5769 ++*errornr;
5770 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005771}
5772
5773/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005774 * Get the nth quickfix entry above the specified entry. Searches backwards in
5775 * the list. If linewise is TRUE, then treat multiple entries on a single line
5776 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005777 */
Bram Moolenaar64416122019-06-07 21:37:13 +02005778 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005779qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005780{
5781 while (n-- > 0 && !got_int)
5782 {
5783 if (entry->qf_prev == NULL
5784 || entry->qf_prev->qf_fnum != entry->qf_fnum)
5785 break;
5786
5787 entry = entry->qf_prev;
5788 --*errornr;
5789
5790 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005791 if (linewise)
5792 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005793 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005794}
5795
5796/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005797 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
5798 * the specified direction. Returns the error number in the quickfix list or 0
5799 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005800 */
5801 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005802qf_find_nth_adj_entry(
5803 qf_list_T *qfl,
5804 int bnr,
5805 pos_T *pos,
5806 int n,
5807 int dir,
5808 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005809{
5810 qfline_T *adj_entry;
5811 int errornr;
5812
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005813 // Find an entry closest to the specified position
5814 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005815 if (adj_entry == NULL)
5816 return 0;
5817
5818 if (--n > 0)
5819 {
5820 // Go to the n'th entry in the current buffer
5821 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02005822 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005823 else
Bram Moolenaar64416122019-06-07 21:37:13 +02005824 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005825 }
5826
5827 return errornr;
5828}
5829
5830/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005831 * Jump to a quickfix entry in the current file nearest to the current line or
5832 * current line/col.
5833 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
5834 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005835 */
5836 void
5837ex_cbelow(exarg_T *eap)
5838{
5839 qf_info_T *qi;
5840 qf_list_T *qfl;
5841 int dir;
5842 int buf_has_flag;
5843 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005844 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005845
5846 if (eap->addr_count > 0 && eap->line2 <= 0)
5847 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005848 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005849 return;
5850 }
5851
5852 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005853 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
5854 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005855 buf_has_flag = BUF_HAS_QF_ENTRY;
5856 else
5857 buf_has_flag = BUF_HAS_LL_ENTRY;
5858 if (!(curbuf->b_has_qf_entry & buf_has_flag))
5859 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005860 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005861 return;
5862 }
5863
5864 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5865 return;
5866
5867 qfl = qf_get_curlist(qi);
5868 // check if the list has valid errors
5869 if (!qf_list_has_valid_entries(qfl))
5870 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005871 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005872 return;
5873 }
5874
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005875 if (eap->cmdidx == CMD_cbelow
5876 || eap->cmdidx == CMD_lbelow
5877 || eap->cmdidx == CMD_cafter
5878 || eap->cmdidx == CMD_lafter)
5879 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005880 dir = FORWARD;
5881 else
5882 dir = BACKWARD;
5883
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005884 pos = curwin->w_cursor;
5885 // A quickfix entry column number is 1 based whereas cursor column
5886 // number is 0 based. Adjust the column number.
5887 pos.col++;
5888 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
5889 eap->addr_count > 0 ? eap->line2 : 0, dir,
5890 eap->cmdidx == CMD_cbelow
5891 || eap->cmdidx == CMD_lbelow
5892 || eap->cmdidx == CMD_cabove
5893 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005894
5895 if (errornr > 0)
5896 qf_jump(qi, 0, errornr, FALSE);
5897 else
5898 emsg(_(e_no_more_items));
5899}
5900
5901/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01005902 * Return the autocmd name for the :cfile Ex commands
5903 */
5904 static char_u *
5905cfile_get_auname(cmdidx_T cmdidx)
5906{
5907 switch (cmdidx)
5908 {
5909 case CMD_cfile: return (char_u *)"cfile";
5910 case CMD_cgetfile: return (char_u *)"cgetfile";
5911 case CMD_caddfile: return (char_u *)"caddfile";
5912 case CMD_lfile: return (char_u *)"lfile";
5913 case CMD_lgetfile: return (char_u *)"lgetfile";
5914 case CMD_laddfile: return (char_u *)"laddfile";
5915 default: return NULL;
5916 }
5917}
5918
5919/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005920 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005921 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 */
5923 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005924ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005926 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005927 win_T *wp = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00005928 qf_info_T *qi = &ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01005929 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005930 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005931 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005932
Bram Moolenaara16123a2019-03-28 20:31:07 +01005933 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005934 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5935 NULL, FALSE, curbuf))
5936 {
5937#ifdef FEAT_EVAL
5938 if (aborting())
5939 return;
5940#endif
5941 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01005942
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005943 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02005944#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02005945 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02005946 {
5947 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02005948 NULL, NULL,
5949 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02005950 if (browse_file == NULL)
5951 return;
5952 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
5953 vim_free(browse_file);
5954 }
5955 else
5956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005958 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005959
Bram Moolenaar39665952018-08-15 20:59:48 +02005960 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01005961 wp = curwin;
5962
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005963 incr_quickfix_busy();
5964
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005965 // This function is used by the :cfile, :cgetfile and :caddfile
5966 // commands.
Colin Kennedy21570352024-03-03 16:16:47 +01005967 // :cfile always creates a new quickfix list and may jump to the
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005968 // first error.
5969 // :cgetfile creates a new quickfix list but doesn't jump to the
5970 // first error.
5971 // :caddfile adds to an existing quickfix list. If there is no
5972 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005973 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02005974 && eap->cmdidx != CMD_laddfile),
5975 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01005976 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005977 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01005978 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005979 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005980 {
5981 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005982 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005983 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005984 }
5985 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005986 qf_list_changed(qf_get_curlist(qi));
5987 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01005988 if (au_name != NULL)
5989 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01005990
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005991 // Jump to the first error for a new list and if autocmds didn't
5992 // free the list.
5993 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
5994 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005995 // display the first error
5996 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005997
5998 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02005999}
6000
6001/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006002 * Return the vimgrep autocmd name.
6003 */
6004 static char_u *
6005vgr_get_auname(cmdidx_T cmdidx)
6006{
6007 switch (cmdidx)
6008 {
6009 case CMD_vimgrep: return (char_u *)"vimgrep";
6010 case CMD_lvimgrep: return (char_u *)"lvimgrep";
6011 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
6012 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
6013 case CMD_grep: return (char_u *)"grep";
6014 case CMD_lgrep: return (char_u *)"lgrep";
6015 case CMD_grepadd: return (char_u *)"grepadd";
6016 case CMD_lgrepadd: return (char_u *)"lgrepadd";
6017 default: return NULL;
6018 }
6019}
6020
6021/*
6022 * Initialize the regmatch used by vimgrep for pattern "s".
6023 */
6024 static void
6025vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
6026{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006027 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006028 regmatch->regprog = NULL;
6029
6030 if (s == NULL || *s == NUL)
6031 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006032 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006033 if (last_search_pat() == NULL)
6034 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006035 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006036 return;
6037 }
6038 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
6039 }
6040 else
6041 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
6042
6043 regmatch->rmm_ic = p_ic;
6044 regmatch->rmm_maxcol = 0;
6045}
6046
6047/*
6048 * Display a file name when vimgrep is running.
6049 */
6050 static void
6051vgr_display_fname(char_u *fname)
6052{
6053 char_u *p;
6054
6055 msg_start();
6056 p = msg_strtrunc(fname, TRUE);
6057 if (p == NULL)
6058 msg_outtrans(fname);
6059 else
6060 {
6061 msg_outtrans(p);
6062 vim_free(p);
6063 }
6064 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006065 msg_didout = FALSE; // overwrite this message
6066 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006067 msg_col = 0;
6068 out_flush();
6069}
6070
6071/*
6072 * Load a dummy buffer to search for a pattern using vimgrep.
6073 */
6074 static buf_T *
6075vgr_load_dummy_buf(
6076 char_u *fname,
6077 char_u *dirname_start,
6078 char_u *dirname_now)
6079{
6080 int save_mls;
6081#if defined(FEAT_SYN_HL)
6082 char_u *save_ei = NULL;
6083#endif
6084 buf_T *buf;
6085
6086#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006087 // Don't do Filetype autocommands to avoid loading syntax and
6088 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006089 save_ei = au_event_disable(",Filetype");
6090#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006091 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006092 save_mls = p_mls;
6093 p_mls = 0;
6094
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006095 // Load file into a buffer, so that 'fileencoding' is detected,
6096 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006097 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
6098
6099 p_mls = save_mls;
6100#if defined(FEAT_SYN_HL)
6101 au_event_restore(save_ei);
6102#endif
6103
6104 return buf;
6105}
6106
6107/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006108 * Check whether a quickfix/location list is valid. Autocmds may remove or
6109 * change a quickfix list when vimgrep is running. If the list is not found,
6110 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006111 */
6112 static int
6113vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006114 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006115 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006116 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006117 char_u *title)
6118{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006119 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006120 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006121 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006122 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006123 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006124 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02006125 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006126 return FALSE;
6127 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006128 else
6129 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006130 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006131 qf_new_list(qi, title);
6132 return TRUE;
6133 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006134 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006135
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02006136 if (qf_restore_list(qi, qfid) == FAIL)
6137 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006138
6139 return TRUE;
6140}
6141
6142/*
6143 * Search for a pattern in all the lines in a buffer and add the matching lines
6144 * to a quickfix list.
6145 */
6146 static int
6147vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006148 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006149 char_u *fname,
6150 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006151 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006152 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01006153 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006154 int duplicate_name,
6155 int flags)
6156{
6157 int found_match = FALSE;
6158 long lnum;
6159 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02006160 int pat_len = (int)STRLEN(spat);
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006161 if (pat_len > MAX_FUZZY_MATCHES)
6162 pat_len = MAX_FUZZY_MATCHES;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006163
Bram Moolenaar1c299432018-10-28 14:36:09 +01006164 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006165 {
6166 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006167 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006168 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006169 // Regular expression match
6170 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006171 col, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006172 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006173 // Pass the buffer number so that it gets used even for a
6174 // dummy buffer, unless duplicate_name is set, then the
6175 // buffer will be wiped out below.
6176 if (qf_add_entry(qfl,
6177 NULL, // dir
6178 fname,
6179 NULL,
6180 duplicate_name ? 0 : buf->b_fnum,
6181 ml_get_buf(buf,
6182 regmatch->startpos[0].lnum + lnum, FALSE),
6183 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02006184 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006185 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02006186 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006187 FALSE, // vis_col
6188 NULL, // search pattern
6189 0, // nr
6190 0, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02006191 NULL, // user_data
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006192 TRUE // valid
6193 ) == QF_FAIL)
6194 {
6195 got_int = TRUE;
6196 break;
6197 }
6198 found_match = TRUE;
6199 if (--*tomatch == 0)
6200 break;
6201 if ((flags & VGR_GLOBAL) == 0
6202 || regmatch->endpos[0].lnum > 0)
6203 break;
6204 col = regmatch->endpos[0].col
6205 + (col == regmatch->endpos[0].col);
zeertzjq94b7c322024-03-12 21:50:32 +01006206 if (col > ml_get_buf_len(buf, lnum))
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006207 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006208 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006209 }
6210 else
6211 {
6212 char_u *str = ml_get_buf(buf, lnum, FALSE);
zeertzjq8c55d602024-03-13 20:42:26 +01006213 colnr_T linelen = ml_get_buf_len(buf, lnum);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006214 int score;
6215 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006216 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006217
6218 // Fuzzy string match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006219 CLEAR_FIELD(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006220 while (fuzzy_match(str + col, spat, FALSE, &score, matches, sz) > 0)
6221 {
6222 // Pass the buffer number so that it gets used even for a
6223 // dummy buffer, unless duplicate_name is set, then the
6224 // buffer will be wiped out below.
6225 if (qf_add_entry(qfl,
6226 NULL, // dir
6227 fname,
6228 NULL,
6229 duplicate_name ? 0 : buf->b_fnum,
6230 str,
6231 lnum,
thinca6864efa2021-06-19 20:45:20 +02006232 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006233 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006234 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006235 FALSE, // vis_col
6236 NULL, // search pattern
6237 0, // nr
6238 0, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02006239 NULL, // user_data
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006240 TRUE // valid
6241 ) == QF_FAIL)
6242 {
6243 got_int = TRUE;
6244 break;
6245 }
6246 found_match = TRUE;
6247 if (--*tomatch == 0)
6248 break;
6249 if ((flags & VGR_GLOBAL) == 0)
6250 break;
6251 col = matches[pat_len - 1] + col + 1;
zeertzjq8c55d602024-03-13 20:42:26 +01006252 if (col > linelen)
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006253 break;
6254 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006255 }
6256 line_breakcheck();
6257 if (got_int)
6258 break;
6259 }
6260
6261 return found_match;
6262}
6263
6264/*
6265 * Jump to the first match and update the directory.
6266 */
6267 static void
6268vgr_jump_to_match(
6269 qf_info_T *qi,
6270 int forceit,
6271 int *redraw_for_dummy,
6272 buf_T *first_match_buf,
6273 char_u *target_dir)
6274{
6275 buf_T *buf;
6276
6277 buf = curbuf;
6278 qf_jump(qi, 0, 0, forceit);
6279 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006280 // If we jumped to another buffer redrawing will already be
6281 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006282 *redraw_for_dummy = FALSE;
6283
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006284 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006285 if (curbuf == first_match_buf && target_dir != NULL)
6286 {
6287 exarg_T ea;
6288
Bram Moolenaara80faa82020-04-12 19:37:17 +02006289 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006290 ea.arg = target_dir;
6291 ea.cmdidx = CMD_lcd;
6292 ex_cd(&ea);
6293 }
6294}
6295
6296/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006297 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006298 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006299typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006300{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006301 long tomatch; // maximum number of matches to find
6302 char_u *spat; // search pattern
6303 int flags; // search modifier
6304 char_u **fnames; // list of files to search
6305 int fcount; // number of files
6306 regmmatch_T regmatch; // compiled search pattern
6307 char_u *qf_title; // quickfix list title
6308} vgr_args_T;
6309
6310/*
6311 * Process :vimgrep command arguments. The command syntax is:
6312 *
6313 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6314 */
6315 static int
6316vgr_process_args(
6317 exarg_T *eap,
6318 vgr_args_T *args)
6319{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006320 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006321
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00006322 CLEAR_POINTER(args);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006323
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006324 args->regmatch.regprog = NULL;
6325 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006326
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006327 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006328 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006329 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006330 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006331
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006332 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006333 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006334 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006335 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006336 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006337 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006338 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006339
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006340 vgr_init_regmatch(&args->regmatch, args->spat);
6341 if (args->regmatch.regprog == NULL)
6342 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006343
6344 p = skipwhite(p);
6345 if (*p == NUL)
6346 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006347 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006348 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006349 }
6350
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006351 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006352 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6353 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006354 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006355 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006356 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006357 }
6358
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006359 return OK;
6360}
6361
6362/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006363 * Return TRUE if "buf" had an existing swap file, the current swap file does
6364 * not end in ".swp".
6365 */
6366 static int
6367existing_swapfile(buf_T *buf)
6368{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006369 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006370 {
6371 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6372 size_t len = STRLEN(fname);
6373
6374 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6375 }
6376 return FALSE;
6377}
6378
6379/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006380 * Search for a pattern in a list of files and populate the quickfix list with
6381 * the matches.
6382 */
6383 static int
6384vgr_process_files(
6385 win_T *wp,
6386 qf_info_T *qi,
6387 vgr_args_T *cmd_args,
6388 int *redraw_for_dummy,
6389 buf_T **first_match_buf,
6390 char_u **target_dir)
6391{
6392 int status = FAIL;
6393 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6394 time_t seconds = 0;
6395 char_u *fname;
6396 int fi;
6397 buf_T *buf;
6398 int duplicate_name = FALSE;
6399 int using_dummy;
6400 char_u *dirname_start = NULL;
6401 char_u *dirname_now = NULL;
6402 int found_match;
6403 aco_save_T aco;
6404
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006405 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6406 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006407 if (dirname_start == NULL || dirname_now == NULL)
6408 goto theend;
6409
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006410 // Remember the current directory, because a BufRead autocommand that does
6411 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006412 mch_dirname(dirname_start, MAXPATHL);
6413
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006414 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006415 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6416 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006417 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006418 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006419 if (time(NULL) > seconds)
6420 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006421 // Display the file name every second or so, show the user we are
6422 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006423 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006424 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006425 }
6426
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006427 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006428 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6429 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006430 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006431 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006432 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006433 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006434
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006435 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006436 }
6437 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006438 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006439 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006440
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006441 // Check whether the quickfix list is still valid. When loading a
6442 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006443 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006444 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006445
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006446 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006447
Bram Moolenaar81695252004-12-29 20:58:21 +00006448 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006449 {
6450 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006451 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006452 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006453 else
6454 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006455 // Try for a match in all lines of the buffer.
6456 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006457 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006458 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006459 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006460
Bram Moolenaar81695252004-12-29 20:58:21 +00006461 if (using_dummy)
6462 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006463 if (found_match && *first_match_buf == NULL)
6464 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006465 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006466 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006467 // Never keep a dummy buffer if there is another buffer
6468 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006469 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006470 buf = NULL;
6471 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006472 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006473 || buf->b_p_bh[0] == 'u' // "unload"
6474 || buf->b_p_bh[0] == 'w' // "wipe"
6475 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006476 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006477 // When no match was found we don't need to remember the
6478 // buffer, wipe it out. If there was a match and it
6479 // wasn't the first one or we won't jump there: only
6480 // unload the buffer.
6481 // Ignore 'hidden' here, because it may lead to having too
6482 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006483 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006484 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006485 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006486 buf = NULL;
6487 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006488 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006489 || (cmd_args->flags & VGR_NOJUMP)
6490 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006491 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006492 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006493 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006494 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006495 buf = NULL;
6496 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006497 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006498
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006499 if (buf != NULL)
6500 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006501 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006502 buf->b_flags &= ~BF_DUMMY;
6503
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006504 // If the buffer is still loaded we need to use the
6505 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006506 if (buf == *first_match_buf
6507 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006508 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006509 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006510
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006511 // The buffer is still loaded, the Filetype autocommands
6512 // need to be done now, in that buffer. And the modelines
6513 // need to be done (again). But not the window-local
6514 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006515 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006516 if (curbuf == buf)
6517 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006518#if defined(FEAT_SYN_HL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00006519 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006520 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006521#endif
Bram Moolenaare76062c2022-11-28 18:51:43 +00006522 do_modelines(OPT_NOWIN);
6523 aucmd_restbuf(&aco);
6524 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006525 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006526 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006527 }
6528 }
6529
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006530 status = OK;
6531
6532theend:
6533 vim_free(dirname_now);
6534 vim_free(dirname_start);
6535 return status;
6536}
6537
6538/*
6539 * ":vimgrep {pattern} file(s)"
6540 * ":vimgrepadd {pattern} file(s)"
6541 * ":lvimgrep {pattern} file(s)"
6542 * ":lvimgrepadd {pattern} file(s)"
6543 */
6544 void
6545ex_vimgrep(exarg_T *eap)
6546{
6547 vgr_args_T args;
6548 qf_info_T *qi;
6549 qf_list_T *qfl;
6550 int_u save_qfid;
6551 win_T *wp = NULL;
6552 int redraw_for_dummy = FALSE;
6553 buf_T *first_match_buf = NULL;
6554 char_u *target_dir = NULL;
6555 char_u *au_name = NULL;
6556 int status;
6557
Colin Kennedy21570352024-03-03 16:16:47 +01006558 if (!check_can_set_curbuf_forceit(eap->forceit))
6559 return;
6560
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006561 au_name = vgr_get_auname(eap->cmdidx);
6562 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6563 curbuf->b_fname, TRUE, curbuf))
6564 {
6565#ifdef FEAT_EVAL
6566 if (aborting())
6567 return;
6568#endif
6569 }
6570
6571 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6572 if (qi == NULL)
6573 return;
6574
6575 if (vgr_process_args(eap, &args) == FAIL)
6576 goto theend;
6577
6578 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6579 && eap->cmdidx != CMD_vimgrepadd
6580 && eap->cmdidx != CMD_lvimgrepadd)
6581 || qf_stack_empty(qi))
6582 // make place for a new list
6583 qf_new_list(qi, args.qf_title);
6584
6585 incr_quickfix_busy();
6586
6587 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6588 &first_match_buf, &target_dir);
6589 if (status != OK)
6590 {
6591 FreeWild(args.fcount, args.fnames);
6592 decr_quickfix_busy();
6593 goto theend;
6594 }
6595
6596 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006597
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006598 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006599 qfl->qf_nonevalid = FALSE;
6600 qfl->qf_ptr = qfl->qf_start;
6601 qfl->qf_index = 1;
6602 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006603
Bram Moolenaar864293a2016-06-02 13:40:04 +02006604 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006605
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006606 // Remember the current quickfix list identifier, so that we can check for
6607 // autocommands changing the current quickfix list.
6608 save_qfid = qf_get_curlist(qi)->qf_id;
6609
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006610 if (au_name != NULL)
6611 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6612 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006613 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6614 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006615 if (!qflist_valid(wp, save_qfid)
6616 || qf_restore_list(qi, save_qfid) == FAIL)
6617 {
6618 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006619 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006620 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006621
zeertzjq8c55d602024-03-13 20:42:26 +01006622 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006623 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006624 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006625 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006626 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6627 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006628 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006629 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006630 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006631
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006632 decr_quickfix_busy();
6633
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006634 // If we loaded a dummy buffer into the current window, the autocommands
6635 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006636 if (redraw_for_dummy)
6637 {
6638#ifdef FEAT_FOLDING
6639 foldUpdateAll(curwin);
6640#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006641 redraw_later(UPD_NOT_VALID);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006642#endif
6643 }
6644
Bram Moolenaar86b68352004-12-27 21:59:20 +00006645theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006646 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006647 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006648 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006649}
6650
6651/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006652 * Restore current working directory to "dirname_start" if they differ, taking
6653 * into account whether it is set locally or globally.
6654 */
6655 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006656restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006657{
6658 char_u *dirname_now = alloc(MAXPATHL);
6659
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006660 if (dirname_now == NULL)
6661 return;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006662
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006663 mch_dirname(dirname_now, MAXPATHL);
6664 if (STRCMP(dirname_start, dirname_now) != 0)
6665 {
6666 // If the directory has changed, change it back by building up an
6667 // appropriate ex command and executing it.
6668 exarg_T ea;
6669
6670 CLEAR_FIELD(ea);
6671 ea.arg = dirname_start;
6672 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6673 ex_cd(&ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006674 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006675 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006676}
6677
6678/*
6679 * Load file "fname" into a dummy buffer and return the buffer pointer,
6680 * placing the directory resulting from the buffer load into the
6681 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6682 * prior to calling this function. Restores directory to "dirname_start" prior
6683 * to returning, if autocmds or the 'autochdir' option have changed it.
6684 *
6685 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6686 * or wipe_dummy_buffer() later!
6687 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006688 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006689 */
6690 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006691load_dummy_buffer(
6692 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006693 char_u *dirname_start, // in: old directory
6694 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006695{
6696 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006697 bufref_T newbufref;
6698 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006699 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006700 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006701 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006702
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006703 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006704 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6705 if (newbuf == NULL)
6706 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006707 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006708
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006709 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006710 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6711
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006712 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006713 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006714 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006715 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006716 ++newbuf->b_locked;
6717
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006718 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006719 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006720 if (curbuf == newbuf)
Bram Moolenaar81695252004-12-29 20:58:21 +00006721 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00006722 // Need to set the filename for autocommands.
6723 (void)setfname(curbuf, fname, NULL, FALSE);
6724
6725 // Create swap file now to avoid the ATTENTION message.
6726 check_need_swap(TRUE);
6727
6728 // Remove the "dummy" flag, otherwise autocommands may not
6729 // work.
6730 curbuf->b_flags &= ~BF_DUMMY;
6731
6732 newbuf_to_wipe.br_buf = NULL;
6733 readfile_result = readfile(fname, NULL,
6734 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6735 NULL, READ_NEW | READ_DUMMY);
6736 --newbuf->b_locked;
6737 if (readfile_result == OK
6738 && !got_int
6739 && !(curbuf->b_flags & BF_NEW))
Bram Moolenaar81695252004-12-29 20:58:21 +00006740 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00006741 failed = FALSE;
6742 if (curbuf != newbuf)
6743 {
6744 // Bloody autocommands changed the buffer! Can happen when
6745 // using netrw and editing a remote file. Use the current
6746 // buffer instead, delete the dummy one after restoring the
6747 // window stuff.
6748 set_bufref(&newbuf_to_wipe, newbuf);
6749 newbuf = curbuf;
6750 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006751 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00006752
6753 // restore curwin/curbuf and a few other things
6754 aucmd_restbuf(&aco);
Bram Moolenaar81695252004-12-29 20:58:21 +00006755
Bram Moolenaar84497cd2022-11-28 20:34:52 +00006756 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
6757 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
6758 }
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006759
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006760 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
6761 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02006762 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006763 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006764
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006765 // When autocommands/'autochdir' option changed directory: go back.
6766 // Let the caller know what the resulting dir was first, in case it is
6767 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006768 mch_dirname(resulting_dir, MAXPATHL);
6769 restore_start_dir(dirname_start);
6770
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006771 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00006772 return NULL;
6773 if (failed)
6774 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006775 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006776 return NULL;
6777 }
6778 return newbuf;
6779}
6780
6781/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006782 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
6783 * directory to "dirname_start" prior to returning, if autocmds or the
6784 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006785 */
6786 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006787wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006788{
Bram Moolenaar2573af32020-03-14 17:21:34 +01006789 // If any autocommand opened a window on the dummy buffer, close that
6790 // window. If we can't close them all then give up.
6791 while (buf->b_nwindows > 0)
6792 {
6793 int did_one = FALSE;
6794 win_T *wp;
6795
6796 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02006797 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01006798 if (wp->w_buffer == buf)
6799 {
6800 if (win_close(wp, FALSE) == OK)
6801 did_one = TRUE;
6802 break;
6803 }
6804 if (!did_one)
6805 return;
6806 }
6807
6808 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00006809 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006810#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00006811 cleanup_T cs;
6812
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006813 // Reset the error/interrupt/exception state here so that aborting()
6814 // returns FALSE when wiping out the buffer. Otherwise it doesn't
6815 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006816 enter_cleanup(&cs);
6817#endif
6818
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01006819 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006820
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006821#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006822 // Restore the error/interrupt/exception state if not discarded by a
6823 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00006824 leave_cleanup(&cs);
6825#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006826 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006827 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00006828 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006829}
6830
6831/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006832 * Unload the dummy buffer that load_dummy_buffer() created. Restores
6833 * directory to "dirname_start" prior to returning, if autocmds or the
6834 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00006835 */
6836 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006837unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00006838{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006839 if (curbuf == buf) // safety check
6840 return;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006841
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006842 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
6843
6844 // When autocommands/'autochdir' option changed directory: go back.
6845 restore_start_dir(dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00006846}
6847
Bram Moolenaar05159a02005-02-26 23:04:13 +00006848#if defined(FEAT_EVAL) || defined(PROTO)
6849/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006850 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01006851 * to 'list'. Returns OK on success.
6852 */
6853 static int
6854get_qfline_items(qfline_T *qfp, list_T *list)
6855{
6856 int bufnum;
6857 dict_T *dict;
6858 char_u buf[2];
6859
6860 // Handle entries with a non-existing buffer number.
6861 bufnum = qfp->qf_fnum;
6862 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
6863 bufnum = 0;
6864
6865 if ((dict = dict_alloc()) == NULL)
6866 return FAIL;
6867 if (list_append_dict(list, dict) == FAIL)
6868 return FAIL;
6869
6870 buf[0] = qfp->qf_type;
6871 buf[1] = NUL;
6872 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02006873 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
6874 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
6875 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
6876 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
6877 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
6878 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01006879 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
6880 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
6881 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
6882 || dict_add_string(dict, "type", buf) == FAIL
Tom Praschanca6ac992023-08-11 23:26:12 +02006883 || (qfp->qf_user_data.v_type != VAR_UNKNOWN
6884 && dict_add_tv(dict, "user_data", &qfp->qf_user_data) == FAIL )
Bram Moolenaara16123a2019-03-28 20:31:07 +01006885 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
6886 return FAIL;
6887
6888 return OK;
6889}
6890
6891/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006892 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006893 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02006894 * If eidx is not 0, then return only the specified entry. Otherwise return
6895 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00006896 */
Bram Moolenaare677df82019-09-02 22:31:11 +02006897 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02006898get_errorlist(
6899 qf_info_T *qi_arg,
6900 win_T *wp,
6901 int qf_idx,
6902 int eidx,
6903 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006904{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006905 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01006906 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006907 qfline_T *qfp;
6908 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006909
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006910 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006911 {
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006912 qi = &ql_info;
6913 if (wp != NULL)
6914 {
6915 qi = GET_LOC_LIST(wp);
6916 if (qi == NULL)
6917 return FAIL;
6918 }
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006919 }
6920
Bram Moolenaar858ba062020-05-31 23:11:59 +02006921 if (eidx < 0)
6922 return OK;
6923
Bram Moolenaar29ce4092018-04-28 21:56:44 +02006924 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02006925 qf_idx = qi->qf_curlist;
6926
Bram Moolenaar0398e002019-03-21 21:12:49 +01006927 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006928 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006929
Bram Moolenaar0398e002019-03-21 21:12:49 +01006930 qfl = qf_get_list(qi, qf_idx);
6931 if (qf_list_empty(qfl))
6932 return FAIL;
6933
Bram Moolenaara16123a2019-03-28 20:31:07 +01006934 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006935 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02006936 if (eidx > 0)
6937 {
6938 if (eidx == i)
6939 return get_qfline_items(qfp, list);
6940 }
6941 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006942 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006943 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006944
Bram Moolenaar05159a02005-02-26 23:04:13 +00006945 return OK;
6946}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00006947
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006948// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02006949enum {
6950 QF_GETLIST_NONE = 0x0,
6951 QF_GETLIST_TITLE = 0x1,
6952 QF_GETLIST_ITEMS = 0x2,
6953 QF_GETLIST_NR = 0x4,
6954 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02006955 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02006956 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02006957 QF_GETLIST_IDX = 0x40,
6958 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01006959 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02006960 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01006961 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02006962 QF_GETLIST_QFTF = 0x800,
6963 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02006964};
6965
6966/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02006967 * Parse text from 'di' and return the quickfix list items.
6968 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006969 */
6970 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02006971qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006972{
6973 int status = FAIL;
6974 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02006975 char_u *errorformat = p_efm;
6976 dictitem_T *efm_di;
6977 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006978
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006979 // Only a List value is supported
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006980 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
6981 return FAIL;
6982
6983 // If errorformat is supplied then use it, otherwise use the 'efm'
6984 // option setting
6985 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006986 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006987 if (efm_di->di_tv.v_type != VAR_STRING ||
6988 efm_di->di_tv.vval.v_string == NULL)
Bram Moolenaarda732532017-08-31 20:58:02 +02006989 return FAIL;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006990 errorformat = efm_di->di_tv.vval.v_string;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02006991 }
6992
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006993 l = list_alloc();
6994 if (l == NULL)
6995 return FAIL;
6996
6997 qi = qf_alloc_stack(QFLT_INTERNAL);
6998 if (qi != NULL)
6999 {
7000 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
7001 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
7002 {
7003 (void)get_errorlist(qi, NULL, 0, 0, l);
7004 qf_free(&qi->qf_lists[0]);
7005 }
7006 free(qi);
7007 }
7008 dict_add_list(retdict, "items", l);
7009 status = OK;
7010
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007011 return status;
7012}
7013
7014/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01007015 * Return the quickfix/location list window identifier in the current tabpage.
7016 */
7017 static int
7018qf_winid(qf_info_T *qi)
7019{
7020 win_T *win;
7021
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007022 // The quickfix window can be opened even if the quickfix list is not set
7023 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01007024 if (qi == NULL)
7025 return 0;
7026 win = qf_find_win(qi);
7027 if (win != NULL)
7028 return win->w_id;
7029 return 0;
7030}
7031
7032/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007033 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00007034 * window. If there is no buffer associated with the list or the buffer is
7035 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007036 */
7037 static int
7038qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
7039{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00007040 int bufnum = 0;
7041
7042 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
7043 bufnum = qi->qf_bufnr;
7044
7045 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007046}
7047
7048/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007049 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007050 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007051 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007052qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007053{
Bram Moolenaard823fa92016-08-12 16:29:27 +02007054 int flags = QF_GETLIST_NONE;
7055
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007056 if (dict_has_key(what, "all"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007057 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007058 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007059 if (!loclist)
7060 // File window ID is applicable only to location list windows
7061 flags &= ~ QF_GETLIST_FILEWINID;
7062 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007063
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007064 if (dict_has_key(what, "title"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007065 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007066
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007067 if (dict_has_key(what, "nr"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01007068 flags |= QF_GETLIST_NR;
7069
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007070 if (dict_has_key(what, "winid"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007071 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007072
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007073 if (dict_has_key(what, "context"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007074 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007075
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007076 if (dict_has_key(what, "id"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01007077 flags |= QF_GETLIST_ID;
7078
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007079 if (dict_has_key(what, "items"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007080 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007081
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007082 if (dict_has_key(what, "idx"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007083 flags |= QF_GETLIST_IDX;
7084
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007085 if (dict_has_key(what, "size"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007086 flags |= QF_GETLIST_SIZE;
7087
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007088 if (dict_has_key(what, "changedtick"))
Bram Moolenaarb254af32017-12-18 19:48:58 +01007089 flags |= QF_GETLIST_TICK;
7090
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007091 if (loclist && dict_has_key(what, "filewinid"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007092 flags |= QF_GETLIST_FILEWINID;
7093
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007094 if (dict_has_key(what, "qfbufnr"))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007095 flags |= QF_GETLIST_QFBUFNR;
7096
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007097 if (dict_has_key(what, "quickfixtextfunc"))
Bram Moolenaard43906d2020-07-20 21:31:32 +02007098 flags |= QF_GETLIST_QFTF;
7099
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007100 return flags;
7101}
Bram Moolenaara6d48492017-12-12 22:45:31 +01007102
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007103/*
7104 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
7105 * If 'nr' and 'id' are not present in 'what' then return the current
7106 * quickfix list index.
7107 * If 'nr' is zero then return the current quickfix list index.
7108 * If 'nr' is '$' then return the last quickfix list index.
7109 * If 'id' is present then return the index of the quickfix list with that id.
7110 * If 'id' is zero then return the quickfix list index specified by 'nr'.
7111 * Return -1, if quickfix list is not present or if the stack is empty.
7112 */
7113 static int
7114qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
7115{
7116 int qf_idx;
7117 dictitem_T *di;
7118
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007119 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007120 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7121 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007122 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007123 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01007124 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007125 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007126 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01007127 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007128 qf_idx = di->di_tv.vval.v_number - 1;
7129 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007130 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007131 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01007132 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007133 else if (di->di_tv.v_type == VAR_STRING
7134 && di->di_tv.vval.v_string != NULL
7135 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007136 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007137 qf_idx = qi->qf_listcount - 1;
7138 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007139 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007140 }
7141
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007142 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
7143 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007144 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007145 if (di->di_tv.v_type == VAR_NUMBER)
7146 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007147 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007148 if (di->di_tv.vval.v_number != 0)
7149 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
7150 }
7151 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007152 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007153 }
7154
7155 return qf_idx;
7156}
7157
7158/*
7159 * Return default values for quickfix list properties in retdict.
7160 */
7161 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007162qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007163{
7164 int status = OK;
7165
7166 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007167 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007168 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
7169 {
7170 list_T *l = list_alloc();
7171 if (l != NULL)
7172 status = dict_add_list(retdict, "items", l);
7173 else
7174 status = FAIL;
7175 }
7176 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007177 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007178 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007179 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007180 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007181 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007182 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007183 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007184 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007185 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007186 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007187 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007188 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007189 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007190 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007191 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007192 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7193 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007194 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7195 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007196
7197 return status;
7198}
7199
7200/*
7201 * Return the quickfix list title as 'title' in retdict
7202 */
7203 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007204qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007205{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007206 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007207}
7208
7209/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007210 * Returns the identifier of the window used to display files from a location
7211 * list. If there is no associated window, then returns 0. Useful only when
7212 * called from a location list window.
7213 */
7214 static int
7215qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7216{
7217 int winid = 0;
7218
7219 if (wp != NULL && IS_LL_WINDOW(wp))
7220 {
7221 win_T *ll_wp = qf_find_win_with_loclist(qi);
7222 if (ll_wp != NULL)
7223 winid = ll_wp->w_id;
7224 }
7225
7226 return dict_add_number(retdict, "filewinid", winid);
7227}
7228
7229/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007230 * Return the quickfix list items/entries as 'items' in retdict.
7231 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007232 */
7233 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007234qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007235{
7236 int status = OK;
7237 list_T *l = list_alloc();
7238 if (l != NULL)
7239 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007240 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007241 dict_add_list(retdict, "items", l);
7242 }
7243 else
7244 status = FAIL;
7245
7246 return status;
7247}
7248
7249/*
7250 * Return the quickfix list context (if any) as 'context' in retdict.
7251 */
7252 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007253qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007254{
7255 int status;
7256 dictitem_T *di;
7257
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007258 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007259 {
7260 di = dictitem_alloc((char_u *)"context");
7261 if (di != NULL)
7262 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007263 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007264 status = dict_add(retdict, di);
7265 if (status == FAIL)
7266 dictitem_free(di);
7267 }
7268 else
7269 status = FAIL;
7270 }
7271 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007272 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007273
7274 return status;
7275}
7276
7277/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007278 * Return the current quickfix list index as 'idx' in retdict.
7279 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007280 */
7281 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007282qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007283{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007284 if (eidx == 0)
7285 {
7286 eidx = qfl->qf_index;
7287 if (qf_list_empty(qfl))
7288 // For empty lists, current index is set to 0
7289 eidx = 0;
7290 }
7291 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007292}
7293
7294/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007295 * Return the 'quickfixtextfunc' function of a quickfix/location list
7296 */
7297 static int
7298qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7299{
7300 int status;
7301
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007302 if (qfl->qf_qftf_cb.cb_name != NULL)
Bram Moolenaard43906d2020-07-20 21:31:32 +02007303 {
7304 typval_T tv;
7305
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007306 put_callback(&qfl->qf_qftf_cb, &tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007307 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7308 clear_tv(&tv);
7309 }
7310 else
7311 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7312
7313 return status;
7314}
7315
7316/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007317 * Return quickfix/location list details (title) as a
7318 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7319 * then current list is used. Otherwise the specified list is used.
7320 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007321 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007322qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7323{
7324 qf_info_T *qi = &ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007325 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007326 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007327 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007328 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007329 dictitem_T *di;
7330 int flags = QF_GETLIST_NONE;
7331
7332 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7333 return qf_get_list_from_lines(what, di, retdict);
7334
7335 if (wp != NULL)
7336 qi = GET_LOC_LIST(wp);
7337
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007338 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007339
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007340 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007341 qf_idx = qf_getprop_qfidx(qi, what);
7342
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007343 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007344 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007345 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007346
Bram Moolenaar0398e002019-03-21 21:12:49 +01007347 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007348
Bram Moolenaar858ba062020-05-31 23:11:59 +02007349 // If an entry index is specified, use that
7350 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7351 {
7352 if (di->di_tv.v_type != VAR_NUMBER)
7353 return FAIL;
7354 eidx = di->di_tv.vval.v_number;
7355 }
7356
Bram Moolenaard823fa92016-08-12 16:29:27 +02007357 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007358 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007359 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007360 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007361 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007362 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007363 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007364 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007365 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007366 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007367 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007368 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007369 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007370 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007371 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007372 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007373 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007374 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007375 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7376 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007377 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7378 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007379 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7380 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007381
Bram Moolenaard823fa92016-08-12 16:29:27 +02007382 return status;
7383}
7384
7385/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007386 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007387 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7388 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007389 */
7390 static int
7391qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007392 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007393 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007394 int first_entry,
7395 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007396{
7397 static int did_bufnr_emsg;
7398 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007399 int bufnum, valid, status, col, end_col, vcol, nr;
7400 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007401
7402 if (first_entry)
7403 did_bufnr_emsg = FALSE;
7404
Bram Moolenaard61efa52022-07-23 09:52:04 +01007405 filename = dict_get_string(d, "filename", TRUE);
7406 module = dict_get_string(d, "module", TRUE);
7407 bufnum = (int)dict_get_number(d, "bufnr");
7408 lnum = (int)dict_get_number(d, "lnum");
7409 end_lnum = (int)dict_get_number(d, "end_lnum");
7410 col = (int)dict_get_number(d, "col");
7411 end_col = (int)dict_get_number(d, "end_col");
7412 vcol = (int)dict_get_number(d, "vcol");
7413 nr = (int)dict_get_number(d, "nr");
7414 type = dict_get_string(d, "type", TRUE);
7415 pattern = dict_get_string(d, "pattern", TRUE);
7416 text = dict_get_string(d, "text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007417 if (text == NULL)
7418 text = vim_strsave((char_u *)"");
Tom Praschanca6ac992023-08-11 23:26:12 +02007419 typval_T user_data;
7420 user_data.v_type = VAR_UNKNOWN;
7421 dict_get_tv(d, "user_data", &user_data);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007422
7423 valid = TRUE;
7424 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7425 valid = FALSE;
7426
7427 // Mark entries with non-existing buffer number as not valid. Give the
7428 // error message only once.
7429 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7430 {
7431 if (!did_bufnr_emsg)
7432 {
7433 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007434 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007435 }
7436 valid = FALSE;
7437 bufnum = 0;
7438 }
7439
7440 // If the 'valid' field is present it overrules the detected value.
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007441 if (dict_has_key(d, "valid"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01007442 valid = (int)dict_get_bool(d, "valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007443
Bram Moolenaar0398e002019-03-21 21:12:49 +01007444 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007445 NULL, // dir
7446 filename,
7447 module,
7448 bufnum,
7449 text,
7450 lnum,
thinca6864efa2021-06-19 20:45:20 +02007451 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007452 col,
thinca6864efa2021-06-19 20:45:20 +02007453 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007454 vcol, // vis_col
7455 pattern, // search pattern
7456 nr,
7457 type == NULL ? NUL : *type,
Tom Praschanca6ac992023-08-11 23:26:12 +02007458 &user_data,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007459 valid);
7460
7461 vim_free(filename);
7462 vim_free(module);
7463 vim_free(pattern);
7464 vim_free(text);
7465 vim_free(type);
Tom Praschanca6ac992023-08-11 23:26:12 +02007466 clear_tv(&user_data);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007467
Bram Moolenaar9752c722018-12-22 16:49:34 +01007468 if (valid)
7469 *valid_entry = TRUE;
7470
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007471 return status;
7472}
7473
7474/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007475 * Add list of entries to quickfix/location list. Each list entry is
7476 * a dictionary with item information.
7477 */
7478 static int
7479qf_add_entries(
7480 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007481 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007482 list_T *list,
7483 char_u *title,
7484 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007485{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007486 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007487 listitem_T *li;
7488 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007489 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007490 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007491 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007492
Bram Moolenaara3921f42017-06-04 15:30:34 +02007493 if (action == ' ' || qf_idx == qi->qf_listcount)
7494 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007495 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007496 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007497 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007498 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007499 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01007500 else if (action == 'a' && !qf_list_empty(qfl))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007501 // Adding to existing list, use last entry.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007502 old_last = qfl->qf_last;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007503 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007504 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007505 qf_free_items(qfl);
7506 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007507 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007508
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007509 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007510 {
7511 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007512 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007513
7514 d = li->li_tv.vval.v_dict;
7515 if (d == NULL)
7516 continue;
7517
Bram Moolenaar0398e002019-03-21 21:12:49 +01007518 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007519 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007520 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007521 break;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007522 }
7523
Bram Moolenaar9752c722018-12-22 16:49:34 +01007524 // Check if any valid error entries are added to the list.
7525 if (valid_entry)
7526 qfl->qf_nonevalid = FALSE;
7527 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007528 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007529 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007530
7531 // If not appending to the list, set the current error to the first entry
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007532 if (action != 'a')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007533 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007534
7535 // Update the current error index if not appending to the list or if the
7536 // list was empty before and it is not empty now.
Bram Moolenaar0398e002019-03-21 21:12:49 +01007537 if ((action != 'a' || qfl->qf_index == 0) && !qf_list_empty(qfl))
Bram Moolenaar9752c722018-12-22 16:49:34 +01007538 qfl->qf_index = 1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007539
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007540 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007541 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007542
7543 return retval;
7544}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007545
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007546/*
7547 * Get the quickfix list index from 'nr' or 'id'
7548 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007549 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007550qf_setprop_get_qfidx(
7551 qf_info_T *qi,
7552 dict_T *what,
7553 int action,
7554 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007555{
7556 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007557 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007558
Bram Moolenaard823fa92016-08-12 16:29:27 +02007559 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7560 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007561 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007562 if (di->di_tv.v_type == VAR_NUMBER)
7563 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007564 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007565 if (di->di_tv.vval.v_number != 0)
7566 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007567
Bram Moolenaar55b69262017-08-13 13:42:01 +02007568 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7569 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007570 // When creating a new list, accept qf_idx pointing to the next
7571 // non-available list and add the new list at the end of the
7572 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007573 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007574 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007575 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007576 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007577 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007578 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007579 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007580 }
7581 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007582 && di->di_tv.vval.v_string != NULL
7583 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007584 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007585 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007586 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007587 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007588 qf_idx = 0;
7589 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007590 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007591 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007592 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007593 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007594 }
7595
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007596 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007597 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007598 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007599 if (di->di_tv.v_type != VAR_NUMBER)
7600 return INVALID_QFIDX;
7601
7602 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007603 }
7604
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007605 return qf_idx;
7606}
7607
7608/*
7609 * Set the quickfix list title.
7610 */
7611 static int
7612qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7613{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007614 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007615
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007616 if (di->di_tv.v_type != VAR_STRING)
7617 return FAIL;
7618
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007619 vim_free(qfl->qf_title);
Bram Moolenaard61efa52022-07-23 09:52:04 +01007620 qfl->qf_title = dict_get_string(what, "title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007621 if (qf_idx == qi->qf_curlist)
7622 qf_update_win_titlevar(qi);
7623
7624 return OK;
7625}
7626
7627/*
7628 * Set quickfix list items/entries.
7629 */
7630 static int
7631qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
7632{
7633 int retval = FAIL;
7634 char_u *title_save;
7635
7636 if (di->di_tv.v_type != VAR_LIST)
7637 return FAIL;
7638
7639 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
7640 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
7641 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007642 vim_free(title_save);
7643
7644 return retval;
7645}
7646
7647/*
7648 * Set quickfix list items/entries from a list of lines.
7649 */
7650 static int
7651qf_setprop_items_from_lines(
7652 qf_info_T *qi,
7653 int qf_idx,
7654 dict_T *what,
7655 dictitem_T *di,
7656 int action)
7657{
7658 char_u *errorformat = p_efm;
7659 dictitem_T *efm_di;
7660 int retval = FAIL;
7661
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007662 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007663 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
7664 {
7665 if (efm_di->di_tv.v_type != VAR_STRING ||
7666 efm_di->di_tv.vval.v_string == NULL)
7667 return FAIL;
7668 errorformat = efm_di->di_tv.vval.v_string;
7669 }
7670
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007671 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007672 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7673 return FAIL;
7674
7675 if (action == 'r')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007676 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007677 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01007678 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007679 retval = OK;
7680
7681 return retval;
7682}
7683
7684/*
7685 * Set quickfix list context.
7686 */
7687 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007688qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007689{
7690 typval_T *ctx;
7691
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007692 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007693 ctx = alloc_tv();
7694 if (ctx != NULL)
7695 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007696 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007697
7698 return OK;
7699}
7700
7701/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007702 * Set the current index in the specified quickfix list
7703 */
7704 static int
7705qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
7706{
7707 int denote = FALSE;
7708 int newidx;
7709 int old_qfidx;
7710 qfline_T *qf_ptr;
7711
7712 // If the specified index is '$', then use the last entry
7713 if (di->di_tv.v_type == VAR_STRING
7714 && di->di_tv.vval.v_string != NULL
7715 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
7716 newidx = qfl->qf_count;
7717 else
7718 {
7719 // Otherwise use the specified index
7720 newidx = tv_get_number_chk(&di->di_tv, &denote);
7721 if (denote)
7722 return FAIL;
7723 }
7724
7725 if (newidx < 1) // sanity check
7726 return FAIL;
7727 if (newidx > qfl->qf_count)
7728 newidx = qfl->qf_count;
7729
7730 old_qfidx = qfl->qf_index;
7731 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
7732 if (qf_ptr == NULL)
7733 return FAIL;
7734 qfl->qf_ptr = qf_ptr;
7735 qfl->qf_index = newidx;
7736
7737 // If the current list is modified and it is displayed in the quickfix
7738 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007739 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007740 qf_win_pos_update(qi, old_qfidx);
7741
7742 return OK;
7743}
7744
7745/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007746 * Set the current index in the specified quickfix list
7747 */
7748 static int
7749qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
7750{
Bram Moolenaard43906d2020-07-20 21:31:32 +02007751 callback_T cb;
7752
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007753 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007754 cb = get_callback(&di->di_tv);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007755 if (cb.cb_name == NULL || *cb.cb_name == NUL)
7756 return OK;
7757
7758 set_callback(&qfl->qf_qftf_cb, &cb);
7759 if (cb.cb_free_name)
7760 vim_free(cb.cb_name);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007761
7762 return OK;
7763}
7764
7765/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007766 * Set quickfix/location list properties (title, items, context).
7767 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02007768 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007769 */
7770 static int
7771qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
7772{
7773 dictitem_T *di;
7774 int retval = FAIL;
7775 int qf_idx;
7776 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007777 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007778
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007779 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007780 newlist = TRUE;
7781
7782 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007783 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007784 return FAIL;
7785
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007786 if (newlist)
7787 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02007788 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02007789 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007790 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007791 }
7792
Bram Moolenaar0398e002019-03-21 21:12:49 +01007793 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007794 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007795 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007796 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007797 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02007798 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007799 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007800 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007801 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01007802 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7803 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02007804 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
7805 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007806
Bram Moolenaar287153c2020-11-29 14:20:27 +01007807 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007808 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01007809 if (newlist)
7810 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007811
Bram Moolenaard823fa92016-08-12 16:29:27 +02007812 return retval;
7813}
7814
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007815/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007816 * Free the entire quickfix/location list stack.
7817 * If the quickfix/location list window is open, then clear it.
7818 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007819 static void
7820qf_free_stack(win_T *wp, qf_info_T *qi)
7821{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007822 win_T *qfwin = qf_find_win(qi);
7823 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007824
7825 if (qfwin != NULL)
7826 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007827 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007828 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007829 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007830 qf_update_buffer(qi, NULL);
7831 }
7832
7833 if (wp != NULL && IS_LL_WINDOW(wp))
7834 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007835 // If in the location list window, then use the non-location list
7836 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007837 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007838 if (llwin != NULL)
7839 wp = llwin;
7840 }
7841
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007842 qf_free_all(wp);
7843 if (wp == NULL)
7844 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007845 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007846 qi->qf_curlist = 0;
7847 qi->qf_listcount = 0;
7848 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01007849 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007850 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007851 // If the location list window is open, then create a new empty
7852 // location list
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007853 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02007854
Bram Moolenaar95946f12019-03-31 15:31:59 +02007855 if (new_ll != NULL)
7856 {
7857 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02007858
Bram Moolenaar95946f12019-03-31 15:31:59 +02007859 // first free the list reference in the location list window
7860 ll_free_all(&qfwin->w_llist_ref);
7861
7862 qfwin->w_llist_ref = new_ll;
7863 if (wp != qfwin)
7864 win_set_loclist(wp, new_ll);
7865 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02007866 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007867}
7868
Bram Moolenaard823fa92016-08-12 16:29:27 +02007869/*
7870 * Populate the quickfix list with the items supplied in the list
7871 * of dictionaries. "title" will be copied to w:quickfix_title.
7872 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list.
Bram Moolenaar90049492020-07-01 13:04:05 +02007873 * When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007874 */
7875 int
7876set_errorlist(
7877 win_T *wp,
7878 list_T *list,
7879 int action,
7880 char_u *title,
7881 dict_T *what)
7882{
7883 qf_info_T *qi = &ql_info;
7884 int retval = OK;
7885
7886 if (wp != NULL)
7887 {
7888 qi = ll_get_or_alloc_list(wp);
7889 if (qi == NULL)
7890 return FAIL;
7891 }
7892
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007893 if (action == 'f')
7894 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007895 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007896 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007897 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02007898 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007899
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007900 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02007901 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007902 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007903 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02007904 _("cannot have both a list and a \"what\" argument"));
7905 return FAIL;
7906 }
7907
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007908 incr_quickfix_busy();
7909
7910 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02007911 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007912 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01007913 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02007914 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007915 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01007916 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01007917 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007918
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02007919 decr_quickfix_busy();
7920
Bram Moolenaard823fa92016-08-12 16:29:27 +02007921 return retval;
7922}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007923
Tom Praschanca6ac992023-08-11 23:26:12 +02007924static int mark_quickfix_user_data(qf_info_T *qi, int copyID)
7925{
7926 int abort = FALSE;
7927 for (int i = 0; i < LISTCOUNT && !abort; ++i)
7928 {
7929 qf_list_T *qfl = &qi->qf_lists[i];
7930 if (!qfl->qf_has_user_data)
7931 continue;
7932 qfline_T *qfp;
7933 int j;
7934 FOR_ALL_QFL_ITEMS(qfl, qfp, j)
7935 {
7936 typval_T* user_data = &qfp->qf_user_data;
7937 if (user_data != NULL && user_data->v_type != VAR_NUMBER
7938 && user_data->v_type != VAR_STRING && user_data->v_type != VAR_FLOAT)
7939 abort = abort || set_ref_in_item(user_data, copyID, NULL, NULL);
7940 }
7941 }
7942 return abort;
7943}
7944
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007945/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007946 * Mark the quickfix context and callback function as in use for all the lists
7947 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02007948 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007949 static int
7950mark_quickfix_ctx(qf_info_T *qi, int copyID)
7951{
7952 int i;
7953 int abort = FALSE;
7954 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007955 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007956
7957 for (i = 0; i < LISTCOUNT && !abort; ++i)
7958 {
7959 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007960 if (ctx != NULL && ctx->v_type != VAR_NUMBER
7961 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007962 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
7963
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007964 cb = &qi->qf_lists[i].qf_qftf_cb;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007965 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007966 }
7967
7968 return abort;
7969}
7970
7971/*
7972 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007973 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007974 */
7975 int
7976set_ref_in_quickfix(int copyID)
7977{
7978 int abort = FALSE;
7979 tabpage_T *tp;
7980 win_T *win;
7981
7982 abort = mark_quickfix_ctx(&ql_info, copyID);
7983 if (abort)
7984 return abort;
7985
Tom Praschanca6ac992023-08-11 23:26:12 +02007986 abort = mark_quickfix_user_data(&ql_info, copyID);
7987 if (abort)
7988 return abort;
7989
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00007990 abort = set_ref_in_callback(&qftf_cb, copyID);
7991 if (abort)
7992 return abort;
7993
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007994 FOR_ALL_TAB_WINDOWS(tp, win)
7995 {
7996 if (win->w_llist != NULL)
7997 {
7998 abort = mark_quickfix_ctx(win->w_llist, copyID);
7999 if (abort)
8000 return abort;
Tom Praschanca6ac992023-08-11 23:26:12 +02008001
8002 abort = mark_quickfix_user_data(win->w_llist, copyID);
8003 if (abort)
8004 return abort;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008005 }
Bram Moolenaar12237442017-12-19 12:38:52 +01008006 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
8007 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008008 // In a location list window and none of the other windows is
8009 // referring to this location list. Mark the location list
8010 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01008011 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
8012 if (abort)
8013 return abort;
zeertzjqbe4bd182024-09-14 10:32:31 +02008014
8015 abort = mark_quickfix_user_data(win->w_llist_ref, copyID);
8016 if (abort)
8017 return abort;
Bram Moolenaar12237442017-12-19 12:38:52 +01008018 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008019 }
8020
8021 return abort;
8022}
Bram Moolenaar05159a02005-02-26 23:04:13 +00008023#endif
8024
Bram Moolenaar81695252004-12-29 20:58:21 +00008025/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01008026 * Return the autocmd name for the :cbuffer Ex commands
8027 */
8028 static char_u *
8029cbuffer_get_auname(cmdidx_T cmdidx)
8030{
8031 switch (cmdidx)
8032 {
8033 case CMD_cbuffer: return (char_u *)"cbuffer";
8034 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
8035 case CMD_caddbuffer: return (char_u *)"caddbuffer";
8036 case CMD_lbuffer: return (char_u *)"lbuffer";
8037 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
8038 case CMD_laddbuffer: return (char_u *)"laddbuffer";
8039 default: return NULL;
8040 }
8041}
8042
8043/*
8044 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
8045 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
8046 */
8047 static int
8048cbuffer_process_args(
8049 exarg_T *eap,
8050 buf_T **bufp,
8051 linenr_T *line1,
8052 linenr_T *line2)
8053{
8054 buf_T *buf = NULL;
8055
8056 if (*eap->arg == NUL)
8057 buf = curbuf;
8058 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
8059 buf = buflist_findnr(atoi((char *)eap->arg));
8060
8061 if (buf == NULL)
8062 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008063 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008064 return FAIL;
8065 }
8066
8067 if (buf->b_ml.ml_mfp == NULL)
8068 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008069 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008070 return FAIL;
8071 }
8072
8073 if (eap->addr_count == 0)
8074 {
8075 eap->line1 = 1;
8076 eap->line2 = buf->b_ml.ml_line_count;
8077 }
8078
8079 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
8080 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
8081 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008082 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008083 return FAIL;
8084 }
8085
8086 *line1 = eap->line1;
8087 *line2 = eap->line2;
8088 *bufp = buf;
8089
8090 return OK;
8091}
8092
8093/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00008094 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00008095 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00008096 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008097 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00008098 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00008099 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008100 */
8101 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008102ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008103{
8104 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008105 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008106 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01008107 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02008108 int_u save_qfid;
8109 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01008110 char_u *qf_title;
8111 linenr_T line1;
8112 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008113
Bram Moolenaara16123a2019-03-28 20:31:07 +01008114 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01008115 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01008116 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008117 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008118#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008119 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008120 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008121#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008122 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008123
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008124 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008125 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8126 if (qi == NULL)
8127 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01008128
Bram Moolenaara16123a2019-03-28 20:31:07 +01008129 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
8130 return;
8131
8132 qf_title = qf_cmdtitle(*eap->cmdlinep);
8133
8134 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008135 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01008136 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
8137 (char *)qf_title, (char *)buf->b_sfname);
8138 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008139 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01008140
8141 incr_quickfix_busy();
8142
8143 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
8144 (eap->cmdidx != CMD_caddbuffer
8145 && eap->cmdidx != CMD_laddbuffer),
8146 line1, line2,
8147 qf_title, NULL);
8148 if (qf_stack_empty(qi))
8149 {
8150 decr_quickfix_busy();
8151 return;
8152 }
8153 if (res >= 0)
8154 qf_list_changed(qf_get_curlist(qi));
8155
8156 // Remember the current quickfix list identifier, so that we can
8157 // check for autocommands changing the current quickfix list.
8158 save_qfid = qf_get_curlist(qi)->qf_id;
8159 if (au_name != NULL)
8160 {
8161 buf_T *curbuf_old = curbuf;
8162
8163 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
8164 TRUE, curbuf);
8165 if (curbuf != curbuf_old)
8166 // Autocommands changed buffer, don't jump now, "qi" may
8167 // be invalid.
8168 res = 0;
8169 }
8170 // Jump to the first error for a new list and if autocmds didn't
8171 // free the list.
8172 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
8173 eap->cmdidx == CMD_lbuffer)
8174 && qflist_valid(wp, save_qfid))
8175 // display the first error
8176 qf_jump_first(qi, save_qfid, eap->forceit);
8177
8178 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00008179}
8180
Bram Moolenaar1e015462005-09-25 22:16:38 +00008181#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008182/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01008183 * Return the autocmd name for the :cexpr Ex commands.
8184 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008185 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01008186cexpr_get_auname(cmdidx_T cmdidx)
8187{
8188 switch (cmdidx)
8189 {
8190 case CMD_cexpr: return (char_u *)"cexpr";
8191 case CMD_cgetexpr: return (char_u *)"cgetexpr";
8192 case CMD_caddexpr: return (char_u *)"caddexpr";
8193 case CMD_lexpr: return (char_u *)"lexpr";
8194 case CMD_lgetexpr: return (char_u *)"lgetexpr";
8195 case CMD_laddexpr: return (char_u *)"laddexpr";
8196 default: return NULL;
8197 }
8198}
8199
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008200 int
8201trigger_cexpr_autocmd(int cmdidx)
8202{
8203 char_u *au_name = cexpr_get_auname(cmdidx);
8204
8205 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8206 curbuf->b_fname, TRUE, curbuf))
8207 {
8208 if (aborting())
8209 return FAIL;
8210 }
8211 return OK;
8212}
8213
8214 int
8215cexpr_core(exarg_T *eap, typval_T *tv)
8216{
8217 qf_info_T *qi;
8218 win_T *wp = NULL;
8219
8220 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8221 if (qi == NULL)
8222 return FAIL;
8223
8224 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
8225 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
8226 {
8227 int res;
8228 int_u save_qfid;
8229 char_u *au_name = cexpr_get_auname(eap->cmdidx);
8230
8231 incr_quickfix_busy();
8232 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
8233 (eap->cmdidx != CMD_caddexpr
8234 && eap->cmdidx != CMD_laddexpr),
8235 (linenr_T)0, (linenr_T)0,
8236 qf_cmdtitle(*eap->cmdlinep), NULL);
8237 if (qf_stack_empty(qi))
8238 {
8239 decr_quickfix_busy();
8240 return FAIL;
8241 }
8242 if (res >= 0)
8243 qf_list_changed(qf_get_curlist(qi));
8244
8245 // Remember the current quickfix list identifier, so that we can
8246 // check for autocommands changing the current quickfix list.
8247 save_qfid = qf_get_curlist(qi)->qf_id;
8248 if (au_name != NULL)
8249 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8250 curbuf->b_fname, TRUE, curbuf);
8251
8252 // Jump to the first error for a new list and if autocmds didn't
8253 // free the list.
8254 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8255 && qflist_valid(wp, save_qfid))
8256 // display the first error
8257 qf_jump_first(qi, save_qfid, eap->forceit);
8258 decr_quickfix_busy();
8259 return OK;
8260 }
8261
Bram Moolenaar677658a2022-01-05 16:09:06 +00008262 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008263 return FAIL;
8264}
8265
Bram Moolenaara16123a2019-03-28 20:31:07 +01008266/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008267 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8268 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008269 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008270 */
8271 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008272ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008273{
8274 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008275
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008276 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008277 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008278
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008279 // Evaluate the expression. When the result is a string or a list we can
8280 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008281 tv = eval_expr(eap->arg, eap);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00008282 if (tv == NULL)
8283 return;
8284
8285 (void)cexpr_core(eap, tv);
8286 free_tv(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008287}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008288#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008289
8290/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008291 * Get the location list for ":lhelpgrep"
8292 */
8293 static qf_info_T *
8294hgr_get_ll(int *new_ll)
8295{
8296 win_T *wp;
8297 qf_info_T *qi;
8298
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008299 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008300 if (bt_help(curwin->w_buffer))
8301 wp = curwin;
8302 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008303 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008304 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008305
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008306 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008307 qi = NULL;
8308 else
8309 qi = wp->w_llist;
8310
8311 if (qi == NULL)
8312 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008313 // Allocate a new location list for help text matches
Bram Moolenaar2d67d302018-11-16 18:46:02 +01008314 if ((qi = qf_alloc_stack(QFLT_LOCATION)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008315 return NULL;
8316 *new_ll = TRUE;
8317 }
8318
8319 return qi;
8320}
8321
8322/*
8323 * Search for a pattern in a help file.
8324 */
8325 static void
8326hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008327 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008328 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008329 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008330 regmatch_T *p_regmatch)
8331{
8332 FILE *fd;
8333 long lnum;
8334
8335 fd = mch_fopen((char *)fname, "r");
8336 if (fd == NULL)
8337 return;
8338
8339 lnum = 1;
8340 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8341 {
8342 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008343
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008344 // Convert a line if 'encoding' is not utf-8 and
8345 // the line contains a non-ASCII character.
Yegappan Lakshmanandf1bbea2022-03-05 14:35:12 +00008346 if (p_vc->vc_type != CONV_NONE && has_non_ascii(IObuff))
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008347 {
8348 line = string_convert(p_vc, IObuff, NULL);
8349 if (line == NULL)
8350 line = IObuff;
8351 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008352
8353 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8354 {
8355 int l = (int)STRLEN(line);
8356
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008357 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008358 while (l > 0 && line[l - 1] <= ' ')
8359 line[--l] = NUL;
8360
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008361 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008362 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008363 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008364 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008365 0,
8366 line,
8367 lnum,
thinca6864efa2021-06-19 20:45:20 +02008368 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008369 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008370 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008371 (int)(p_regmatch->endp[0] - line)
8372 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008373 FALSE, // vis_col
8374 NULL, // search pattern
8375 0, // nr
8376 1, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02008377 NULL, // user_data
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008378 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008379 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008380 {
8381 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008382 if (line != IObuff)
8383 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008384 break;
8385 }
8386 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008387 if (line != IObuff)
8388 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008389 ++lnum;
8390 line_breakcheck();
8391 }
8392 fclose(fd);
8393}
8394
8395/*
8396 * Search for a pattern in all the help files in the doc directory under
8397 * the given directory.
8398 */
8399 static void
8400hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008401 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008402 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008403 regmatch_T *p_regmatch,
8404 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008405#ifdef FEAT_MULTI_LANG
8406 , char_u *lang
8407#endif
8408 )
8409{
8410 int fcount;
8411 char_u **fnames;
8412 int fi;
8413
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008414 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008415 add_pathsep(dirname);
8416 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8417 if (gen_expand_wildcards(1, &dirname, &fcount,
8418 &fnames, EW_FILE|EW_SILENT) == OK
8419 && fcount > 0)
8420 {
8421 for (fi = 0; fi < fcount && !got_int; ++fi)
8422 {
8423#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008424 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008425 if (lang != NULL
8426 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008427 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008428 && !(STRNICMP(lang, "en", 2) == 0
8429 && STRNICMP("txt", fnames[fi]
8430 + STRLEN(fnames[fi]) - 3, 3) == 0))
8431 continue;
8432#endif
8433
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008434 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008435 }
8436 FreeWild(fcount, fnames);
8437 }
8438}
8439
8440/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008441 * Search for a pattern in all the help files in the 'runtimepath'
8442 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008443 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008444 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008445 */
8446 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008447hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008448{
8449 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008450
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008451 vimconv_T vc;
8452
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008453 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8454 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008455 vc.vc_type = CONV_NONE;
8456 if (!enc_utf8)
8457 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008458
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008459 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008460 p = p_rtp;
8461 while (*p != NUL && !got_int)
8462 {
8463 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8464
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008465 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008466#ifdef FEAT_MULTI_LANG
8467 , lang
8468#endif
8469 );
8470 }
8471
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008472 if (vc.vc_type != CONV_NONE)
8473 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008474}
8475
8476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 * ":helpgrep {pattern}"
8478 */
8479 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008480ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481{
8482 regmatch_T regmatch;
8483 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008484 int save_cpo_allocated;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008485 qf_info_T *qi = &ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008486 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008487 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008488 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008489 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490
Bram Moolenaar73633f82012-01-20 13:39:07 +01008491 switch (eap->cmdidx)
8492 {
8493 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8494 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8495 default: break;
8496 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008497 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8498 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008499 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008500#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008501 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008502 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008503#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008504 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008505
Bram Moolenaar39665952018-08-15 20:59:48 +02008506 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008507 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008508 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008509 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008510 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008511 }
8512
Bram Moolenaara16123a2019-03-28 20:31:07 +01008513 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8514 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008515 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008516 p_cpo = empty_option;
8517
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008518 incr_quickfix_busy();
8519
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008520#ifdef FEAT_MULTI_LANG
8521 // Check for a specified language
8522 lang = check_help_lang(eap->arg);
8523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8525 regmatch.rm_ic = FALSE;
8526 if (regmatch.regprog != NULL)
8527 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008528 qf_list_T *qfl;
8529
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008530 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008531 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008532 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008534 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008535
Bram Moolenaar473de612013-06-08 18:19:48 +02008536 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008538 qfl->qf_nonevalid = FALSE;
8539 qfl->qf_ptr = qfl->qf_start;
8540 qfl->qf_index = 1;
8541 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008542 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 }
8544
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008545 if (p_cpo == empty_option)
8546 p_cpo = save_cpo;
8547 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008548 {
8549 // Darn, some plugin changed the value. If it's still empty it was
8550 // changed and restored, need to restore in the complicated way.
8551 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008552 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008553 if (save_cpo_allocated)
8554 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008555 }
8556
8557 if (updated)
8558 // This may open a window and source scripts, do this after 'cpo' was
8559 // restored.
8560 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561
Bram Moolenaar73633f82012-01-20 13:39:07 +01008562 if (au_name != NULL)
8563 {
8564 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8565 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008566 // When adding a location list to an existing location list stack,
8567 // if the autocmd made the stack invalid, then just return.
8568 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008569 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008570 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008571 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008572 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008573 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008574
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008575 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008576 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008577 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008578 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008579 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008580
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008581 decr_quickfix_busy();
8582
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008583 if (eap->cmdidx == CMD_lhelpgrep)
8584 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008585 // If the help window is not opened or if it already points to the
8586 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008587 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008588 {
8589 if (new_qi)
8590 ll_free_all(&qi);
8591 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008592 else if (curwin->w_llist == NULL && new_qi)
8593 // current window didn't have a location list associated with it
8594 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008595 curwin->w_llist = qi;
8596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597}
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008598
8599# if defined(EXITFREE) || defined(PROTO)
8600 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00008601free_quickfix(void)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008602{
8603 win_T *win;
8604 tabpage_T *tab;
8605
8606 qf_free_all(NULL);
8607 // Free all location lists
8608 FOR_ALL_TAB_WINDOWS(tab, win)
8609 qf_free_all(win);
8610
8611 ga_clear(&qfga);
8612}
8613# endif
8614
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008615#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008616
8617#if defined(FEAT_EVAL) || defined(PROTO)
8618# ifdef FEAT_QUICKFIX
8619 static void
8620get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
8621{
8622 if (what_arg->v_type == VAR_UNKNOWN)
8623 {
8624 if (rettv_list_alloc(rettv) == OK)
8625 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02008626 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02008627 }
8628 else
8629 {
8630 if (rettv_dict_alloc(rettv) == OK)
8631 if (is_qf || (wp != NULL))
8632 {
8633 if (what_arg->v_type == VAR_DICT)
8634 {
8635 dict_T *d = what_arg->vval.v_dict;
8636
8637 if (d != NULL)
8638 qf_get_properties(wp, d, rettv->vval.v_dict);
8639 }
8640 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008641 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008642 }
8643 }
8644}
8645# endif
8646
8647/*
8648 * "getloclist()" function
8649 */
8650 void
8651f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8652{
8653# ifdef FEAT_QUICKFIX
8654 win_T *wp;
8655
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008656 if (in_vim9script()
8657 && (check_for_number_arg(argvars, 0) == FAIL
8658 || check_for_opt_dict_arg(argvars, 1) == FAIL))
8659 return;
8660
Bram Moolenaare677df82019-09-02 22:31:11 +02008661 wp = find_win_by_nr_or_id(&argvars[0]);
8662 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
8663# endif
8664}
8665
8666/*
8667 * "getqflist()" function
8668 */
8669 void
8670f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8671{
8672# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02008673 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
8674 return;
8675
Bram Moolenaare677df82019-09-02 22:31:11 +02008676 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
8677# endif
8678}
8679
8680/*
8681 * Used by "setqflist()" and "setloclist()" functions
8682 */
8683 static void
8684set_qf_ll_list(
8685 win_T *wp UNUSED,
8686 typval_T *list_arg UNUSED,
8687 typval_T *action_arg UNUSED,
8688 typval_T *what_arg UNUSED,
8689 typval_T *rettv)
8690{
8691# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008692 char_u *act;
8693 int action = 0;
8694 static int recursive = 0;
8695# endif
8696
8697 rettv->vval.v_number = -1;
8698
8699# ifdef FEAT_QUICKFIX
8700 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008701 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008702 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00008703 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02008704 else
8705 {
8706 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02008707 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02008708 int valid_dict = TRUE;
8709
8710 if (action_arg->v_type == VAR_STRING)
8711 {
8712 act = tv_get_string_chk(action_arg);
8713 if (act == NULL)
8714 return; // type error; errmsg already given
8715 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
8716 act[1] == NUL)
8717 action = *act;
8718 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008719 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02008720 }
8721 else if (action_arg->v_type == VAR_UNKNOWN)
8722 action = ' ';
8723 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008724 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008725
8726 if (action_arg->v_type != VAR_UNKNOWN
8727 && what_arg->v_type != VAR_UNKNOWN)
8728 {
Bram Moolenaar90049492020-07-01 13:04:05 +02008729 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
8730 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02008731 else
8732 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008733 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02008734 valid_dict = FALSE;
8735 }
8736 }
8737
8738 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02008739 if (l != NULL && action && valid_dict
8740 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02008741 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02008742 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02008743 rettv->vval.v_number = 0;
8744 --recursive;
8745 }
8746# endif
8747}
8748
8749/*
8750 * "setloclist()" function
8751 */
8752 void
8753f_setloclist(typval_T *argvars, typval_T *rettv)
8754{
8755 win_T *win;
8756
8757 rettv->vval.v_number = -1;
8758
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008759 if (in_vim9script()
8760 && (check_for_number_arg(argvars, 0) == FAIL
8761 || check_for_list_arg(argvars, 1) == FAIL
8762 || check_for_opt_string_arg(argvars, 2) == FAIL
8763 || (argvars[2].v_type != VAR_UNKNOWN
8764 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
8765 return;
8766
Bram Moolenaare677df82019-09-02 22:31:11 +02008767 win = find_win_by_nr_or_id(&argvars[0]);
8768 if (win != NULL)
8769 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
8770}
8771
8772/*
8773 * "setqflist()" function
8774 */
8775 void
8776f_setqflist(typval_T *argvars, typval_T *rettv)
8777{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02008778 if (in_vim9script()
8779 && (check_for_list_arg(argvars, 0) == FAIL
8780 || check_for_opt_string_arg(argvars, 1) == FAIL
8781 || (argvars[1].v_type != VAR_UNKNOWN
8782 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
8783 return;
8784
Bram Moolenaare677df82019-09-02 22:31:11 +02008785 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
8786}
8787#endif