blob: 9722058e38d44d66c9f94870b3c6fde03869b403 [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
Austin Chang29822992024-10-03 10:50:05 +020039 char_u *qf_fname; // different filename if there're hard links
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020040 char_u *qf_pattern; // search pattern for the error
41 char_u *qf_text; // description of the error
thinca6864efa2021-06-19 20:45:20 +020042 char_u qf_viscol; // set to TRUE if qf_col and qf_end_col is
43 // screen column
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020044 char_u qf_cleared; // set to TRUE if line has been deleted
45 char_u qf_type; // type of the error (mostly 'E'); 1 for
46 // :helpgrep
Tom Praschanca6ac992023-08-11 23:26:12 +020047 typval_T qf_user_data; // custom user data associated with this item
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +020048 char_u qf_valid; // valid error message detected
Bram Moolenaar071d4272004-06-13 20:20:40 +000049};
50
51/*
52 * There is a stack of error lists.
53 */
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
64-bitman88d41ab2025-04-06 17:20:39 +0200115 int qf_maxcount; // maximum number of lists
116 qf_list_T *qf_lists;
Bram Moolenaar2d67d302018-11-16 18:46:02 +0100117 qfltype_T qfl_type; // type of list
Bram Moolenaaree8188f2019-02-05 21:23:04 +0100118 int qf_bufnr; // quickfix window buffer number
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000119};
120
64-bitman88d41ab2025-04-06 17:20:39 +0200121static qf_info_T ql_info_actual; // global quickfix list
122static qf_info_T *ql_info; // points to ql_info_actual if memory allocation is sucessful.
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200123static int_u last_qf_id = 0; // Last used quickfix list id
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124
Yegappan Lakshmananb7318002023-10-25 20:50:28 +0200125#define FMT_PATTERNS 14 // maximum number of % recognized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
127/*
128 * Structure used to hold the info of one part of 'errorformat'
129 */
Bram Moolenaar01265852006-03-20 21:50:15 +0000130typedef struct efm_S efm_T;
131struct efm_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200133 regprog_T *prog; // pre-formatted part of 'errorformat'
134 efm_T *next; // pointer to next (NULL if last)
135 char_u addr[FMT_PATTERNS]; // indices of used % patterns
136 char_u prefix; // prefix of this format line:
137 // 'D' enter directory
138 // 'X' leave directory
139 // 'A' start of multi-line message
140 // 'E' error message
141 // 'W' warning message
142 // 'I' informational message
Bram Moolenaare9283662020-06-07 14:10:47 +0200143 // 'N' note message
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200144 // 'C' continuation line
145 // 'Z' end of multi-line message
146 // 'G' general, unspecific message
147 // 'P' push file (partial) message
148 // 'Q' pop/quit file (partial) message
149 // 'O' overread (partial) message
150 char_u flags; // additional flags given in prefix
151 // '-' do not include this line
152 // '+' include whole line in message
153 int conthere; // %> used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154};
155
Bram Moolenaar9f84ded2018-10-20 20:54:02 +0200156// List of location lists to be deleted.
157// Used to delay the deletion of locations lists by autocmds.
158typedef struct qf_delq_S
159{
160 struct qf_delq_S *next;
161 qf_info_T *qi;
162} qf_delq_T;
163static qf_delq_T *qf_delq_head = NULL;
164
165// Counter to prevent autocmds from freeing up location lists when they are
166// still being used.
167static int quickfix_busy = 0;
168
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200169static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100170
Bram Moolenaard43906d2020-07-20 21:31:32 +0200171// callback function for 'quickfixtextfunc'
172static callback_T qftf_cb;
173
64-bitman88d41ab2025-04-06 17:20:39 +0200174static void qf_pop_stack(qf_info_T *qi, int adjust);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100175static void qf_new_list(qf_info_T *qi, char_u *qf_title);
Tom Praschanca6ac992023-08-11 23:26:12 +0200176static 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);
64-bitman88d41ab2025-04-06 17:20:39 +0200177static int qf_resize_stack(qf_info_T *qi, int n);
178static void qf_sync_llw_to_win(win_T *llw);
179static void qf_sync_win_to_llw(win_T *pwp);
180static qf_info_T *qf_alloc_stack(qfltype_T qfltype, int n);
181static qf_list_T *qf_alloc_list_stack(int n);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200182static void qf_free(qf_list_T *qfl);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100183static char_u *qf_types(int, int);
Bram Moolenaar0398e002019-03-21 21:12:49 +0100184static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200185static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100186static char_u *qf_pop_dir(struct dir_stack_T **);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +0200187static char_u *qf_guess_filepath(qf_list_T *qfl, char_u *);
64-bitman88d41ab2025-04-06 17:20:39 +0200188static win_T *qf_find_win_with_loclist(qf_info_T *ll);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200189static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, int newwin);
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +0100190static void qf_fmt_text(garray_T *gap, char_u *text);
191static void qf_range_text(garray_T *gap, qfline_T *qfp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100192static int qf_win_pos_update(qf_info_T *qi, int old_qf_index);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100193static win_T *qf_find_win(qf_info_T *qi);
194static buf_T *qf_find_buf(qf_info_T *qi);
Bram Moolenaar864293a2016-06-02 13:40:04 +0200195static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last);
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +0200196static 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 +0100197static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir);
198static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start);
199static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start);
200static qf_info_T *ll_get_or_alloc_list(win_T *);
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +0200201static int entry_is_closer_to_target(qfline_T *entry, qfline_T *other_entry, int target_fnum, int target_lnum, int target_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200203// Quickfix window check helper macro
kylo252ae6f1d82022-02-16 19:24:07 +0000204#define IS_QF_WINDOW(wp) (bt_quickfix((wp)->w_buffer) && (wp)->w_llist_ref == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200205// Location list window check helper macro
kylo252ae6f1d82022-02-16 19:24:07 +0000206#define IS_LL_WINDOW(wp) (bt_quickfix((wp)->w_buffer) && (wp)->w_llist_ref != NULL)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200207
208// Quickfix and location list stack check helper macros
kylo252ae6f1d82022-02-16 19:24:07 +0000209#define IS_QF_STACK(qi) ((qi)->qfl_type == QFLT_QUICKFIX)
210#define IS_LL_STACK(qi) ((qi)->qfl_type == QFLT_LOCATION)
211#define IS_QF_LIST(qfl) ((qfl)->qfl_type == QFLT_QUICKFIX)
212#define IS_LL_LIST(qfl) ((qfl)->qfl_type == QFLT_LOCATION)
Bram Moolenaar4d77c652018-08-18 19:59:54 +0200213
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000214/*
215 * Return location list for window 'wp'
216 * For location list window, return the referenced location list
217 */
kylo252ae6f1d82022-02-16 19:24:07 +0000218#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? (wp)->w_llist_ref : (wp)->w_llist)
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000219
Bram Moolenaar95946f12019-03-31 15:31:59 +0200220// Macro to loop through all the items in a quickfix list
221// Quickfix item index starts from 1, so i below starts at 1
Bram Moolenaara16123a2019-03-28 20:31:07 +0100222#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
kylo252ae6f1d82022-02-16 19:24:07 +0000223 for ((i) = 1, (qfp) = (qfl)->qf_start; \
224 !got_int && (i) <= (qfl)->qf_count && (qfp) != NULL; \
225 ++(i), (qfp) = (qfp)->qf_next)
Bram Moolenaara16123a2019-03-28 20:31:07 +0100226
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227/*
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200228 * Looking up a buffer can be slow if there are many. Remember the last one
229 * to make this a lot faster if there are multiple matches in the same file.
230 */
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200231static char_u *qf_last_bufname = NULL;
232static bufref_T qf_last_bufref = {NULL, 0, 0};
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200233
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +0100234static garray_T qfga;
235
236/*
237 * Get a growarray to buffer text in. Shared between various commands to avoid
238 * many alloc/free calls.
239 */
240 static garray_T *
241qfga_get(void)
242{
243 static int initialized = FALSE;
244
245 if (!initialized)
246 {
247 initialized = TRUE;
248 ga_init2(&qfga, 1, 256);
249 }
250
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +0100251 // Reset the length to zero. Retain ga_data from previous use to avoid
252 // many alloc/free calls.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +0100253 qfga.ga_len = 0;
254
255 return &qfga;
256}
257
Bram Moolenaar6dd4a532017-05-28 07:56:36 +0200258/*
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +0100259 * The "qfga" grow array buffer is reused across multiple quickfix commands as
260 * a temporary buffer to reduce the number of alloc/free calls. But if the
261 * buffer size is large, then to avoid holding on to that memory, clear the
262 * grow array. Otherwise just reset the grow array length.
263 */
264 static void
265qfga_clear(void)
266{
267 if (qfga.ga_maxlen > 1000)
268 ga_clear(&qfga);
269 else
270 qfga.ga_len = 0;
271}
272
273/*
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +0200274 * Maximum number of bytes allowed per line while reading a errorfile.
275 */
276#define LINE_MAXLEN 4096
277
haya14busae023d492022-02-08 18:09:29 +0000278/*
279 * Patterns used. Keep in sync with qf_parse_fmt[].
280 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200281static struct fmtpattern
282{
283 char_u convchar;
284 char *pattern;
285} fmt_pat[FMT_PATTERNS] =
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200286 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200287 {'f', ".\\+"}, // only used when at end
Yegappan Lakshmananb7318002023-10-25 20:50:28 +0200288 {'b', "\\d\\+"}, // 1
289 {'n', "\\d\\+"}, // 2
290 {'l', "\\d\\+"}, // 3
291 {'e', "\\d\\+"}, // 4
292 {'c', "\\d\\+"}, // 5
293 {'k', "\\d\\+"}, // 6
294 {'t', "."}, // 7
295#define FMT_PATTERN_M 8
296 {'m', ".\\+"}, // 8
297#define FMT_PATTERN_R 9
298 {'r', ".*"}, // 9
299 {'p', "[- .]*"}, // 10
300 {'v', "\\d\\+"}, // 11
301 {'s', ".\\+"}, // 12
302 {'o', ".\\+"} // 13
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200303 };
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200304
305/*
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200306 * Convert an errorformat pattern to a regular expression pattern.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200307 * See fmt_pat definition above for the list of supported patterns. The
308 * pattern specifier is supplied in "efmpat". The converted pattern is stored
309 * in "regpat". Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200310 */
311 static char_u *
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200312efmpat_to_regpat(
313 char_u *efmpat,
314 char_u *regpat,
315 efm_T *efminfo,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200316 int idx,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100317 int round)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200318{
319 char_u *srcptr;
320
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200321 if (efminfo->addr[idx])
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200322 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200323 // Each errorformat pattern can occur only once
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000324 semsg(_(e_too_many_chr_in_format_string), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200325 return NULL;
326 }
haya14busae023d492022-02-08 18:09:29 +0000327 if ((idx && idx < FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200328 && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL)
haya14busae023d492022-02-08 18:09:29 +0000329 || (idx == FMT_PATTERN_R
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200330 && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL))
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200331 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000332 semsg(_(e_unexpected_chr_in_format_str), *efmpat);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200333 return NULL;
334 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200335 efminfo->addr[idx] = (char_u)++round;
336 *regpat++ = '\\';
337 *regpat++ = '(';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200338#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200339 if (*efmpat == 'f')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200340 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200341 // Also match "c:" in the file name, even when
342 // checking for a colon next: "%f:".
343 // "\%(\a:\)\="
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200344 STRCPY(regpat, "\\%(\\a:\\)\\=");
345 regpat += 10;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200346 }
347#endif
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200348 if (*efmpat == 'f' && efmpat[1] != NUL)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200349 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200350 if (efmpat[1] != '\\' && efmpat[1] != '%')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200351 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200352 // A file name may contain spaces, but this isn't
353 // in "\f". For "%f:%l:%m" there may be a ":" in
354 // the file name. Use ".\{-1,}x" instead (x is
355 // the next character), the requirement that :999:
356 // follows should work.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200357 STRCPY(regpat, ".\\{-1,}");
358 regpat += 7;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200359 }
360 else
361 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200362 // File name followed by '\\' or '%': include as
363 // many file name chars as possible.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200364 STRCPY(regpat, "\\f\\+");
365 regpat += 4;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200366 }
367 }
368 else
369 {
370 srcptr = (char_u *)fmt_pat[idx].pattern;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200371 while ((*regpat = *srcptr++) != NUL)
372 ++regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200373 }
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200374 *regpat++ = '\\';
375 *regpat++ = ')';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200376
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200377 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200378}
379
380/*
381 * Convert a scanf like format in 'errorformat' to a regular expression.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200382 * Returns a pointer to the location after the pattern.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200383 */
384 static char_u *
385scanf_fmt_to_regpat(
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200386 char_u **pefmp,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200387 char_u *efm,
388 int len,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100389 char_u *regpat)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200390{
391 char_u *efmp = *pefmp;
392
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200393 if (*efmp == '[' || *efmp == '\\')
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200394 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200395 if ((*regpat++ = *efmp) == '[') // %*[^a-z0-9] etc.
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200396 {
397 if (efmp[1] == '^')
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200398 *regpat++ = *++efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200399 if (efmp < efm + len)
400 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200401 *regpat++ = *++efmp; // could be ']'
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200402 while (efmp < efm + len
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200403 && (*regpat++ = *++efmp) != ']')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200404 // skip ;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200405 if (efmp == efm + len)
406 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000407 emsg(_(e_missing_rsb_in_format_string));
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200408 return NULL;
409 }
410 }
411 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200412 else if (efmp < efm + len) // %*\D, %*\s etc.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200413 *regpat++ = *++efmp;
414 *regpat++ = '\\';
415 *regpat++ = '+';
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200416 }
417 else
418 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200419 // TODO: scanf()-like: %*ud, %*3c, %*f, ... ?
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000420 semsg(_(e_unsupported_chr_in_format_string), *efmp);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200421 return NULL;
422 }
423
424 *pefmp = efmp;
425
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200426 return regpat;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200427}
428
429/*
430 * Analyze/parse an errorformat prefix.
431 */
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200432 static char_u *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100433efm_analyze_prefix(char_u *efmp, efm_T *efminfo)
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200434{
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200435 if (vim_strchr((char_u *)"+-", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200436 efminfo->flags = *efmp++;
Bram Moolenaare9283662020-06-07 14:10:47 +0200437 if (vim_strchr((char_u *)"DXAEWINCZGOPQ", *efmp) != NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200438 efminfo->prefix = *efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200439 else
440 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000441 semsg(_(e_invalid_chr_in_format_string_prefix), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200442 return NULL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200443 }
444
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200445 return efmp;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200446}
447
448/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200449 * Converts a 'errorformat' string part in 'efm' to a regular expression
450 * pattern. The resulting regex pattern is returned in "regpat". Additional
451 * information about the 'erroformat' pattern is returned in "fmt_ptr".
452 * Returns OK or FAIL.
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200453 */
454 static int
455efm_to_regpat(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200456 char_u *efm,
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200457 int len,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +0200458 efm_T *fmt_ptr,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100459 char_u *regpat)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200460{
461 char_u *ptr;
462 char_u *efmp;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200463 int round;
464 int idx = 0;
465
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200466 // Build a regexp pattern for a 'errorformat' option part
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200467 ptr = regpat;
468 *ptr++ = '^';
469 round = 0;
470 for (efmp = efm; efmp < efm + len; ++efmp)
471 {
472 if (*efmp == '%')
473 {
474 ++efmp;
475 for (idx = 0; idx < FMT_PATTERNS; ++idx)
476 if (fmt_pat[idx].convchar == *efmp)
477 break;
478 if (idx < FMT_PATTERNS)
479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100480 ptr = efmpat_to_regpat(efmp, ptr, fmt_ptr, idx, round);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200481 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200482 return FAIL;
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200483 round++;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200484 }
485 else if (*efmp == '*')
486 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200487 ++efmp;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100488 ptr = scanf_fmt_to_regpat(&efmp, efm, len, ptr);
Bram Moolenaar6bff7192018-05-20 15:41:17 +0200489 if (ptr == NULL)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200490 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200491 }
492 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200493 *ptr++ = *efmp; // regexp magic characters
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200494 else if (*efmp == '#')
495 *ptr++ = '*';
496 else if (*efmp == '>')
497 fmt_ptr->conthere = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200498 else if (efmp == efm + 1) // analyse prefix
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200499 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200500 // prefix is allowed only at the beginning of the errorformat
501 // option part
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100502 efmp = efm_analyze_prefix(efmp, fmt_ptr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200503 if (efmp == NULL)
504 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200505 }
506 else
507 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000508 semsg(_(e_invalid_chr_in_format_string), *efmp);
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200509 return FAIL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200510 }
511 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200512 else // copy normal character
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200513 {
514 if (*efmp == '\\' && efmp + 1 < efm + len)
515 ++efmp;
516 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200517 *ptr++ = '\\'; // escape regexp atoms
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200518 if (*efmp)
519 *ptr++ = *efmp;
520 }
521 }
522 *ptr++ = '$';
523 *ptr = NUL;
524
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200525 return OK;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200526}
527
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200528/*
529 * Free the 'errorformat' information list
530 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200531 static void
532free_efm_list(efm_T **efm_first)
533{
534 efm_T *efm_ptr;
535
536 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first)
537 {
538 *efm_first = efm_ptr->next;
539 vim_regfree(efm_ptr->prog);
540 vim_free(efm_ptr);
541 }
Bram Moolenaar63bed3d2016-11-12 15:36:54 +0100542 fmt_start = NULL;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200543}
544
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200545/*
546 * Compute the size of the buffer used to convert a 'errorformat' pattern into
547 * a regular expression pattern.
548 */
549 static int
550efm_regpat_bufsz(char_u *efm)
551{
552 int sz;
553 int i;
554
555 sz = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
556 for (i = FMT_PATTERNS; i > 0; )
557 sz += (int)STRLEN(fmt_pat[--i].pattern);
558#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200559 sz += 12; // "%f" can become twelve chars longer (see efm_to_regpat)
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200560#else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200561 sz += 2; // "%f" can become two chars longer
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200562#endif
563
564 return sz;
565}
566
567/*
568 * Return the length of a 'errorformat' option part (separated by ",").
569 */
570 static int
571efm_option_part_len(char_u *efm)
572{
573 int len;
574
575 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
576 if (efm[len] == '\\' && efm[len + 1] != NUL)
577 ++len;
578
579 return len;
580}
581
582/*
583 * Parse the 'errorformat' option. Multiple parts in the 'errorformat' option
584 * are parsed and converted to regular expressions. Returns information about
585 * the parsed 'errorformat' option.
586 */
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200587 static efm_T *
588parse_efm_option(char_u *efm)
589{
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200590 efm_T *fmt_ptr = NULL;
591 efm_T *fmt_first = NULL;
592 efm_T *fmt_last = NULL;
593 char_u *fmtstr = NULL;
594 int len;
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200595 int sz;
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200596
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200597 // Each part of the format string is copied and modified from errorformat
598 // to regex prog. Only a few % characters are allowed.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200599
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200600 // Get some space to modify the format string into.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200601 sz = efm_regpat_bufsz(efm);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000602 if ((fmtstr = alloc_id(sz, aid_qf_efm_fmtstr)) == NULL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200603 goto parse_efm_error;
604
605 while (efm[0] != NUL)
606 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200607 // Allocate a new eformat structure and put it at the end of the list
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000608 fmt_ptr = ALLOC_CLEAR_ONE_ID(efm_T, aid_qf_efm_fmtpart);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200609 if (fmt_ptr == NULL)
610 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200611 if (fmt_first == NULL) // first one
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200612 fmt_first = fmt_ptr;
613 else
614 fmt_last->next = fmt_ptr;
615 fmt_last = fmt_ptr;
616
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200617 // Isolate one part in the 'errorformat' option
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200618 len = efm_option_part_len(efm);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200619
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100620 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr) == FAIL)
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200621 goto parse_efm_error;
622 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
623 goto parse_efm_error;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200624 // Advance to next part
625 efm = skip_to_option_part(efm + len); // skip comma and spaces
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200626 }
627
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200628 if (fmt_first == NULL) // nothing found
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000629 emsg(_(e_errorformat_contains_no_pattern));
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200630
631 goto parse_efm_end;
632
633parse_efm_error:
634 free_efm_list(&fmt_first);
635
636parse_efm_end:
637 vim_free(fmtstr);
Bram Moolenaar688e3d12016-06-26 22:05:54 +0200638
639 return fmt_first;
640}
641
Bram Moolenaare0d37972016-07-15 22:36:01 +0200642enum {
643 QF_FAIL = 0,
644 QF_OK = 1,
645 QF_END_OF_INPUT = 2,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200646 QF_NOMEM = 3,
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200647 QF_IGNORE_LINE = 4,
648 QF_MULTISCAN = 5,
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +0100649 QF_ABORT = 6
Bram Moolenaare0d37972016-07-15 22:36:01 +0200650};
651
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200652/*
653 * State information used to parse lines and add entries to a quickfix/location
654 * list.
655 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200656typedef struct {
657 char_u *linebuf;
658 int linelen;
659 char_u *growbuf;
660 int growbufsiz;
661 FILE *fd;
662 typval_T *tv;
663 char_u *p_str;
664 listitem_T *p_li;
665 buf_T *buf;
666 linenr_T buflnum;
667 linenr_T lnumlast;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100668 vimconv_T vc;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200669} qfstate_T;
670
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200671/*
672 * Allocate more memory for the line buffer used for parsing lines.
673 */
Bram Moolenaare0d37972016-07-15 22:36:01 +0200674 static char_u *
675qf_grow_linebuf(qfstate_T *state, int newsz)
676{
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200677 char_u *p;
678
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200679 // If the line exceeds LINE_MAXLEN exclude the last
680 // byte since it's not a NL character.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200681 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz;
682 if (state->growbuf == NULL)
683 {
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000684 state->growbuf = alloc_id(state->linelen + 1, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200685 if (state->growbuf == NULL)
686 return NULL;
687 state->growbufsiz = state->linelen;
688 }
689 else if (state->linelen > state->growbufsiz)
690 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200691 if ((p = vim_realloc(state->growbuf, state->linelen + 1)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200692 return NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200693 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200694 state->growbufsiz = state->linelen;
695 }
696 return state->growbuf;
697}
698
699/*
700 * Get the next string (separated by newline) from state->p_str.
701 */
702 static int
703qf_get_next_str_line(qfstate_T *state)
704{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200705 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200706 char_u *p_str = state->p_str;
707 char_u *p;
708 int len;
709
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200710 if (*p_str == NUL) // Reached the end of the string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200711 return QF_END_OF_INPUT;
712
713 p = vim_strchr(p_str, '\n');
714 if (p != NULL)
715 len = (int)(p - p_str) + 1;
716 else
717 len = (int)STRLEN(p_str);
718
719 if (len > IOSIZE - 2)
720 {
721 state->linebuf = qf_grow_linebuf(state, len);
722 if (state->linebuf == NULL)
723 return QF_NOMEM;
724 }
725 else
726 {
727 state->linebuf = IObuff;
728 state->linelen = len;
729 }
730 vim_strncpy(state->linebuf, p_str, state->linelen);
731
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200732 // Increment using len in order to discard the rest of the
733 // line if it exceeds LINE_MAXLEN.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200734 p_str += len;
735 state->p_str = p_str;
736
737 return QF_OK;
738}
739
740/*
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000741 * Get the next string from the List item state->p_li.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200742 */
743 static int
744qf_get_next_list_line(qfstate_T *state)
745{
746 listitem_T *p_li = state->p_li;
747 int len;
748
749 while (p_li != NULL
750 && (p_li->li_tv.v_type != VAR_STRING
751 || p_li->li_tv.vval.v_string == NULL))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200752 p_li = p_li->li_next; // Skip non-string items
Bram Moolenaare0d37972016-07-15 22:36:01 +0200753
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200754 if (p_li == NULL) // End of the list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200755 {
756 state->p_li = NULL;
757 return QF_END_OF_INPUT;
758 }
759
760 len = (int)STRLEN(p_li->li_tv.vval.v_string);
761 if (len > IOSIZE - 2)
762 {
763 state->linebuf = qf_grow_linebuf(state, len);
764 if (state->linebuf == NULL)
765 return QF_NOMEM;
766 }
767 else
768 {
769 state->linebuf = IObuff;
770 state->linelen = len;
771 }
772
773 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen);
774
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200775 state->p_li = p_li->li_next; // next item
Bram Moolenaare0d37972016-07-15 22:36:01 +0200776 return QF_OK;
777}
778
779/*
780 * Get the next string from state->buf.
781 */
782 static int
783qf_get_next_buf_line(qfstate_T *state)
784{
785 char_u *p_buf = NULL;
786 int len;
787
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200788 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200789 if (state->buflnum > state->lnumlast)
790 return QF_END_OF_INPUT;
791
792 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +0100793 len = ml_get_buf_len(state->buf, state->buflnum);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200794 state->buflnum += 1;
795
Bram Moolenaare0d37972016-07-15 22:36:01 +0200796 if (len > IOSIZE - 2)
797 {
798 state->linebuf = qf_grow_linebuf(state, len);
799 if (state->linebuf == NULL)
800 return QF_NOMEM;
801 }
802 else
803 {
804 state->linebuf = IObuff;
805 state->linelen = len;
806 }
807 vim_strncpy(state->linebuf, p_buf, state->linelen);
808
809 return QF_OK;
810}
811
812/*
813 * Get the next string from file state->fd.
814 */
815 static int
816qf_get_next_file_line(qfstate_T *state)
817{
818 int discard;
819 int growbuflen;
820
821 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL)
822 return QF_END_OF_INPUT;
823
824 discard = FALSE;
825 state->linelen = (int)STRLEN(IObuff);
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200826 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n'))
Bram Moolenaare0d37972016-07-15 22:36:01 +0200827 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200828 // The current line exceeds IObuff, continue reading using
829 // growbuf until EOL or LINE_MAXLEN bytes is read.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200830 if (state->growbuf == NULL)
831 {
832 state->growbufsiz = 2 * (IOSIZE - 1);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000833 state->growbuf = alloc_id(state->growbufsiz, aid_qf_linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200834 if (state->growbuf == NULL)
835 return QF_NOMEM;
836 }
837
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200838 // Copy the read part of the line, excluding null-terminator
Bram Moolenaare0d37972016-07-15 22:36:01 +0200839 memcpy(state->growbuf, IObuff, IOSIZE - 1);
840 growbuflen = state->linelen;
841
842 for (;;)
843 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200844 char_u *p;
845
Bram Moolenaare0d37972016-07-15 22:36:01 +0200846 if (fgets((char *)state->growbuf + growbuflen,
847 state->growbufsiz - growbuflen, state->fd) == NULL)
848 break;
849 state->linelen = (int)STRLEN(state->growbuf + growbuflen);
850 growbuflen += state->linelen;
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200851 if ((state->growbuf)[growbuflen - 1] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200852 break;
853 if (state->growbufsiz == LINE_MAXLEN)
854 {
855 discard = TRUE;
856 break;
857 }
858
859 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN
860 ? 2 * state->growbufsiz : LINE_MAXLEN;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200861 if ((p = vim_realloc(state->growbuf, state->growbufsiz)) == NULL)
Bram Moolenaare0d37972016-07-15 22:36:01 +0200862 return QF_NOMEM;
Bram Moolenaar18cebf42018-05-08 22:31:37 +0200863 state->growbuf = p;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200864 }
865
866 while (discard)
867 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200868 // The current line is longer than LINE_MAXLEN, continue
869 // reading but discard everything until EOL or EOF is
870 // reached.
Bram Moolenaare0d37972016-07-15 22:36:01 +0200871 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL
872 || (int)STRLEN(IObuff) < IOSIZE - 1
Bram Moolenaar59941cb2020-09-05 17:03:40 +0200873 || IObuff[IOSIZE - 2] == '\n')
Bram Moolenaare0d37972016-07-15 22:36:01 +0200874 break;
875 }
876
877 state->linebuf = state->growbuf;
878 state->linelen = growbuflen;
879 }
880 else
881 state->linebuf = IObuff;
882
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200883 // Convert a line if it contains a non-ASCII character.
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200884 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf))
885 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100886 char_u *line;
887
888 line = string_convert(&state->vc, state->linebuf, &state->linelen);
889 if (line != NULL)
890 {
891 if (state->linelen < IOSIZE)
892 {
893 STRCPY(state->linebuf, line);
894 vim_free(line);
895 }
896 else
897 {
898 vim_free(state->growbuf);
899 state->linebuf = state->growbuf = line;
900 state->growbufsiz = state->linelen < LINE_MAXLEN
901 ? state->linelen : LINE_MAXLEN;
902 }
903 }
904 }
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100905
Bram Moolenaare0d37972016-07-15 22:36:01 +0200906 return QF_OK;
907}
908
909/*
910 * Get the next string from a file/buffer/list/string.
911 */
912 static int
913qf_get_nextline(qfstate_T *state)
914{
915 int status = QF_FAIL;
916
917 if (state->fd == NULL)
918 {
919 if (state->tv != NULL)
920 {
921 if (state->tv->v_type == VAR_STRING)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200922 // Get the next line from the supplied string
Bram Moolenaare0d37972016-07-15 22:36:01 +0200923 status = qf_get_next_str_line(state);
924 else if (state->tv->v_type == VAR_LIST)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200925 // Get the next line from the supplied list
Bram Moolenaare0d37972016-07-15 22:36:01 +0200926 status = qf_get_next_list_line(state);
927 }
928 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200929 // Get the next line from the supplied buffer
Bram Moolenaare0d37972016-07-15 22:36:01 +0200930 status = qf_get_next_buf_line(state);
931 }
932 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200933 // Get the next line from the supplied file
Bram Moolenaare0d37972016-07-15 22:36:01 +0200934 status = qf_get_next_file_line(state);
935
936 if (status != QF_OK)
937 return status;
938
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200939 // remove newline/CR from the line
Bram Moolenaare0d37972016-07-15 22:36:01 +0200940 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n')
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200941 {
Bram Moolenaare0d37972016-07-15 22:36:01 +0200942 state->linebuf[state->linelen - 1] = NUL;
943#ifdef USE_CRNL
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200944 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r')
945 state->linebuf[state->linelen - 2] = NUL;
Bram Moolenaare0d37972016-07-15 22:36:01 +0200946#endif
Bram Moolenaar796aa9c2016-08-02 21:41:28 +0200947 }
Bram Moolenaare0d37972016-07-15 22:36:01 +0200948
Bram Moolenaare0d37972016-07-15 22:36:01 +0200949 remove_bom(state->linebuf);
Bram Moolenaare0d37972016-07-15 22:36:01 +0200950
951 return QF_OK;
952}
953
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200954typedef struct {
955 char_u *namebuf;
Yegappan Lakshmananb7318002023-10-25 20:50:28 +0200956 int bnr;
Bram Moolenaard76ce852018-05-01 15:02:04 +0200957 char_u *module;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200958 char_u *errmsg;
959 int errmsglen;
960 long lnum;
thinca6864efa2021-06-19 20:45:20 +0200961 long end_lnum;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200962 int col;
thinca6864efa2021-06-19 20:45:20 +0200963 int end_col;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200964 char_u use_viscol;
965 char_u *pattern;
966 int enr;
967 int type;
Tom Praschanca6ac992023-08-11 23:26:12 +0200968 typval_T *user_data;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +0200969 int valid;
970} qffields_T;
971
972/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200973 * Parse the match for filename ('%f') pattern in regmatch.
974 * Return the matched value in "fields->namebuf".
975 */
976 static int
977qf_parse_fmt_f(regmatch_T *rmp, int midx, qffields_T *fields, int prefix)
978{
979 int c;
980
981 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
982 return QF_FAIL;
983
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200984 // Expand ~/file and $HOME/file to full path.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200985 c = *rmp->endp[midx];
986 *rmp->endp[midx] = NUL;
987 expand_env(rmp->startp[midx], fields->namebuf, CMDBUFFSIZE);
988 *rmp->endp[midx] = c;
989
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +0200990 // For separate filename patterns (%O, %P and %Q), the specified file
991 // should exist.
Bram Moolenaarde3b3672018-08-07 21:54:41 +0200992 if (vim_strchr((char_u *)"OPQ", prefix) != NULL
993 && mch_getperm(fields->namebuf) == -1)
994 return QF_FAIL;
995
996 return QF_OK;
997}
998
999/*
zeertzjqb0221812023-10-26 23:15:44 +02001000 * Parse the match for buffer number ('%b') pattern in regmatch.
1001 * Return the matched value in "fields->bnr".
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001002 */
1003 static int
Yegappan Lakshmananb7318002023-10-25 20:50:28 +02001004qf_parse_fmt_b(regmatch_T *rmp, int midx, qffields_T *fields)
1005{
1006 if (rmp->startp[midx] == NULL)
1007 return QF_FAIL;
1008 int bnr = (int)atol((char *)rmp->startp[midx]);
1009 if (buflist_findnr(bnr) == NULL)
1010 return QF_FAIL;
1011 fields->bnr = bnr;
1012 return QF_OK;
1013}
1014
1015/*
1016 * Parse the match for error number ('%n') pattern in regmatch.
1017 * Return the matched value in "fields->enr".
1018 */
1019 static int
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001020qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields)
1021{
1022 if (rmp->startp[midx] == NULL)
1023 return QF_FAIL;
1024 fields->enr = (int)atol((char *)rmp->startp[midx]);
1025 return QF_OK;
1026}
1027
1028/*
haya14busae023d492022-02-08 18:09:29 +00001029 * Parse the match for line number ('%l') pattern in regmatch.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001030 * Return the matched value in "fields->lnum".
1031 */
1032 static int
1033qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields)
1034{
1035 if (rmp->startp[midx] == NULL)
1036 return QF_FAIL;
1037 fields->lnum = atol((char *)rmp->startp[midx]);
1038 return QF_OK;
1039}
1040
1041/*
haya14busae023d492022-02-08 18:09:29 +00001042 * Parse the match for end line number ('%e') pattern in regmatch.
1043 * Return the matched value in "fields->end_lnum".
1044 */
1045 static int
1046qf_parse_fmt_e(regmatch_T *rmp, int midx, qffields_T *fields)
1047{
1048 if (rmp->startp[midx] == NULL)
1049 return QF_FAIL;
1050 fields->end_lnum = atol((char *)rmp->startp[midx]);
1051 return QF_OK;
1052}
1053
1054/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001055 * Parse the match for column number ('%c') pattern in regmatch.
1056 * Return the matched value in "fields->col".
1057 */
1058 static int
1059qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields)
1060{
1061 if (rmp->startp[midx] == NULL)
1062 return QF_FAIL;
1063 fields->col = (int)atol((char *)rmp->startp[midx]);
1064 return QF_OK;
1065}
1066
1067/*
haya14busae023d492022-02-08 18:09:29 +00001068 * Parse the match for end column number ('%k') pattern in regmatch.
1069 * Return the matched value in "fields->end_col".
1070 */
1071 static int
1072qf_parse_fmt_k(regmatch_T *rmp, int midx, qffields_T *fields)
1073{
1074 if (rmp->startp[midx] == NULL)
1075 return QF_FAIL;
1076 fields->end_col = (int)atol((char *)rmp->startp[midx]);
1077 return QF_OK;
1078}
1079
1080/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001081 * Parse the match for error type ('%t') pattern in regmatch.
1082 * Return the matched value in "fields->type".
1083 */
1084 static int
1085qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
1086{
1087 if (rmp->startp[midx] == NULL)
1088 return QF_FAIL;
1089 fields->type = *rmp->startp[midx];
1090 return QF_OK;
1091}
1092
1093/*
Bram Moolenaarf4140482020-02-15 23:06:45 +01001094 * Copy a non-error line into the error string. Return the matched line in
1095 * "fields->errmsg".
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001096 */
1097 static int
Bram Moolenaarf4140482020-02-15 23:06:45 +01001098copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001099{
1100 char_u *p;
1101
1102 if (linelen >= fields->errmsglen)
1103 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001104 // linelen + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001105 if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1106 return QF_NOMEM;
1107 fields->errmsg = p;
1108 fields->errmsglen = linelen + 1;
1109 }
Bram Moolenaarf4140482020-02-15 23:06:45 +01001110 // copy whole line to error message
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001111 vim_strncpy(fields->errmsg, linebuf, linelen);
Bram Moolenaarf4140482020-02-15 23:06:45 +01001112
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001113 return QF_OK;
1114}
1115
1116/*
1117 * Parse the match for error message ('%m') pattern in regmatch.
1118 * Return the matched value in "fields->errmsg".
1119 */
1120 static int
1121qf_parse_fmt_m(regmatch_T *rmp, int midx, qffields_T *fields)
1122{
1123 char_u *p;
1124 int len;
1125
1126 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1127 return QF_FAIL;
1128 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1129 if (len >= fields->errmsglen)
1130 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001131 // len + null terminator
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001132 if ((p = vim_realloc(fields->errmsg, len + 1)) == NULL)
1133 return QF_NOMEM;
1134 fields->errmsg = p;
1135 fields->errmsglen = len + 1;
1136 }
1137 vim_strncpy(fields->errmsg, rmp->startp[midx], len);
1138 return QF_OK;
1139}
1140
1141/*
1142 * Parse the match for rest of a single-line file message ('%r') pattern.
1143 * Return the matched value in "tail".
1144 */
1145 static int
1146qf_parse_fmt_r(regmatch_T *rmp, int midx, char_u **tail)
1147{
1148 if (rmp->startp[midx] == NULL)
1149 return QF_FAIL;
1150 *tail = rmp->startp[midx];
1151 return QF_OK;
1152}
1153
1154/*
1155 * Parse the match for the pointer line ('%p') pattern in regmatch.
1156 * Return the matched value in "fields->col".
1157 */
1158 static int
1159qf_parse_fmt_p(regmatch_T *rmp, int midx, qffields_T *fields)
1160{
1161 char_u *match_ptr;
1162
1163 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1164 return QF_FAIL;
1165 fields->col = 0;
1166 for (match_ptr = rmp->startp[midx]; match_ptr != rmp->endp[midx];
1167 ++match_ptr)
1168 {
1169 ++fields->col;
1170 if (*match_ptr == TAB)
1171 {
1172 fields->col += 7;
1173 fields->col -= fields->col % 8;
1174 }
1175 }
1176 ++fields->col;
1177 fields->use_viscol = TRUE;
1178 return QF_OK;
1179}
1180
1181/*
1182 * Parse the match for the virtual column number ('%v') pattern in regmatch.
1183 * Return the matched value in "fields->col".
1184 */
1185 static int
1186qf_parse_fmt_v(regmatch_T *rmp, int midx, qffields_T *fields)
1187{
1188 if (rmp->startp[midx] == NULL)
1189 return QF_FAIL;
1190 fields->col = (int)atol((char *)rmp->startp[midx]);
1191 fields->use_viscol = TRUE;
1192 return QF_OK;
1193}
1194
1195/*
1196 * Parse the match for the search text ('%s') pattern in regmatch.
1197 * Return the matched value in "fields->pattern".
1198 */
1199 static int
1200qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields)
1201{
1202 int len;
1203
1204 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1205 return QF_FAIL;
1206 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1207 if (len > CMDBUFFSIZE - 5)
1208 len = CMDBUFFSIZE - 5;
1209 STRCPY(fields->pattern, "^\\V");
1210 STRNCAT(fields->pattern, rmp->startp[midx], len);
1211 fields->pattern[len + 3] = '\\';
1212 fields->pattern[len + 4] = '$';
1213 fields->pattern[len + 5] = NUL;
1214 return QF_OK;
1215}
1216
1217/*
1218 * Parse the match for the module ('%o') pattern in regmatch.
1219 * Return the matched value in "fields->module".
1220 */
1221 static int
1222qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields)
1223{
1224 int len;
1225
1226 if (rmp->startp[midx] == NULL || rmp->endp[midx] == NULL)
1227 return QF_FAIL;
1228 len = (int)(rmp->endp[midx] - rmp->startp[midx]);
1229 if (len > CMDBUFFSIZE)
1230 len = CMDBUFFSIZE;
1231 STRNCAT(fields->module, rmp->startp[midx], len);
1232 return QF_OK;
1233}
1234
1235/*
1236 * 'errorformat' format pattern parser functions.
1237 * The '%f' and '%r' formats are parsed differently from other formats.
1238 * See qf_parse_match() for details.
haya14busae023d492022-02-08 18:09:29 +00001239 * Keep in sync with fmt_pat[].
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001240 */
1241static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) =
1242{
haya14busae023d492022-02-08 18:09:29 +00001243 NULL, // %f
Yegappan Lakshmananb7318002023-10-25 20:50:28 +02001244 qf_parse_fmt_b,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001245 qf_parse_fmt_n,
1246 qf_parse_fmt_l,
haya14busae023d492022-02-08 18:09:29 +00001247 qf_parse_fmt_e,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001248 qf_parse_fmt_c,
haya14busae023d492022-02-08 18:09:29 +00001249 qf_parse_fmt_k,
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001250 qf_parse_fmt_t,
1251 qf_parse_fmt_m,
haya14busae023d492022-02-08 18:09:29 +00001252 NULL, // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001253 qf_parse_fmt_p,
1254 qf_parse_fmt_v,
1255 qf_parse_fmt_s,
1256 qf_parse_fmt_o
1257};
1258
1259/*
1260 * Parse the error format pattern matches in "regmatch" and set the values in
1261 * "fields". fmt_ptr contains the 'efm' format specifiers/prefixes that have a
1262 * match. Returns QF_OK if all the matches are successfully parsed. On
1263 * failure, returns QF_FAIL or QF_NOMEM.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001264 */
1265 static int
1266qf_parse_match(
1267 char_u *linebuf,
1268 int linelen,
1269 efm_T *fmt_ptr,
1270 regmatch_T *regmatch,
1271 qffields_T *fields,
1272 int qf_multiline,
1273 int qf_multiscan,
1274 char_u **tail)
1275{
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001276 int idx = fmt_ptr->prefix;
1277 int i;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001278 int midx;
1279 int status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001280
1281 if ((idx == 'C' || idx == 'Z') && !qf_multiline)
1282 return QF_FAIL;
Bram Moolenaare9283662020-06-07 14:10:47 +02001283 if (vim_strchr((char_u *)"EWIN", idx) != NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001284 fields->type = idx;
1285 else
1286 fields->type = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001287
1288 // Extract error message data from matched line.
1289 // We check for an actual submatch, because "\[" and "\]" in
1290 // the 'errorformat' may cause the wrong submatch to be used.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001291 for (i = 0; i < FMT_PATTERNS; i++)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001292 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001293 status = QF_OK;
1294 midx = (int)fmt_ptr->addr[i];
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001295 if (i == 0 && midx > 0) // %f
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001296 status = qf_parse_fmt_f(regmatch, midx, fields, idx);
haya14busae023d492022-02-08 18:09:29 +00001297 else if (i == FMT_PATTERN_M)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001298 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001299 if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
Bram Moolenaarf4140482020-02-15 23:06:45 +01001300 status = copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001301 else if (midx > 0) // %m
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001302 status = qf_parse_fmt_m(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001303 }
haya14busae023d492022-02-08 18:09:29 +00001304 else if (i == FMT_PATTERN_R && midx > 0) // %r
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001305 status = qf_parse_fmt_r(regmatch, midx, tail);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001306 else if (midx > 0) // others
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001307 status = (qf_parse_fmt[i])(regmatch, midx, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001308
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001309 if (status != QF_OK)
1310 return status;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001311 }
1312
1313 return QF_OK;
1314}
1315
1316/*
1317 * Parse an error line in 'linebuf' using a single error format string in
1318 * 'fmt_ptr->prog' and return the matching values in 'fields'.
1319 * Returns QF_OK if the efm format matches completely and the fields are
1320 * successfully copied. Otherwise returns QF_FAIL or QF_NOMEM.
1321 */
1322 static int
1323qf_parse_get_fields(
1324 char_u *linebuf,
1325 int linelen,
1326 efm_T *fmt_ptr,
1327 qffields_T *fields,
1328 int qf_multiline,
1329 int qf_multiscan,
1330 char_u **tail)
1331{
1332 regmatch_T regmatch;
1333 int status = QF_FAIL;
1334 int r;
1335
1336 if (qf_multiscan &&
1337 vim_strchr((char_u *)"OPQ", fmt_ptr->prefix) == NULL)
1338 return QF_FAIL;
1339
1340 fields->namebuf[0] = NUL;
Yegappan Lakshmananb7318002023-10-25 20:50:28 +02001341 fields->bnr = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001342 fields->module[0] = NUL;
1343 fields->pattern[0] = NUL;
1344 if (!qf_multiscan)
1345 fields->errmsg[0] = NUL;
1346 fields->lnum = 0;
thinca6864efa2021-06-19 20:45:20 +02001347 fields->end_lnum = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001348 fields->col = 0;
thinca6864efa2021-06-19 20:45:20 +02001349 fields->end_col = 0;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001350 fields->use_viscol = FALSE;
1351 fields->enr = -1;
1352 fields->type = 0;
1353 *tail = NULL;
1354
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001355 // Always ignore case when looking for a matching error.
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001356 regmatch.rm_ic = TRUE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001357 regmatch.regprog = fmt_ptr->prog;
1358 r = vim_regexec(&regmatch, linebuf, (colnr_T)0);
1359 fmt_ptr->prog = regmatch.regprog;
1360 if (r)
1361 status = qf_parse_match(linebuf, linelen, fmt_ptr, &regmatch,
1362 fields, qf_multiline, qf_multiscan, tail);
1363
1364 return status;
1365}
1366
1367/*
1368 * Parse directory error format prefixes (%D and %X).
1369 * Push and pop directories from the directory stack when scanning directory
1370 * names.
1371 */
1372 static int
1373qf_parse_dir_pfx(int idx, qffields_T *fields, qf_list_T *qfl)
1374{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001375 if (idx == 'D') // enter directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001376 {
1377 if (*fields->namebuf == NUL)
1378 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001379 emsg(_(e_missing_or_empty_directory_name));
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001380 return QF_FAIL;
1381 }
1382 qfl->qf_directory =
1383 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE);
1384 if (qfl->qf_directory == NULL)
1385 return QF_FAIL;
1386 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001387 else if (idx == 'X') // leave directory
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001388 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack);
1389
1390 return QF_OK;
1391}
1392
1393/*
1394 * Parse global file name error format prefixes (%O, %P and %Q).
1395 */
1396 static int
1397qf_parse_file_pfx(
1398 int idx,
1399 qffields_T *fields,
1400 qf_list_T *qfl,
1401 char_u *tail)
1402{
1403 fields->valid = FALSE;
1404 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0)
1405 {
1406 if (*fields->namebuf && idx == 'P')
1407 qfl->qf_currfile =
1408 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE);
1409 else if (idx == 'Q')
1410 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack);
1411 *fields->namebuf = NUL;
1412 if (tail && *tail)
1413 {
1414 STRMOVE(IObuff, skipwhite(tail));
1415 qfl->qf_multiscan = TRUE;
1416 return QF_MULTISCAN;
1417 }
1418 }
1419
1420 return QF_OK;
1421}
1422
1423/*
1424 * Parse a non-error line (a line which doesn't match any of the error
1425 * format in 'efm').
1426 */
1427 static int
1428qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
1429{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001430 fields->namebuf[0] = NUL; // no match found, remove file name
1431 fields->lnum = 0; // don't jump to this line
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001432 fields->valid = FALSE;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001433
Bram Moolenaarf4140482020-02-15 23:06:45 +01001434 return copy_nonerror_line(linebuf, linelen, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001435}
1436
1437/*
1438 * Parse multi-line error format prefixes (%C and %Z)
1439 */
1440 static int
1441qf_parse_multiline_pfx(
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001442 int idx,
1443 qf_list_T *qfl,
1444 qffields_T *fields)
1445{
1446 char_u *ptr;
1447 int len;
1448
1449 if (!qfl->qf_multiignore)
1450 {
1451 qfline_T *qfprev = qfl->qf_last;
1452
1453 if (qfprev == NULL)
1454 return QF_FAIL;
1455 if (*fields->errmsg && !qfl->qf_multiignore)
1456 {
1457 len = (int)STRLEN(qfprev->qf_text);
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00001458 ptr = alloc_id(len + STRLEN(fields->errmsg) + 2,
1459 aid_qf_multiline_pfx);
1460 if (ptr == NULL)
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001461 return QF_FAIL;
1462 STRCPY(ptr, qfprev->qf_text);
1463 vim_free(qfprev->qf_text);
1464 qfprev->qf_text = ptr;
1465 *(ptr += len) = '\n';
1466 STRCPY(++ptr, fields->errmsg);
1467 }
1468 if (qfprev->qf_nr == -1)
1469 qfprev->qf_nr = fields->enr;
1470 if (vim_isprintc(fields->type) && !qfprev->qf_type)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001471 // only printable chars allowed
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001472 qfprev->qf_type = fields->type;
1473
1474 if (!qfprev->qf_lnum)
1475 qfprev->qf_lnum = fields->lnum;
haya14busae023d492022-02-08 18:09:29 +00001476 if (!qfprev->qf_end_lnum)
1477 qfprev->qf_end_lnum = fields->end_lnum;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001478 if (!qfprev->qf_col)
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001479 {
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001480 qfprev->qf_col = fields->col;
Bram Moolenaarc95940c2020-10-20 14:59:12 +02001481 qfprev->qf_viscol = fields->use_viscol;
1482 }
haya14busae023d492022-02-08 18:09:29 +00001483 if (!qfprev->qf_end_col)
1484 qfprev->qf_end_col = fields->end_col;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001485 if (!qfprev->qf_fnum)
Bram Moolenaar0398e002019-03-21 21:12:49 +01001486 qfprev->qf_fnum = qf_get_fnum(qfl,
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001487 qfl->qf_directory,
1488 *fields->namebuf || qfl->qf_directory != NULL
1489 ? fields->namebuf
1490 : qfl->qf_currfile != NULL && fields->valid
1491 ? qfl->qf_currfile : 0);
1492 }
1493 if (idx == 'Z')
1494 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
1495 line_breakcheck();
1496
1497 return QF_IGNORE_LINE;
1498}
1499
1500/*
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001501 * Parse a line and get the quickfix fields.
1502 * Return the QF_ status.
1503 */
1504 static int
1505qf_parse_line(
Bram Moolenaar0398e002019-03-21 21:12:49 +01001506 qf_list_T *qfl,
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001507 char_u *linebuf,
1508 int linelen,
1509 efm_T *fmt_first,
1510 qffields_T *fields)
1511{
1512 efm_T *fmt_ptr;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001513 int idx = 0;
1514 char_u *tail = NULL;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001515 int status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001516
Bram Moolenaare333e792018-04-08 13:27:39 +02001517restofline:
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001518 // If there was no %> item start at the first pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001519 if (fmt_start == NULL)
1520 fmt_ptr = fmt_first;
1521 else
1522 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001523 // Otherwise start from the last used pattern
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001524 fmt_ptr = fmt_start;
1525 fmt_start = NULL;
1526 }
1527
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001528 // Try to match each part of 'errorformat' until we find a complete
1529 // match or no match.
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001530 fields->valid = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001531 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
1532 {
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001533 idx = fmt_ptr->prefix;
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001534 status = qf_parse_get_fields(linebuf, linelen, fmt_ptr, fields,
1535 qfl->qf_multiline, qfl->qf_multiscan, &tail);
1536 if (status == QF_NOMEM)
1537 return status;
1538 if (status == QF_OK)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001539 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001540 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001541 qfl->qf_multiscan = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001542
1543 if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
1544 {
1545 if (fmt_ptr != NULL)
1546 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001547 // 'D' and 'X' directory specifiers
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001548 status = qf_parse_dir_pfx(idx, fields, qfl);
1549 if (status != QF_OK)
1550 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001551 }
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001552
1553 status = qf_parse_line_nomatch(linebuf, linelen, fields);
1554 if (status != QF_OK)
1555 return status;
1556
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001557 if (fmt_ptr == NULL)
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001558 qfl->qf_multiline = qfl->qf_multiignore = FALSE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001559 }
1560 else if (fmt_ptr != NULL)
1561 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001562 // honor %> item
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001563 if (fmt_ptr->conthere)
1564 fmt_start = fmt_ptr;
1565
Bram Moolenaare9283662020-06-07 14:10:47 +02001566 if (vim_strchr((char_u *)"AEWIN", idx) != NULL)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001567 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001568 qfl->qf_multiline = TRUE; // start of a multi-line message
1569 qfl->qf_multiignore = FALSE;// reset continuation
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001570 }
1571 else if (vim_strchr((char_u *)"CZ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001572 { // continuation of multi-line msg
Bram Moolenaar0398e002019-03-21 21:12:49 +01001573 status = qf_parse_multiline_pfx(idx, qfl, fields);
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001574 if (status != QF_OK)
1575 return status;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001576 }
1577 else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001578 { // global file names
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001579 status = qf_parse_file_pfx(idx, fields, qfl, tail);
1580 if (status == QF_MULTISCAN)
1581 goto restofline;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001582 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001583 if (fmt_ptr->flags == '-') // generally exclude this line
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001584 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001585 if (qfl->qf_multiline)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001586 // also exclude continuation lines
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001587 qfl->qf_multiignore = TRUE;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001588 return QF_IGNORE_LINE;
1589 }
1590 }
1591
1592 return QF_OK;
1593}
1594
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001595/*
Bram Moolenaar019dfe62018-10-07 14:38:49 +02001596 * Returns TRUE if the specified quickfix/location stack is empty
1597 */
1598 static int
1599qf_stack_empty(qf_info_T *qi)
1600{
1601 return qi == NULL || qi->qf_listcount <= 0;
1602}
1603
1604/*
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001605 * Returns TRUE if the specified quickfix/location list is empty.
1606 */
1607 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01001608qf_list_empty(qf_list_T *qfl)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001609{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001610 return qfl == NULL || qfl->qf_count <= 0;
1611}
1612
1613/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02001614 * Returns TRUE if the specified quickfix/location list is not empty and
1615 * has valid entries.
1616 */
1617 static int
1618qf_list_has_valid_entries(qf_list_T *qfl)
1619{
1620 return !qf_list_empty(qfl) && !qfl->qf_nonevalid;
1621}
1622
1623/*
Bram Moolenaar0398e002019-03-21 21:12:49 +01001624 * Return a pointer to a list in the specified quickfix stack
1625 */
1626 static qf_list_T *
1627qf_get_list(qf_info_T *qi, int idx)
1628{
1629 return &qi->qf_lists[idx];
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001630}
1631
Bram Moolenaarde3b3672018-08-07 21:54:41 +02001632/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001633 * Allocate the fields used for parsing lines and populating a quickfix list.
1634 */
1635 static int
1636qf_alloc_fields(qffields_T *pfields)
1637{
1638 pfields->namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf);
1639 pfields->module = alloc_id(CMDBUFFSIZE + 1, aid_qf_module);
1640 pfields->errmsglen = CMDBUFFSIZE + 1;
1641 pfields->errmsg = alloc_id(pfields->errmsglen, aid_qf_errmsg);
1642 pfields->pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1643 if (pfields->namebuf == NULL || pfields->errmsg == NULL
1644 || pfields->pattern == NULL || pfields->module == NULL)
1645 return FAIL;
1646
1647 return OK;
1648}
1649
1650/*
1651 * Free the fields used for parsing lines and populating a quickfix list.
1652 */
1653 static void
1654qf_free_fields(qffields_T *pfields)
1655{
1656 vim_free(pfields->namebuf);
1657 vim_free(pfields->module);
1658 vim_free(pfields->errmsg);
1659 vim_free(pfields->pattern);
1660}
1661
1662/*
1663 * Setup the state information used for parsing lines and populating a
1664 * quickfix list.
1665 */
1666 static int
1667qf_setup_state(
1668 qfstate_T *pstate,
1669 char_u *enc,
1670 char_u *efile,
1671 typval_T *tv,
1672 buf_T *buf,
1673 linenr_T lnumfirst,
1674 linenr_T lnumlast)
1675{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001676 pstate->vc.vc_type = CONV_NONE;
1677 if (enc != NULL && *enc != NUL)
1678 convert_setup(&pstate->vc, enc, p_enc);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001679
1680 if (efile != NULL && (pstate->fd = mch_fopen((char *)efile, "r")) == NULL)
1681 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001682 semsg(_(e_cant_open_errorfile_str), efile);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001683 return FAIL;
1684 }
1685
1686 if (tv != NULL)
1687 {
1688 if (tv->v_type == VAR_STRING)
1689 pstate->p_str = tv->vval.v_string;
1690 else if (tv->v_type == VAR_LIST)
1691 pstate->p_li = tv->vval.v_list->lv_first;
1692 pstate->tv = tv;
1693 }
1694 pstate->buf = buf;
1695 pstate->buflnum = lnumfirst;
1696 pstate->lnumlast = lnumlast;
1697
1698 return OK;
1699}
1700
1701/*
1702 * Cleanup the state information used for parsing lines and populating a
1703 * quickfix list.
1704 */
1705 static void
1706qf_cleanup_state(qfstate_T *pstate)
1707{
1708 if (pstate->fd != NULL)
1709 fclose(pstate->fd);
1710
1711 vim_free(pstate->growbuf);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001712 if (pstate->vc.vc_type != CONV_NONE)
1713 convert_setup(&pstate->vc, NULL, NULL);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001714}
1715
1716/*
Bram Moolenaar95946f12019-03-31 15:31:59 +02001717 * Process the next line from a file/buffer/list/string and add it
1718 * to the quickfix list 'qfl'.
1719 */
1720 static int
1721qf_init_process_nextline(
1722 qf_list_T *qfl,
1723 efm_T *fmt_first,
1724 qfstate_T *state,
1725 qffields_T *fields)
1726{
1727 int status;
1728
1729 // Get the next line from a file/buffer/list/string
1730 status = qf_get_nextline(state);
1731 if (status != QF_OK)
1732 return status;
1733
1734 status = qf_parse_line(qfl, state->linebuf, state->linelen,
1735 fmt_first, fields);
1736 if (status != QF_OK)
1737 return status;
1738
1739 return qf_add_entry(qfl,
1740 qfl->qf_directory,
1741 (*fields->namebuf || qfl->qf_directory != NULL)
1742 ? fields->namebuf
1743 : ((qfl->qf_currfile != NULL && fields->valid)
1744 ? qfl->qf_currfile : (char_u *)NULL),
1745 fields->module,
Yegappan Lakshmananb7318002023-10-25 20:50:28 +02001746 fields->bnr,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001747 fields->errmsg,
1748 fields->lnum,
thinca6864efa2021-06-19 20:45:20 +02001749 fields->end_lnum,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001750 fields->col,
thinca6864efa2021-06-19 20:45:20 +02001751 fields->end_col,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001752 fields->use_viscol,
1753 fields->pattern,
1754 fields->enr,
1755 fields->type,
Tom Praschanca6ac992023-08-11 23:26:12 +02001756 fields->user_data,
Bram Moolenaar95946f12019-03-31 15:31:59 +02001757 fields->valid);
1758}
1759
1760/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00001761 * Read the errorfile "efile" into memory, line by line, building the error
1762 * list.
Bram Moolenaar864293a2016-06-02 13:40:04 +02001763 * Alternative: when "efile" is NULL read errors from buffer "buf".
1764 * Alternative: when "tv" is not NULL get errors from the string or list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001765 * Always use 'errorformat' from "buf" if there is a local value.
Bram Moolenaar7fd73202010-07-25 16:58:46 +02001766 * Then "lnumfirst" and "lnumlast" specify the range of lines to use.
1767 * Set the title of the list to "qf_title".
Bram Moolenaar86b68352004-12-27 21:59:20 +00001768 * Return -1 for error, number of errors for success.
1769 */
1770 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001771qf_init_ext(
1772 qf_info_T *qi,
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001773 int qf_idx,
Bram Moolenaar05540972016-01-30 20:31:25 +01001774 char_u *efile,
1775 buf_T *buf,
1776 typval_T *tv,
1777 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001778 int newlist, // TRUE: start a new error list
1779 linenr_T lnumfirst, // first line number to use
1780 linenr_T lnumlast, // last line number to use
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001781 char_u *qf_title,
1782 char_u *enc)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001783{
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001784 qf_list_T *qfl;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01001785 qfstate_T state;
1786 qffields_T fields;
Bram Moolenaar864293a2016-06-02 13:40:04 +02001787 qfline_T *old_last = NULL;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001788 int adding = FALSE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001789 static efm_T *fmt_first = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 char_u *efm;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001791 static char_u *last_efm = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001792 int retval = -1; // default: return error flag
Bram Moolenaare0d37972016-07-15 22:36:01 +02001793 int status;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001795 // Do not used the cached buffer, it may have been wiped out.
Bram Moolenaard23a8232018-02-10 18:45:26 +01001796 VIM_CLEAR(qf_last_bufname);
Bram Moolenaar6dd4a532017-05-28 07:56:36 +02001797
Bram Moolenaara80faa82020-04-12 19:37:17 +02001798 CLEAR_FIELD(state);
1799 CLEAR_FIELD(fields);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001800 if ((qf_alloc_fields(&fields) == FAIL) ||
1801 (qf_setup_state(&state, enc, efile, tv, buf,
1802 lnumfirst, lnumlast) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 goto qf_init_end;
1804
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001805 if (newlist || qf_idx == qi->qf_listcount)
1806 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001807 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01001808 qf_new_list(qi, qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001809 qf_idx = qi->qf_curlist;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001810 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001811 }
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001812 else
Bram Moolenaar864293a2016-06-02 13:40:04 +02001813 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001814 // Adding to existing list, use last entry.
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001815 adding = TRUE;
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01001816 qfl = qf_get_list(qi, qf_idx);
1817 if (!qf_list_empty(qfl))
1818 old_last = qfl->qf_last;
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001821 // Use the local value of 'errorformat' if it's set.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001822 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00001823 efm = buf->b_p_efm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 else
1825 efm = errorformat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001827 // If the errorformat didn't change between calls, then reuse the
1828 // previously parsed values.
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001829 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0))
1830 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001831 // free the previously parsed data
Bram Moolenaard23a8232018-02-10 18:45:26 +01001832 VIM_CLEAR(last_efm);
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001833 free_efm_list(&fmt_first);
1834
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001835 // parse the current 'efm'
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001836 fmt_first = parse_efm_option(efm);
1837 if (fmt_first != NULL)
1838 last_efm = vim_strsave(efm);
1839 }
1840
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001841 if (fmt_first == NULL) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001844 // got_int is reset here, because it was probably set when killing the
1845 // ":make" command, but we still want to read the errorfile then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 got_int = FALSE;
1847
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001848 // Read the lines in the error file one by one.
1849 // Try to recognize one of the error formats in each line.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001850 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
Bram Moolenaar95946f12019-03-31 15:31:59 +02001852 status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001853 if (status == QF_NOMEM) // memory alloc failure
Bram Moolenaare0d37972016-07-15 22:36:01 +02001854 goto qf_init_end;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001855 if (status == QF_END_OF_INPUT) // end of input
Bram Moolenaare0d37972016-07-15 22:36:01 +02001856 break;
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001857 if (status == QF_FAIL)
1858 goto error2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 line_breakcheck();
1861 }
Bram Moolenaare0d37972016-07-15 22:36:01 +02001862 if (state.fd == NULL || !ferror(state.fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001864 if (qfl->qf_index == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001866 // no valid entry found
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001867 qfl->qf_ptr = qfl->qf_start;
1868 qfl->qf_index = 1;
1869 qfl->qf_nonevalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 }
1871 else
1872 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001873 qfl->qf_nonevalid = FALSE;
1874 if (qfl->qf_ptr == NULL)
1875 qfl->qf_ptr = qfl->qf_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001877 // return number of matches
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001878 retval = qfl->qf_count;
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001879 goto qf_init_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 }
Bram Moolenaard8e44472021-07-21 22:20:33 +02001881 emsg(_(e_error_while_reading_errorfile));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882error2:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001883 if (!adding)
1884 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001885 // Error when creating a new list. Free the new list
Bram Moolenaar108e7b42018-10-11 17:39:12 +02001886 qf_free(qfl);
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001887 qi->qf_listcount--;
1888 if (qi->qf_curlist > 0)
1889 --qi->qf_curlist;
1890 }
Bram Moolenaarbcf77722016-06-28 21:11:32 +02001891qf_init_end:
Bram Moolenaara7df8c72017-07-19 13:23:06 +02001892 if (qf_idx == qi->qf_curlist)
1893 qf_update_buffer(qi, old_last);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001894 qf_cleanup_state(&state);
1895 qf_free_fields(&fields);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
1897 return retval;
1898}
1899
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001900/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001901 * Read the errorfile "efile" into memory, line by line, building the error
1902 * list. Set the error list's title to qf_title.
1903 * Return -1 for error, number of errors for success.
1904 */
1905 int
1906qf_init(win_T *wp,
1907 char_u *efile,
1908 char_u *errorformat,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02001909 int newlist, // TRUE: start a new error list
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001910 char_u *qf_title,
1911 char_u *enc)
1912{
64-bitman88d41ab2025-04-06 17:20:39 +02001913 qf_info_T *qi = ql_info;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001914
1915 if (wp != NULL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001916 qi = ll_get_or_alloc_list(wp);
64-bitman88d41ab2025-04-06 17:20:39 +02001917 if (qi == NULL)
1918 return FAIL;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02001919
1920 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat,
1921 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc);
1922}
1923
1924/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02001925 * Set the title of the specified quickfix list. Frees the previous title.
1926 * Prepends ':' to the title.
1927 */
Bram Moolenaarfb604092014-07-23 15:55:00 +02001928 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001929qf_store_title(qf_list_T *qfl, char_u *title)
Bram Moolenaarfb604092014-07-23 15:55:00 +02001930{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02001931 VIM_CLEAR(qfl->qf_title);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001932
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001933 if (title == NULL)
1934 return;
Bram Moolenaarfb604092014-07-23 15:55:00 +02001935
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00001936 char_u *p = alloc_id(STRLEN(title) + 2, aid_qf_title);
1937
1938 qfl->qf_title = p;
1939 if (p != NULL)
1940 STRCPY(p, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02001941}
1942
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943/*
Bram Moolenaar8b62e312018-05-13 15:29:04 +02001944 * The title of a quickfix/location list is set, by default, to the command
1945 * that created the quickfix list with the ":" prefix.
1946 * Create a quickfix list title string by prepending ":" to a user command.
1947 * Returns a pointer to a static buffer with the title.
1948 */
1949 static char_u *
1950qf_cmdtitle(char_u *cmd)
1951{
1952 static char_u qftitle_str[IOSIZE];
1953
1954 vim_snprintf((char *)qftitle_str, IOSIZE, ":%s", (char *)cmd);
1955 return qftitle_str;
1956}
1957
1958/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001959 * Return a pointer to the current list in the specified quickfix stack
1960 */
1961 static qf_list_T *
1962qf_get_curlist(qf_info_T *qi)
1963{
Bram Moolenaar0398e002019-03-21 21:12:49 +01001964 return qf_get_list(qi, qi->qf_curlist);
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01001965}
1966
1967/*
64-bitman88d41ab2025-04-06 17:20:39 +02001968 * Pop a quickfix list from the quickfix/location list stack
1969 * Automatically adjust qf_curlist so that it stays pointed
1970 * to the same list, unless it is deleted, if so then use the
1971 * newest created list instead. qf_listcount will be set correctly.
1972 * The above will only happen if <adjust> is TRUE.
1973 */
1974 static void
1975qf_pop_stack(qf_info_T *qi, int adjust)
1976{
1977 int i;
1978 qf_free(&qi->qf_lists[0]);
1979 for (i = 1; i < qi->qf_listcount; ++i)
1980 qi->qf_lists[i - 1] = qi->qf_lists[i];
1981
1982 // fill with zeroes now unused list at the top
1983 vim_memset(qi->qf_lists + qi->qf_listcount - 1, 0, sizeof(*qi->qf_lists));
1984
1985 if (adjust)
1986 {
1987 qi->qf_listcount--;
1988 if (qi->qf_curlist == 0)
1989 qi->qf_curlist = qi->qf_listcount - 1;
1990 else
1991 qi->qf_curlist--;
1992 }
1993}
1994
1995/*
Bram Moolenaar55b69262017-08-13 13:42:01 +02001996 * Prepare for adding a new quickfix list. If the current list is in the
1997 * middle of the stack, then all the following lists are freed and then
1998 * the new list is added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 */
2000 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002001qf_new_list(qf_info_T *qi, char_u *qf_title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002003 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002005 // If the current entry is not the last entry, delete entries beyond
2006 // the current entry. This makes it possible to browse in a tree-like
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002007 // way with ":grep".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002008 while (qi->qf_listcount > qi->qf_curlist + 1)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002009 qf_free(&qi->qf_lists[--qi->qf_listcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002011 // When the stack is full, remove to oldest entry
2012 // Otherwise, add a new entry.
64-bitman88d41ab2025-04-06 17:20:39 +02002013 if (qi->qf_listcount == qi->qf_maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
64-bitman88d41ab2025-04-06 17:20:39 +02002015 qf_pop_stack(qi, FALSE);
2016 qi->qf_curlist = qi->qf_listcount - 1; // point to new empty list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018 else
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002019 qi->qf_curlist = qi->qf_listcount++;
64-bitman88d41ab2025-04-06 17:20:39 +02002020
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01002021 qfl = qf_get_curlist(qi);
Bram Moolenaara80faa82020-04-12 19:37:17 +02002022 CLEAR_POINTER(qfl);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002023 qf_store_title(qfl, qf_title);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002024 qfl->qfl_type = qi->qfl_type;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002025 qfl->qf_id = ++last_qf_id;
Tom Praschanca6ac992023-08-11 23:26:12 +02002026 qfl->qf_has_user_data = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027}
2028
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002029/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002030 * Queue location list stack delete request.
2031 */
2032 static void
2033locstack_queue_delreq(qf_info_T *qi)
2034{
2035 qf_delq_T *q;
2036
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002037 q = ALLOC_ONE(qf_delq_T);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002038 if (q == NULL)
2039 return;
2040
2041 q->qi = qi;
2042 q->next = qf_delq_head;
2043 qf_delq_head = q;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002044}
2045
2046/*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002047 * Return the global quickfix stack window buffer number.
2048 */
2049 int
2050qf_stack_get_bufnr(void)
2051{
64-bitman88d41ab2025-04-06 17:20:39 +02002052 if (ql_info == NULL)
2053 return INVALID_QFBUFNR;
2054 return ql_info->qf_bufnr;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002055}
2056
2057/*
2058 * Wipe the quickfix window buffer (if present) for the specified
2059 * quickfix/location list.
2060 */
2061 static void
2062wipe_qf_buffer(qf_info_T *qi)
2063{
2064 buf_T *qfbuf;
2065
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002066 if (qi->qf_bufnr == INVALID_QFBUFNR)
2067 return;
2068
2069 qfbuf = buflist_findnr(qi->qf_bufnr);
2070 if (qfbuf != NULL && qfbuf->b_nwindows == 0)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002071 {
Christian Brabandtce80c592025-03-28 19:13:32 +01002072 int buf_was_null = FALSE;
2073 // can happen when curwin is going to be closed e.g. curwin->w_buffer
2074 // was already closed in win_close(), and we are now closing the
2075 // window related location list buffer from win_free_mem()
2076 // but close_buffer() calls CHECK_CURBUF() macro and requires
2077 // curwin->w_buffer == curbuf
2078 if (curwin->w_buffer == NULL)
2079 {
2080 curwin->w_buffer = curbuf;
2081 buf_was_null = TRUE;
2082 }
2083
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002084 // If the quickfix buffer is not loaded in any window, then
2085 // wipe the buffer.
2086 close_buffer(NULL, qfbuf, DOBUF_WIPE, FALSE, FALSE);
2087 qi->qf_bufnr = INVALID_QFBUFNR;
Christian Brabandtce80c592025-03-28 19:13:32 +01002088 if (buf_was_null)
2089 curwin->w_buffer = NULL;
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002090 }
2091}
2092
64-bitman88d41ab2025-04-06 17:20:39 +02002093
2094/*
2095 * Free all lists in the stack (not including the stack)
2096 */
2097 static void
2098qf_free_list_stack_items(qf_info_T *qi)
2099{
2100 for (int i = 0; i < qi->qf_listcount; ++i)
2101 qf_free(qf_get_list(qi, i));
2102}
2103
2104/*
2105 * Free a qf_ifo_T struct completely
2106 */
2107 static void
2108qf_free_lists(qf_info_T *qi)
2109{
2110 qf_free_list_stack_items(qi);
2111
2112 vim_free(qi->qf_lists);
2113 vim_free(qi);
2114}
2115
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002116/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002117 * Free a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002118 */
2119 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002120ll_free_all(qf_info_T **pqi)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002121{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002122 qf_info_T *qi;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002123
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002124 qi = *pqi;
2125 if (qi == NULL)
2126 return;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002127 *pqi = NULL; // Remove reference to this list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002128
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002129 // If the location list is still in use, then queue the delete request
2130 // to be processed later.
2131 if (quickfix_busy > 0)
2132 {
2133 locstack_queue_delreq(qi);
2134 return;
2135 }
2136
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002137 qi->qf_refcount--;
2138 if (qi->qf_refcount < 1)
2139 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002140 // No references to this location list.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002141 // If the quickfix window buffer is loaded, then wipe it
2142 wipe_qf_buffer(qi);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01002143
64-bitman88d41ab2025-04-06 17:20:39 +02002144 qf_free_lists(qi);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002145 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002146}
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002147
Bram Moolenaar18cebf42018-05-08 22:31:37 +02002148/*
2149 * Free all the quickfix/location lists in the stack.
2150 */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002151 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002152qf_free_all(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002153{
64-bitman88d41ab2025-04-06 17:20:39 +02002154 qf_info_T *qi = ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002155
2156 if (wp != NULL)
2157 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002158 // location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002159 ll_free_all(&wp->w_llist);
2160 ll_free_all(&wp->w_llist_ref);
2161 }
64-bitman88d41ab2025-04-06 17:20:39 +02002162 else if (qi != NULL)
2163 qf_free_list_stack_items(qi); // quickfix list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002164}
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002165
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166/*
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002167 * Delay freeing of location list stacks when the quickfix code is running.
2168 * Used to avoid problems with autocmds freeing location list stacks when the
2169 * quickfix code is still referencing the stack.
2170 * Must always call decr_quickfix_busy() exactly once after this.
2171 */
2172 static void
2173incr_quickfix_busy(void)
2174{
2175 quickfix_busy++;
2176}
2177
2178/*
2179 * Safe to free location list stacks. Process any delayed delete requests.
2180 */
2181 static void
2182decr_quickfix_busy(void)
2183{
2184 if (--quickfix_busy == 0)
2185 {
2186 // No longer referencing the location lists. Process all the pending
2187 // delete requests.
2188 while (qf_delq_head != NULL)
2189 {
2190 qf_delq_T *q = qf_delq_head;
2191
2192 qf_delq_head = q->next;
2193 ll_free_all(&q->qi);
2194 vim_free(q);
2195 }
2196 }
2197#ifdef ABORT_ON_INTERNAL_ERROR
2198 if (quickfix_busy < 0)
2199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002200 emsg("quickfix_busy has become negative");
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002201 abort();
2202 }
2203#endif
2204}
2205
2206#if defined(EXITFREE) || defined(PROTO)
2207 void
2208check_quickfix_busy(void)
2209{
2210 if (quickfix_busy != 0)
2211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002212 semsg("quickfix_busy not zero on exit: %ld", (long)quickfix_busy);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002213# ifdef ABORT_ON_INTERNAL_ERROR
2214 abort();
2215# endif
2216 }
2217}
2218#endif
2219
2220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 * Add an entry to the end of the list of errors.
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00002222 * Returns QF_OK on success or QF_FAIL on a memory allocation failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 */
2224 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002225qf_add_entry(
Bram Moolenaar0398e002019-03-21 21:12:49 +01002226 qf_list_T *qfl, // quickfix list entry
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002227 char_u *dir, // optional directory name
2228 char_u *fname, // file name or NULL
2229 char_u *module, // module name or NULL
2230 int bufnum, // buffer number or zero
2231 char_u *mesg, // message
2232 long lnum, // line number
thinca6864efa2021-06-19 20:45:20 +02002233 long end_lnum, // line number for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002234 int col, // column
thinca6864efa2021-06-19 20:45:20 +02002235 int end_col, // column for end
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002236 int vis_col, // using visual column
2237 char_u *pattern, // search pattern
2238 int nr, // error number
2239 int type, // type character
Tom Praschanca6ac992023-08-11 23:26:12 +02002240 typval_T *user_data, // custom user data or NULL
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002241 int valid) // valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242{
Austin Chang29822992024-10-03 10:50:05 +02002243 buf_T *buf;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002244 qfline_T *qfp;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002245 qfline_T **lastp; // pointer to qf_last or NULL
Austin Chang29822992024-10-03 10:50:05 +02002246 char_u *fullname = NULL;
2247 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002249 if ((qfp = ALLOC_ONE_ID(qfline_T, aid_qf_qfline)) == NULL)
Bram Moolenaar95946f12019-03-31 15:31:59 +02002250 return QF_FAIL;
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002251 if (bufnum != 0)
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002252 {
Austin Chang29822992024-10-03 10:50:05 +02002253 buf = buflist_findnr(bufnum);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002254
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002255 qfp->qf_fnum = bufnum;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002256 if (buf != NULL)
Bram Moolenaarc1542742016-07-20 21:44:37 +02002257 buf->b_has_qf_entry |=
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002258 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002259 }
Bram Moolenaar48b66fb2007-02-04 01:58:18 +00002260 else
Austin Chang29822992024-10-03 10:50:05 +02002261 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002262 qfp->qf_fnum = qf_get_fnum(qfl, dir, fname);
Austin Chang29822992024-10-03 10:50:05 +02002263 buf = buflist_findnr(qfp->qf_fnum);
2264 }
2265 if (fname != NULL)
2266 fullname = fix_fname(fname);
2267 qfp->qf_fname = NULL;
2268 if (buf != NULL &&
2269 buf->b_ffname != NULL && fullname != NULL)
2270 {
2271 if (fnamecmp(fullname, buf->b_ffname) != 0)
2272 {
2273 p = shorten_fname1(fullname);
2274 if (p != NULL)
2275 qfp->qf_fname = vim_strsave(p);
2276 }
2277 }
2278 vim_free(fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
2280 {
2281 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002282 return QF_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 }
2284 qfp->qf_lnum = lnum;
thinca6864efa2021-06-19 20:45:20 +02002285 qfp->qf_end_lnum = end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 qfp->qf_col = col;
thinca6864efa2021-06-19 20:45:20 +02002287 qfp->qf_end_col = end_col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002288 qfp->qf_viscol = vis_col;
Tom Praschanca6ac992023-08-11 23:26:12 +02002289 if (user_data == NULL || user_data->v_type == VAR_UNKNOWN)
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02002290 qfp->qf_user_data.v_type = VAR_UNKNOWN;
Tom Praschanca6ac992023-08-11 23:26:12 +02002291 else
2292 {
2293 copy_tv(user_data, &qfp->qf_user_data);
2294 qfl->qf_has_user_data = TRUE;
2295 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002296 if (pattern == NULL || *pattern == NUL)
2297 qfp->qf_pattern = NULL;
2298 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
2299 {
2300 vim_free(qfp->qf_text);
2301 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002302 return QF_FAIL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002303 }
Bram Moolenaard76ce852018-05-01 15:02:04 +02002304 if (module == NULL || *module == NUL)
2305 qfp->qf_module = NULL;
2306 else if ((qfp->qf_module = vim_strsave(module)) == NULL)
2307 {
2308 vim_free(qfp->qf_text);
2309 vim_free(qfp->qf_pattern);
2310 vim_free(qfp);
Bram Moolenaar95946f12019-03-31 15:31:59 +02002311 return QF_FAIL;
Bram Moolenaard76ce852018-05-01 15:02:04 +02002312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 qfp->qf_nr = nr;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002314 if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 type = 0;
2316 qfp->qf_type = type;
2317 qfp->qf_valid = valid;
2318
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002319 lastp = &qfl->qf_last;
Bram Moolenaar0398e002019-03-21 21:12:49 +01002320 if (qf_list_empty(qfl)) // first element in the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002322 qfl->qf_start = qfp;
2323 qfl->qf_ptr = qfp;
2324 qfl->qf_index = 0;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002325 qfp->qf_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327 else
2328 {
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002329 qfp->qf_prev = *lastp;
2330 (*lastp)->qf_next = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002332 qfp->qf_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 qfp->qf_cleared = FALSE;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02002334 *lastp = qfp;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002335 ++qfl->qf_count;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002336 if (qfl->qf_index == 0 && qfp->qf_valid) // first valid entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002338 qfl->qf_index = qfl->qf_count;
2339 qfl->qf_ptr = qfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341
Bram Moolenaar95946f12019-03-31 15:31:59 +02002342 return QF_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343}
2344
2345/*
64-bitman88d41ab2025-04-06 17:20:39 +02002346 * Resize global quickfix stack to be able to hold n amount of lists.
2347 * returns FAIL on failure and OK on success.
2348 */
2349 int
2350qf_resize_quickfix_stack(int n)
2351{
2352 if (ql_info == NULL)
2353 {
2354 emsg(_(e_no_quickfix_stack));
2355 return FAIL;
2356 }
2357
2358 if (qf_resize_stack(ql_info, n) == FAIL)
2359 return FAIL;
2360
2361 return OK;
2362}
2363
2364/*
2365 * Resize location list stack for window 'wp' to be able to
2366 * hold n amount of lists. Returns FAIL on failure and OK on success
2367 */
2368 int
2369ll_resize_stack(win_T *wp, int n)
2370{
2371 // check if current window is a location list window;
2372 // if so then sync its 'lhistory' to the parent window or vice versa
2373 if (IS_LL_WINDOW(curwin))
2374 qf_sync_llw_to_win(wp);
2375 else
2376 qf_sync_win_to_llw(wp);
2377
2378 qf_info_T *qi = ll_get_or_alloc_list(wp);
2379
2380 if (qf_resize_stack(qi, n) == FAIL)
2381 return FAIL;
2382
2383 return OK;
2384}
2385
2386/*
2387 * Resize quickfix stack to be able to hold n amount of quickfix lists.
2388 * Returns FAIL on failure and OK on success.
2389 */
2390 static int
2391qf_resize_stack(qf_info_T *qi, int n)
2392{
2393 qf_list_T *new;
2394 int amount_to_rm = 0, i;
2395 size_t lsz = sizeof(*qi->qf_lists);
2396
2397 if (n == qi->qf_maxcount)
2398 return OK;
2399 else if (n < qi->qf_maxcount && n < qi->qf_listcount)
2400 {
2401 // We have too many lists to store them all in the new stack,
2402 // pop lists until we can fit them all in the newly resized stack
2403 amount_to_rm = qi->qf_listcount - n;
2404
2405 for (i = 0; i < amount_to_rm; i++)
2406 qf_pop_stack(qi, TRUE);
2407 }
2408
2409 new = vim_realloc(qi->qf_lists, lsz * n);
2410
2411 if (new == NULL)
2412 return FAIL;
2413
2414 // fill with zeroes any newly allocated memory
2415 if (n > qi->qf_maxcount)
2416 vim_memset(new + qi->qf_maxcount, 0, lsz * (n - qi->qf_maxcount));
2417
2418 qi->qf_lists = new;
2419 qi->qf_maxcount = n;
2420
2421 qf_update_buffer(qi, NULL);
2422
2423 return OK;
2424}
2425
2426/*
2427 * Initialize global quickfix list, should only be called once.
2428 * Returns FAIL on failure and OK on success.
2429 */
2430 int
2431qf_init_quickfix_stack(void)
2432{
2433 ql_info_actual.qf_lists = qf_alloc_list_stack(p_chi);
2434
2435 if (ql_info_actual.qf_lists == NULL)
2436 return FAIL;
2437
2438 ql_info = &ql_info_actual;
2439 ql_info->qfl_type = QFLT_QUICKFIX;
2440 ql_info->qf_maxcount = p_chi;
2441 return OK;
2442}
2443
2444/*
2445 * Sync a location list window's 'lhistory' value to the parent window
2446 */
2447 static void
2448qf_sync_llw_to_win(win_T *llw)
2449{
2450 win_T *wp = qf_find_win_with_loclist(llw->w_llist_ref);
2451
2452 if (wp != NULL)
2453 wp->w_p_lhi = llw->w_p_lhi;
2454}
2455
2456/*
2457 * Sync a window's 'lhistory' value to its location list window, if any
2458 */
2459 static void
2460qf_sync_win_to_llw(win_T *pwp)
2461{
2462 win_T *wp;
2463 qf_info_T *llw = pwp->w_llist;
2464
2465 if (llw != NULL)
2466 FOR_ALL_WINDOWS(wp)
2467 if (wp->w_llist_ref == llw && bt_quickfix(wp->w_buffer))
2468 {
2469 wp->w_p_lhi = pwp->w_p_lhi;
2470 return;
2471 }
2472}
2473
2474/*
2475 * Allocate a new quickfix/location list stack that is able to hold
2476 * up to n amount of lists
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002477 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002478 static qf_info_T *
64-bitman88d41ab2025-04-06 17:20:39 +02002479qf_alloc_stack(qfltype_T qfltype, int n)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002480{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002481 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002482
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002483 qi = ALLOC_CLEAR_ONE_ID(qf_info_T, aid_qf_qfinfo);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002484 if (qi == NULL)
2485 return NULL;
2486
2487 qi->qf_refcount++;
2488 qi->qfl_type = qfltype;
2489 qi->qf_bufnr = INVALID_QFBUFNR;
64-bitman88d41ab2025-04-06 17:20:39 +02002490
2491 qi->qf_lists = qf_alloc_list_stack(n);
2492
2493 if (qi->qf_lists == NULL)
2494 {
2495 vim_free(qi);
2496 return NULL;
2497 }
2498
2499 qi->qf_maxcount = n;
2500
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002501 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002502}
2503
2504/*
64-bitman88d41ab2025-04-06 17:20:39 +02002505 * Allocate memory for qf_lists member of qf_info_T struct.
2506 * 'actual' is the actual amount of lists that have been allocated for
2507 * (only set when function returns sucessfully)
2508 */
2509 static qf_list_T *
2510qf_alloc_list_stack(int n)
2511{
2512 return ALLOC_CLEAR_MULT(qf_list_T, n);
2513}
2514
2515/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002516 * Return the location list stack for window 'wp'.
2517 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002518 */
2519 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002520ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002521{
2522 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002523 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002524 return wp->w_llist_ref;
2525
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002526 // For a non-location list window, w_llist_ref should not point to a
2527 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002528 ll_free_all(&wp->w_llist_ref);
2529
2530 if (wp->w_llist == NULL)
64-bitman88d41ab2025-04-06 17:20:39 +02002531 // new location list
2532 wp->w_llist = qf_alloc_stack(QFLT_LOCATION, wp->w_p_lhi);
2533
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002534 return wp->w_llist;
2535}
2536
2537/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002538 * Get the quickfix/location list stack to use for the specified Ex command.
2539 * For a location list command, returns the stack for the current window. If
2540 * the location list is not found, then returns NULL and prints an error
2541 * message if 'print_emsg' is TRUE.
2542 */
2543 static qf_info_T *
2544qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2545{
64-bitman88d41ab2025-04-06 17:20:39 +02002546 qf_info_T *qi = ql_info;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002547
2548 if (is_loclist_cmd(eap->cmdidx))
2549 {
2550 qi = GET_LOC_LIST(curwin);
2551 if (qi == NULL)
2552 {
2553 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002554 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002555 return NULL;
2556 }
2557 }
64-bitman88d41ab2025-04-06 17:20:39 +02002558 if (qi == NULL && print_emsg)
2559 emsg(_(e_no_quickfix_stack));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002560
2561 return qi;
2562}
2563
2564/*
2565 * Get the quickfix/location list stack to use for the specified Ex command.
2566 * For a location list command, returns the stack for the current window.
2567 * If the location list is not present, then allocates a new one.
2568 * Returns NULL if the allocation fails. For a location list command, sets
2569 * 'pwinp' to curwin.
2570 */
2571 static qf_info_T *
2572qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2573{
64-bitman88d41ab2025-04-06 17:20:39 +02002574 qf_info_T *qi = ql_info;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002575
2576 if (is_loclist_cmd(eap->cmdidx))
2577 {
2578 qi = ll_get_or_alloc_list(curwin);
2579 if (qi == NULL)
2580 return NULL;
2581 *pwinp = curwin;
2582 }
2583
2584 return qi;
2585}
2586
2587/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002588 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2589 */
2590 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002591copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002592{
2593 int i;
2594 qfline_T *from_qfp;
2595 qfline_T *prevp;
2596
2597 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002598 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002599 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002600 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002601 NULL,
2602 NULL,
2603 from_qfp->qf_module,
2604 0,
2605 from_qfp->qf_text,
2606 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002607 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002608 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002609 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002610 from_qfp->qf_viscol,
2611 from_qfp->qf_pattern,
2612 from_qfp->qf_nr,
2613 0,
Tom Praschanca6ac992023-08-11 23:26:12 +02002614 &from_qfp->qf_user_data,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002615 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002616 return FAIL;
2617
2618 // qf_add_entry() will not set the qf_num field, as the
2619 // directory and file names are not supplied. So the qf_fnum
2620 // field is copied here.
2621 prevp = to_qfl->qf_last;
2622 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2623 prevp->qf_type = from_qfp->qf_type; // error type
2624 if (from_qfl->qf_ptr == from_qfp)
2625 to_qfl->qf_ptr = prevp; // current location
2626 }
2627
2628 return OK;
2629}
2630
2631/*
2632 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2633 */
2634 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002635copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002636{
2637 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002638 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002639 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
Tom Praschanca6ac992023-08-11 23:26:12 +02002640 to_qfl->qf_has_user_data = from_qfl->qf_has_user_data;
Bram Moolenaar09037502018-09-25 22:08:14 +02002641 to_qfl->qf_count = 0;
2642 to_qfl->qf_index = 0;
2643 to_qfl->qf_start = NULL;
2644 to_qfl->qf_last = NULL;
2645 to_qfl->qf_ptr = NULL;
2646 if (from_qfl->qf_title != NULL)
2647 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2648 else
2649 to_qfl->qf_title = NULL;
2650 if (from_qfl->qf_ctx != NULL)
2651 {
2652 to_qfl->qf_ctx = alloc_tv();
2653 if (to_qfl->qf_ctx != NULL)
2654 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2655 }
2656 else
2657 to_qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002658 if (from_qfl->qf_qftf_cb.cb_name != NULL)
2659 copy_callback(&to_qfl->qf_qftf_cb, &from_qfl->qf_qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002660 else
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002661 to_qfl->qf_qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002662
2663 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002664 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002665 return FAIL;
2666
2667 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2668
2669 // Assign a new ID for the location list
2670 to_qfl->qf_id = ++last_qf_id;
2671 to_qfl->qf_changedtick = 0L;
2672
2673 // When no valid entries are present in the list, qf_ptr points to
2674 // the first item in the list
2675 if (to_qfl->qf_nonevalid)
2676 {
2677 to_qfl->qf_ptr = to_qfl->qf_start;
2678 to_qfl->qf_index = 1;
2679 }
2680
2681 return OK;
2682}
2683
2684/*
2685 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002686 */
2687 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002688copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002689{
2690 qf_info_T *qi;
2691 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002692
Bram Moolenaar09037502018-09-25 22:08:14 +02002693 // When copying from a location list window, copy the referenced
2694 // location list. For other windows, copy the location list for
2695 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002696 if (IS_LL_WINDOW(from))
2697 qi = from->w_llist_ref;
2698 else
2699 qi = from->w_llist;
2700
Bram Moolenaar09037502018-09-25 22:08:14 +02002701 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002702 return;
2703
64-bitman88d41ab2025-04-06 17:20:39 +02002704 // allocate a new location list, set size of stack to 'from' window value
2705 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION, from->w_p_lhi)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002706 return;
64-bitman88d41ab2025-04-06 17:20:39 +02002707 else
2708 // set 'to' lhi to reflect new value
2709 to->w_p_lhi = to->w_llist->qf_maxcount;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002710
2711 to->w_llist->qf_listcount = qi->qf_listcount;
2712
Bram Moolenaar09037502018-09-25 22:08:14 +02002713 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002714 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002715 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002716 to->w_llist->qf_curlist = idx;
2717
Bram Moolenaar0398e002019-03-21 21:12:49 +01002718 if (copy_loclist(qf_get_list(qi, idx),
2719 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002720 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002721 qf_free_all(to);
2722 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002723 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002724 }
2725
Bram Moolenaar09037502018-09-25 22:08:14 +02002726 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002727}
2728
2729/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002730 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002731 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 */
2733 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002734qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735{
Bram Moolenaar82404332016-07-10 17:00:38 +02002736 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002737 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002738 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002739
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002740 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
Bram Moolenaare60acc12011-05-10 16:41:25 +02002743#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002744 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002745#endif
2746#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002747 if (directory != NULL)
2748 slash_adjust(directory);
2749 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002750#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002751 if (directory != NULL && !vim_isAbsName(fname)
2752 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2753 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002754 // Here we check if the file really exists.
2755 // This should normally be true, but if make works without
2756 // "leaving directory"-messages we might have missed a
2757 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002758 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002761 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002762 if (directory)
2763 ptr = concat_fnames(directory, fname, TRUE);
2764 else
2765 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002767 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002768 bufname = ptr;
2769 }
2770 else
2771 bufname = fname;
2772
2773 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002774 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002775 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002776 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002777 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002779 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002780 {
2781 vim_free(qf_last_bufname);
2782 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2783 if (bufname == ptr)
2784 qf_last_bufname = bufname;
2785 else
2786 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002787 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002788 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002789 if (buf == NULL)
2790 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002791
Bram Moolenaarc1542742016-07-20 21:44:37 +02002792 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002793 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002794 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795}
2796
2797/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002798 * Push dirbuf onto the directory stack and return pointer to actual dir or
2799 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 */
2801 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002802qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803{
2804 struct dir_stack_T *ds_new;
2805 struct dir_stack_T *ds_ptr;
2806
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002807 // allocate new stack element and hook it in
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002808 ds_new = ALLOC_ONE_ID(struct dir_stack_T, aid_qf_dirstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 if (ds_new == NULL)
2810 return NULL;
2811
2812 ds_new->next = *stackptr;
2813 *stackptr = ds_new;
2814
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002815 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 if (vim_isAbsName(dirbuf)
2817 || (*stackptr)->next == NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=dd410372022-05-15 13:59:11 +01002818 || is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 (*stackptr)->dirname = vim_strsave(dirbuf);
2820 else
2821 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002822 // Okay we don't have an absolute path.
2823 // dirbuf must be a subdir of one of the directories on the stack.
2824 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 ds_new = (*stackptr)->next;
2826 (*stackptr)->dirname = NULL;
2827 while (ds_new)
2828 {
2829 vim_free((*stackptr)->dirname);
2830 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2831 TRUE);
2832 if (mch_isdir((*stackptr)->dirname) == TRUE)
2833 break;
2834
2835 ds_new = ds_new->next;
2836 }
2837
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002838 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 while ((*stackptr)->next != ds_new)
2840 {
2841 ds_ptr = (*stackptr)->next;
2842 (*stackptr)->next = (*stackptr)->next->next;
2843 vim_free(ds_ptr->dirname);
2844 vim_free(ds_ptr);
2845 }
2846
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002847 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 if (ds_new == NULL)
2849 {
2850 vim_free((*stackptr)->dirname);
2851 (*stackptr)->dirname = vim_strsave(dirbuf);
2852 }
2853 }
2854
2855 if ((*stackptr)->dirname != NULL)
2856 return (*stackptr)->dirname;
2857 else
2858 {
2859 ds_ptr = *stackptr;
2860 *stackptr = (*stackptr)->next;
2861 vim_free(ds_ptr);
2862 return NULL;
2863 }
2864}
2865
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866/*
2867 * pop dirbuf from the directory stack and return previous directory or NULL if
2868 * stack is empty
2869 */
2870 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002871qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872{
2873 struct dir_stack_T *ds_ptr;
2874
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002875 // TODO: Should we check if dirbuf is the directory on top of the stack?
2876 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002878 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 if (*stackptr != NULL)
2880 {
2881 ds_ptr = *stackptr;
2882 *stackptr = (*stackptr)->next;
2883 vim_free(ds_ptr->dirname);
2884 vim_free(ds_ptr);
2885 }
2886
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002887 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 return *stackptr ? (*stackptr)->dirname : NULL;
2889}
2890
2891/*
2892 * clean up directory stack
2893 */
2894 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002895qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896{
2897 struct dir_stack_T *ds_ptr;
2898
2899 while ((ds_ptr = *stackptr) != NULL)
2900 {
2901 *stackptr = (*stackptr)->next;
2902 vim_free(ds_ptr->dirname);
2903 vim_free(ds_ptr);
2904 }
2905}
2906
2907/*
2908 * Check in which directory of the directory stack the given file can be
2909 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002910 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 * Cleans up intermediate directory entries.
2912 *
2913 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002914 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 * ./
2916 * ./aa
2917 * ./aa/bb
2918 * ./bb
2919 * ./bb/x.c
2920 * and make says:
2921 * making all in aa
2922 * making all in bb
2923 * x.c:9: Error
2924 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2925 * qf_guess_filepath will return NULL.
2926 */
2927 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002928qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929{
2930 struct dir_stack_T *ds_ptr;
2931 struct dir_stack_T *ds_tmp;
2932 char_u *fullname;
2933
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002934 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002935 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 return NULL;
2937
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002938 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 fullname = NULL;
2940 while (ds_ptr)
2941 {
2942 vim_free(fullname);
2943 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2944
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002945 // If concat_fnames failed, just go on. The worst thing that can happen
2946 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2948 break;
2949
2950 ds_ptr = ds_ptr->next;
2951 }
2952
2953 vim_free(fullname);
2954
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002955 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002956 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002958 ds_tmp = qfl->qf_dir_stack->next;
2959 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 vim_free(ds_tmp->dirname);
2961 vim_free(ds_tmp);
2962 }
2963
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002964 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965}
2966
2967/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002968 * Returns TRUE if a quickfix/location list with the given identifier exists.
2969 */
2970 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002971qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002972{
64-bitman88d41ab2025-04-06 17:20:39 +02002973 qf_info_T *qi = ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002974 int i;
2975
2976 if (wp != NULL)
2977 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002978 if (!win_valid(wp))
2979 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002980 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002981 }
64-bitman88d41ab2025-04-06 17:20:39 +02002982 if (qi == NULL)
2983 return FALSE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002984
2985 for (i = 0; i < qi->qf_listcount; ++i)
2986 if (qi->qf_lists[i].qf_id == qf_id)
2987 return TRUE;
2988
2989 return FALSE;
2990}
2991
2992/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002993 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002994 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002995 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002996 * Similar to location list.
2997 */
2998 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002999is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003000{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003001 qfline_T *qfp;
3002 int i;
3003
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003004 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01003005 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
3006 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003007 break;
3008
Bram Moolenaar95946f12019-03-31 15:31:59 +02003009 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003010 return FALSE;
3011
3012 return TRUE;
3013}
3014
3015/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003016 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003017 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003018 */
3019 static qfline_T *
3020get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003021 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003022 qfline_T *qf_ptr,
3023 int *qf_index,
3024 int dir)
3025{
3026 int idx;
3027 int old_qf_fnum;
3028
3029 idx = *qf_index;
3030 old_qf_fnum = qf_ptr->qf_fnum;
3031
3032 do
3033 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003034 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003035 return NULL;
3036 ++idx;
3037 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003038 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003039 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
3040
3041 *qf_index = idx;
3042 return qf_ptr;
3043}
3044
3045/*
3046 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003047 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003048 */
3049 static qfline_T *
3050get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003051 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003052 qfline_T *qf_ptr,
3053 int *qf_index,
3054 int dir)
3055{
3056 int idx;
3057 int old_qf_fnum;
3058
3059 idx = *qf_index;
3060 old_qf_fnum = qf_ptr->qf_fnum;
3061
3062 do
3063 {
3064 if (idx == 1 || qf_ptr->qf_prev == NULL)
3065 return NULL;
3066 --idx;
3067 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003068 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003069 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
3070
3071 *qf_index = idx;
3072 return qf_ptr;
3073}
3074
3075/*
3076 * Get the n'th (errornr) previous/next valid entry from the current entry in
3077 * the quickfix list.
3078 * dir == FORWARD or FORWARD_FILE: next valid entry
3079 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
3080 */
3081 static qfline_T *
3082get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003083 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003084 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003085 int dir,
3086 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003087{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003088 qfline_T *qf_ptr = qfl->qf_ptr;
3089 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003090 qfline_T *prev_qf_ptr;
3091 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003092 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003093
3094 while (errornr--)
3095 {
3096 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003097 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003098
3099 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003100 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003101 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003102 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003103 if (qf_ptr == NULL)
3104 {
3105 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003106 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003107 if (err != NULL)
3108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003109 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003110 return NULL;
3111 }
3112 break;
3113 }
3114
3115 err = NULL;
3116 }
3117
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003118 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003119 return qf_ptr;
3120}
3121
3122/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003123 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
3124 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003125 */
3126 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003127get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003128{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003129 qfline_T *qf_ptr = qfl->qf_ptr;
3130 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003131
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003132 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003133 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
3134 {
3135 --qf_idx;
3136 qf_ptr = qf_ptr->qf_prev;
3137 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003138 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003139 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
3140 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003141 {
3142 ++qf_idx;
3143 qf_ptr = qf_ptr->qf_next;
3144 }
3145
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003146 *new_qfidx = qf_idx;
3147 return qf_ptr;
3148}
3149
3150/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003151 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003152 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
3153 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
3154 * Returns a pointer to the entry and the index of the new entry is stored in
3155 * 'new_qfidx'.
3156 */
3157 static qfline_T *
3158qf_get_entry(
3159 qf_list_T *qfl,
3160 int errornr,
3161 int dir,
3162 int *new_qfidx)
3163{
3164 qfline_T *qf_ptr = qfl->qf_ptr;
3165 int qfidx = qfl->qf_index;
3166
3167 if (dir != 0) // next/prev valid entry
3168 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
3169 else if (errornr != 0) // go to specified number
3170 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
3171
3172 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003173 return qf_ptr;
3174}
3175
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003176/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003177 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003178 */
3179 static win_T *
3180qf_find_help_win(void)
3181{
3182 win_T *wp;
3183
3184 FOR_ALL_WINDOWS(wp)
3185 if (bt_help(wp->w_buffer))
3186 return wp;
3187
3188 return NULL;
3189}
3190
3191/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003192 * Set the location list for the specified window to 'qi'.
3193 */
3194 static void
3195win_set_loclist(win_T *wp, qf_info_T *qi)
3196{
3197 wp->w_llist = qi;
3198 qi->qf_refcount++;
3199}
3200
3201/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01003202 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
3203 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003204 */
3205 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003206jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003207{
3208 win_T *wp;
3209 int flags;
3210
Bram Moolenaare1004402020-10-24 20:49:43 +02003211 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003212 wp = NULL;
3213 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003214 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003215 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
3216 win_enter(wp, TRUE);
3217 else
3218 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003219 // Split off help window; put it at far top if no position
3220 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003221 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02003222 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003223 && curwin->w_width < 80)
3224 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003225 // If the user asks to open a new window, then copy the location list.
3226 // Otherwise, don't copy the location list.
3227 if (IS_LL_STACK(qi) && !newwin)
3228 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003229
3230 if (win_split(0, flags) == FAIL)
3231 return FAIL;
3232
3233 *opened_window = TRUE;
3234
3235 if (curwin->w_height < p_hh)
3236 win_setheight((int)p_hh);
3237
Bram Moolenaarb2443732018-11-11 22:50:27 +01003238 // When using location list, the new window should use the supplied
3239 // location list. If the user asks to open a new window, then the new
3240 // window will get a copy of the location list.
3241 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003242 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003243 }
3244
3245 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003246 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003247
3248 return OK;
3249}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003250
3251/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003252 * Find a non-quickfix window using the given location list stack in the
3253 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003254 * Returns NULL if a matching window is not found.
3255 */
3256 static win_T *
3257qf_find_win_with_loclist(qf_info_T *ll)
3258{
3259 win_T *wp;
3260
3261 FOR_ALL_WINDOWS(wp)
3262 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
3263 return wp;
3264
3265 return NULL;
3266}
3267
3268/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003269 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003270 */
3271 static win_T *
3272qf_find_win_with_normal_buf(void)
3273{
3274 win_T *wp;
3275
3276 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02003277 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003278 return wp;
3279
3280 return NULL;
3281}
3282
3283/*
3284 * Go to a window in any tabpage containing the specified file. Returns TRUE
3285 * if successfully jumped to the window. Otherwise returns FALSE.
3286 */
3287 static int
3288qf_goto_tabwin_with_file(int fnum)
3289{
3290 tabpage_T *tp;
3291 win_T *wp;
3292
3293 FOR_ALL_TAB_WINDOWS(tp, wp)
3294 if (wp->w_buffer->b_fnum == fnum)
3295 {
3296 goto_tabpage_win(tp, wp);
3297 return TRUE;
3298 }
3299
3300 return FALSE;
3301}
3302
3303/*
3304 * Create a new window to show a file above the quickfix window. Called when
3305 * only the quickfix window is present.
3306 */
3307 static int
3308qf_open_new_file_win(qf_info_T *ll_ref)
3309{
3310 int flags;
3311
3312 flags = WSP_ABOVE;
3313 if (ll_ref != NULL)
3314 flags |= WSP_NEWLOC;
3315 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003316 return FAIL; // not enough room for window
3317 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003318 swb_flags = 0;
3319 RESET_BINDING(curwin);
3320 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003321 // The new window should use the location list from the
3322 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003323 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003324 return OK;
3325}
3326
3327/*
3328 * Go to a window that shows the right buffer. If the window is not found, go
3329 * to the window just above the location list window. This is used for opening
3330 * a file from a location window and not from a quickfix window. If some usable
3331 * window is previously found, then it is supplied in 'use_win'.
3332 */
3333 static void
3334qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
3335{
3336 win_T *win = use_win;
3337
3338 if (win == NULL)
3339 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003340 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003341 FOR_ALL_WINDOWS(win)
3342 if (win->w_buffer->b_fnum == qf_fnum)
3343 break;
3344 if (win == NULL)
3345 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003346 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003347 win = curwin;
3348 do
3349 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02003350 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003351 break;
3352 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003353 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003354 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003355 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003356 } while (win != curwin);
3357 }
3358 }
3359 win_goto(win);
3360
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003361 // If the location list for the window is not set, then set it
3362 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003363 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003364 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003365}
3366
3367/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003368 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3369 * not found, then go to the window just above the quickfix window. This is
3370 * used for opening a file from a quickfix window and not from a location
3371 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003372 */
3373 static void
3374qf_goto_win_with_qfl_file(int qf_fnum)
3375{
3376 win_T *win;
3377 win_T *altwin;
3378
3379 win = curwin;
3380 altwin = NULL;
3381 for (;;)
3382 {
3383 if (win->w_buffer->b_fnum == qf_fnum)
3384 break;
3385 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003386 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003387 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003388 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003389
3390 if (IS_QF_WINDOW(win))
3391 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003392 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003393 // window, unless 'switchbuf' contains 'uselast': in this case we
3394 // try to jump to the previously used window first.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003395 if ((swb_flags & SWB_USELAST) && win_valid(prevwin) &&
3396 !prevwin->w_p_wfb)
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003397 win = prevwin;
3398 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003399 win = altwin;
3400 else if (curwin->w_prev != NULL)
3401 win = curwin->w_prev;
3402 else
3403 win = curwin->w_next;
3404 break;
3405 }
3406
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003407 // Remember a usable window.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003408 if (altwin == NULL && !win->w_p_pvw && !win->w_p_wfb &&
3409 bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003410 altwin = win;
3411 }
3412
3413 win_goto(win);
3414}
3415
3416/*
3417 * Find a suitable window for opening a file (qf_fnum) from the
3418 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003419 * window, jump to it. Otherwise open a new window to display the file. If
3420 * 'newwin' is TRUE, then always open a new window. This is called from either
3421 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003422 */
3423 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003424qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003425{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003426 win_T *usable_wp = NULL;
3427 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003428 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003429
Bram Moolenaarb2443732018-11-11 22:50:27 +01003430 // If opening a new window, then don't use the location list referred by
3431 // the current window. Otherwise two windows will refer to the same
3432 // location list.
3433 if (!newwin)
3434 ll_ref = curwin->w_llist_ref;
3435
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003436 if (ll_ref != NULL)
3437 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003438 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003439 usable_wp = qf_find_win_with_loclist(ll_ref);
3440 if (usable_wp != NULL)
3441 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003442 }
3443
3444 if (!usable_win)
3445 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003446 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003447 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003448 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003449 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003450 }
3451
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003452 // If no usable window is found and 'switchbuf' contains "usetab"
3453 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003454 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003455 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003456
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003457 // If there is only one window and it is the quickfix window, create a
3458 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003459 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003460 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003461 if (qf_open_new_file_win(ll_ref) != OK)
3462 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003463 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003464 }
3465 else
3466 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003467 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003468 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003469 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003470 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003471 }
3472
3473 return OK;
3474}
3475
3476/*
3477 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003478 * Returns OK if successfully edited the file, FAIL on failing to open the
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003479 * buffer and QF_ABORT if the quickfix/location list was freed by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003480 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003481 */
3482 static int
3483qf_jump_edit_buffer(
3484 qf_info_T *qi,
3485 qfline_T *qf_ptr,
3486 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003487 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003488 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003489{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003490 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003491 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003492 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003493 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003494 int old_qf_curlist = qi->qf_curlist;
3495 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003496
3497 if (qf_ptr->qf_type == 1)
3498 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003499 // Open help file (do_ecmd() will set b_help flag, readfile() will
3500 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003501 if (!can_abandon(curbuf, forceit))
3502 {
3503 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003504 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003505 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003506
3507 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3508 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003509 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003510 }
3511 else
Colin Kennedy21570352024-03-03 16:16:47 +01003512 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003513 int fnum = qf_ptr->qf_fnum;
3514
3515 if (!forceit && curwin->w_p_wfb && curbuf->b_fnum != fnum)
Colin Kennedy21570352024-03-03 16:16:47 +01003516 {
3517 if (qi->qfl_type == QFLT_LOCATION)
3518 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003519 // Location lists cannot split or reassign their window
3520 // so 'winfixbuf' windows must fail
3521 emsg(_(e_winfixbuf_cannot_go_to_buffer));
3522 return FAIL;
Colin Kennedy21570352024-03-03 16:16:47 +01003523 }
3524
Sean Dewar4bb505e2024-03-05 20:39:07 +01003525 if (win_valid(prevwin) && !prevwin->w_p_wfb &&
3526 !bt_quickfix(prevwin->w_buffer))
Colin Kennedy21570352024-03-03 16:16:47 +01003527 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003528 // 'winfixbuf' is set; attempt to change to a window without it
3529 // that isn't a quickfix/location list window.
3530 win_goto(prevwin);
3531 }
3532 if (curwin->w_p_wfb)
3533 {
3534 // Split the window, which will be 'nowinfixbuf', and set curwin
3535 // to that
3536 if (win_split(0, 0) == OK)
3537 *opened_window = TRUE;
3538
3539 if (curwin->w_p_wfb)
3540 {
3541 // Autocommands set 'winfixbuf' or sent us to another window
Sean Dewar769eb2d2024-03-07 21:37:50 +01003542 // with it set, or we failed to split the window. Give up,
3543 // but don't return immediately, as they may have messed
3544 // with the list.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003545 emsg(_(e_winfixbuf_cannot_go_to_buffer));
3546 retval = FAIL;
3547 }
Colin Kennedy21570352024-03-03 16:16:47 +01003548 }
3549 }
3550
Sean Dewar4bb505e2024-03-05 20:39:07 +01003551 if (retval == OK)
3552 {
3553 retval = buflist_getfile(fnum,
3554 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
3555 }
Colin Kennedy21570352024-03-03 16:16:47 +01003556 }
Bram Moolenaar3c097222017-12-21 20:54:49 +01003557
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003558 // If a location list, check whether the associated window is still
3559 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003560 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003561 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003562 win_T *wp = win_id2wp(prev_winid);
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003563
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003564 if (wp == NULL && curwin->w_llist != qi)
3565 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003566 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003567 *opened_window = FALSE;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003568 return QF_ABORT;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003569 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003570 }
3571
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003572 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003573 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003574 emsg(_(e_current_quickfix_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003575 return QF_ABORT;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003576 }
3577
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003578 // Check if the list was changed. The pointers may happen to be identical,
3579 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003580 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003581 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003582 || !is_qf_entry_present(qfl, qf_ptr))
3583 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003584 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003585 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003586 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003587 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003588 return QF_ABORT;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003589 }
3590
3591 return retval;
3592}
3593
3594/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003595 * Go to the error line in the current file using either line/column number or
3596 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003597 */
3598 static void
3599qf_jump_goto_line(
3600 linenr_T qf_lnum,
3601 int qf_col,
3602 char_u qf_viscol,
3603 char_u *qf_pattern)
3604{
3605 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003606
3607 if (qf_pattern == NULL)
3608 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003609 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003610 i = qf_lnum;
3611 if (i > 0)
3612 {
3613 if (i > curbuf->b_ml.ml_line_count)
3614 i = curbuf->b_ml.ml_line_count;
3615 curwin->w_cursor.lnum = i;
3616 }
3617 if (qf_col > 0)
3618 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003619 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003620 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003621 coladvance(qf_col - 1);
3622 else
3623 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003624 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003625 check_cursor();
3626 }
3627 else
3628 beginline(BL_WHITE | BL_FIX);
3629 }
3630 else
3631 {
3632 pos_T save_cursor;
3633
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003634 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003635 save_cursor = curwin->w_cursor;
3636 curwin->w_cursor.lnum = 0;
John Marriott8c85a2a2024-05-20 19:18:26 +02003637 if (!do_search(NULL, '/', '/', qf_pattern, STRLEN(qf_pattern), (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003638 curwin->w_cursor = save_cursor;
3639 }
3640}
3641
3642/*
3643 * Display quickfix list index and size message
3644 */
3645 static void
3646qf_jump_print_msg(
3647 qf_info_T *qi,
3648 int qf_index,
3649 qfline_T *qf_ptr,
3650 buf_T *old_curbuf,
3651 linenr_T old_lnum)
3652{
3653 linenr_T i;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003654 garray_T *gap;
3655
3656 gap = qfga_get();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003657
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003658 // Update the screen before showing the message, unless the screen
3659 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003660 if (!msg_scrolled)
3661 update_topline_redraw();
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003662 vim_snprintf((char *)IObuff, IOSIZE, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003663 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003664 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3665 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003666 // Add the message, skipping leading whitespace and newlines.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003667 ga_concat(gap, IObuff);
3668 qf_fmt_text(gap, skipwhite(qf_ptr->qf_text));
Shane Harper5bf04282023-06-07 19:09:57 +01003669 ga_append(gap, NUL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003670
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003671 // Output the message. Overwrite to avoid scrolling when the 'O'
3672 // flag is present in 'shortmess'; But when not jumping, print the
3673 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003674 i = msg_scroll;
3675 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3676 msg_scroll = TRUE;
3677 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3678 msg_scroll = FALSE;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003679 msg_attr_keep((char *)gap->ga_data, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003680 msg_scroll = i;
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01003681
3682 qfga_clear();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003683}
3684
3685/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003686 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003687 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3688 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003689 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3690 * able to jump/open a window. Returns NOTDONE if a file is not associated
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003691 * with the entry. Returns QF_ABORT if the quickfix/location list was modified
3692 * by an autocmd.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003693 */
3694 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003695qf_jump_open_window(
3696 qf_info_T *qi,
3697 qfline_T *qf_ptr,
3698 int newwin,
3699 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003700{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003701 qf_list_T *qfl = qf_get_curlist(qi);
3702 int old_changedtick = qfl->qf_changedtick;
3703 int old_qf_curlist = qi->qf_curlist;
3704 qfltype_T qfl_type = qfl->qfl_type;
3705
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003706 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003707 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3708 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003709 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003710 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003711 if (old_qf_curlist != qi->qf_curlist
3712 || old_changedtick != qfl->qf_changedtick
3713 || !is_qf_entry_present(qfl, qf_ptr))
3714 {
3715 if (qfl_type == QFLT_QUICKFIX)
3716 emsg(_(e_current_quickfix_list_was_changed));
3717 else
3718 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003719 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003720 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003721
3722 // If currently in the quickfix window, find another window to show the
3723 // file in.
3724 if (bt_quickfix(curbuf) && !*opened_window)
3725 {
3726 // If there is no file specified, we don't know where to go.
3727 // But do advance, otherwise ":cn" gets stuck.
3728 if (qf_ptr->qf_fnum == 0)
3729 return NOTDONE;
3730
Bram Moolenaarb2443732018-11-11 22:50:27 +01003731 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3732 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003733 return FAIL;
3734 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003735 if (old_qf_curlist != qi->qf_curlist
3736 || old_changedtick != qfl->qf_changedtick
3737 || !is_qf_entry_present(qfl, qf_ptr))
3738 {
3739 if (qfl_type == QFLT_QUICKFIX)
3740 emsg(_(e_current_quickfix_list_was_changed));
3741 else
3742 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003743 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003744 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003745
3746 return OK;
3747}
3748
3749/*
3750 * Edit a selected file from the quickfix/location list and jump to a
3751 * particular line/column, adjust the folds and display a message about the
3752 * jump.
3753 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003754 * QF_ABORT if the quickfix/location list is freed by an autocmd when opening
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003755 * the file.
3756 */
3757 static int
3758qf_jump_to_buffer(
3759 qf_info_T *qi,
3760 int qf_index,
3761 qfline_T *qf_ptr,
3762 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003763 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003764 int *opened_window,
3765 int openfold,
3766 int print_message)
3767{
3768 buf_T *old_curbuf;
3769 linenr_T old_lnum;
3770 int retval = OK;
3771
3772 // If there is a file name, read the wanted file if needed, and check
3773 // autowrite etc.
3774 old_curbuf = curbuf;
3775 old_lnum = curwin->w_cursor.lnum;
3776
3777 if (qf_ptr->qf_fnum != 0)
3778 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003779 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003780 opened_window);
3781 if (retval != OK)
3782 return retval;
3783 }
3784
3785 // When not switched to another buffer, still need to set pc mark
3786 if (curbuf == old_curbuf)
3787 setpcmark();
3788
3789 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3790 qf_ptr->qf_pattern);
3791
3792#ifdef FEAT_FOLDING
3793 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3794 foldOpenCursor();
3795#endif
3796 if (print_message)
3797 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3798
3799 return retval;
3800}
3801
3802/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003803 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 */
3805 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003806qf_jump(qf_info_T *qi,
3807 int dir,
3808 int errornr,
3809 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003811 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3812}
3813
3814/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003815 * Jump to a quickfix line.
3816 * If dir == 0 go to entry "errornr".
3817 * If dir == FORWARD go "errornr" valid entries forward.
3818 * If dir == BACKWARD go "errornr" valid entries backward.
3819 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3820 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3821 * else if "errornr" is zero, redisplay the same line
3822 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003823 * If 'newwin' is TRUE, then open the file in a new window.
3824 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003825 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003826qf_jump_newwin(qf_info_T *qi,
3827 int dir,
3828 int errornr,
3829 int forceit,
3830 int newwin)
3831{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003832 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003833 qfline_T *qf_ptr;
3834 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003837 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003838 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003839 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003842 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003843 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003845 if (qi == NULL)
64-bitman88d41ab2025-04-06 17:20:39 +02003846 {
3847 if (ql_info == NULL)
3848 {
3849 emsg(_(e_no_quickfix_stack));
3850 return;
3851 }
3852 qi = ql_info;
3853 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003854
Bram Moolenaar0398e002019-03-21 21:12:49 +01003855 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003857 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 return;
3859 }
3860
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003861 incr_quickfix_busy();
3862
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003863 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003864
3865 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003867 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003869
3870 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3871 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003873 qf_ptr = old_qf_ptr;
3874 qf_index = old_qf_index;
3875 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003878 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003879 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003880 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003881 // No need to print the error message if it's visible in the error
3882 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 print_message = FALSE;
3884
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003885 prev_winid = curwin->w_id;
3886
Bram Moolenaarb2443732018-11-11 22:50:27 +01003887 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003888 if (retval == FAIL)
3889 goto failed;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003890 if (retval == QF_ABORT)
3891 {
3892 qi = NULL;
3893 qf_ptr = NULL;
3894 goto theend;
3895 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003896 if (retval == NOTDONE)
3897 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003898
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003899 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003900 &opened_window, old_KeyTyped, print_message);
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003901 if (retval == QF_ABORT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 {
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003903 // Quickfix/location list was modified by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003904 qi = NULL;
3905 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003908 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003911 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003912 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003914 // Couldn't open file, so put index back where it was. This could
3915 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 qf_ptr = old_qf_ptr;
3918 qf_index = old_qf_index;
3919 }
3920 }
3921theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003922 if (qi != NULL)
3923 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003924 qfl->qf_ptr = qf_ptr;
3925 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003926 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003927 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003929 // Restore old 'switchbuf' value, but not when an autocommand or
3930 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003931 p_swb = old_swb;
3932 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003934 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935}
3936
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003937// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003938static int qfFileAttr;
3939static int qfSepAttr;
3940static int qfLineAttr;
3941
3942/*
3943 * Display information about a single entry from the quickfix/location list.
3944 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003945 * 'cursel' will be set to TRUE for the currently selected entry in the
3946 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003947 */
3948 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003949qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003950{
3951 char_u *fname;
3952 buf_T *buf;
3953 int filter_entry;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003954 garray_T *gap;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003955
3956 fname = NULL;
3957 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3958 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3959 (char *)qfp->qf_module);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00003960 else
3961 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003962 if (qfp->qf_fnum != 0
3963 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3964 {
Austin Chang29822992024-10-03 10:50:05 +02003965 if (qfp->qf_fname == NULL)
3966 fname = buf->b_fname;
3967 else
3968 fname = qfp->qf_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003969 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003970 fname = gettail(fname);
3971 }
3972 if (fname == NULL)
3973 sprintf((char *)IObuff, "%2d", qf_idx);
3974 else
3975 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3976 qf_idx, (char *)fname);
3977 }
3978
3979 // Support for filtering entries using :filter /pat/ clist
3980 // Match against the module name, file name, search pattern and
3981 // text of the entry.
3982 filter_entry = TRUE;
3983 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3984 filter_entry &= message_filtered(qfp->qf_module);
3985 if (filter_entry && fname != NULL)
3986 filter_entry &= message_filtered(fname);
3987 if (filter_entry && qfp->qf_pattern != NULL)
3988 filter_entry &= message_filtered(qfp->qf_pattern);
3989 if (filter_entry)
3990 filter_entry &= message_filtered(qfp->qf_text);
3991 if (filter_entry)
3992 return;
3993
3994 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003995 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003996
3997 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003998 msg_puts_attr(":", qfSepAttr);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003999 gap = qfga_get();
Shane Harper5bf04282023-06-07 19:09:57 +01004000 if (qfp->qf_lnum != 0)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004001 qf_range_text(gap, qfp);
4002 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
4003 ga_append(gap, NUL);
4004 msg_puts_attr((char *)gap->ga_data, qfLineAttr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004005 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02004006 if (qfp->qf_pattern != NULL)
4007 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004008 gap = qfga_get();
4009 qf_fmt_text(gap, qfp->qf_pattern);
Shane Harper5bf04282023-06-07 19:09:57 +01004010 ga_append(gap, NUL);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004011 msg_puts((char *)gap->ga_data);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004012 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02004013 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004014 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02004015
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004016 // Remove newlines and leading whitespace from the text. For an
4017 // unrecognized line keep the indent, the compiler may mark a word
4018 // with ^^^^.
4019 gap = qfga_get();
4020 qf_fmt_text(gap, (fname != NULL || qfp->qf_lnum != 0)
Shane Harper5bf04282023-06-07 19:09:57 +01004021 ? skipwhite(qfp->qf_text) : qfp->qf_text);
4022 ga_append(gap, NUL);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004023 msg_prt_line((char_u *)gap->ga_data, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004024 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02004025}
4026
4027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004029 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 */
4031 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004032qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004034 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004035 qfline_T *qfp;
4036 int i;
4037 int idx1 = 1;
4038 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004039 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02004040 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004041 int all = eap->forceit; // if not :cl!, only show
4042 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004043 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004045 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4046 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004047
Bram Moolenaar0398e002019-03-21 21:12:49 +01004048 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004050 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 return;
4052 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02004053 if (*arg == '+')
4054 {
4055 ++arg;
4056 plus = TRUE;
4057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
4059 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004060 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 return;
4062 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004063 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02004064 if (plus)
4065 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004066 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02004067 idx2 = i + idx1;
4068 idx1 = i;
4069 }
4070 else
4071 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004072 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02004073 if (idx1 < 0)
4074 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
4075 if (idx2 < 0)
4076 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
4077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004079 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02004080 shorten_fnames(FALSE);
4081
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004082 // Get the attributes for the different quickfix highlight items. Note
4083 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01004084 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
4085 if (qfFileAttr == 0)
4086 qfFileAttr = HL_ATTR(HLF_D);
4087 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
4088 if (qfSepAttr == 0)
4089 qfSepAttr = HL_ATTR(HLF_D);
4090 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
4091 if (qfLineAttr == 0)
4092 qfLineAttr = HL_ATTR(HLF_N);
4093
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004094 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02004096 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 {
4098 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004099 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004100
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 ui_breakcheck();
4102 }
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01004103 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104}
4105
4106/*
4107 * Remove newlines and leading whitespace from an error message.
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004108 * Add the result to the grow array "gap".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 */
4110 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004111qf_fmt_text(garray_T *gap, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 char_u *p = text;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004114 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 {
4116 if (*p == '\n')
4117 {
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004118 ga_append(gap, ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004120 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 break;
4122 }
4123 else
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004124 ga_append(gap, *p++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126}
4127
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004128/*
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004129 * Add the range information from the lnum, col, end_lnum, and end_col values
4130 * of a quickfix entry to the grow array "gap".
thinca6864efa2021-06-19 20:45:20 +02004131 */
4132 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004133qf_range_text(garray_T *gap, qfline_T *qfp)
thinca6864efa2021-06-19 20:45:20 +02004134{
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004135 char_u *buf = IObuff;
4136 int bufsize = IOSIZE;
thinca6864efa2021-06-19 20:45:20 +02004137 int len;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004138
thinca6864efa2021-06-19 20:45:20 +02004139 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
4140 len = (int)STRLEN(buf);
4141
4142 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
4143 {
Shane Harper5bf04282023-06-07 19:09:57 +01004144 vim_snprintf((char *)buf + len, bufsize - len, "-%ld",
4145 qfp->qf_end_lnum);
thinca6864efa2021-06-19 20:45:20 +02004146 len += (int)STRLEN(buf + len);
4147 }
4148 if (qfp->qf_col > 0)
4149 {
4150 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
4151 len += (int)STRLEN(buf + len);
4152 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
4153 {
Shane Harper5bf04282023-06-07 19:09:57 +01004154 vim_snprintf((char *)buf + len, bufsize - len, "-%d",
4155 qfp->qf_end_col);
thinca6864efa2021-06-19 20:45:20 +02004156 len += (int)STRLEN(buf + len);
4157 }
4158 }
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004159
4160 ga_concat_len(gap, buf, len);
thinca6864efa2021-06-19 20:45:20 +02004161}
4162
4163/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004164 * Display information (list number, list size and the title) about a
4165 * quickfix/location list.
4166 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004167 static void
4168qf_msg(qf_info_T *qi, int which, char *lead)
4169{
4170 char *title = (char *)qi->qf_lists[which].qf_title;
4171 int count = qi->qf_lists[which].qf_count;
4172 char_u buf[IOSIZE];
4173
4174 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
4175 lead,
4176 which + 1,
4177 qi->qf_listcount,
4178 count);
4179
4180 if (title != NULL)
4181 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02004182 size_t len = STRLEN(buf);
4183
4184 if (len < 34)
4185 {
4186 vim_memset(buf + len, ' ', 34 - len);
4187 buf[34] = NUL;
4188 }
4189 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004190 }
4191 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004192 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004193}
4194
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195/*
4196 * ":colder [count]": Up in the quickfix stack.
4197 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004198 * ":lolder [count]": Up in the location list stack.
4199 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 */
4201 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004202qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004204 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 int count;
4206
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004207 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4208 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004209
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 if (eap->addr_count != 0)
4211 count = eap->line2;
4212 else
4213 count = 1;
4214 while (count--)
4215 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004216 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004218 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004220 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02004221 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004223 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225 else
4226 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004227 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004229 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02004230 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004232 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
4234 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004235 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004236 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237}
4238
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004239/*
4240 * Display the information about all the quickfix/location lists in the stack
4241 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004242 void
4243qf_history(exarg_T *eap)
4244{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004245 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004246 int i;
4247
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004248 if (eap->addr_count > 0)
4249 {
4250 if (qi == NULL)
4251 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004252 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004253 return;
4254 }
4255
4256 // Jump to the specified quickfix list
4257 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
4258 {
4259 qi->qf_curlist = eap->line2 - 1;
4260 qf_msg(qi, qi->qf_curlist, "");
4261 qf_update_buffer(qi, NULL);
4262 }
4263 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004264 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004265
4266 return;
4267 }
4268
Bram Moolenaar5b69c222019-01-11 14:50:06 +01004269 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004270 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004271 else
4272 for (i = 0; i < qi->qf_listcount; ++i)
4273 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
4274}
4275
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004277 * Free all the entries in the error list "idx". Note that other information
4278 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 */
4280 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004281qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004283 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004284 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01004285 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004287 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004289 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004290 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004291 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01004292 {
Austin Chang29822992024-10-03 10:50:05 +02004293 vim_free(qfp->qf_fname);
Bram Moolenaard76ce852018-05-01 15:02:04 +02004294 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004295 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004296 vim_free(qfp->qf_pattern);
Tom Praschanca6ac992023-08-11 23:26:12 +02004297 clear_tv(&qfp->qf_user_data);
Bram Moolenaard76ce852018-05-01 15:02:04 +02004298 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004299 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01004300 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004301 // Somehow qf_count may have an incorrect value, set it to 1
4302 // to avoid crashing when it's wrong.
4303 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004304 qfl->qf_count = 1;
Christian Brabandt567cae22023-11-19 16:19:27 +01004305 else
4306 qfl->qf_start = qfpnext;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01004307 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004308 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004310
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004311 qfl->qf_index = 0;
4312 qfl->qf_start = NULL;
4313 qfl->qf_last = NULL;
4314 qfl->qf_ptr = NULL;
4315 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02004316
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004317 qf_clean_dir_stack(&qfl->qf_dir_stack);
4318 qfl->qf_directory = NULL;
4319 qf_clean_dir_stack(&qfl->qf_file_stack);
4320 qfl->qf_currfile = NULL;
4321 qfl->qf_multiline = FALSE;
4322 qfl->qf_multiignore = FALSE;
4323 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324}
4325
4326/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004327 * Free error list "idx". Frees all the entries in the quickfix list,
4328 * associated context information and the title.
4329 */
4330 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004331qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004332{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004333 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004334
Bram Moolenaard23a8232018-02-10 18:45:26 +01004335 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004336 free_tv(qfl->qf_ctx);
4337 qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004338 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004339 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004340 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004341}
4342
4343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 * qf_mark_adjust: adjust marks
4345 */
4346 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004347qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02004348 win_T *wp,
4349 linenr_T line1,
4350 linenr_T line2,
4351 long amount,
4352 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004354 int i;
4355 qfline_T *qfp;
4356 int idx;
64-bitman88d41ab2025-04-06 17:20:39 +02004357 qf_info_T *qi = ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004358 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02004359 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
Bram Moolenaarc1542742016-07-20 21:44:37 +02004361 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004362 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004363 if (wp != NULL)
4364 {
4365 if (wp->w_llist == NULL)
4366 return;
4367 qi = wp->w_llist;
4368 }
64-bitman88d41ab2025-04-06 17:20:39 +02004369 else if (qi == NULL)
4370 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004371
4372 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01004373 {
4374 qf_list_T *qfl = qf_get_list(qi, idx);
4375
4376 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01004377 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 if (qfp->qf_fnum == curbuf->b_fnum)
4379 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004380 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
4382 {
4383 if (amount == MAXLNUM)
4384 qfp->qf_cleared = TRUE;
4385 else
4386 qfp->qf_lnum += amount;
4387 }
4388 else if (amount_after && qfp->qf_lnum > line2)
4389 qfp->qf_lnum += amount_after;
4390 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01004391 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004392
4393 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02004394 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395}
4396
4397/*
4398 * Make a nice message out of the error character and the error number:
4399 * char number message
4400 * e or E 0 " error"
4401 * w or W 0 " warning"
4402 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004403 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 * 0 0 ""
4405 * other 0 " c"
4406 * e or E n " error n"
4407 * w or W n " warning n"
4408 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004409 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 * 0 n " error n"
4411 * other n " c n"
4412 * 1 x "" :helpgrep
4413 */
4414 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004415qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416{
4417 static char_u buf[20];
4418 static char_u cc[3];
4419 char_u *p;
4420
4421 if (c == 'W' || c == 'w')
4422 p = (char_u *)" warning";
4423 else if (c == 'I' || c == 'i')
4424 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004425 else if (c == 'N' || c == 'n')
4426 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4428 p = (char_u *)" error";
4429 else if (c == 0 || c == 1)
4430 p = (char_u *)"";
4431 else
4432 {
4433 cc[0] = ' ';
4434 cc[1] = c;
4435 cc[2] = NUL;
4436 p = cc;
4437 }
4438
4439 if (nr <= 0)
4440 return p;
4441
4442 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4443 return buf;
4444}
4445
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004447 * When "split" is FALSE: Open the entry/result under the cursor.
4448 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4449 */
4450 void
4451qf_view_result(int split)
4452{
64-bitman88d41ab2025-04-06 17:20:39 +02004453 qf_info_T *qi = ql_info;
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004454
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004455 if (IS_LL_WINDOW(curwin))
4456 qi = GET_LOC_LIST(curwin);
64-bitman88d41ab2025-04-06 17:20:39 +02004457 else if (qi == NULL)
4458 {
4459 emsg(_(e_no_quickfix_stack));
4460 return;
4461 }
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004462
Bram Moolenaar0398e002019-03-21 21:12:49 +01004463 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004464 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004465 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004466 return;
4467 }
4468
4469 if (split)
4470 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004471 // Open the selected entry in a new window
4472 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4473 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004474 return;
4475 }
4476
4477 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4478}
4479
4480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 * ":cwindow": open the quickfix window if we have errors to display,
4482 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004483 * ":lwindow": open the location list window if we have locations to display,
4484 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 */
4486 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004487ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004489 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004490 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 win_T *win;
4492
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004493 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4494 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004495
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004496 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004497
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004498 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004499 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004501 // If a quickfix window is open but we have no errors to display,
4502 // close the window. If a quickfix window is not open, then open
4503 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004504 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004505 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004506 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 {
4508 if (win != NULL)
4509 ex_cclose(eap);
4510 }
4511 else if (win == NULL)
4512 ex_copen(eap);
4513}
4514
4515/*
4516 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004517 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004520ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004522 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004523 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004525 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4526 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004528 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004529 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 if (win != NULL)
4531 win_close(win, FALSE);
4532}
4533
4534/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004535 * Set "w:quickfix_title" if "qi" has a title.
4536 */
4537 static void
4538qf_set_title_var(qf_list_T *qfl)
4539{
4540 if (qfl->qf_title != NULL)
4541 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4542}
4543
4544/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004545 * Goto a quickfix or location list window (if present).
4546 * Returns OK if the window is found, FAIL otherwise.
4547 */
4548 static int
4549qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4550{
4551 win_T *win;
4552
4553 win = qf_find_win(qi);
4554 if (win == NULL)
4555 return FAIL;
4556
4557 win_goto(win);
4558 if (resize)
4559 {
4560 if (vertsplit)
4561 {
4562 if (sz != win->w_width)
4563 win_setwidth(sz);
4564 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004565 else if (sz != win->w_height && win->w_height
4566 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004567 win_setheight(sz);
4568 }
4569
4570 return OK;
4571}
4572
4573/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004574 * Set options for the buffer in the quickfix or location list window.
4575 */
4576 static void
4577qf_set_cwindow_options(void)
4578{
4579 // switch off 'swapfile'
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004580 set_option_value_give_err((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4581 set_option_value_give_err((char_u *)"bt",
4582 0L, (char_u *)"quickfix", OPT_LOCAL);
4583 set_option_value_give_err((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004584 RESET_BINDING(curwin);
4585#ifdef FEAT_DIFF
4586 curwin->w_p_diff = FALSE;
4587#endif
4588#ifdef FEAT_FOLDING
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004589 set_option_value_give_err((char_u *)"fdm", 0L, (char_u *)"manual",
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004590 OPT_LOCAL);
4591#endif
4592}
4593
4594/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004595 * Open a new quickfix or location list window, load the quickfix buffer and
4596 * set the appropriate options for the window.
4597 * Returns FAIL if the window could not be opened.
4598 */
4599 static int
4600qf_open_new_cwindow(qf_info_T *qi, int height)
4601{
4602 buf_T *qf_buf;
4603 win_T *oldwin = curwin;
4604 tabpage_T *prevtab = curtab;
4605 int flags = 0;
4606 win_T *win;
4607
4608 qf_buf = qf_find_buf(qi);
4609
4610 // The current window becomes the previous window afterwards.
4611 win = curwin;
4612
Bram Moolenaare1004402020-10-24 20:49:43 +02004613 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004614 // Create the new quickfix window at the very bottom, except when
4615 // :belowright or :aboveleft is used.
4616 win_goto(lastwin);
4617 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004618 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004619 flags = WSP_BELOW;
4620 flags |= WSP_NEWLOC;
4621 if (win_split(height, flags) == FAIL)
4622 return FAIL; // not enough room for window
4623 RESET_BINDING(curwin);
4624
4625 if (IS_LL_STACK(qi))
4626 {
4627 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004628 // location list stack from the window 'win'.
4629 curwin->w_llist_ref = qi;
4630 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004631 }
4632
4633 if (oldwin != curwin)
4634 oldwin = NULL; // don't store info when in another window
4635 if (qf_buf != NULL)
4636 {
4637 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004638 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004639 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004640 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004641 }
4642 else
4643 {
4644 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004645 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4646 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004647 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004648
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004649 // save the number of the new buffer
4650 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004651 }
4652
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004653 // Set the options for the quickfix buffer/window (if not already done)
4654 // Do this even if the quickfix buffer was already present, as an autocmd
4655 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004656 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004657 qf_set_cwindow_options();
4658
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004659 // Only set the height when still in the same tab page and there is no
4660 // window to the side.
4661 if (curtab == prevtab && curwin->w_width == Columns)
4662 win_setheight(height);
4663 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4664 if (win_valid(win))
4665 prevwin = win;
4666
4667 return OK;
4668}
4669
4670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004672 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 */
4674 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004675ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004677 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004678 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004680 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004681 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004683 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4684 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004685
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004686 incr_quickfix_busy();
4687
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 if (eap->addr_count != 0)
4689 height = eap->line2;
4690 else
4691 height = QF_WINHEIGHT;
4692
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004693 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694#ifdef FEAT_GUI
4695 need_mouse_correct = TRUE;
4696#endif
4697
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004698 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004699 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004700 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004701 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004702 if (status == FAIL)
4703 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004704 {
4705 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004706 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004709 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004710 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004711 // Save the current index here, as updating the quickfix buffer may free
4712 // the quickfix list
4713 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004714
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004715 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004716 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004718 decr_quickfix_busy();
4719
4720 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 curwin->w_cursor.col = 0;
4722 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004723 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724}
4725
4726/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004727 * Move the cursor in the quickfix window to "lnum".
4728 */
4729 static void
4730qf_win_goto(win_T *win, linenr_T lnum)
4731{
4732 win_T *old_curwin = curwin;
4733
4734 curwin = win;
4735 curbuf = win->w_buffer;
4736 curwin->w_cursor.lnum = lnum;
4737 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004738 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004739 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004740 update_topline(); // scroll to show the line
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004741 redraw_later(UPD_VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004742 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004743 curwin = old_curwin;
4744 curbuf = curwin->w_buffer;
4745}
4746
4747/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004748 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004749 */
4750 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004751ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004752{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004753 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004754 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004755
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004756 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4757 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004758
4759 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004760 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4761 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4762}
4763
4764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 * Return the number of the current entry (line number in the quickfix
4766 * window).
4767 */
4768 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004769qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
64-bitman88d41ab2025-04-06 17:20:39 +02004771 qf_info_T *qi = ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004772
4773 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004774 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004775 qi = wp->w_llist_ref;
64-bitman88d41ab2025-04-06 17:20:39 +02004776 else if (qi == NULL)
4777 return 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004778
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004779 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780}
4781
4782/*
4783 * Update the cursor position in the quickfix window to the current error.
4784 * Return TRUE if there is a quickfix window.
4785 */
4786 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004787qf_win_pos_update(
4788 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004789 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790{
4791 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004792 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004794 // Put the cursor on the current error in the quickfix window, so that
4795 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004796 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 if (win != NULL
4798 && qf_index <= win->w_buffer->b_ml.ml_line_count
4799 && old_qf_index != qf_index)
4800 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 if (qf_index > old_qf_index)
4802 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004803 win->w_redraw_top = old_qf_index;
4804 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 }
4806 else
4807 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004808 win->w_redraw_top = qf_index;
4809 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004811 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 }
4813 return win != NULL;
4814}
4815
4816/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004817 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004818 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004819 */
4820 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004821is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004822{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004823 // A window displaying the quickfix buffer will have the w_llist_ref field
4824 // set to NULL.
4825 // A window displaying a location list buffer will have the w_llist_ref
4826 // pointing to the location list.
Christian Brabandtfc682992023-09-03 20:20:52 +02004827 if (buf_valid(win->w_buffer) && bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004828 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4829 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004830 return TRUE;
4831
4832 return FALSE;
4833}
4834
4835/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004836 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4837 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004838 */
4839 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004840qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004841{
4842 win_T *win;
4843
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004844 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004845 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004846 return win;
4847 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004848}
4849
4850/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004851 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004852 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 */
4854 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004855qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004857 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004858 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004860 if (qi->qf_bufnr != INVALID_QFBUFNR)
4861 {
4862 buf_T *qfbuf;
4863 qfbuf = buflist_findnr(qi->qf_bufnr);
4864 if (qfbuf != NULL)
4865 return qfbuf;
4866 // buffer is no longer present
4867 qi->qf_bufnr = INVALID_QFBUFNR;
4868 }
4869
Bram Moolenaar9c102382006-05-03 21:26:49 +00004870 FOR_ALL_TAB_WINDOWS(tp, win)
4871 if (is_qf_win(win, qi))
4872 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004873
Bram Moolenaar9c102382006-05-03 21:26:49 +00004874 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875}
4876
4877/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004878 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004879 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004880 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004881 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004882did_set_quickfixtextfunc(optset_T *args UNUSED)
Bram Moolenaard43906d2020-07-20 21:31:32 +02004883{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004884 if (option_set_callback_func(p_qftf, &qftf_cb) == FAIL)
4885 return e_invalid_argument;
4886
4887 return NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004888}
4889
4890/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004891 * Update the w:quickfix_title variable in the quickfix/location list window in
4892 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004893 */
4894 static void
4895qf_update_win_titlevar(qf_info_T *qi)
4896{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004897 qf_list_T *qfl = qf_get_curlist(qi);
4898 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004899 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004900 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004901
Bram Moolenaar530bed92020-12-16 21:02:56 +01004902 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004903 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004904 if (is_qf_win(win, qi))
4905 {
4906 curwin = win;
4907 qf_set_title_var(qfl);
4908 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004909 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004910 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004911}
4912
4913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 * Find the quickfix buffer. If it exists, update the contents.
4915 */
4916 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004917qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918{
4919 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004920 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004923 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004924 buf = qf_find_buf(qi);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004925 if (buf == NULL)
4926 return;
4927
4928 linenr_T old_line_count = buf->b_ml.ml_line_count;
4929 int qf_winid = 0;
4930
4931 if (IS_LL_STACK(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004933 if (curwin->w_llist == qi)
4934 win = curwin;
4935 else
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004936 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004937 // Find the file window (non-quickfix) with this location list
4938 win = qf_find_win_with_loclist(qi);
4939 if (win == NULL)
4940 // File window is not found. Find the location list window.
4941 win = qf_find_win(qi);
4942 if (win == NULL)
4943 return;
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004944 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004945 qf_winid = win->w_id;
4946 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004947
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004948 // autocommands may cause trouble
4949 incr_quickfix_busy();
Bram Moolenaard0fab102022-10-20 16:03:33 +01004950
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004951 int do_fill = TRUE;
4952 if (old_last == NULL)
4953 {
4954 // set curwin/curbuf to buf and save a few things
4955 aucmd_prepbuf(&aco, buf);
4956 if (curbuf != buf)
4957 do_fill = FALSE; // failed to find a window for "buf"
4958 }
4959
4960 if (do_fill)
4961 {
4962 qf_update_win_titlevar(qi);
4963
4964 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
4965 ++CHANGEDTICK(buf);
4966
Bram Moolenaar864293a2016-06-02 13:40:04 +02004967 if (old_last == NULL)
4968 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004969 (void)qf_win_pos_update(qi, 0);
4970
4971 // restore curwin/curbuf and a few other things
4972 aucmd_restbuf(&aco);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004975
4976 // Only redraw when added lines are visible. This avoids flickering
4977 // when the added lines are not visible.
4978 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4979 redraw_buf_later(buf, UPD_NOT_VALID);
4980
4981 // always called after incr_quickfix_busy()
4982 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983}
4984
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004985/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004986 * Add an error line to the quickfix buffer.
4987 */
4988 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004989qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004990 buf_T *buf, // quickfix window buffer
4991 linenr_T lnum,
4992 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004993 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004994 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004995 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004996{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004997 buf_T *errbuf;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004998 garray_T *gap;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004999
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005000 gap = qfga_get();
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005001
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005002 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02005003 // for this entry, then use it.
5004 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00005005 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005006 ga_concat(gap, qftf_str);
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00005007 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005008 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005009 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02005010 if (qfp->qf_module != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005011 ga_concat(gap, qfp->qf_module);
Bram Moolenaar858ba062020-05-31 23:11:59 +02005012 else if (qfp->qf_fnum != 0
5013 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
5014 && errbuf->b_fname != NULL)
5015 {
5016 if (qfp->qf_type == 1) // :helpgrep
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005017 ga_concat(gap, gettail(errbuf->b_fname));
Bram Moolenaar858ba062020-05-31 23:11:59 +02005018 else
5019 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02005020 // Shorten the file name if not done already.
5021 // For optimization, do this only for the first entry in a
5022 // buffer.
5023 if (first_bufline && (errbuf->b_sfname == NULL
5024 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005025 {
5026 if (*dirname == NUL)
5027 mch_dirname(dirname, MAXPATHL);
5028 shorten_buf_fname(errbuf, dirname, FALSE);
5029 }
Austin Chang29822992024-10-03 10:50:05 +02005030 if (qfp->qf_fname == NULL)
5031 ga_concat(gap, errbuf->b_fname);
5032 else
5033 ga_concat(gap, qfp->qf_fname);
Bram Moolenaar858ba062020-05-31 23:11:59 +02005034 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02005035 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02005036
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005037 ga_append(gap, '|');
Bram Moolenaar858ba062020-05-31 23:11:59 +02005038
5039 if (qfp->qf_lnum > 0)
5040 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005041 qf_range_text(gap, qfp);
5042 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005043 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02005044 else if (qfp->qf_pattern != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005045 qf_fmt_text(gap, qfp->qf_pattern);
5046 ga_append(gap, '|');
5047 ga_append(gap, ' ');
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005048
Bram Moolenaar858ba062020-05-31 23:11:59 +02005049 // Remove newlines and leading whitespace from the text.
5050 // For an unrecognized line keep the indent, the compiler may
5051 // mark a word with ^^^^.
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00005052 qf_fmt_text(gap, gap->ga_len > 3 ? skipwhite(qfp->qf_text)
5053 : qfp->qf_text);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005054 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005055
Shane Harper5bf04282023-06-07 19:09:57 +01005056 ga_append(gap, NUL);
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00005057 if (ml_append_buf(buf, lnum, gap->ga_data, gap->ga_len, FALSE) == FAIL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005058 return FAIL;
5059
5060 return OK;
5061}
5062
Bram Moolenaard43906d2020-07-20 21:31:32 +02005063/*
5064 * Call the 'quickfixtextfunc' function to get the list of lines to display in
5065 * the quickfix window for the entries 'start_idx' to 'end_idx'.
5066 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005067 static list_T *
5068call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
5069{
Bram Moolenaard43906d2020-07-20 21:31:32 +02005070 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005071 list_T *qftf_list = NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01005072 static int recursive = FALSE;
5073
5074 if (recursive)
5075 return NULL; // this doesn't work properly recursively
5076 recursive = TRUE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005077
5078 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
5079 // the text to display. Use the local value of 'quickfixtextfunc' if it is
5080 // set.
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01005081 if (qfl->qf_qftf_cb.cb_name != NULL)
5082 cb = &qfl->qf_qftf_cb;
5083 if (cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005084 {
5085 typval_T args[1];
5086 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02005087 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005088
5089 // create the dict argument
5090 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
Bram Moolenaard6c67622022-08-24 20:07:22 +01005091 {
5092 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005093 return NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01005094 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005095 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
5096 dict_add_number(d, "winid", (long)qf_winid);
5097 dict_add_number(d, "id", (long)qfl->qf_id);
5098 dict_add_number(d, "start_idx", start_idx);
5099 dict_add_number(d, "end_idx", end_idx);
5100 ++d->dv_refcount;
5101 args[0].v_type = VAR_DICT;
5102 args[0].vval.v_dict = d;
5103
Bram Moolenaard43906d2020-07-20 21:31:32 +02005104 qftf_list = NULL;
5105 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
5106 {
5107 if (rettv.v_type == VAR_LIST)
5108 {
5109 qftf_list = rettv.vval.v_list;
5110 qftf_list->lv_refcount++;
5111 }
5112 clear_tv(&rettv);
5113 }
5114 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005115 }
5116
Bram Moolenaard6c67622022-08-24 20:07:22 +01005117 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005118 return qftf_list;
5119}
5120
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 * Fill current buffer with quickfix errors, replacing any previous contents.
5123 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02005124 * If "old_last" is not NULL append the items after this one.
5125 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
5126 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 */
5128 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02005129qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005131 linenr_T lnum;
5132 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005133 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005134 list_T *qftf_list = NULL;
5135 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136
Bram Moolenaar864293a2016-06-02 13:40:04 +02005137 if (old_last == NULL)
5138 {
zeertzjqc7a8eb52024-05-08 20:22:40 +02005139 win_T *wp;
5140 tabpage_T *tp;
5141
Bram Moolenaar864293a2016-06-02 13:40:04 +02005142 if (buf != curbuf)
5143 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005144 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02005145 return;
5146 }
5147
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005148 // delete all existing lines
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01005149 //
5150 // Note: we cannot store undo information, because
5151 // qf buffer is usually not allowed to be modified.
5152 //
5153 // So we need to clean up undo information
5154 // otherwise autocommands may invalidate the undo stack
Bram Moolenaar864293a2016-06-02 13:40:04 +02005155 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02005156 (void)ml_delete((linenr_T)1);
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01005157
zeertzjqc7a8eb52024-05-08 20:22:40 +02005158 FOR_ALL_TAB_WINDOWS(tp, wp)
5159 if (wp->w_buffer == curbuf)
5160 wp->w_skipcol = 0;
5161
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01005162 // Remove all undo information
Christian Brabandt9071ed82024-02-15 20:17:37 +01005163 u_clearallandblockfree(curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005166 // Check if there is anything to display
Bram Moolenaar4f1b0832022-08-29 20:45:16 +01005167 if (qfl != NULL && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005169 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02005170 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02005171 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02005172
5173 *dirname = NUL;
5174
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005175 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01005176 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02005177 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005178 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005179 lnum = 0;
5180 }
5181 else
5182 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01005183 if (old_last->qf_next != NULL)
5184 qfp = old_last->qf_next;
5185 else
5186 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005187 lnum = buf->b_ml.ml_line_count;
5188 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005189
5190 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
5191 (long)qfl->qf_count);
5192 if (qftf_list != NULL)
5193 qftf_li = qftf_list->lv_first;
5194
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005195 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005197 char_u *qftf_str = NULL;
5198
Bram Moolenaard43906d2020-07-20 21:31:32 +02005199 // Use the text supplied by the user defined function (if any).
5200 // If the returned value is not string, then ignore the rest
5201 // of the returned values and use the default.
5202 if (qftf_li != NULL && !invalid_val)
5203 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005204 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02005205 if (qftf_str == NULL)
5206 invalid_val = TRUE;
5207 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005208
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02005209 if (qf_buf_add_line(buf, lnum, qfp, dirname,
5210 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005212
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02005213 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005214 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005216 if (qfp == NULL)
5217 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005218
5219 if (qftf_li != NULL)
5220 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02005222
5223 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005224 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02005225 (void)ml_delete(lnum + 1);
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01005226
5227 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005230 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 check_lnums(TRUE);
5232
Bram Moolenaar864293a2016-06-02 13:40:04 +02005233 if (old_last == NULL)
5234 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005235 // Set the 'filetype' to "qf" each time after filling the buffer.
5236 // This resembles reading a file into a buffer, it's more logical when
5237 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02005238 ++curbuf_lock;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005239 set_option_value_give_err((char_u *)"ft",
5240 0L, (char_u *)"qf", OPT_LOCAL);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005241 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
zeertzjq5bf6c212024-03-31 18:41:27 +02005243 curbuf->b_keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02005244 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005246 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 FALSE, curbuf);
zeertzjq5bf6c212024-03-31 18:41:27 +02005248 curbuf->b_keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02005249 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005250
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005251 // make sure it will be redrawn
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005252 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005255 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 KeyTyped = old_KeyTyped;
5257}
5258
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005259/*
5260 * For every change made to the quickfix list, update the changed tick.
5261 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01005262 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005263qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01005264{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005265 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005266}
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005269 * Return the quickfix/location list number with the given identifier.
5270 * Returns -1 if list is not found.
5271 */
5272 static int
5273qf_id2nr(qf_info_T *qi, int_u qfid)
5274{
5275 int qf_idx;
5276
5277 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
5278 if (qi->qf_lists[qf_idx].qf_id == qfid)
5279 return qf_idx;
5280 return INVALID_QFIDX;
5281}
5282
5283/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005284 * If the current list is not "save_qfid" and we can find the list with that ID
5285 * then make it the current list.
5286 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005287 * Returns OK if successfully restored the list. Returns FAIL if the list with
5288 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005289 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005290 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005291qf_restore_list(qf_info_T *qi, int_u save_qfid)
5292{
5293 int curlist;
5294
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00005295 if (qf_get_curlist(qi)->qf_id == save_qfid)
5296 return OK;
5297
5298 curlist = qf_id2nr(qi, save_qfid);
5299 if (curlist < 0)
5300 // list is not present
5301 return FAIL;
5302 qi->qf_curlist = curlist;
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005303 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005304}
5305
5306/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005307 * Jump to the first entry if there is one.
5308 */
5309 static void
5310qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
5311{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005312 if (qf_restore_list(qi, save_qfid) == FAIL)
5313 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005314
Colin Kennedy21570352024-03-03 16:16:47 +01005315
5316 if (!check_can_set_curbuf_forceit(forceit))
5317 return;
5318
5319
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005320 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01005321 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005322 qf_jump(qi, 0, 0, forceit);
5323}
5324
5325/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005326 * Return TRUE when using ":vimgrep" for ":grep".
5327 */
5328 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005329grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005330{
Bram Moolenaar754b5602006-02-09 23:53:20 +00005331 return ((cmdidx == CMD_grep
5332 || cmdidx == CMD_lgrep
5333 || cmdidx == CMD_grepadd
5334 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005335 && STRCMP("internal",
5336 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
5337}
5338
5339/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005340 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005342 static char_u *
5343make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005345 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02005346 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005347 case CMD_make: return (char_u *)"make";
5348 case CMD_lmake: return (char_u *)"lmake";
5349 case CMD_grep: return (char_u *)"grep";
5350 case CMD_lgrep: return (char_u *)"lgrep";
5351 case CMD_grepadd: return (char_u *)"grepadd";
5352 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5353 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02005354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355}
5356
5357/*
5358 * Return the name for the errorfile, in allocated memory.
5359 * Find a new unique name when 'makeef' contains "##".
5360 * Returns NULL for error.
5361 */
5362 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01005363get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
5365 char_u *p;
5366 char_u *name;
5367 static int start = -1;
5368 static int off = 0;
5369#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02005370 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371#endif
5372
5373 if (*p_mef == NUL)
5374 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005375 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005377 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 return name;
5379 }
5380
5381 for (p = p_mef; *p; ++p)
5382 if (p[0] == '#' && p[1] == '#')
5383 break;
5384
5385 if (*p == NUL)
5386 return vim_strsave(p_mef);
5387
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005388 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 for (;;)
5390 {
5391 if (start == -1)
5392 start = mch_get_pid();
5393 else
5394 off += 19;
5395
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005396 name = alloc_id(STRLEN(p_mef) + 30, aid_qf_mef_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 if (name == NULL)
5398 break;
5399 STRCPY(name, p_mef);
5400 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
5401 STRCAT(name, p + 2);
5402 if (mch_getperm(name) < 0
5403#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005404 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 && mch_lstat((char *)name, &sb) < 0
5406#endif
5407 )
5408 break;
5409 vim_free(name);
5410 }
5411 return name;
5412}
5413
5414/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005415 * Form the complete command line to invoke 'make'/'grep'. Quote the command
5416 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
5417 */
5418 static char_u *
5419make_get_fullcmd(char_u *makecmd, char_u *fname)
5420{
5421 char_u *cmd;
5422 unsigned len;
5423
5424 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
5425 if (*p_sp != NUL)
5426 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005427 cmd = alloc_id(len, aid_qf_makecmd);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005428 if (cmd == NULL)
5429 return NULL;
5430 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
5431 (char *)p_shq);
5432
5433 // If 'shellpipe' empty: don't redirect to 'errorfile'.
5434 if (*p_sp != NUL)
5435 append_redir(cmd, len, p_sp, fname);
5436
5437 // Display the fully formed command. Output a newline if there's something
5438 // else than the :make command that was typed (in which case the cursor is
5439 // in column 0).
5440 if (msg_col == 0)
5441 msg_didout = FALSE;
5442 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005443 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005444 msg_outtrans(cmd); // show what we are doing
5445
5446 return cmd;
5447}
5448
5449/*
5450 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5451 */
5452 void
5453ex_make(exarg_T *eap)
5454{
5455 char_u *fname;
5456 char_u *cmd;
5457 char_u *enc = NULL;
5458 win_T *wp = NULL;
64-bitman88d41ab2025-04-06 17:20:39 +02005459 qf_info_T *qi = ql_info;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005460 int res;
5461 char_u *au_name = NULL;
5462 int_u save_qfid;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005463 char_u *errorformat = p_efm;
5464 int newlist = TRUE;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005465
5466 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5467 if (grep_internal(eap->cmdidx))
5468 {
5469 ex_vimgrep(eap);
5470 return;
5471 }
5472
5473 au_name = make_get_auname(eap->cmdidx);
5474 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5475 curbuf->b_fname, TRUE, curbuf))
5476 {
5477#ifdef FEAT_EVAL
5478 if (aborting())
5479 return;
5480#endif
5481 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005482 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005483
5484 if (is_loclist_cmd(eap->cmdidx))
5485 wp = curwin;
5486
5487 autowrite_all();
5488 fname = get_mef_name();
5489 if (fname == NULL)
5490 return;
5491 mch_remove(fname); // in case it's not unique
5492
5493 cmd = make_get_fullcmd(eap->arg, fname);
5494 if (cmd == NULL)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005495 {
5496 vim_free(fname);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005497 return;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005498 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005499
5500 // let the shell know if we are redirecting output or not
5501 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5502
5503#ifdef AMIGA
5504 out_flush();
5505 // read window status report and redraw before message
5506 (void)char_avail();
5507#endif
5508
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005509 incr_quickfix_busy();
5510
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005511 if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake)
5512 errorformat = p_gefm;
5513 if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
5514 newlist = FALSE;
5515
5516 res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep),
5517 enc);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005518 if (wp != NULL)
5519 {
5520 qi = GET_LOC_LIST(wp);
5521 if (qi == NULL)
5522 goto cleanup;
5523 }
64-bitman88d41ab2025-04-06 17:20:39 +02005524 else if (qi == NULL)
5525 goto cleanup;
5526
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005527 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005528 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005529
5530 // Remember the current quickfix list identifier, so that we can
5531 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005532 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005533 if (au_name != NULL)
5534 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5535 curbuf->b_fname, TRUE, curbuf);
5536 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5537 // display the first error
5538 qf_jump_first(qi, save_qfid, FALSE);
5539
5540cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005541 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005542 mch_remove(fname);
5543 vim_free(fname);
5544 vim_free(cmd);
5545}
5546
5547/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005548 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005549 */
5550 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005551qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005552{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005553 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005554
5555 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5556 return 0;
5557 return qf_get_curlist(qi)->qf_count;
5558}
5559
5560/*
5561 * Returns the number of valid entries in the current quickfix/location list.
5562 */
5563 int
5564qf_get_valid_size(exarg_T *eap)
5565{
5566 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005567 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005568 qfline_T *qfp;
5569 int i, sz = 0;
5570 int prev_fnum = 0;
5571
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005572 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5573 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005574
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005575 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005576 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005577 {
5578 if (qfp->qf_valid)
5579 {
5580 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005581 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005582 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5583 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005584 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005585 sz++;
5586 prev_fnum = qfp->qf_fnum;
5587 }
5588 }
5589 }
5590
5591 return sz;
5592}
5593
5594/*
5595 * Returns the current index of the quickfix/location list.
5596 * Returns 0 if there is an error.
5597 */
5598 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005599qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005600{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005601 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005602
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005603 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5604 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005605
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005606 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005607}
5608
5609/*
5610 * Returns the current index in the quickfix/location list (counting only valid
5611 * entries). If no valid entries are in the list, then returns 1.
5612 */
5613 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005614qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005615{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005616 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005617 qf_list_T *qfl;
5618 qfline_T *qfp;
5619 int i, eidx = 0;
5620 int prev_fnum = 0;
5621
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005622 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5623 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005624
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005625 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005626 qfp = qfl->qf_start;
5627
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005628 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005629 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005630 return 1;
5631
5632 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5633 {
5634 if (qfp->qf_valid)
5635 {
5636 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5637 {
5638 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5639 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005640 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005641 eidx++;
5642 prev_fnum = qfp->qf_fnum;
5643 }
5644 }
5645 else
5646 eidx++;
5647 }
5648 }
5649
5650 return eidx ? eidx : 1;
5651}
5652
5653/*
5654 * Get the 'n'th valid error entry in the quickfix or location list.
5655 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5656 * For :cdo and :ldo returns the 'n'th valid error entry.
5657 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5658 */
5659 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005660qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005661{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005662 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005663 int i, eidx;
5664 int prev_fnum = 0;
5665
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005666 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005667 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005668 return 1;
5669
Bram Moolenaar95946f12019-03-31 15:31:59 +02005670 eidx = 0;
5671 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005672 {
5673 if (qfp->qf_valid)
5674 {
5675 if (fdo)
5676 {
5677 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5678 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005679 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005680 eidx++;
5681 prev_fnum = qfp->qf_fnum;
5682 }
5683 }
5684 else
5685 eidx++;
5686 }
5687
5688 if (eidx == n)
5689 break;
5690 }
5691
5692 if (i <= qfl->qf_count)
5693 return i;
5694 else
5695 return 1;
5696}
5697
5698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005700 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005701 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 */
5703 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005704ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005706 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005707 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005708
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005709 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5710 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005711
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005712 if (eap->addr_count > 0)
5713 errornr = (int)eap->line2;
5714 else
5715 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005716 switch (eap->cmdidx)
5717 {
5718 case CMD_cc: case CMD_ll:
5719 errornr = 0;
5720 break;
5721 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5722 case CMD_lfirst:
5723 errornr = 1;
5724 break;
5725 default:
5726 errornr = 32767;
5727 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005728 }
5729
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005730 // For cdo and ldo commands, jump to the nth valid error.
5731 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005732 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5733 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005734 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005735 eap->addr_count > 0 ? (int)eap->line1 : 1,
5736 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5737
5738 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739}
5740
5741/*
5742 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005743 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005744 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 */
5746 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005747ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005749 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005750 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005751 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005752
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005753 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5754 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005755
Bram Moolenaar55b69262017-08-13 13:42:01 +02005756 if (eap->addr_count > 0
5757 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5758 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005759 errornr = (int)eap->line2;
5760 else
5761 errornr = 1;
5762
Bram Moolenaar39665952018-08-15 20:59:48 +02005763 // Depending on the command jump to either next or previous entry/file.
5764 switch (eap->cmdidx)
5765 {
5766 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5767 dir = FORWARD;
5768 break;
5769 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5770 case CMD_lNext:
5771 dir = BACKWARD;
5772 break;
5773 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5774 dir = FORWARD_FILE;
5775 break;
5776 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5777 dir = BACKWARD_FILE;
5778 break;
5779 default:
5780 dir = FORWARD;
5781 break;
5782 }
5783
5784 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785}
5786
5787/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005788 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5789 * The index of the entry is stored in 'errornr'.
5790 * Returns NULL if an entry is not found.
5791 */
5792 static qfline_T *
5793qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5794{
5795 qfline_T *qfp = NULL;
5796 int idx = 0;
5797
5798 // Find the first entry in this file
5799 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5800 if (qfp->qf_fnum == bnr)
5801 break;
5802
5803 *errornr = idx;
5804 return qfp;
5805}
5806
5807/*
5808 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5809 * with the error number for the first entry. Assumes the entries are sorted in
5810 * the quickfix list by line number.
5811 */
5812 static qfline_T *
5813qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5814{
5815 while (!got_int
5816 && entry->qf_prev != NULL
5817 && entry->qf_fnum == entry->qf_prev->qf_fnum
5818 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5819 {
5820 entry = entry->qf_prev;
5821 --*errornr;
5822 }
5823
5824 return entry;
5825}
5826
5827/*
5828 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5829 * with the error number for the last entry. Assumes the entries are sorted in
5830 * the quickfix list by line number.
5831 */
5832 static qfline_T *
5833qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5834{
5835 while (!got_int &&
5836 entry->qf_next != NULL
5837 && entry->qf_fnum == entry->qf_next->qf_fnum
5838 && entry->qf_lnum == entry->qf_next->qf_lnum)
5839 {
5840 entry = entry->qf_next;
5841 ++*errornr;
5842 }
5843
5844 return entry;
5845}
5846
5847/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005848 * Returns TRUE if the specified quickfix entry is
5849 * after the given line (linewise is TRUE)
5850 * or after the line and column.
5851 */
5852 static int
5853qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5854{
5855 if (linewise)
5856 return qfp->qf_lnum > pos->lnum;
5857 else
5858 return (qfp->qf_lnum > pos->lnum ||
5859 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5860}
5861
5862/*
5863 * Returns TRUE if the specified quickfix entry is
5864 * before the given line (linewise is TRUE)
5865 * or before the line and column.
5866 */
5867 static int
5868qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5869{
5870 if (linewise)
5871 return qfp->qf_lnum < pos->lnum;
5872 else
5873 return (qfp->qf_lnum < pos->lnum ||
5874 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5875}
5876
5877/*
5878 * Returns TRUE if the specified quickfix entry is
5879 * on or after the given line (linewise is TRUE)
5880 * or on or after the line and column.
5881 */
5882 static int
5883qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5884{
5885 if (linewise)
5886 return qfp->qf_lnum >= pos->lnum;
5887 else
5888 return (qfp->qf_lnum > pos->lnum ||
5889 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5890}
5891
5892/*
5893 * Returns TRUE if the specified quickfix entry is
5894 * on or before the given line (linewise is TRUE)
5895 * or on or before the line and column.
5896 */
5897 static int
5898qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5899{
5900 if (linewise)
5901 return qfp->qf_lnum <= pos->lnum;
5902 else
5903 return (qfp->qf_lnum < pos->lnum ||
5904 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5905}
5906
5907/*
5908 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5909 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5910 * multiple entries on a single line as one. Otherwise returns the entry after
5911 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005912 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5913 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005914 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005915 */
5916 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005917qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005918 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005919 pos_T *pos,
5920 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005921 qfline_T *qfp,
5922 int *errornr)
5923{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005924 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005925 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005926 return qfp;
5927
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005928 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005929 while (qfp->qf_next != NULL
5930 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005931 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005932 {
5933 qfp = qfp->qf_next;
5934 ++*errornr;
5935 }
5936
5937 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005938 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005939 return NULL;
5940
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005941 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005942 qfp = qfp->qf_next;
5943 ++*errornr;
5944
5945 return qfp;
5946}
5947
5948/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005949 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5950 * If 'linewise' is TRUE, returns the entry before the specified line and
5951 * treats multiple entries on a single line as one. Otherwise returns the entry
5952 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005953 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5954 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005955 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005956 */
5957 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005958qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005959 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005960 pos_T *pos,
5961 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005962 qfline_T *qfp,
5963 int *errornr)
5964{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005965 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005966 while (qfp->qf_next != NULL
5967 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005968 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005969 {
5970 qfp = qfp->qf_next;
5971 ++*errornr;
5972 }
5973
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005974 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005975 return NULL;
5976
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005977 if (linewise)
5978 // If multiple entries are on the same line, then use the first entry
5979 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005980
5981 return qfp;
5982}
5983
5984/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005985 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005986 * the direction 'dir'.
5987 */
5988 static qfline_T *
5989qf_find_closest_entry(
5990 qf_list_T *qfl,
5991 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005992 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005993 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005994 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005995 int *errornr)
5996{
5997 qfline_T *qfp;
5998
5999 *errornr = 0;
6000
6001 // Find the first entry in this file
6002 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
6003 if (qfp == NULL)
6004 return NULL; // no entry in this file
6005
6006 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006007 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006008 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006009 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006010
6011 return qfp;
6012}
6013
6014/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006015 * Get the nth quickfix entry below the specified entry. Searches forward in
6016 * the list. If linewise is TRUE, then treat multiple entries on a single line
6017 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006018 */
Bram Moolenaar64416122019-06-07 21:37:13 +02006019 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006020qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006021{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006022 qfline_T *entry = entry_arg;
6023
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006024 while (n-- > 0 && !got_int)
6025 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006026 int first_errornr = *errornr;
6027
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006028 if (linewise)
6029 // Treat all the entries on the same line in this file as one
6030 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006031
6032 if (entry->qf_next == NULL
6033 || entry->qf_next->qf_fnum != entry->qf_fnum)
6034 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006035 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006036 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006037 break;
6038 }
6039
6040 entry = entry->qf_next;
6041 ++*errornr;
6042 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006043}
6044
6045/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006046 * Get the nth quickfix entry above the specified entry. Searches backwards in
6047 * the list. If linewise is TRUE, then treat multiple entries on a single line
6048 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006049 */
Bram Moolenaar64416122019-06-07 21:37:13 +02006050 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006051qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006052{
6053 while (n-- > 0 && !got_int)
6054 {
6055 if (entry->qf_prev == NULL
6056 || entry->qf_prev->qf_fnum != entry->qf_fnum)
6057 break;
6058
6059 entry = entry->qf_prev;
6060 --*errornr;
6061
6062 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006063 if (linewise)
6064 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006065 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006066}
6067
6068/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006069 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
6070 * the specified direction. Returns the error number in the quickfix list or 0
6071 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006072 */
6073 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006074qf_find_nth_adj_entry(
6075 qf_list_T *qfl,
6076 int bnr,
6077 pos_T *pos,
6078 int n,
6079 int dir,
6080 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006081{
6082 qfline_T *adj_entry;
6083 int errornr;
6084
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006085 // Find an entry closest to the specified position
6086 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006087 if (adj_entry == NULL)
6088 return 0;
6089
6090 if (--n > 0)
6091 {
6092 // Go to the n'th entry in the current buffer
6093 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02006094 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006095 else
Bram Moolenaar64416122019-06-07 21:37:13 +02006096 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006097 }
6098
6099 return errornr;
6100}
6101
6102/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006103 * Jump to a quickfix entry in the current file nearest to the current line or
6104 * current line/col.
6105 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
6106 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006107 */
6108 void
6109ex_cbelow(exarg_T *eap)
6110{
6111 qf_info_T *qi;
6112 qf_list_T *qfl;
6113 int dir;
6114 int buf_has_flag;
6115 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006116 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006117
6118 if (eap->addr_count > 0 && eap->line2 <= 0)
6119 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006120 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006121 return;
6122 }
6123
6124 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006125 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
6126 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006127 buf_has_flag = BUF_HAS_QF_ENTRY;
6128 else
6129 buf_has_flag = BUF_HAS_LL_ENTRY;
6130 if (!(curbuf->b_has_qf_entry & buf_has_flag))
6131 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006132 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006133 return;
6134 }
6135
6136 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
6137 return;
6138
6139 qfl = qf_get_curlist(qi);
6140 // check if the list has valid errors
6141 if (!qf_list_has_valid_entries(qfl))
6142 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006143 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006144 return;
6145 }
6146
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006147 if (eap->cmdidx == CMD_cbelow
6148 || eap->cmdidx == CMD_lbelow
6149 || eap->cmdidx == CMD_cafter
6150 || eap->cmdidx == CMD_lafter)
6151 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006152 dir = FORWARD;
6153 else
6154 dir = BACKWARD;
6155
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006156 pos = curwin->w_cursor;
6157 // A quickfix entry column number is 1 based whereas cursor column
6158 // number is 0 based. Adjust the column number.
6159 pos.col++;
6160 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
6161 eap->addr_count > 0 ? eap->line2 : 0, dir,
6162 eap->cmdidx == CMD_cbelow
6163 || eap->cmdidx == CMD_lbelow
6164 || eap->cmdidx == CMD_cabove
6165 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006166
6167 if (errornr > 0)
6168 qf_jump(qi, 0, errornr, FALSE);
6169 else
6170 emsg(_(e_no_more_items));
6171}
6172
6173/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01006174 * Return the autocmd name for the :cfile Ex commands
6175 */
6176 static char_u *
6177cfile_get_auname(cmdidx_T cmdidx)
6178{
6179 switch (cmdidx)
6180 {
6181 case CMD_cfile: return (char_u *)"cfile";
6182 case CMD_cgetfile: return (char_u *)"cgetfile";
6183 case CMD_caddfile: return (char_u *)"caddfile";
6184 case CMD_lfile: return (char_u *)"lfile";
6185 case CMD_lgetfile: return (char_u *)"lgetfile";
6186 case CMD_laddfile: return (char_u *)"laddfile";
6187 default: return NULL;
6188 }
6189}
6190
6191/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006192 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006193 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 */
6195 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006196ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006198 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006199 win_T *wp = NULL;
64-bitman88d41ab2025-04-06 17:20:39 +02006200 qf_info_T *qi = ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01006201 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006202 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006203 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006204
Bram Moolenaara16123a2019-03-28 20:31:07 +01006205 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02006206 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6207 NULL, FALSE, curbuf))
6208 {
6209#ifdef FEAT_EVAL
6210 if (aborting())
6211 return;
6212#endif
6213 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006214
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006215 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02006216#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006217 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02006218 {
6219 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02006220 NULL, NULL,
6221 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02006222 if (browse_file == NULL)
6223 return;
6224 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
6225 vim_free(browse_file);
6226 }
6227 else
6228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006230 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006231
Bram Moolenaar39665952018-08-15 20:59:48 +02006232 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01006233 wp = curwin;
6234
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006235 incr_quickfix_busy();
6236
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006237 // This function is used by the :cfile, :cgetfile and :caddfile
6238 // commands.
Colin Kennedy21570352024-03-03 16:16:47 +01006239 // :cfile always creates a new quickfix list and may jump to the
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006240 // first error.
6241 // :cgetfile creates a new quickfix list but doesn't jump to the
6242 // first error.
6243 // :caddfile adds to an existing quickfix list. If there is no
6244 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006245 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006246 && eap->cmdidx != CMD_laddfile),
6247 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006248 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006249 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01006250 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006251 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006252 {
6253 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006254 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006255 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006256 }
64-bitman88d41ab2025-04-06 17:20:39 +02006257 else if (qi == NULL)
6258 {
6259 decr_quickfix_busy();
6260 return;
6261 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006262 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006263 qf_list_changed(qf_get_curlist(qi));
6264 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006265 if (au_name != NULL)
6266 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01006267
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006268 // Jump to the first error for a new list and if autocmds didn't
6269 // free the list.
6270 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
6271 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006272 // display the first error
6273 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006274
6275 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006276}
6277
6278/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006279 * Return the vimgrep autocmd name.
6280 */
6281 static char_u *
6282vgr_get_auname(cmdidx_T cmdidx)
6283{
6284 switch (cmdidx)
6285 {
6286 case CMD_vimgrep: return (char_u *)"vimgrep";
6287 case CMD_lvimgrep: return (char_u *)"lvimgrep";
6288 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
6289 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
6290 case CMD_grep: return (char_u *)"grep";
6291 case CMD_lgrep: return (char_u *)"lgrep";
6292 case CMD_grepadd: return (char_u *)"grepadd";
6293 case CMD_lgrepadd: return (char_u *)"lgrepadd";
6294 default: return NULL;
6295 }
6296}
6297
6298/*
6299 * Initialize the regmatch used by vimgrep for pattern "s".
6300 */
6301 static void
6302vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
6303{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006304 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006305 regmatch->regprog = NULL;
6306
6307 if (s == NULL || *s == NUL)
6308 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006309 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006310 if (last_search_pat() == NULL)
6311 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006312 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006313 return;
6314 }
6315 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
6316 }
6317 else
6318 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
6319
6320 regmatch->rmm_ic = p_ic;
6321 regmatch->rmm_maxcol = 0;
6322}
6323
6324/*
6325 * Display a file name when vimgrep is running.
6326 */
6327 static void
6328vgr_display_fname(char_u *fname)
6329{
6330 char_u *p;
6331
6332 msg_start();
6333 p = msg_strtrunc(fname, TRUE);
6334 if (p == NULL)
6335 msg_outtrans(fname);
6336 else
6337 {
6338 msg_outtrans(p);
6339 vim_free(p);
6340 }
6341 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006342 msg_didout = FALSE; // overwrite this message
6343 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006344 msg_col = 0;
6345 out_flush();
6346}
6347
6348/*
6349 * Load a dummy buffer to search for a pattern using vimgrep.
6350 */
6351 static buf_T *
6352vgr_load_dummy_buf(
6353 char_u *fname,
6354 char_u *dirname_start,
6355 char_u *dirname_now)
6356{
6357 int save_mls;
6358#if defined(FEAT_SYN_HL)
6359 char_u *save_ei = NULL;
6360#endif
6361 buf_T *buf;
6362
6363#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006364 // Don't do Filetype autocommands to avoid loading syntax and
6365 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006366 save_ei = au_event_disable(",Filetype");
6367#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006368 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006369 save_mls = p_mls;
6370 p_mls = 0;
6371
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006372 // Load file into a buffer, so that 'fileencoding' is detected,
6373 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006374 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
6375
6376 p_mls = save_mls;
6377#if defined(FEAT_SYN_HL)
6378 au_event_restore(save_ei);
6379#endif
6380
6381 return buf;
6382}
6383
6384/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006385 * Check whether a quickfix/location list is valid. Autocmds may remove or
6386 * change a quickfix list when vimgrep is running. If the list is not found,
6387 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006388 */
6389 static int
6390vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006391 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006392 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006393 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006394 char_u *title)
6395{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006396 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006397 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006398 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006399 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006400 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006401 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02006402 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006403 return FALSE;
6404 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006405 else
6406 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006407 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006408 qf_new_list(qi, title);
6409 return TRUE;
6410 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006411 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006412
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02006413 if (qf_restore_list(qi, qfid) == FAIL)
6414 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006415
6416 return TRUE;
6417}
6418
6419/*
6420 * Search for a pattern in all the lines in a buffer and add the matching lines
6421 * to a quickfix list.
6422 */
6423 static int
6424vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006425 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006426 char_u *fname,
6427 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006428 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006429 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01006430 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006431 int duplicate_name,
6432 int flags)
6433{
6434 int found_match = FALSE;
6435 long lnum;
6436 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02006437 int pat_len = (int)STRLEN(spat);
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006438 if (pat_len > MAX_FUZZY_MATCHES)
6439 pat_len = MAX_FUZZY_MATCHES;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006440
Bram Moolenaar1c299432018-10-28 14:36:09 +01006441 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006442 {
6443 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006444 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006445 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006446 // Regular expression match
6447 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006448 col, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006449 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006450 // Pass the buffer number so that it gets used even for a
6451 // dummy buffer, unless duplicate_name is set, then the
6452 // buffer will be wiped out below.
6453 if (qf_add_entry(qfl,
6454 NULL, // dir
6455 fname,
6456 NULL,
6457 duplicate_name ? 0 : buf->b_fnum,
6458 ml_get_buf(buf,
6459 regmatch->startpos[0].lnum + lnum, FALSE),
6460 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02006461 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006462 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02006463 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006464 FALSE, // vis_col
6465 NULL, // search pattern
6466 0, // nr
6467 0, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02006468 NULL, // user_data
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006469 TRUE // valid
6470 ) == QF_FAIL)
6471 {
6472 got_int = TRUE;
6473 break;
6474 }
6475 found_match = TRUE;
6476 if (--*tomatch == 0)
6477 break;
6478 if ((flags & VGR_GLOBAL) == 0
6479 || regmatch->endpos[0].lnum > 0)
6480 break;
6481 col = regmatch->endpos[0].col
6482 + (col == regmatch->endpos[0].col);
zeertzjq94b7c322024-03-12 21:50:32 +01006483 if (col > ml_get_buf_len(buf, lnum))
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006484 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006485 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006486 }
6487 else
6488 {
6489 char_u *str = ml_get_buf(buf, lnum, FALSE);
zeertzjq8c55d602024-03-13 20:42:26 +01006490 colnr_T linelen = ml_get_buf_len(buf, lnum);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006491 int score;
6492 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006493 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006494
6495 // Fuzzy string match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006496 CLEAR_FIELD(matches);
glepnir28e40a72025-03-16 21:24:22 +01006497 while (fuzzy_match(str + col, spat, FALSE, &score,
6498 matches, sz, TRUE) > 0)
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006499 {
6500 // Pass the buffer number so that it gets used even for a
6501 // dummy buffer, unless duplicate_name is set, then the
6502 // buffer will be wiped out below.
6503 if (qf_add_entry(qfl,
6504 NULL, // dir
6505 fname,
6506 NULL,
6507 duplicate_name ? 0 : buf->b_fnum,
6508 str,
6509 lnum,
thinca6864efa2021-06-19 20:45:20 +02006510 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006511 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006512 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006513 FALSE, // vis_col
6514 NULL, // search pattern
6515 0, // nr
6516 0, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02006517 NULL, // user_data
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006518 TRUE // valid
6519 ) == QF_FAIL)
6520 {
6521 got_int = TRUE;
6522 break;
6523 }
6524 found_match = TRUE;
6525 if (--*tomatch == 0)
6526 break;
6527 if ((flags & VGR_GLOBAL) == 0)
6528 break;
6529 col = matches[pat_len - 1] + col + 1;
zeertzjq8c55d602024-03-13 20:42:26 +01006530 if (col > linelen)
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006531 break;
6532 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006533 }
6534 line_breakcheck();
6535 if (got_int)
6536 break;
6537 }
6538
6539 return found_match;
6540}
6541
6542/*
6543 * Jump to the first match and update the directory.
6544 */
6545 static void
6546vgr_jump_to_match(
6547 qf_info_T *qi,
6548 int forceit,
6549 int *redraw_for_dummy,
6550 buf_T *first_match_buf,
6551 char_u *target_dir)
6552{
6553 buf_T *buf;
6554
6555 buf = curbuf;
6556 qf_jump(qi, 0, 0, forceit);
6557 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006558 // If we jumped to another buffer redrawing will already be
6559 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006560 *redraw_for_dummy = FALSE;
6561
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006562 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006563 if (curbuf == first_match_buf && target_dir != NULL)
6564 {
6565 exarg_T ea;
6566
Bram Moolenaara80faa82020-04-12 19:37:17 +02006567 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006568 ea.arg = target_dir;
6569 ea.cmdidx = CMD_lcd;
6570 ex_cd(&ea);
6571 }
6572}
6573
6574/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006575 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006576 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006577typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006578{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006579 long tomatch; // maximum number of matches to find
6580 char_u *spat; // search pattern
6581 int flags; // search modifier
6582 char_u **fnames; // list of files to search
6583 int fcount; // number of files
6584 regmmatch_T regmatch; // compiled search pattern
6585 char_u *qf_title; // quickfix list title
6586} vgr_args_T;
6587
6588/*
6589 * Process :vimgrep command arguments. The command syntax is:
6590 *
6591 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6592 */
6593 static int
6594vgr_process_args(
6595 exarg_T *eap,
6596 vgr_args_T *args)
6597{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006598 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006599
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00006600 CLEAR_POINTER(args);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006601
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006602 args->regmatch.regprog = NULL;
6603 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006604
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006605 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006606 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006607 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006608 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006609
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006610 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006611 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006612 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006613 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006614 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006615 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006616 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006617
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006618 vgr_init_regmatch(&args->regmatch, args->spat);
6619 if (args->regmatch.regprog == NULL)
6620 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006621
6622 p = skipwhite(p);
6623 if (*p == NUL)
6624 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006625 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006626 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006627 }
6628
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006629 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006630 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6631 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006632 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006633 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006634 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006635 }
6636
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006637 return OK;
6638}
6639
6640/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006641 * Return TRUE if "buf" had an existing swap file, the current swap file does
6642 * not end in ".swp".
6643 */
6644 static int
6645existing_swapfile(buf_T *buf)
6646{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006647 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006648 {
6649 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6650 size_t len = STRLEN(fname);
6651
6652 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6653 }
6654 return FALSE;
6655}
6656
6657/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006658 * Search for a pattern in a list of files and populate the quickfix list with
6659 * the matches.
6660 */
6661 static int
6662vgr_process_files(
6663 win_T *wp,
6664 qf_info_T *qi,
6665 vgr_args_T *cmd_args,
6666 int *redraw_for_dummy,
6667 buf_T **first_match_buf,
6668 char_u **target_dir)
6669{
6670 int status = FAIL;
6671 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6672 time_t seconds = 0;
6673 char_u *fname;
6674 int fi;
6675 buf_T *buf;
6676 int duplicate_name = FALSE;
6677 int using_dummy;
6678 char_u *dirname_start = NULL;
6679 char_u *dirname_now = NULL;
6680 int found_match;
6681 aco_save_T aco;
6682
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006683 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6684 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006685 if (dirname_start == NULL || dirname_now == NULL)
6686 goto theend;
6687
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006688 // Remember the current directory, because a BufRead autocommand that does
6689 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006690 mch_dirname(dirname_start, MAXPATHL);
6691
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006692 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006693 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6694 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006695 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006696 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006697 if (time(NULL) > seconds)
6698 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006699 // Display the file name every second or so, show the user we are
6700 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006701 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006702 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006703 }
6704
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006705 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006706 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6707 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006708 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006709 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006710 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006711 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006712
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006713 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006714 }
6715 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006716 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006717 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006718
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006719 // Check whether the quickfix list is still valid. When loading a
6720 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006721 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006722 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006723
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006724 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006725
Bram Moolenaar81695252004-12-29 20:58:21 +00006726 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006727 {
6728 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006729 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006730 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006731 else
6732 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006733 // Try for a match in all lines of the buffer.
6734 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006735 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006736 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006737 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006738
Bram Moolenaar81695252004-12-29 20:58:21 +00006739 if (using_dummy)
6740 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006741 if (found_match && *first_match_buf == NULL)
6742 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006743 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006744 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006745 // Never keep a dummy buffer if there is another buffer
6746 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006747 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006748 buf = NULL;
6749 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006750 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006751 || buf->b_p_bh[0] == 'u' // "unload"
6752 || buf->b_p_bh[0] == 'w' // "wipe"
6753 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006754 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006755 // When no match was found we don't need to remember the
6756 // buffer, wipe it out. If there was a match and it
6757 // wasn't the first one or we won't jump there: only
6758 // unload the buffer.
6759 // Ignore 'hidden' here, because it may lead to having too
6760 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006761 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006762 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006763 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006764 buf = NULL;
6765 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006766 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006767 || (cmd_args->flags & VGR_NOJUMP)
6768 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006769 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006770 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006771 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006772 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006773 buf = NULL;
6774 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006775 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006776
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006777 if (buf != NULL)
6778 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006779 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006780 buf->b_flags &= ~BF_DUMMY;
6781
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006782 // If the buffer is still loaded we need to use the
6783 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006784 if (buf == *first_match_buf
6785 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006786 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006787 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006788
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006789 // The buffer is still loaded, the Filetype autocommands
6790 // need to be done now, in that buffer. And the modelines
6791 // need to be done (again). But not the window-local
6792 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006793 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006794 if (curbuf == buf)
6795 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006796#if defined(FEAT_SYN_HL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00006797 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006798 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006799#endif
Bram Moolenaare76062c2022-11-28 18:51:43 +00006800 do_modelines(OPT_NOWIN);
6801 aucmd_restbuf(&aco);
6802 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006803 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006804 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006805 }
6806 }
6807
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006808 status = OK;
6809
6810theend:
6811 vim_free(dirname_now);
6812 vim_free(dirname_start);
6813 return status;
6814}
6815
6816/*
6817 * ":vimgrep {pattern} file(s)"
6818 * ":vimgrepadd {pattern} file(s)"
6819 * ":lvimgrep {pattern} file(s)"
6820 * ":lvimgrepadd {pattern} file(s)"
6821 */
6822 void
6823ex_vimgrep(exarg_T *eap)
6824{
6825 vgr_args_T args;
6826 qf_info_T *qi;
6827 qf_list_T *qfl;
6828 int_u save_qfid;
6829 win_T *wp = NULL;
6830 int redraw_for_dummy = FALSE;
6831 buf_T *first_match_buf = NULL;
6832 char_u *target_dir = NULL;
6833 char_u *au_name = NULL;
6834 int status;
6835
Colin Kennedy21570352024-03-03 16:16:47 +01006836 if (!check_can_set_curbuf_forceit(eap->forceit))
6837 return;
6838
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006839 au_name = vgr_get_auname(eap->cmdidx);
6840 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6841 curbuf->b_fname, TRUE, curbuf))
6842 {
6843#ifdef FEAT_EVAL
6844 if (aborting())
6845 return;
6846#endif
6847 }
6848
6849 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6850 if (qi == NULL)
6851 return;
6852
6853 if (vgr_process_args(eap, &args) == FAIL)
6854 goto theend;
6855
6856 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6857 && eap->cmdidx != CMD_vimgrepadd
6858 && eap->cmdidx != CMD_lvimgrepadd)
6859 || qf_stack_empty(qi))
6860 // make place for a new list
6861 qf_new_list(qi, args.qf_title);
6862
6863 incr_quickfix_busy();
6864
6865 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6866 &first_match_buf, &target_dir);
6867 if (status != OK)
6868 {
6869 FreeWild(args.fcount, args.fnames);
6870 decr_quickfix_busy();
6871 goto theend;
6872 }
6873
6874 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006875
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006876 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006877 qfl->qf_nonevalid = FALSE;
6878 qfl->qf_ptr = qfl->qf_start;
6879 qfl->qf_index = 1;
6880 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006881
Bram Moolenaar864293a2016-06-02 13:40:04 +02006882 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006883
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006884 // Remember the current quickfix list identifier, so that we can check for
6885 // autocommands changing the current quickfix list.
6886 save_qfid = qf_get_curlist(qi)->qf_id;
6887
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006888 if (au_name != NULL)
6889 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6890 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006891 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6892 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006893 if (!qflist_valid(wp, save_qfid)
6894 || qf_restore_list(qi, save_qfid) == FAIL)
6895 {
6896 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006897 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006898 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006899
zeertzjq8c55d602024-03-13 20:42:26 +01006900 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006901 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006902 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006903 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006904 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6905 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006906 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006907 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006908 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006909
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006910 decr_quickfix_busy();
6911
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006912 // If we loaded a dummy buffer into the current window, the autocommands
6913 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006914 if (redraw_for_dummy)
6915 {
6916#ifdef FEAT_FOLDING
6917 foldUpdateAll(curwin);
6918#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006919 redraw_later(UPD_NOT_VALID);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006920#endif
6921 }
6922
Bram Moolenaar86b68352004-12-27 21:59:20 +00006923theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006924 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006925 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006926 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006927}
6928
6929/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006930 * Restore current working directory to "dirname_start" if they differ, taking
6931 * into account whether it is set locally or globally.
6932 */
6933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006934restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006935{
6936 char_u *dirname_now = alloc(MAXPATHL);
6937
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006938 if (dirname_now == NULL)
6939 return;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006940
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006941 mch_dirname(dirname_now, MAXPATHL);
6942 if (STRCMP(dirname_start, dirname_now) != 0)
6943 {
6944 // If the directory has changed, change it back by building up an
6945 // appropriate ex command and executing it.
6946 exarg_T ea;
6947
6948 CLEAR_FIELD(ea);
6949 ea.arg = dirname_start;
6950 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6951 ex_cd(&ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006952 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006953 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006954}
6955
6956/*
6957 * Load file "fname" into a dummy buffer and return the buffer pointer,
6958 * placing the directory resulting from the buffer load into the
6959 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6960 * prior to calling this function. Restores directory to "dirname_start" prior
6961 * to returning, if autocmds or the 'autochdir' option have changed it.
6962 *
6963 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6964 * or wipe_dummy_buffer() later!
6965 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006966 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006967 */
6968 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006969load_dummy_buffer(
6970 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006971 char_u *dirname_start, // in: old directory
6972 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006973{
6974 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006975 bufref_T newbufref;
6976 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006977 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006978 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006979 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006980
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006981 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006982 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6983 if (newbuf == NULL)
6984 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006985 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006986
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006987 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006988 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6989
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006990 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006991 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006992 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006993 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006994 ++newbuf->b_locked;
6995
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006996 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006997 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006998 if (curbuf == newbuf)
Bram Moolenaar81695252004-12-29 20:58:21 +00006999 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00007000 // Need to set the filename for autocommands.
7001 (void)setfname(curbuf, fname, NULL, FALSE);
7002
7003 // Create swap file now to avoid the ATTENTION message.
7004 check_need_swap(TRUE);
7005
7006 // Remove the "dummy" flag, otherwise autocommands may not
7007 // work.
7008 curbuf->b_flags &= ~BF_DUMMY;
7009
7010 newbuf_to_wipe.br_buf = NULL;
7011 readfile_result = readfile(fname, NULL,
7012 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
7013 NULL, READ_NEW | READ_DUMMY);
7014 --newbuf->b_locked;
7015 if (readfile_result == OK
7016 && !got_int
7017 && !(curbuf->b_flags & BF_NEW))
Bram Moolenaar81695252004-12-29 20:58:21 +00007018 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00007019 failed = FALSE;
7020 if (curbuf != newbuf)
7021 {
7022 // Bloody autocommands changed the buffer! Can happen when
7023 // using netrw and editing a remote file. Use the current
7024 // buffer instead, delete the dummy one after restoring the
7025 // window stuff.
7026 set_bufref(&newbuf_to_wipe, newbuf);
7027 newbuf = curbuf;
7028 }
Bram Moolenaar81695252004-12-29 20:58:21 +00007029 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00007030
7031 // restore curwin/curbuf and a few other things
7032 aucmd_restbuf(&aco);
Bram Moolenaar81695252004-12-29 20:58:21 +00007033
Bram Moolenaar84497cd2022-11-28 20:34:52 +00007034 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
7035 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
7036 }
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02007037
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007038 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
7039 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02007040 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00007041 }
Bram Moolenaar81695252004-12-29 20:58:21 +00007042
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007043 // When autocommands/'autochdir' option changed directory: go back.
7044 // Let the caller know what the resulting dir was first, in case it is
7045 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007046 mch_dirname(resulting_dir, MAXPATHL);
7047 restore_start_dir(dirname_start);
7048
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007049 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00007050 return NULL;
7051 if (failed)
7052 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007053 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00007054 return NULL;
7055 }
7056 return newbuf;
7057}
7058
7059/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007060 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
7061 * directory to "dirname_start" prior to returning, if autocmds or the
7062 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00007063 */
7064 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007065wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00007066{
Bram Moolenaar2573af32020-03-14 17:21:34 +01007067 // If any autocommand opened a window on the dummy buffer, close that
7068 // window. If we can't close them all then give up.
7069 while (buf->b_nwindows > 0)
7070 {
7071 int did_one = FALSE;
7072 win_T *wp;
7073
7074 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007075 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01007076 if (wp->w_buffer == buf)
7077 {
7078 if (win_close(wp, FALSE) == OK)
7079 did_one = TRUE;
7080 break;
7081 }
7082 if (!did_one)
7083 return;
7084 }
7085
7086 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00007087 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007088#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00007089 cleanup_T cs;
7090
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007091 // Reset the error/interrupt/exception state here so that aborting()
7092 // returns FALSE when wiping out the buffer. Otherwise it doesn't
7093 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00007094 enter_cleanup(&cs);
7095#endif
7096
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01007097 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00007098
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007099#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007100 // Restore the error/interrupt/exception state if not discarded by a
7101 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00007102 leave_cleanup(&cs);
7103#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007104 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007105 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00007106 }
Bram Moolenaar81695252004-12-29 20:58:21 +00007107}
7108
7109/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007110 * Unload the dummy buffer that load_dummy_buffer() created. Restores
7111 * directory to "dirname_start" prior to returning, if autocmds or the
7112 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00007113 */
7114 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007115unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00007116{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007117 if (curbuf == buf) // safety check
7118 return;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007119
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007120 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
7121
7122 // When autocommands/'autochdir' option changed directory: go back.
7123 restore_start_dir(dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00007124}
7125
Bram Moolenaar05159a02005-02-26 23:04:13 +00007126#if defined(FEAT_EVAL) || defined(PROTO)
7127/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007128 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01007129 * to 'list'. Returns OK on success.
7130 */
7131 static int
7132get_qfline_items(qfline_T *qfp, list_T *list)
7133{
7134 int bufnum;
7135 dict_T *dict;
7136 char_u buf[2];
7137
7138 // Handle entries with a non-existing buffer number.
7139 bufnum = qfp->qf_fnum;
7140 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7141 bufnum = 0;
7142
7143 if ((dict = dict_alloc()) == NULL)
7144 return FAIL;
7145 if (list_append_dict(list, dict) == FAIL)
7146 return FAIL;
7147
7148 buf[0] = qfp->qf_type;
7149 buf[1] = NUL;
7150 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02007151 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
7152 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
7153 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
7154 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
7155 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
7156 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01007157 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
7158 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
7159 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
7160 || dict_add_string(dict, "type", buf) == FAIL
Tom Praschanca6ac992023-08-11 23:26:12 +02007161 || (qfp->qf_user_data.v_type != VAR_UNKNOWN
7162 && dict_add_tv(dict, "user_data", &qfp->qf_user_data) == FAIL )
Bram Moolenaara16123a2019-03-28 20:31:07 +01007163 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
7164 return FAIL;
7165
7166 return OK;
7167}
7168
7169/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007170 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007171 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02007172 * If eidx is not 0, then return only the specified entry. Otherwise return
7173 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00007174 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007175 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007176get_errorlist(
7177 qf_info_T *qi_arg,
7178 win_T *wp,
7179 int qf_idx,
7180 int eidx,
7181 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007182{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007183 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007184 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007185 qfline_T *qfp;
7186 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007187
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007188 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007189 {
64-bitman88d41ab2025-04-06 17:20:39 +02007190 qi = ql_info;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007191 if (wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007192 qi = GET_LOC_LIST(wp);
64-bitman88d41ab2025-04-06 17:20:39 +02007193 if (qi == NULL)
7194 return FAIL;
7195
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007196 }
7197
Bram Moolenaar858ba062020-05-31 23:11:59 +02007198 if (eidx < 0)
7199 return OK;
7200
Bram Moolenaar29ce4092018-04-28 21:56:44 +02007201 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007202 qf_idx = qi->qf_curlist;
7203
Bram Moolenaar0398e002019-03-21 21:12:49 +01007204 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007205 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007206
Bram Moolenaar0398e002019-03-21 21:12:49 +01007207 qfl = qf_get_list(qi, qf_idx);
7208 if (qf_list_empty(qfl))
7209 return FAIL;
7210
Bram Moolenaara16123a2019-03-28 20:31:07 +01007211 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007212 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007213 if (eidx > 0)
7214 {
7215 if (eidx == i)
7216 return get_qfline_items(qfp, list);
7217 }
7218 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007219 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007220 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007221
Bram Moolenaar05159a02005-02-26 23:04:13 +00007222 return OK;
7223}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007224
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007225// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007226enum {
7227 QF_GETLIST_NONE = 0x0,
7228 QF_GETLIST_TITLE = 0x1,
7229 QF_GETLIST_ITEMS = 0x2,
7230 QF_GETLIST_NR = 0x4,
7231 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007232 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007233 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007234 QF_GETLIST_IDX = 0x40,
7235 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01007236 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007237 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007238 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02007239 QF_GETLIST_QFTF = 0x800,
7240 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007241};
7242
7243/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007244 * Parse text from 'di' and return the quickfix list items.
7245 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007246 */
7247 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02007248qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007249{
7250 int status = FAIL;
7251 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02007252 char_u *errorformat = p_efm;
7253 dictitem_T *efm_di;
7254 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007255
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007256 // Only a List value is supported
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007257 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7258 return FAIL;
7259
7260 // If errorformat is supplied then use it, otherwise use the 'efm'
7261 // option setting
7262 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007263 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007264 if (efm_di->di_tv.v_type != VAR_STRING ||
7265 efm_di->di_tv.vval.v_string == NULL)
Bram Moolenaarda732532017-08-31 20:58:02 +02007266 return FAIL;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007267 errorformat = efm_di->di_tv.vval.v_string;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007268 }
7269
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007270 l = list_alloc();
7271 if (l == NULL)
7272 return FAIL;
7273
64-bitman88d41ab2025-04-06 17:20:39 +02007274 qi = qf_alloc_stack(QFLT_INTERNAL, 1);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007275 if (qi != NULL)
7276 {
7277 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
7278 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
7279 {
7280 (void)get_errorlist(qi, NULL, 0, 0, l);
7281 qf_free(&qi->qf_lists[0]);
7282 }
64-bitman88d41ab2025-04-06 17:20:39 +02007283
7284 qf_free_lists(qi);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007285 }
7286 dict_add_list(retdict, "items", l);
7287 status = OK;
7288
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007289 return status;
7290}
7291
7292/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01007293 * Return the quickfix/location list window identifier in the current tabpage.
7294 */
7295 static int
7296qf_winid(qf_info_T *qi)
7297{
7298 win_T *win;
7299
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007300 // The quickfix window can be opened even if the quickfix list is not set
7301 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01007302 if (qi == NULL)
7303 return 0;
7304 win = qf_find_win(qi);
7305 if (win != NULL)
7306 return win->w_id;
7307 return 0;
7308}
7309
7310/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007311 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00007312 * window. If there is no buffer associated with the list or the buffer is
7313 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007314 */
7315 static int
7316qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
7317{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00007318 int bufnum = 0;
7319
7320 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
7321 bufnum = qi->qf_bufnr;
7322
7323 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007324}
7325
7326/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007327 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007328 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007329 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007330qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007331{
Bram Moolenaard823fa92016-08-12 16:29:27 +02007332 int flags = QF_GETLIST_NONE;
7333
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007334 if (dict_has_key(what, "all"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007335 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007336 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007337 if (!loclist)
7338 // File window ID is applicable only to location list windows
7339 flags &= ~ QF_GETLIST_FILEWINID;
7340 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007341
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007342 if (dict_has_key(what, "title"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007343 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007344
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007345 if (dict_has_key(what, "nr"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01007346 flags |= QF_GETLIST_NR;
7347
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007348 if (dict_has_key(what, "winid"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007349 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007350
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007351 if (dict_has_key(what, "context"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007352 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007353
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007354 if (dict_has_key(what, "id"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01007355 flags |= QF_GETLIST_ID;
7356
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007357 if (dict_has_key(what, "items"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007358 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007359
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007360 if (dict_has_key(what, "idx"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007361 flags |= QF_GETLIST_IDX;
7362
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007363 if (dict_has_key(what, "size"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007364 flags |= QF_GETLIST_SIZE;
7365
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007366 if (dict_has_key(what, "changedtick"))
Bram Moolenaarb254af32017-12-18 19:48:58 +01007367 flags |= QF_GETLIST_TICK;
7368
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007369 if (loclist && dict_has_key(what, "filewinid"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007370 flags |= QF_GETLIST_FILEWINID;
7371
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007372 if (dict_has_key(what, "qfbufnr"))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007373 flags |= QF_GETLIST_QFBUFNR;
7374
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007375 if (dict_has_key(what, "quickfixtextfunc"))
Bram Moolenaard43906d2020-07-20 21:31:32 +02007376 flags |= QF_GETLIST_QFTF;
7377
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007378 return flags;
7379}
Bram Moolenaara6d48492017-12-12 22:45:31 +01007380
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007381/*
7382 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
7383 * If 'nr' and 'id' are not present in 'what' then return the current
7384 * quickfix list index.
7385 * If 'nr' is zero then return the current quickfix list index.
7386 * If 'nr' is '$' then return the last quickfix list index.
7387 * If 'id' is present then return the index of the quickfix list with that id.
7388 * If 'id' is zero then return the quickfix list index specified by 'nr'.
7389 * Return -1, if quickfix list is not present or if the stack is empty.
7390 */
7391 static int
7392qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
7393{
7394 int qf_idx;
7395 dictitem_T *di;
7396
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007397 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007398 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7399 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007400 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007401 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01007402 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007403 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007404 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01007405 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007406 qf_idx = di->di_tv.vval.v_number - 1;
7407 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007408 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007409 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01007410 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007411 else if (di->di_tv.v_type == VAR_STRING
7412 && di->di_tv.vval.v_string != NULL
7413 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007414 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007415 qf_idx = qi->qf_listcount - 1;
7416 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007417 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007418 }
7419
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007420 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
7421 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007422 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007423 if (di->di_tv.v_type == VAR_NUMBER)
7424 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007425 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007426 if (di->di_tv.vval.v_number != 0)
7427 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
7428 }
7429 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007430 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007431 }
7432
7433 return qf_idx;
7434}
7435
7436/*
7437 * Return default values for quickfix list properties in retdict.
7438 */
7439 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007440qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007441{
7442 int status = OK;
7443
7444 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007445 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007446 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
7447 {
7448 list_T *l = list_alloc();
7449 if (l != NULL)
7450 status = dict_add_list(retdict, "items", l);
7451 else
7452 status = FAIL;
7453 }
7454 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007455 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007456 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007457 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007458 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007459 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007460 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007461 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007462 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007463 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007464 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007465 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007466 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007467 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007468 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007469 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007470 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7471 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007472 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7473 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007474
7475 return status;
7476}
7477
7478/*
7479 * Return the quickfix list title as 'title' in retdict
7480 */
7481 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007482qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007483{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007484 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007485}
7486
7487/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007488 * Returns the identifier of the window used to display files from a location
7489 * list. If there is no associated window, then returns 0. Useful only when
7490 * called from a location list window.
7491 */
7492 static int
7493qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7494{
7495 int winid = 0;
7496
7497 if (wp != NULL && IS_LL_WINDOW(wp))
7498 {
7499 win_T *ll_wp = qf_find_win_with_loclist(qi);
7500 if (ll_wp != NULL)
7501 winid = ll_wp->w_id;
7502 }
7503
7504 return dict_add_number(retdict, "filewinid", winid);
7505}
7506
7507/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007508 * Return the quickfix list items/entries as 'items' in retdict.
7509 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007510 */
7511 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007512qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007513{
7514 int status = OK;
7515 list_T *l = list_alloc();
7516 if (l != NULL)
7517 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007518 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007519 dict_add_list(retdict, "items", l);
7520 }
7521 else
7522 status = FAIL;
7523
7524 return status;
7525}
7526
7527/*
7528 * Return the quickfix list context (if any) as 'context' in retdict.
7529 */
7530 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007531qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007532{
7533 int status;
7534 dictitem_T *di;
7535
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007536 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007537 {
7538 di = dictitem_alloc((char_u *)"context");
7539 if (di != NULL)
7540 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007541 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007542 status = dict_add(retdict, di);
7543 if (status == FAIL)
7544 dictitem_free(di);
7545 }
7546 else
7547 status = FAIL;
7548 }
7549 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007550 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007551
7552 return status;
7553}
7554
7555/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007556 * Return the current quickfix list index as 'idx' in retdict.
7557 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007558 */
7559 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007560qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007561{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007562 if (eidx == 0)
7563 {
7564 eidx = qfl->qf_index;
7565 if (qf_list_empty(qfl))
7566 // For empty lists, current index is set to 0
7567 eidx = 0;
7568 }
7569 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007570}
7571
7572/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007573 * Return the 'quickfixtextfunc' function of a quickfix/location list
7574 */
7575 static int
7576qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7577{
7578 int status;
7579
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007580 if (qfl->qf_qftf_cb.cb_name != NULL)
Bram Moolenaard43906d2020-07-20 21:31:32 +02007581 {
7582 typval_T tv;
7583
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007584 put_callback(&qfl->qf_qftf_cb, &tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007585 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7586 clear_tv(&tv);
7587 }
7588 else
7589 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7590
7591 return status;
7592}
7593
7594/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007595 * Return quickfix/location list details (title) as a
7596 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7597 * then current list is used. Otherwise the specified list is used.
7598 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007599 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007600qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7601{
64-bitman88d41ab2025-04-06 17:20:39 +02007602 qf_info_T *qi = ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007603 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007604 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007605 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007606 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007607 dictitem_T *di;
7608 int flags = QF_GETLIST_NONE;
7609
7610 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7611 return qf_get_list_from_lines(what, di, retdict);
7612
7613 if (wp != NULL)
7614 qi = GET_LOC_LIST(wp);
64-bitman88d41ab2025-04-06 17:20:39 +02007615 else if (qi == NULL)
7616 return FAIL;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007617
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007618 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007619
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007620 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007621 qf_idx = qf_getprop_qfidx(qi, what);
7622
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007623 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007624 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007625 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007626
Bram Moolenaar0398e002019-03-21 21:12:49 +01007627 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007628
Bram Moolenaar858ba062020-05-31 23:11:59 +02007629 // If an entry index is specified, use that
7630 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7631 {
7632 if (di->di_tv.v_type != VAR_NUMBER)
7633 return FAIL;
7634 eidx = di->di_tv.vval.v_number;
7635 }
7636
Bram Moolenaard823fa92016-08-12 16:29:27 +02007637 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007638 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007639 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007640 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007641 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007642 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007643 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007644 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007645 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007646 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007647 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007648 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007649 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007650 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007651 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007652 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007653 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007654 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007655 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7656 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007657 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7658 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007659 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7660 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007661
Bram Moolenaard823fa92016-08-12 16:29:27 +02007662 return status;
7663}
7664
7665/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007666 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007667 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7668 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007669 */
7670 static int
7671qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007672 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007673 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007674 int first_entry,
7675 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007676{
7677 static int did_bufnr_emsg;
7678 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007679 int bufnum, valid, status, col, end_col, vcol, nr;
7680 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007681
7682 if (first_entry)
7683 did_bufnr_emsg = FALSE;
7684
Bram Moolenaard61efa52022-07-23 09:52:04 +01007685 filename = dict_get_string(d, "filename", TRUE);
7686 module = dict_get_string(d, "module", TRUE);
7687 bufnum = (int)dict_get_number(d, "bufnr");
7688 lnum = (int)dict_get_number(d, "lnum");
7689 end_lnum = (int)dict_get_number(d, "end_lnum");
7690 col = (int)dict_get_number(d, "col");
7691 end_col = (int)dict_get_number(d, "end_col");
7692 vcol = (int)dict_get_number(d, "vcol");
7693 nr = (int)dict_get_number(d, "nr");
7694 type = dict_get_string(d, "type", TRUE);
7695 pattern = dict_get_string(d, "pattern", TRUE);
7696 text = dict_get_string(d, "text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007697 if (text == NULL)
7698 text = vim_strsave((char_u *)"");
Tom Praschanca6ac992023-08-11 23:26:12 +02007699 typval_T user_data;
7700 user_data.v_type = VAR_UNKNOWN;
7701 dict_get_tv(d, "user_data", &user_data);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007702
7703 valid = TRUE;
7704 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7705 valid = FALSE;
7706
7707 // Mark entries with non-existing buffer number as not valid. Give the
7708 // error message only once.
7709 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7710 {
7711 if (!did_bufnr_emsg)
7712 {
7713 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007714 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007715 }
7716 valid = FALSE;
7717 bufnum = 0;
7718 }
7719
7720 // If the 'valid' field is present it overrules the detected value.
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007721 if (dict_has_key(d, "valid"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01007722 valid = (int)dict_get_bool(d, "valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007723
Bram Moolenaar0398e002019-03-21 21:12:49 +01007724 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007725 NULL, // dir
7726 filename,
7727 module,
7728 bufnum,
7729 text,
7730 lnum,
thinca6864efa2021-06-19 20:45:20 +02007731 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007732 col,
thinca6864efa2021-06-19 20:45:20 +02007733 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007734 vcol, // vis_col
7735 pattern, // search pattern
7736 nr,
7737 type == NULL ? NUL : *type,
Tom Praschanca6ac992023-08-11 23:26:12 +02007738 &user_data,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007739 valid);
7740
7741 vim_free(filename);
7742 vim_free(module);
7743 vim_free(pattern);
7744 vim_free(text);
7745 vim_free(type);
Tom Praschanca6ac992023-08-11 23:26:12 +02007746 clear_tv(&user_data);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007747
Bram Moolenaar9752c722018-12-22 16:49:34 +01007748 if (valid)
7749 *valid_entry = TRUE;
7750
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007751 return status;
7752}
7753
7754/*
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007755 * Check if `entry` is closer to the target than `other_entry`.
7756 *
7757 * Only returns TRUE if `entry` is definitively closer. If it's further
7758 * away, or there's not enough information to tell, return FALSE.
7759 */
7760 static int
7761entry_is_closer_to_target(
7762 qfline_T *entry,
7763 qfline_T *other_entry,
7764 int target_fnum,
7765 int target_lnum,
7766 int target_col)
7767{
7768 // First, compare entries to target file.
7769 if (!target_fnum)
7770 // Without a target file, we can't know which is closer.
7771 return FALSE;
7772
7773 int is_target_file = entry->qf_fnum && entry->qf_fnum == target_fnum;
7774 int other_is_target_file = other_entry->qf_fnum && other_entry->qf_fnum == target_fnum;
7775 if (!is_target_file && other_is_target_file)
7776 return FALSE;
7777 else if (is_target_file && !other_is_target_file)
7778 return TRUE;
7779
7780 // Both entries are pointing at the exact same file. Now compare line
7781 // numbers.
7782 if (!target_lnum)
7783 // Without a target line number, we can't know which is closer.
7784 return FALSE;
7785
7786 int line_distance = entry->qf_lnum ? labs(entry->qf_lnum - target_lnum) : INT_MAX;
7787 int other_line_distance = other_entry->qf_lnum ? labs(other_entry->qf_lnum - target_lnum) : INT_MAX;
7788 if (line_distance > other_line_distance)
7789 return FALSE;
7790 else if (line_distance < other_line_distance)
7791 return TRUE;
7792
7793 // Both entries are pointing at the exact same line number (or no line
7794 // number at all). Now compare columns.
7795 if (!target_col)
7796 // Without a target column, we can't know which is closer.
7797 return FALSE;
7798
7799 int column_distance = entry->qf_col ? abs(entry->qf_col - target_col) : INT_MAX;
7800 int other_column_distance = other_entry->qf_col ? abs(other_entry->qf_col - target_col): INT_MAX;
7801 if (column_distance > other_column_distance)
7802 return FALSE;
7803 else if (column_distance < other_column_distance)
7804 return TRUE;
7805
7806 // It's a complete tie! The exact same file, line, and column.
7807 return FALSE;
7808}
7809
7810/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007811 * Add list of entries to quickfix/location list. Each list entry is
7812 * a dictionary with item information.
7813 */
7814 static int
7815qf_add_entries(
7816 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007817 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007818 list_T *list,
7819 char_u *title,
7820 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007821{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007822 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007823 listitem_T *li;
7824 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007825 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007826 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007827 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007828
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007829 // If there's an entry selected in the quickfix list, remember its location
7830 // (file, line, column), so we can select the nearest entry in the updated
7831 // quickfix list.
7832 int prev_fnum = 0;
7833 int prev_lnum = 0;
7834 int prev_col = 0;
7835 if (qfl->qf_ptr)
7836 {
7837 prev_fnum = qfl->qf_ptr->qf_fnum;
7838 prev_lnum = qfl->qf_ptr->qf_lnum;
7839 prev_col = qfl->qf_ptr->qf_col;
7840 }
7841
7842 int select_first_entry = FALSE;
7843 int select_nearest_entry = FALSE;
7844
Bram Moolenaara3921f42017-06-04 15:30:34 +02007845 if (action == ' ' || qf_idx == qi->qf_listcount)
7846 {
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007847 select_first_entry = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007848 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007849 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007850 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007851 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007852 }
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007853 else if (action == 'a')
7854 {
7855 if (qf_list_empty(qfl))
7856 // Appending to empty list, select first entry.
7857 select_first_entry = TRUE;
7858 else
7859 // Adding to existing list, use last entry.
7860 old_last = qfl->qf_last;
7861 }
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007862 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007863 {
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007864 select_first_entry = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007865 qf_free_items(qfl);
7866 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007867 }
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007868 else if (action == 'u')
7869 {
7870 select_nearest_entry = TRUE;
7871 qf_free_items(qfl);
7872 qf_store_title(qfl, title);
7873 }
7874
7875 qfline_T *entry_to_select = NULL;
7876 int entry_to_select_index = 0;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007877
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007878 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007879 {
7880 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007881 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007882
7883 d = li->li_tv.vval.v_dict;
7884 if (d == NULL)
7885 continue;
7886
Bram Moolenaar0398e002019-03-21 21:12:49 +01007887 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007888 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007889 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007890 break;
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007891
7892 qfline_T *entry = qfl->qf_last;
7893 if (
7894 (select_first_entry && entry_to_select == NULL) ||
7895 (select_nearest_entry &&
7896 (entry_to_select == NULL ||
7897 entry_is_closer_to_target(entry, entry_to_select, prev_fnum,
7898 prev_lnum, prev_col))))
7899 {
7900 entry_to_select = entry;
7901 entry_to_select_index = qfl->qf_count;
7902 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007903 }
7904
Bram Moolenaar9752c722018-12-22 16:49:34 +01007905 // Check if any valid error entries are added to the list.
7906 if (valid_entry)
7907 qfl->qf_nonevalid = FALSE;
7908 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007909 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007910 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007911
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007912 // Set the current error.
7913 if (entry_to_select)
7914 {
7915 qfl->qf_ptr = entry_to_select;
7916 qfl->qf_index = entry_to_select_index;
7917 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007918
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007919 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007920 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007921
7922 return retval;
7923}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007924
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007925/*
7926 * Get the quickfix list index from 'nr' or 'id'
7927 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007928 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007929qf_setprop_get_qfidx(
7930 qf_info_T *qi,
7931 dict_T *what,
7932 int action,
7933 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007934{
7935 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007936 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007937
Bram Moolenaard823fa92016-08-12 16:29:27 +02007938 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7939 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007940 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007941 if (di->di_tv.v_type == VAR_NUMBER)
7942 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007943 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007944 if (di->di_tv.vval.v_number != 0)
7945 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007946
Bram Moolenaar55b69262017-08-13 13:42:01 +02007947 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7948 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007949 // When creating a new list, accept qf_idx pointing to the next
7950 // non-available list and add the new list at the end of the
7951 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007952 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007953 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007954 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007955 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007956 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007957 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007958 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007959 }
7960 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007961 && di->di_tv.vval.v_string != NULL
7962 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007963 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007964 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007965 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007966 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007967 qf_idx = 0;
7968 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007969 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007970 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007971 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007972 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007973 }
7974
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007975 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007976 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007977 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007978 if (di->di_tv.v_type != VAR_NUMBER)
7979 return INVALID_QFIDX;
7980
7981 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007982 }
7983
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007984 return qf_idx;
7985}
7986
7987/*
7988 * Set the quickfix list title.
7989 */
7990 static int
7991qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7992{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007993 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007994
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007995 if (di->di_tv.v_type != VAR_STRING)
7996 return FAIL;
7997
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007998 vim_free(qfl->qf_title);
Bram Moolenaard61efa52022-07-23 09:52:04 +01007999 qfl->qf_title = dict_get_string(what, "title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008000 if (qf_idx == qi->qf_curlist)
8001 qf_update_win_titlevar(qi);
8002
8003 return OK;
8004}
8005
8006/*
8007 * Set quickfix list items/entries.
8008 */
8009 static int
8010qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
8011{
8012 int retval = FAIL;
8013 char_u *title_save;
8014
8015 if (di->di_tv.v_type != VAR_LIST)
8016 return FAIL;
8017
8018 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
8019 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
8020 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008021 vim_free(title_save);
8022
8023 return retval;
8024}
8025
8026/*
8027 * Set quickfix list items/entries from a list of lines.
8028 */
8029 static int
8030qf_setprop_items_from_lines(
8031 qf_info_T *qi,
8032 int qf_idx,
8033 dict_T *what,
8034 dictitem_T *di,
8035 int action)
8036{
8037 char_u *errorformat = p_efm;
8038 dictitem_T *efm_di;
8039 int retval = FAIL;
8040
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008041 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008042 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
8043 {
8044 if (efm_di->di_tv.v_type != VAR_STRING ||
8045 efm_di->di_tv.vval.v_string == NULL)
8046 return FAIL;
8047 errorformat = efm_di->di_tv.vval.v_string;
8048 }
8049
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008050 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008051 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
8052 return FAIL;
8053
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02008054 if (action == 'r' || action == 'u')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02008055 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008056 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01008057 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008058 retval = OK;
8059
8060 return retval;
8061}
8062
8063/*
8064 * Set quickfix list context.
8065 */
8066 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02008067qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008068{
8069 typval_T *ctx;
8070
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02008071 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008072 ctx = alloc_tv();
8073 if (ctx != NULL)
8074 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02008075 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008076
8077 return OK;
8078}
8079
8080/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01008081 * Set the current index in the specified quickfix list
8082 */
8083 static int
8084qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
8085{
8086 int denote = FALSE;
8087 int newidx;
8088 int old_qfidx;
8089 qfline_T *qf_ptr;
8090
8091 // If the specified index is '$', then use the last entry
8092 if (di->di_tv.v_type == VAR_STRING
8093 && di->di_tv.vval.v_string != NULL
8094 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
8095 newidx = qfl->qf_count;
8096 else
8097 {
8098 // Otherwise use the specified index
8099 newidx = tv_get_number_chk(&di->di_tv, &denote);
8100 if (denote)
8101 return FAIL;
8102 }
8103
8104 if (newidx < 1) // sanity check
8105 return FAIL;
8106 if (newidx > qfl->qf_count)
8107 newidx = qfl->qf_count;
8108
8109 old_qfidx = qfl->qf_index;
8110 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
8111 if (qf_ptr == NULL)
8112 return FAIL;
8113 qfl->qf_ptr = qf_ptr;
8114 qfl->qf_index = newidx;
8115
8116 // If the current list is modified and it is displayed in the quickfix
8117 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01008118 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01008119 qf_win_pos_update(qi, old_qfidx);
8120
8121 return OK;
8122}
8123
8124/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02008125 * Set the current index in the specified quickfix list
8126 */
8127 static int
8128qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
8129{
Bram Moolenaard43906d2020-07-20 21:31:32 +02008130 callback_T cb;
8131
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01008132 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02008133 cb = get_callback(&di->di_tv);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00008134 if (cb.cb_name == NULL || *cb.cb_name == NUL)
8135 return OK;
8136
8137 set_callback(&qfl->qf_qftf_cb, &cb);
8138 if (cb.cb_free_name)
8139 vim_free(cb.cb_name);
Bram Moolenaar858ba062020-05-31 23:11:59 +02008140
8141 return OK;
8142}
8143
8144/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008145 * Set quickfix/location list properties (title, items, context).
8146 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008147 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008148 */
8149 static int
8150qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
8151{
8152 dictitem_T *di;
8153 int retval = FAIL;
8154 int qf_idx;
8155 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008156 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008157
Bram Moolenaar019dfe62018-10-07 14:38:49 +02008158 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008159 newlist = TRUE;
8160
8161 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008162 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008163 return FAIL;
8164
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02008165 if (newlist)
8166 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02008167 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02008168 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02008169 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02008170 }
8171
Bram Moolenaar0398e002019-03-21 21:12:49 +01008172 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02008173 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008174 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02008175 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008176 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02008177 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008178 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008179 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008180 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01008181 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
8182 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02008183 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
8184 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008185
Bram Moolenaar287153c2020-11-29 14:20:27 +01008186 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008187 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01008188 if (newlist)
8189 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01008190
Bram Moolenaard823fa92016-08-12 16:29:27 +02008191 return retval;
8192}
8193
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008194/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008195 * Free the entire quickfix/location list stack.
8196 * If the quickfix/location list window is open, then clear it.
8197 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008198 static void
8199qf_free_stack(win_T *wp, qf_info_T *qi)
8200{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008201 win_T *qfwin = qf_find_win(qi);
8202 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008203
8204 if (qfwin != NULL)
8205 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008206 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008207 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01008208 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008209 qf_update_buffer(qi, NULL);
8210 }
8211
8212 if (wp != NULL && IS_LL_WINDOW(wp))
8213 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008214 // If in the location list window, then use the non-location list
8215 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01008216 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008217 if (llwin != NULL)
8218 wp = llwin;
8219 }
8220
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008221 qf_free_all(wp);
8222 if (wp == NULL)
8223 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008224 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008225 qi->qf_curlist = 0;
8226 qi->qf_listcount = 0;
8227 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01008228 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008229 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008230 // If the location list window is open, then create a new empty
8231 // location list
64-bitman88d41ab2025-04-06 17:20:39 +02008232 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION, wp->w_p_lhi);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02008233
Bram Moolenaar95946f12019-03-31 15:31:59 +02008234 if (new_ll != NULL)
8235 {
8236 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02008237
Bram Moolenaar95946f12019-03-31 15:31:59 +02008238 // first free the list reference in the location list window
8239 ll_free_all(&qfwin->w_llist_ref);
8240
8241 qfwin->w_llist_ref = new_ll;
8242 if (wp != qfwin)
8243 win_set_loclist(wp, new_ll);
8244 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008245 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008246}
8247
Bram Moolenaard823fa92016-08-12 16:29:27 +02008248/*
8249 * Populate the quickfix list with the items supplied in the list
8250 * of dictionaries. "title" will be copied to w:quickfix_title.
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02008251 * "action" is 'a' for add, 'r' for replace, 'u' for update. Otherwise
8252 * create a new list. When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02008253 */
8254 int
8255set_errorlist(
8256 win_T *wp,
8257 list_T *list,
8258 int action,
8259 char_u *title,
8260 dict_T *what)
8261{
64-bitman88d41ab2025-04-06 17:20:39 +02008262 qf_info_T *qi = ql_info;
Bram Moolenaard823fa92016-08-12 16:29:27 +02008263 int retval = OK;
8264
8265 if (wp != NULL)
8266 {
8267 qi = ll_get_or_alloc_list(wp);
8268 if (qi == NULL)
8269 return FAIL;
8270 }
64-bitman88d41ab2025-04-06 17:20:39 +02008271 else if (qi == NULL)
8272 {
8273 emsg(_(e_no_quickfix_stack));
8274 return FAIL;
8275 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02008276
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008277 if (action == 'f')
8278 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008279 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008280 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008281 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008282 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008283
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008284 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02008285 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008286 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008287 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008288 _("cannot have both a list and a \"what\" argument"));
8289 return FAIL;
8290 }
8291
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008292 incr_quickfix_busy();
8293
8294 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02008295 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02008296 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01008297 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02008298 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01008299 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01008300 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01008301 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02008302
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008303 decr_quickfix_busy();
8304
Bram Moolenaard823fa92016-08-12 16:29:27 +02008305 return retval;
8306}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008307
Tom Praschanca6ac992023-08-11 23:26:12 +02008308static int mark_quickfix_user_data(qf_info_T *qi, int copyID)
8309{
8310 int abort = FALSE;
64-bitman88d41ab2025-04-06 17:20:39 +02008311 for (int i = 0; i < qi->qf_maxcount && !abort; ++i)
Tom Praschanca6ac992023-08-11 23:26:12 +02008312 {
8313 qf_list_T *qfl = &qi->qf_lists[i];
8314 if (!qfl->qf_has_user_data)
8315 continue;
8316 qfline_T *qfp;
8317 int j;
8318 FOR_ALL_QFL_ITEMS(qfl, qfp, j)
8319 {
8320 typval_T* user_data = &qfp->qf_user_data;
8321 if (user_data != NULL && user_data->v_type != VAR_NUMBER
8322 && user_data->v_type != VAR_STRING && user_data->v_type != VAR_FLOAT)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01008323 abort = abort || set_ref_in_item(user_data, copyID, NULL, NULL, NULL);
Tom Praschanca6ac992023-08-11 23:26:12 +02008324 }
8325 }
8326 return abort;
8327}
8328
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008329/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008330 * Mark the quickfix context and callback function as in use for all the lists
8331 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008332 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008333 static int
8334mark_quickfix_ctx(qf_info_T *qi, int copyID)
8335{
8336 int i;
8337 int abort = FALSE;
8338 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008339 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008340
64-bitman88d41ab2025-04-06 17:20:39 +02008341 for (i = 0; i < qi->qf_maxcount && !abort; ++i)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008342 {
8343 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02008344 if (ctx != NULL && ctx->v_type != VAR_NUMBER
8345 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01008346 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL, NULL);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008347
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01008348 cb = &qi->qf_lists[i].qf_qftf_cb;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008349 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008350 }
8351
8352 return abort;
8353}
8354
8355/*
8356 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02008357 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008358 */
8359 int
8360set_ref_in_quickfix(int copyID)
8361{
8362 int abort = FALSE;
8363 tabpage_T *tp;
8364 win_T *win;
8365
64-bitman88d41ab2025-04-06 17:20:39 +02008366 if (ql_info == NULL)
8367 return TRUE;
8368
8369 abort = mark_quickfix_ctx(ql_info, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008370 if (abort)
8371 return abort;
8372
64-bitman88d41ab2025-04-06 17:20:39 +02008373 abort = mark_quickfix_user_data(ql_info, copyID);
Tom Praschanca6ac992023-08-11 23:26:12 +02008374 if (abort)
8375 return abort;
8376
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008377 abort = set_ref_in_callback(&qftf_cb, copyID);
8378 if (abort)
8379 return abort;
8380
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008381 FOR_ALL_TAB_WINDOWS(tp, win)
8382 {
8383 if (win->w_llist != NULL)
8384 {
8385 abort = mark_quickfix_ctx(win->w_llist, copyID);
8386 if (abort)
8387 return abort;
Tom Praschanca6ac992023-08-11 23:26:12 +02008388
8389 abort = mark_quickfix_user_data(win->w_llist, copyID);
8390 if (abort)
8391 return abort;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008392 }
Bram Moolenaar12237442017-12-19 12:38:52 +01008393 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
8394 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008395 // In a location list window and none of the other windows is
8396 // referring to this location list. Mark the location list
8397 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01008398 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
8399 if (abort)
8400 return abort;
zeertzjqbe4bd182024-09-14 10:32:31 +02008401
8402 abort = mark_quickfix_user_data(win->w_llist_ref, copyID);
8403 if (abort)
8404 return abort;
Bram Moolenaar12237442017-12-19 12:38:52 +01008405 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008406 }
8407
8408 return abort;
8409}
Bram Moolenaar05159a02005-02-26 23:04:13 +00008410#endif
8411
Bram Moolenaar81695252004-12-29 20:58:21 +00008412/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01008413 * Return the autocmd name for the :cbuffer Ex commands
8414 */
8415 static char_u *
8416cbuffer_get_auname(cmdidx_T cmdidx)
8417{
8418 switch (cmdidx)
8419 {
8420 case CMD_cbuffer: return (char_u *)"cbuffer";
8421 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
8422 case CMD_caddbuffer: return (char_u *)"caddbuffer";
8423 case CMD_lbuffer: return (char_u *)"lbuffer";
8424 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
8425 case CMD_laddbuffer: return (char_u *)"laddbuffer";
8426 default: return NULL;
8427 }
8428}
8429
8430/*
8431 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
8432 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
8433 */
8434 static int
8435cbuffer_process_args(
8436 exarg_T *eap,
8437 buf_T **bufp,
8438 linenr_T *line1,
8439 linenr_T *line2)
8440{
8441 buf_T *buf = NULL;
8442
8443 if (*eap->arg == NUL)
8444 buf = curbuf;
8445 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
8446 buf = buflist_findnr(atoi((char *)eap->arg));
8447
8448 if (buf == NULL)
8449 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008450 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008451 return FAIL;
8452 }
8453
8454 if (buf->b_ml.ml_mfp == NULL)
8455 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008456 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008457 return FAIL;
8458 }
8459
8460 if (eap->addr_count == 0)
8461 {
8462 eap->line1 = 1;
8463 eap->line2 = buf->b_ml.ml_line_count;
8464 }
8465
8466 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
8467 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
8468 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008469 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008470 return FAIL;
8471 }
8472
8473 *line1 = eap->line1;
8474 *line2 = eap->line2;
8475 *bufp = buf;
8476
8477 return OK;
8478}
8479
8480/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00008481 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00008482 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00008483 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008484 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00008485 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00008486 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008487 */
8488 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008489ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008490{
8491 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008492 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008493 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01008494 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02008495 int_u save_qfid;
8496 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01008497 char_u *qf_title;
8498 linenr_T line1;
8499 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008500
Bram Moolenaara16123a2019-03-28 20:31:07 +01008501 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01008502 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01008503 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008504 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008505#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008506 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008507 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008508#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008509 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008510
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008511 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008512 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8513 if (qi == NULL)
8514 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01008515
Bram Moolenaara16123a2019-03-28 20:31:07 +01008516 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
8517 return;
8518
8519 qf_title = qf_cmdtitle(*eap->cmdlinep);
8520
8521 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008522 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01008523 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
8524 (char *)qf_title, (char *)buf->b_sfname);
8525 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008526 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01008527
8528 incr_quickfix_busy();
8529
8530 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
8531 (eap->cmdidx != CMD_caddbuffer
8532 && eap->cmdidx != CMD_laddbuffer),
8533 line1, line2,
8534 qf_title, NULL);
8535 if (qf_stack_empty(qi))
8536 {
8537 decr_quickfix_busy();
8538 return;
8539 }
8540 if (res >= 0)
8541 qf_list_changed(qf_get_curlist(qi));
8542
8543 // Remember the current quickfix list identifier, so that we can
8544 // check for autocommands changing the current quickfix list.
8545 save_qfid = qf_get_curlist(qi)->qf_id;
8546 if (au_name != NULL)
8547 {
8548 buf_T *curbuf_old = curbuf;
8549
8550 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
8551 TRUE, curbuf);
8552 if (curbuf != curbuf_old)
8553 // Autocommands changed buffer, don't jump now, "qi" may
8554 // be invalid.
8555 res = 0;
8556 }
8557 // Jump to the first error for a new list and if autocmds didn't
8558 // free the list.
8559 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
8560 eap->cmdidx == CMD_lbuffer)
8561 && qflist_valid(wp, save_qfid))
8562 // display the first error
8563 qf_jump_first(qi, save_qfid, eap->forceit);
8564
8565 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00008566}
8567
Bram Moolenaar1e015462005-09-25 22:16:38 +00008568#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008569/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01008570 * Return the autocmd name for the :cexpr Ex commands.
8571 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008572 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01008573cexpr_get_auname(cmdidx_T cmdidx)
8574{
8575 switch (cmdidx)
8576 {
8577 case CMD_cexpr: return (char_u *)"cexpr";
8578 case CMD_cgetexpr: return (char_u *)"cgetexpr";
8579 case CMD_caddexpr: return (char_u *)"caddexpr";
8580 case CMD_lexpr: return (char_u *)"lexpr";
8581 case CMD_lgetexpr: return (char_u *)"lgetexpr";
8582 case CMD_laddexpr: return (char_u *)"laddexpr";
8583 default: return NULL;
8584 }
8585}
8586
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008587 int
8588trigger_cexpr_autocmd(int cmdidx)
8589{
8590 char_u *au_name = cexpr_get_auname(cmdidx);
8591
8592 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8593 curbuf->b_fname, TRUE, curbuf))
8594 {
8595 if (aborting())
8596 return FAIL;
8597 }
8598 return OK;
8599}
8600
8601 int
8602cexpr_core(exarg_T *eap, typval_T *tv)
8603{
8604 qf_info_T *qi;
8605 win_T *wp = NULL;
8606
8607 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8608 if (qi == NULL)
8609 return FAIL;
8610
8611 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
8612 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
8613 {
8614 int res;
8615 int_u save_qfid;
8616 char_u *au_name = cexpr_get_auname(eap->cmdidx);
8617
8618 incr_quickfix_busy();
8619 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
8620 (eap->cmdidx != CMD_caddexpr
8621 && eap->cmdidx != CMD_laddexpr),
8622 (linenr_T)0, (linenr_T)0,
8623 qf_cmdtitle(*eap->cmdlinep), NULL);
8624 if (qf_stack_empty(qi))
8625 {
8626 decr_quickfix_busy();
8627 return FAIL;
8628 }
8629 if (res >= 0)
8630 qf_list_changed(qf_get_curlist(qi));
8631
8632 // Remember the current quickfix list identifier, so that we can
8633 // check for autocommands changing the current quickfix list.
8634 save_qfid = qf_get_curlist(qi)->qf_id;
8635 if (au_name != NULL)
8636 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8637 curbuf->b_fname, TRUE, curbuf);
8638
8639 // Jump to the first error for a new list and if autocmds didn't
8640 // free the list.
8641 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8642 && qflist_valid(wp, save_qfid))
8643 // display the first error
8644 qf_jump_first(qi, save_qfid, eap->forceit);
8645 decr_quickfix_busy();
8646 return OK;
8647 }
8648
Bram Moolenaar677658a2022-01-05 16:09:06 +00008649 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008650 return FAIL;
8651}
8652
Bram Moolenaara16123a2019-03-28 20:31:07 +01008653/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008654 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8655 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008656 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008657 */
8658 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008659ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008660{
8661 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008662
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008663 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008664 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008665
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008666 // Evaluate the expression. When the result is a string or a list we can
8667 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008668 tv = eval_expr(eap->arg, eap);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00008669 if (tv == NULL)
8670 return;
8671
8672 (void)cexpr_core(eap, tv);
8673 free_tv(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008674}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008675#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008676
8677/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008678 * Get the location list for ":lhelpgrep"
8679 */
8680 static qf_info_T *
8681hgr_get_ll(int *new_ll)
8682{
8683 win_T *wp;
8684 qf_info_T *qi;
8685
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008686 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008687 if (bt_help(curwin->w_buffer))
8688 wp = curwin;
8689 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008690 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008691 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008692
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008693 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008694 qi = NULL;
8695 else
8696 qi = wp->w_llist;
8697
8698 if (qi == NULL)
8699 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008700 // Allocate a new location list for help text matches
64-bitman88d41ab2025-04-06 17:20:39 +02008701 if ((qi = qf_alloc_stack(QFLT_LOCATION, 1)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008702 return NULL;
8703 *new_ll = TRUE;
8704 }
8705
8706 return qi;
8707}
8708
8709/*
8710 * Search for a pattern in a help file.
8711 */
8712 static void
8713hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008714 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008715 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008716 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008717 regmatch_T *p_regmatch)
8718{
8719 FILE *fd;
8720 long lnum;
8721
8722 fd = mch_fopen((char *)fname, "r");
8723 if (fd == NULL)
8724 return;
8725
8726 lnum = 1;
8727 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8728 {
8729 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008730
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008731 // Convert a line if 'encoding' is not utf-8 and
8732 // the line contains a non-ASCII character.
Yegappan Lakshmanandf1bbea2022-03-05 14:35:12 +00008733 if (p_vc->vc_type != CONV_NONE && has_non_ascii(IObuff))
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008734 {
8735 line = string_convert(p_vc, IObuff, NULL);
8736 if (line == NULL)
8737 line = IObuff;
8738 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008739
8740 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8741 {
8742 int l = (int)STRLEN(line);
8743
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008744 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008745 while (l > 0 && line[l - 1] <= ' ')
8746 line[--l] = NUL;
8747
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008748 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008749 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008750 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008751 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008752 0,
8753 line,
8754 lnum,
thinca6864efa2021-06-19 20:45:20 +02008755 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008756 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008757 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008758 (int)(p_regmatch->endp[0] - line)
8759 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008760 FALSE, // vis_col
8761 NULL, // search pattern
8762 0, // nr
8763 1, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02008764 NULL, // user_data
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008765 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008766 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008767 {
8768 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008769 if (line != IObuff)
8770 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008771 break;
8772 }
8773 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008774 if (line != IObuff)
8775 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008776 ++lnum;
8777 line_breakcheck();
8778 }
8779 fclose(fd);
8780}
8781
8782/*
8783 * Search for a pattern in all the help files in the doc directory under
8784 * the given directory.
8785 */
8786 static void
8787hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008788 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008789 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008790 regmatch_T *p_regmatch,
8791 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008792#ifdef FEAT_MULTI_LANG
8793 , char_u *lang
8794#endif
8795 )
8796{
8797 int fcount;
8798 char_u **fnames;
8799 int fi;
8800
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008801 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008802 add_pathsep(dirname);
8803 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8804 if (gen_expand_wildcards(1, &dirname, &fcount,
8805 &fnames, EW_FILE|EW_SILENT) == OK
8806 && fcount > 0)
8807 {
8808 for (fi = 0; fi < fcount && !got_int; ++fi)
8809 {
8810#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008811 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008812 if (lang != NULL
8813 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008814 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008815 && !(STRNICMP(lang, "en", 2) == 0
8816 && STRNICMP("txt", fnames[fi]
8817 + STRLEN(fnames[fi]) - 3, 3) == 0))
8818 continue;
8819#endif
8820
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008821 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008822 }
8823 FreeWild(fcount, fnames);
8824 }
8825}
8826
8827/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008828 * Search for a pattern in all the help files in the 'runtimepath'
8829 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008830 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008831 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008832 */
8833 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008834hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008835{
8836 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008837
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008838 vimconv_T vc;
8839
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008840 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8841 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008842 vc.vc_type = CONV_NONE;
8843 if (!enc_utf8)
8844 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008845
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008846 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008847 p = p_rtp;
8848 while (*p != NUL && !got_int)
8849 {
8850 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8851
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008852 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008853#ifdef FEAT_MULTI_LANG
8854 , lang
8855#endif
8856 );
8857 }
8858
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008859 if (vc.vc_type != CONV_NONE)
8860 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008861}
8862
8863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 * ":helpgrep {pattern}"
8865 */
8866 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008867ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868{
8869 regmatch_T regmatch;
8870 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008871 int save_cpo_allocated;
64-bitman88d41ab2025-04-06 17:20:39 +02008872 qf_info_T *qi = ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008873 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008874 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008875 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008876 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
Bram Moolenaar73633f82012-01-20 13:39:07 +01008878 switch (eap->cmdidx)
8879 {
8880 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8881 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8882 default: break;
8883 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008884 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8885 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008886 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008887#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008888 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008889 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008890#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008891 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008892
Bram Moolenaar39665952018-08-15 20:59:48 +02008893 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008894 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008895 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008896 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008897 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008898 }
64-bitman88d41ab2025-04-06 17:20:39 +02008899 else if (qi == NULL)
8900 {
8901 emsg(_(e_no_quickfix_stack));
8902 return;
8903 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008904
Bram Moolenaara16123a2019-03-28 20:31:07 +01008905 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8906 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008907 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008908 p_cpo = empty_option;
8909
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008910 incr_quickfix_busy();
8911
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008912#ifdef FEAT_MULTI_LANG
8913 // Check for a specified language
8914 lang = check_help_lang(eap->arg);
8915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8917 regmatch.rm_ic = FALSE;
8918 if (regmatch.regprog != NULL)
8919 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008920 qf_list_T *qfl;
8921
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008922 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008923 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008924 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008926 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008927
Bram Moolenaar473de612013-06-08 18:19:48 +02008928 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008930 qfl->qf_nonevalid = FALSE;
8931 qfl->qf_ptr = qfl->qf_start;
8932 qfl->qf_index = 1;
8933 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008934 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 }
8936
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008937 if (p_cpo == empty_option)
8938 p_cpo = save_cpo;
8939 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008940 {
8941 // Darn, some plugin changed the value. If it's still empty it was
8942 // changed and restored, need to restore in the complicated way.
8943 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008944 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008945 if (save_cpo_allocated)
8946 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008947 }
8948
8949 if (updated)
8950 // This may open a window and source scripts, do this after 'cpo' was
8951 // restored.
8952 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
Bram Moolenaar73633f82012-01-20 13:39:07 +01008954 if (au_name != NULL)
8955 {
8956 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8957 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008958 // When adding a location list to an existing location list stack,
8959 // if the autocmd made the stack invalid, then just return.
8960 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008961 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008962 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008963 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008964 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008965 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008966
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008967 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008968 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008969 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008970 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008971 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008972
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008973 decr_quickfix_busy();
8974
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008975 if (eap->cmdidx == CMD_lhelpgrep)
8976 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008977 // If the help window is not opened or if it already points to the
8978 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008979 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008980 {
8981 if (new_qi)
8982 ll_free_all(&qi);
8983 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008984 else if (curwin->w_llist == NULL && new_qi)
8985 // current window didn't have a location list associated with it
8986 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008987 curwin->w_llist = qi;
8988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989}
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008990
8991# if defined(EXITFREE) || defined(PROTO)
8992 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00008993free_quickfix(void)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008994{
8995 win_T *win;
8996 tabpage_T *tab;
8997
8998 qf_free_all(NULL);
8999 // Free all location lists
9000 FOR_ALL_TAB_WINDOWS(tab, win)
9001 qf_free_all(win);
9002
9003 ga_clear(&qfga);
9004}
9005# endif
9006
Bram Moolenaar63d9e732019-12-05 21:10:38 +01009007#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02009008
9009#if defined(FEAT_EVAL) || defined(PROTO)
9010# ifdef FEAT_QUICKFIX
9011 static void
9012get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
9013{
9014 if (what_arg->v_type == VAR_UNKNOWN)
9015 {
9016 if (rettv_list_alloc(rettv) == OK)
9017 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02009018 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02009019 }
9020 else
9021 {
9022 if (rettv_dict_alloc(rettv) == OK)
9023 if (is_qf || (wp != NULL))
9024 {
9025 if (what_arg->v_type == VAR_DICT)
9026 {
9027 dict_T *d = what_arg->vval.v_dict;
9028
9029 if (d != NULL)
9030 qf_get_properties(wp, d, rettv->vval.v_dict);
9031 }
9032 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00009033 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02009034 }
9035 }
9036}
9037# endif
9038
9039/*
9040 * "getloclist()" function
9041 */
9042 void
9043f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9044{
9045# ifdef FEAT_QUICKFIX
9046 win_T *wp;
9047
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02009048 if (in_vim9script()
9049 && (check_for_number_arg(argvars, 0) == FAIL
9050 || check_for_opt_dict_arg(argvars, 1) == FAIL))
9051 return;
9052
Bram Moolenaare677df82019-09-02 22:31:11 +02009053 wp = find_win_by_nr_or_id(&argvars[0]);
9054 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
9055# endif
9056}
9057
9058/*
9059 * "getqflist()" function
9060 */
9061 void
9062f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9063{
9064# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02009065 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
9066 return;
9067
Bram Moolenaare677df82019-09-02 22:31:11 +02009068 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
9069# endif
9070}
9071
9072/*
9073 * Used by "setqflist()" and "setloclist()" functions
9074 */
9075 static void
9076set_qf_ll_list(
9077 win_T *wp UNUSED,
9078 typval_T *list_arg UNUSED,
9079 typval_T *action_arg UNUSED,
9080 typval_T *what_arg UNUSED,
9081 typval_T *rettv)
9082{
9083# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02009084 char_u *act;
9085 int action = 0;
9086 static int recursive = 0;
9087# endif
9088
9089 rettv->vval.v_number = -1;
9090
9091# ifdef FEAT_QUICKFIX
9092 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00009093 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02009094 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00009095 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02009096 else
9097 {
9098 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02009099 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02009100 int valid_dict = TRUE;
9101
9102 if (action_arg->v_type == VAR_STRING)
9103 {
9104 act = tv_get_string_chk(action_arg);
9105 if (act == NULL)
9106 return; // type error; errmsg already given
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02009107 if ((*act == 'a' || *act == 'r' || *act == 'u' || *act == ' ' || *act == 'f') &&
Bram Moolenaare677df82019-09-02 22:31:11 +02009108 act[1] == NUL)
9109 action = *act;
9110 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009111 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02009112 }
9113 else if (action_arg->v_type == VAR_UNKNOWN)
9114 action = ' ';
9115 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00009116 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02009117
9118 if (action_arg->v_type != VAR_UNKNOWN
9119 && what_arg->v_type != VAR_UNKNOWN)
9120 {
Bram Moolenaar90049492020-07-01 13:04:05 +02009121 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
9122 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02009123 else
9124 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00009125 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02009126 valid_dict = FALSE;
9127 }
9128 }
9129
9130 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02009131 if (l != NULL && action && valid_dict
9132 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02009133 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02009134 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02009135 rettv->vval.v_number = 0;
9136 --recursive;
9137 }
9138# endif
9139}
9140
9141/*
9142 * "setloclist()" function
9143 */
9144 void
9145f_setloclist(typval_T *argvars, typval_T *rettv)
9146{
9147 win_T *win;
9148
9149 rettv->vval.v_number = -1;
9150
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02009151 if (in_vim9script()
9152 && (check_for_number_arg(argvars, 0) == FAIL
9153 || check_for_list_arg(argvars, 1) == FAIL
9154 || check_for_opt_string_arg(argvars, 2) == FAIL
9155 || (argvars[2].v_type != VAR_UNKNOWN
9156 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
9157 return;
9158
Bram Moolenaare677df82019-09-02 22:31:11 +02009159 win = find_win_by_nr_or_id(&argvars[0]);
9160 if (win != NULL)
9161 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
9162}
9163
9164/*
9165 * "setqflist()" function
9166 */
9167 void
9168f_setqflist(typval_T *argvars, typval_T *rettv)
9169{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02009170 if (in_vim9script()
9171 && (check_for_list_arg(argvars, 0) == FAIL
9172 || check_for_opt_string_arg(argvars, 1) == FAIL
9173 || (argvars[1].v_type != VAR_UNKNOWN
9174 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
9175 return;
9176
Bram Moolenaare677df82019-09-02 22:31:11 +02009177 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
9178}
9179#endif