blob: decc01b413ef446939d73118af377d8a6c4d867b [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);
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +0200177static int qf_resize_stack_base(qf_info_T *qi, int n);
64-bitman88d41ab2025-04-06 17:20:39 +0200178static 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/*
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02002346 * Resize quickfix stack to be able to hold n amount of lists.
64-bitman88d41ab2025-04-06 17:20:39 +02002347 * returns FAIL on failure and OK on success.
2348 */
2349 int
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02002350qf_resize_stack(int n)
64-bitman88d41ab2025-04-06 17:20:39 +02002351{
2352 if (ql_info == NULL)
64-bitman88d41ab2025-04-06 17:20:39 +02002353 return FAIL;
64-bitman88d41ab2025-04-06 17:20:39 +02002354
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02002355 if (qf_resize_stack_base(ql_info, n) == FAIL)
64-bitman88d41ab2025-04-06 17:20:39 +02002356 return FAIL;
2357
2358 return OK;
2359}
2360
2361/*
2362 * Resize location list stack for window 'wp' to be able to
2363 * hold n amount of lists. Returns FAIL on failure and OK on success
2364 */
2365 int
2366ll_resize_stack(win_T *wp, int n)
2367{
2368 // check if current window is a location list window;
2369 // if so then sync its 'lhistory' to the parent window or vice versa
2370 if (IS_LL_WINDOW(curwin))
2371 qf_sync_llw_to_win(wp);
2372 else
2373 qf_sync_win_to_llw(wp);
2374
2375 qf_info_T *qi = ll_get_or_alloc_list(wp);
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02002376 if (qi == NULL)
2377 return FAIL;
64-bitman88d41ab2025-04-06 17:20:39 +02002378
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02002379 if (qf_resize_stack_base(qi, n) == FAIL)
64-bitman88d41ab2025-04-06 17:20:39 +02002380 return FAIL;
2381
2382 return OK;
2383}
2384
2385/*
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02002386 * Resize quickfix/location lists stack to be able to hold n amount of lists.
64-bitman88d41ab2025-04-06 17:20:39 +02002387 * Returns FAIL on failure and OK on success.
2388 */
2389 static int
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02002390qf_resize_stack_base(qf_info_T *qi, int n)
64-bitman88d41ab2025-04-06 17:20:39 +02002391{
2392 qf_list_T *new;
2393 int amount_to_rm = 0, i;
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02002394
2395 if (qi == NULL)
2396 return FAIL;
2397
64-bitman88d41ab2025-04-06 17:20:39 +02002398 size_t lsz = sizeof(*qi->qf_lists);
2399
2400 if (n == qi->qf_maxcount)
2401 return OK;
2402 else if (n < qi->qf_maxcount && n < qi->qf_listcount)
2403 {
2404 // We have too many lists to store them all in the new stack,
2405 // pop lists until we can fit them all in the newly resized stack
2406 amount_to_rm = qi->qf_listcount - n;
2407
2408 for (i = 0; i < amount_to_rm; i++)
2409 qf_pop_stack(qi, TRUE);
2410 }
2411
2412 new = vim_realloc(qi->qf_lists, lsz * n);
2413
2414 if (new == NULL)
2415 return FAIL;
2416
2417 // fill with zeroes any newly allocated memory
2418 if (n > qi->qf_maxcount)
2419 vim_memset(new + qi->qf_maxcount, 0, lsz * (n - qi->qf_maxcount));
2420
2421 qi->qf_lists = new;
2422 qi->qf_maxcount = n;
2423
2424 qf_update_buffer(qi, NULL);
2425
2426 return OK;
2427}
2428
2429/*
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02002430 * Initialize quickfix list, should only be called once.
64-bitman88d41ab2025-04-06 17:20:39 +02002431 */
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02002432 void
2433qf_init_stack(void)
64-bitman88d41ab2025-04-06 17:20:39 +02002434{
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02002435 ql_info = qf_alloc_stack(QFLT_QUICKFIX, p_chi);
64-bitman88d41ab2025-04-06 17:20:39 +02002436}
2437
2438/*
2439 * Sync a location list window's 'lhistory' value to the parent window
2440 */
2441 static void
2442qf_sync_llw_to_win(win_T *llw)
2443{
2444 win_T *wp = qf_find_win_with_loclist(llw->w_llist_ref);
2445
2446 if (wp != NULL)
2447 wp->w_p_lhi = llw->w_p_lhi;
2448}
2449
2450/*
2451 * Sync a window's 'lhistory' value to its location list window, if any
2452 */
2453 static void
2454qf_sync_win_to_llw(win_T *pwp)
2455{
2456 win_T *wp;
2457 qf_info_T *llw = pwp->w_llist;
2458
2459 if (llw != NULL)
2460 FOR_ALL_WINDOWS(wp)
2461 if (wp->w_llist_ref == llw && bt_quickfix(wp->w_buffer))
2462 {
2463 wp->w_p_lhi = pwp->w_p_lhi;
2464 return;
2465 }
2466}
2467
2468/*
2469 * Allocate a new quickfix/location list stack that is able to hold
2470 * up to n amount of lists
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002471 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002472 static qf_info_T *
64-bitman88d41ab2025-04-06 17:20:39 +02002473qf_alloc_stack(qfltype_T qfltype, int n)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002474{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002475 qf_info_T *qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002476
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02002477 if (qfltype == QFLT_QUICKFIX)
2478 qi = &ql_info_actual;
2479 else
2480 {
2481 qi = ALLOC_CLEAR_ONE_ID(qf_info_T, aid_qf_qfinfo);
2482 if (qi == NULL)
2483 return NULL;
2484 qi->qf_refcount++;
2485 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00002486 qi->qfl_type = qfltype;
2487 qi->qf_bufnr = INVALID_QFBUFNR;
64-bitman88d41ab2025-04-06 17:20:39 +02002488 qi->qf_lists = qf_alloc_list_stack(n);
64-bitman88d41ab2025-04-06 17:20:39 +02002489 if (qi->qf_lists == NULL)
2490 {
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02002491 if (qfltype != QFLT_QUICKFIX)
2492 vim_free(qi);
64-bitman88d41ab2025-04-06 17:20:39 +02002493 return NULL;
2494 }
64-bitman88d41ab2025-04-06 17:20:39 +02002495 qi->qf_maxcount = n;
2496
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002497 return qi;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002498}
2499
2500/*
64-bitman88d41ab2025-04-06 17:20:39 +02002501 * Allocate memory for qf_lists member of qf_info_T struct.
2502 * 'actual' is the actual amount of lists that have been allocated for
2503 * (only set when function returns sucessfully)
2504 */
2505 static qf_list_T *
2506qf_alloc_list_stack(int n)
2507{
2508 return ALLOC_CLEAR_MULT(qf_list_T, n);
2509}
2510
2511/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002512 * Return the location list stack for window 'wp'.
2513 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002514 */
2515 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002516ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002517{
2518 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002519 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002520 return wp->w_llist_ref;
2521
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002522 // For a non-location list window, w_llist_ref should not point to a
2523 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002524 ll_free_all(&wp->w_llist_ref);
2525
2526 if (wp->w_llist == NULL)
64-bitman88d41ab2025-04-06 17:20:39 +02002527 // new location list
2528 wp->w_llist = qf_alloc_stack(QFLT_LOCATION, wp->w_p_lhi);
2529
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002530 return wp->w_llist;
2531}
2532
2533/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002534 * Get the quickfix/location list stack to use for the specified Ex command.
2535 * For a location list command, returns the stack for the current window. If
2536 * the location list is not found, then returns NULL and prints an error
2537 * message if 'print_emsg' is TRUE.
2538 */
2539 static qf_info_T *
2540qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2541{
64-bitman88d41ab2025-04-06 17:20:39 +02002542 qf_info_T *qi = ql_info;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002543
2544 if (is_loclist_cmd(eap->cmdidx))
2545 {
2546 qi = GET_LOC_LIST(curwin);
2547 if (qi == NULL)
2548 {
2549 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002550 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002551 return NULL;
2552 }
2553 }
64-bitman88d41ab2025-04-06 17:20:39 +02002554 if (qi == NULL && print_emsg)
2555 emsg(_(e_no_quickfix_stack));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002556
2557 return qi;
2558}
2559
2560/*
2561 * Get the quickfix/location list stack to use for the specified Ex command.
2562 * For a location list command, returns the stack for the current window.
2563 * If the location list is not present, then allocates a new one.
2564 * Returns NULL if the allocation fails. For a location list command, sets
2565 * 'pwinp' to curwin.
2566 */
2567 static qf_info_T *
2568qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2569{
64-bitman88d41ab2025-04-06 17:20:39 +02002570 qf_info_T *qi = ql_info;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002571
2572 if (is_loclist_cmd(eap->cmdidx))
2573 {
2574 qi = ll_get_or_alloc_list(curwin);
2575 if (qi == NULL)
2576 return NULL;
2577 *pwinp = curwin;
2578 }
2579
2580 return qi;
2581}
2582
2583/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002584 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2585 */
2586 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002587copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002588{
2589 int i;
2590 qfline_T *from_qfp;
2591 qfline_T *prevp;
2592
2593 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002594 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002595 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002596 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002597 NULL,
2598 NULL,
2599 from_qfp->qf_module,
2600 0,
2601 from_qfp->qf_text,
2602 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002603 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002604 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002605 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002606 from_qfp->qf_viscol,
2607 from_qfp->qf_pattern,
2608 from_qfp->qf_nr,
2609 0,
Tom Praschanca6ac992023-08-11 23:26:12 +02002610 &from_qfp->qf_user_data,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002611 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002612 return FAIL;
2613
2614 // qf_add_entry() will not set the qf_num field, as the
2615 // directory and file names are not supplied. So the qf_fnum
2616 // field is copied here.
2617 prevp = to_qfl->qf_last;
2618 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2619 prevp->qf_type = from_qfp->qf_type; // error type
2620 if (from_qfl->qf_ptr == from_qfp)
2621 to_qfl->qf_ptr = prevp; // current location
2622 }
2623
2624 return OK;
2625}
2626
2627/*
2628 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2629 */
2630 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002631copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002632{
2633 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002634 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002635 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
Tom Praschanca6ac992023-08-11 23:26:12 +02002636 to_qfl->qf_has_user_data = from_qfl->qf_has_user_data;
Bram Moolenaar09037502018-09-25 22:08:14 +02002637 to_qfl->qf_count = 0;
2638 to_qfl->qf_index = 0;
2639 to_qfl->qf_start = NULL;
2640 to_qfl->qf_last = NULL;
2641 to_qfl->qf_ptr = NULL;
2642 if (from_qfl->qf_title != NULL)
2643 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2644 else
2645 to_qfl->qf_title = NULL;
2646 if (from_qfl->qf_ctx != NULL)
2647 {
2648 to_qfl->qf_ctx = alloc_tv();
2649 if (to_qfl->qf_ctx != NULL)
2650 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2651 }
2652 else
2653 to_qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002654 if (from_qfl->qf_qftf_cb.cb_name != NULL)
2655 copy_callback(&to_qfl->qf_qftf_cb, &from_qfl->qf_qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002656 else
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002657 to_qfl->qf_qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002658
2659 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002660 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002661 return FAIL;
2662
2663 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2664
2665 // Assign a new ID for the location list
2666 to_qfl->qf_id = ++last_qf_id;
2667 to_qfl->qf_changedtick = 0L;
2668
2669 // When no valid entries are present in the list, qf_ptr points to
2670 // the first item in the list
2671 if (to_qfl->qf_nonevalid)
2672 {
2673 to_qfl->qf_ptr = to_qfl->qf_start;
2674 to_qfl->qf_index = 1;
2675 }
2676
2677 return OK;
2678}
2679
2680/*
2681 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002682 */
2683 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002684copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002685{
2686 qf_info_T *qi;
2687 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002688
Bram Moolenaar09037502018-09-25 22:08:14 +02002689 // When copying from a location list window, copy the referenced
2690 // location list. For other windows, copy the location list for
2691 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002692 if (IS_LL_WINDOW(from))
2693 qi = from->w_llist_ref;
2694 else
2695 qi = from->w_llist;
2696
Bram Moolenaar09037502018-09-25 22:08:14 +02002697 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002698 return;
2699
64-bitman88d41ab2025-04-06 17:20:39 +02002700 // allocate a new location list, set size of stack to 'from' window value
2701 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION, from->w_p_lhi)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002702 return;
64-bitman88d41ab2025-04-06 17:20:39 +02002703 else
2704 // set 'to' lhi to reflect new value
2705 to->w_p_lhi = to->w_llist->qf_maxcount;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002706
2707 to->w_llist->qf_listcount = qi->qf_listcount;
2708
Bram Moolenaar09037502018-09-25 22:08:14 +02002709 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002710 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002711 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002712 to->w_llist->qf_curlist = idx;
2713
Bram Moolenaar0398e002019-03-21 21:12:49 +01002714 if (copy_loclist(qf_get_list(qi, idx),
2715 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002716 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002717 qf_free_all(to);
2718 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002719 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002720 }
2721
Bram Moolenaar09037502018-09-25 22:08:14 +02002722 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002723}
2724
2725/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002726 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002727 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 */
2729 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002730qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
Bram Moolenaar82404332016-07-10 17:00:38 +02002732 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002733 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002734 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002735
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002736 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
Bram Moolenaare60acc12011-05-10 16:41:25 +02002739#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002740 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002741#endif
2742#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002743 if (directory != NULL)
2744 slash_adjust(directory);
2745 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002746#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002747 if (directory != NULL && !vim_isAbsName(fname)
2748 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2749 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002750 // Here we check if the file really exists.
2751 // This should normally be true, but if make works without
2752 // "leaving directory"-messages we might have missed a
2753 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002754 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002757 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002758 if (directory)
2759 ptr = concat_fnames(directory, fname, TRUE);
2760 else
2761 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002763 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002764 bufname = ptr;
2765 }
2766 else
2767 bufname = fname;
2768
2769 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002770 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002771 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002772 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002773 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002775 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002776 {
2777 vim_free(qf_last_bufname);
2778 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2779 if (bufname == ptr)
2780 qf_last_bufname = bufname;
2781 else
2782 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002783 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002784 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002785 if (buf == NULL)
2786 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002787
Bram Moolenaarc1542742016-07-20 21:44:37 +02002788 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002789 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002790 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791}
2792
2793/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002794 * Push dirbuf onto the directory stack and return pointer to actual dir or
2795 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 */
2797 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002798qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
2800 struct dir_stack_T *ds_new;
2801 struct dir_stack_T *ds_ptr;
2802
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002803 // allocate new stack element and hook it in
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002804 ds_new = ALLOC_ONE_ID(struct dir_stack_T, aid_qf_dirstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 if (ds_new == NULL)
2806 return NULL;
2807
2808 ds_new->next = *stackptr;
2809 *stackptr = ds_new;
2810
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002811 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 if (vim_isAbsName(dirbuf)
2813 || (*stackptr)->next == NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=dd410372022-05-15 13:59:11 +01002814 || is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 (*stackptr)->dirname = vim_strsave(dirbuf);
2816 else
2817 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002818 // Okay we don't have an absolute path.
2819 // dirbuf must be a subdir of one of the directories on the stack.
2820 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 ds_new = (*stackptr)->next;
2822 (*stackptr)->dirname = NULL;
2823 while (ds_new)
2824 {
2825 vim_free((*stackptr)->dirname);
2826 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2827 TRUE);
2828 if (mch_isdir((*stackptr)->dirname) == TRUE)
2829 break;
2830
2831 ds_new = ds_new->next;
2832 }
2833
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002834 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 while ((*stackptr)->next != ds_new)
2836 {
2837 ds_ptr = (*stackptr)->next;
2838 (*stackptr)->next = (*stackptr)->next->next;
2839 vim_free(ds_ptr->dirname);
2840 vim_free(ds_ptr);
2841 }
2842
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002843 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 if (ds_new == NULL)
2845 {
2846 vim_free((*stackptr)->dirname);
2847 (*stackptr)->dirname = vim_strsave(dirbuf);
2848 }
2849 }
2850
2851 if ((*stackptr)->dirname != NULL)
2852 return (*stackptr)->dirname;
2853 else
2854 {
2855 ds_ptr = *stackptr;
2856 *stackptr = (*stackptr)->next;
2857 vim_free(ds_ptr);
2858 return NULL;
2859 }
2860}
2861
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862/*
2863 * pop dirbuf from the directory stack and return previous directory or NULL if
2864 * stack is empty
2865 */
2866 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002867qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868{
2869 struct dir_stack_T *ds_ptr;
2870
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002871 // TODO: Should we check if dirbuf is the directory on top of the stack?
2872 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002874 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 if (*stackptr != NULL)
2876 {
2877 ds_ptr = *stackptr;
2878 *stackptr = (*stackptr)->next;
2879 vim_free(ds_ptr->dirname);
2880 vim_free(ds_ptr);
2881 }
2882
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002883 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 return *stackptr ? (*stackptr)->dirname : NULL;
2885}
2886
2887/*
2888 * clean up directory stack
2889 */
2890 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002891qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
2893 struct dir_stack_T *ds_ptr;
2894
2895 while ((ds_ptr = *stackptr) != NULL)
2896 {
2897 *stackptr = (*stackptr)->next;
2898 vim_free(ds_ptr->dirname);
2899 vim_free(ds_ptr);
2900 }
2901}
2902
2903/*
2904 * Check in which directory of the directory stack the given file can be
2905 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002906 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 * Cleans up intermediate directory entries.
2908 *
2909 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002910 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 * ./
2912 * ./aa
2913 * ./aa/bb
2914 * ./bb
2915 * ./bb/x.c
2916 * and make says:
2917 * making all in aa
2918 * making all in bb
2919 * x.c:9: Error
2920 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2921 * qf_guess_filepath will return NULL.
2922 */
2923 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002924qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925{
2926 struct dir_stack_T *ds_ptr;
2927 struct dir_stack_T *ds_tmp;
2928 char_u *fullname;
2929
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002930 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002931 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 return NULL;
2933
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002934 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 fullname = NULL;
2936 while (ds_ptr)
2937 {
2938 vim_free(fullname);
2939 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2940
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002941 // If concat_fnames failed, just go on. The worst thing that can happen
2942 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2944 break;
2945
2946 ds_ptr = ds_ptr->next;
2947 }
2948
2949 vim_free(fullname);
2950
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002951 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002952 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002954 ds_tmp = qfl->qf_dir_stack->next;
2955 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 vim_free(ds_tmp->dirname);
2957 vim_free(ds_tmp);
2958 }
2959
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002960 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961}
2962
2963/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002964 * Returns TRUE if a quickfix/location list with the given identifier exists.
2965 */
2966 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002967qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002968{
64-bitman88d41ab2025-04-06 17:20:39 +02002969 qf_info_T *qi = ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002970 int i;
2971
2972 if (wp != NULL)
2973 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002974 if (!win_valid(wp))
2975 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002976 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002977 }
64-bitman88d41ab2025-04-06 17:20:39 +02002978 if (qi == NULL)
2979 return FALSE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002980
2981 for (i = 0; i < qi->qf_listcount; ++i)
2982 if (qi->qf_lists[i].qf_id == qf_id)
2983 return TRUE;
2984
2985 return FALSE;
2986}
2987
2988/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002989 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002990 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002991 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002992 * Similar to location list.
2993 */
2994 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002995is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002996{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002997 qfline_T *qfp;
2998 int i;
2999
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003000 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01003001 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
3002 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003003 break;
3004
Bram Moolenaar95946f12019-03-31 15:31:59 +02003005 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003006 return FALSE;
3007
3008 return TRUE;
3009}
3010
3011/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003012 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003013 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003014 */
3015 static qfline_T *
3016get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003017 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003018 qfline_T *qf_ptr,
3019 int *qf_index,
3020 int dir)
3021{
3022 int idx;
3023 int old_qf_fnum;
3024
3025 idx = *qf_index;
3026 old_qf_fnum = qf_ptr->qf_fnum;
3027
3028 do
3029 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003030 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003031 return NULL;
3032 ++idx;
3033 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003034 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003035 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
3036
3037 *qf_index = idx;
3038 return qf_ptr;
3039}
3040
3041/*
3042 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003043 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003044 */
3045 static qfline_T *
3046get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003047 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003048 qfline_T *qf_ptr,
3049 int *qf_index,
3050 int dir)
3051{
3052 int idx;
3053 int old_qf_fnum;
3054
3055 idx = *qf_index;
3056 old_qf_fnum = qf_ptr->qf_fnum;
3057
3058 do
3059 {
3060 if (idx == 1 || qf_ptr->qf_prev == NULL)
3061 return NULL;
3062 --idx;
3063 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003064 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003065 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
3066
3067 *qf_index = idx;
3068 return qf_ptr;
3069}
3070
3071/*
3072 * Get the n'th (errornr) previous/next valid entry from the current entry in
3073 * the quickfix list.
3074 * dir == FORWARD or FORWARD_FILE: next valid entry
3075 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
3076 */
3077 static qfline_T *
3078get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003079 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003080 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003081 int dir,
3082 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003083{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003084 qfline_T *qf_ptr = qfl->qf_ptr;
3085 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003086 qfline_T *prev_qf_ptr;
3087 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003088 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003089
3090 while (errornr--)
3091 {
3092 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003093 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003094
3095 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003096 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003097 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003098 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003099 if (qf_ptr == NULL)
3100 {
3101 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003102 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003103 if (err != NULL)
3104 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003105 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003106 return NULL;
3107 }
3108 break;
3109 }
3110
3111 err = NULL;
3112 }
3113
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003114 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003115 return qf_ptr;
3116}
3117
3118/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003119 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
3120 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003121 */
3122 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003123get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003124{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003125 qfline_T *qf_ptr = qfl->qf_ptr;
3126 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003127
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003128 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003129 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
3130 {
3131 --qf_idx;
3132 qf_ptr = qf_ptr->qf_prev;
3133 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003134 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003135 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
3136 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003137 {
3138 ++qf_idx;
3139 qf_ptr = qf_ptr->qf_next;
3140 }
3141
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003142 *new_qfidx = qf_idx;
3143 return qf_ptr;
3144}
3145
3146/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003147 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003148 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
3149 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
3150 * Returns a pointer to the entry and the index of the new entry is stored in
3151 * 'new_qfidx'.
3152 */
3153 static qfline_T *
3154qf_get_entry(
3155 qf_list_T *qfl,
3156 int errornr,
3157 int dir,
3158 int *new_qfidx)
3159{
3160 qfline_T *qf_ptr = qfl->qf_ptr;
3161 int qfidx = qfl->qf_index;
3162
3163 if (dir != 0) // next/prev valid entry
3164 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
3165 else if (errornr != 0) // go to specified number
3166 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
3167
3168 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003169 return qf_ptr;
3170}
3171
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003172/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003173 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003174 */
3175 static win_T *
3176qf_find_help_win(void)
3177{
3178 win_T *wp;
3179
3180 FOR_ALL_WINDOWS(wp)
3181 if (bt_help(wp->w_buffer))
3182 return wp;
3183
3184 return NULL;
3185}
3186
3187/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003188 * Set the location list for the specified window to 'qi'.
3189 */
3190 static void
3191win_set_loclist(win_T *wp, qf_info_T *qi)
3192{
3193 wp->w_llist = qi;
3194 qi->qf_refcount++;
3195}
3196
3197/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01003198 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
3199 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003200 */
3201 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003202jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003203{
3204 win_T *wp;
3205 int flags;
3206
Bram Moolenaare1004402020-10-24 20:49:43 +02003207 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003208 wp = NULL;
3209 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003210 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003211 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
3212 win_enter(wp, TRUE);
3213 else
3214 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003215 // Split off help window; put it at far top if no position
3216 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003217 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02003218 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003219 && curwin->w_width < 80)
3220 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003221 // If the user asks to open a new window, then copy the location list.
3222 // Otherwise, don't copy the location list.
3223 if (IS_LL_STACK(qi) && !newwin)
3224 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003225
3226 if (win_split(0, flags) == FAIL)
3227 return FAIL;
3228
3229 *opened_window = TRUE;
3230
3231 if (curwin->w_height < p_hh)
3232 win_setheight((int)p_hh);
3233
Bram Moolenaarb2443732018-11-11 22:50:27 +01003234 // When using location list, the new window should use the supplied
3235 // location list. If the user asks to open a new window, then the new
3236 // window will get a copy of the location list.
3237 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003238 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003239 }
3240
3241 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003242 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003243
3244 return OK;
3245}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003246
3247/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003248 * Find a non-quickfix window using the given location list stack in the
3249 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003250 * Returns NULL if a matching window is not found.
3251 */
3252 static win_T *
3253qf_find_win_with_loclist(qf_info_T *ll)
3254{
3255 win_T *wp;
3256
3257 FOR_ALL_WINDOWS(wp)
3258 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
3259 return wp;
3260
3261 return NULL;
3262}
3263
3264/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003265 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003266 */
3267 static win_T *
3268qf_find_win_with_normal_buf(void)
3269{
3270 win_T *wp;
3271
3272 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02003273 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003274 return wp;
3275
3276 return NULL;
3277}
3278
3279/*
3280 * Go to a window in any tabpage containing the specified file. Returns TRUE
3281 * if successfully jumped to the window. Otherwise returns FALSE.
3282 */
3283 static int
3284qf_goto_tabwin_with_file(int fnum)
3285{
3286 tabpage_T *tp;
3287 win_T *wp;
3288
3289 FOR_ALL_TAB_WINDOWS(tp, wp)
3290 if (wp->w_buffer->b_fnum == fnum)
3291 {
3292 goto_tabpage_win(tp, wp);
3293 return TRUE;
3294 }
3295
3296 return FALSE;
3297}
3298
3299/*
3300 * Create a new window to show a file above the quickfix window. Called when
3301 * only the quickfix window is present.
3302 */
3303 static int
3304qf_open_new_file_win(qf_info_T *ll_ref)
3305{
3306 int flags;
3307
3308 flags = WSP_ABOVE;
3309 if (ll_ref != NULL)
3310 flags |= WSP_NEWLOC;
3311 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003312 return FAIL; // not enough room for window
3313 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003314 swb_flags = 0;
3315 RESET_BINDING(curwin);
3316 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003317 // The new window should use the location list from the
3318 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003319 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003320 return OK;
3321}
3322
3323/*
3324 * Go to a window that shows the right buffer. If the window is not found, go
3325 * to the window just above the location list window. This is used for opening
3326 * a file from a location window and not from a quickfix window. If some usable
3327 * window is previously found, then it is supplied in 'use_win'.
3328 */
3329 static void
3330qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
3331{
3332 win_T *win = use_win;
3333
3334 if (win == NULL)
3335 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003336 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003337 FOR_ALL_WINDOWS(win)
3338 if (win->w_buffer->b_fnum == qf_fnum)
3339 break;
3340 if (win == NULL)
3341 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003342 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003343 win = curwin;
3344 do
3345 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02003346 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003347 break;
3348 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003349 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003350 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003351 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003352 } while (win != curwin);
3353 }
3354 }
3355 win_goto(win);
3356
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003357 // If the location list for the window is not set, then set it
3358 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003359 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003360 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003361}
3362
3363/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003364 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3365 * not found, then go to the window just above the quickfix window. This is
3366 * used for opening a file from a quickfix window and not from a location
3367 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003368 */
3369 static void
3370qf_goto_win_with_qfl_file(int qf_fnum)
3371{
3372 win_T *win;
3373 win_T *altwin;
3374
3375 win = curwin;
3376 altwin = NULL;
3377 for (;;)
3378 {
3379 if (win->w_buffer->b_fnum == qf_fnum)
3380 break;
3381 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003382 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003383 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003384 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003385
3386 if (IS_QF_WINDOW(win))
3387 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003388 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003389 // window, unless 'switchbuf' contains 'uselast': in this case we
3390 // try to jump to the previously used window first.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003391 if ((swb_flags & SWB_USELAST) && win_valid(prevwin) &&
3392 !prevwin->w_p_wfb)
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003393 win = prevwin;
3394 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003395 win = altwin;
3396 else if (curwin->w_prev != NULL)
3397 win = curwin->w_prev;
3398 else
3399 win = curwin->w_next;
3400 break;
3401 }
3402
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003403 // Remember a usable window.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003404 if (altwin == NULL && !win->w_p_pvw && !win->w_p_wfb &&
3405 bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003406 altwin = win;
3407 }
3408
3409 win_goto(win);
3410}
3411
3412/*
3413 * Find a suitable window for opening a file (qf_fnum) from the
3414 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003415 * window, jump to it. Otherwise open a new window to display the file. If
3416 * 'newwin' is TRUE, then always open a new window. This is called from either
3417 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003418 */
3419 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003420qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003421{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003422 win_T *usable_wp = NULL;
3423 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003424 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003425
Bram Moolenaarb2443732018-11-11 22:50:27 +01003426 // If opening a new window, then don't use the location list referred by
3427 // the current window. Otherwise two windows will refer to the same
3428 // location list.
3429 if (!newwin)
3430 ll_ref = curwin->w_llist_ref;
3431
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003432 if (ll_ref != NULL)
3433 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003434 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003435 usable_wp = qf_find_win_with_loclist(ll_ref);
3436 if (usable_wp != NULL)
3437 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003438 }
3439
3440 if (!usable_win)
3441 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003442 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003443 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003444 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003445 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003446 }
3447
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003448 // If no usable window is found and 'switchbuf' contains "usetab"
3449 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003450 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003451 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003452
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003453 // If there is only one window and it is the quickfix window, create a
3454 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003455 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003456 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003457 if (qf_open_new_file_win(ll_ref) != OK)
3458 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003459 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003460 }
3461 else
3462 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003463 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003464 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003465 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003466 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003467 }
3468
3469 return OK;
3470}
3471
3472/*
3473 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003474 * Returns OK if successfully edited the file, FAIL on failing to open the
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003475 * buffer and QF_ABORT if the quickfix/location list was freed by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003476 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003477 */
3478 static int
3479qf_jump_edit_buffer(
3480 qf_info_T *qi,
3481 qfline_T *qf_ptr,
3482 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003483 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003484 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003485{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003486 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003487 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003488 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003489 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003490 int old_qf_curlist = qi->qf_curlist;
3491 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003492
3493 if (qf_ptr->qf_type == 1)
3494 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003495 // Open help file (do_ecmd() will set b_help flag, readfile() will
3496 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003497 if (!can_abandon(curbuf, forceit))
3498 {
3499 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003500 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003501 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003502
3503 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3504 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003505 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003506 }
3507 else
Colin Kennedy21570352024-03-03 16:16:47 +01003508 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003509 int fnum = qf_ptr->qf_fnum;
3510
3511 if (!forceit && curwin->w_p_wfb && curbuf->b_fnum != fnum)
Colin Kennedy21570352024-03-03 16:16:47 +01003512 {
3513 if (qi->qfl_type == QFLT_LOCATION)
3514 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003515 // Location lists cannot split or reassign their window
3516 // so 'winfixbuf' windows must fail
3517 emsg(_(e_winfixbuf_cannot_go_to_buffer));
3518 return FAIL;
Colin Kennedy21570352024-03-03 16:16:47 +01003519 }
3520
Sean Dewar4bb505e2024-03-05 20:39:07 +01003521 if (win_valid(prevwin) && !prevwin->w_p_wfb &&
3522 !bt_quickfix(prevwin->w_buffer))
Colin Kennedy21570352024-03-03 16:16:47 +01003523 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003524 // 'winfixbuf' is set; attempt to change to a window without it
3525 // that isn't a quickfix/location list window.
3526 win_goto(prevwin);
3527 }
3528 if (curwin->w_p_wfb)
3529 {
3530 // Split the window, which will be 'nowinfixbuf', and set curwin
3531 // to that
3532 if (win_split(0, 0) == OK)
3533 *opened_window = TRUE;
3534
3535 if (curwin->w_p_wfb)
3536 {
3537 // Autocommands set 'winfixbuf' or sent us to another window
Sean Dewar769eb2d2024-03-07 21:37:50 +01003538 // with it set, or we failed to split the window. Give up,
3539 // but don't return immediately, as they may have messed
3540 // with the list.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003541 emsg(_(e_winfixbuf_cannot_go_to_buffer));
3542 retval = FAIL;
3543 }
Colin Kennedy21570352024-03-03 16:16:47 +01003544 }
3545 }
3546
Sean Dewar4bb505e2024-03-05 20:39:07 +01003547 if (retval == OK)
3548 {
3549 retval = buflist_getfile(fnum,
3550 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
3551 }
Colin Kennedy21570352024-03-03 16:16:47 +01003552 }
Bram Moolenaar3c097222017-12-21 20:54:49 +01003553
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003554 // If a location list, check whether the associated window is still
3555 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003556 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003557 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003558 win_T *wp = win_id2wp(prev_winid);
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003559
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003560 if (wp == NULL && curwin->w_llist != qi)
3561 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003562 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003563 *opened_window = FALSE;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003564 return QF_ABORT;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003565 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003566 }
3567
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003568 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003569 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003570 emsg(_(e_current_quickfix_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003571 return QF_ABORT;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003572 }
3573
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003574 // Check if the list was changed. The pointers may happen to be identical,
3575 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003576 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003577 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003578 || !is_qf_entry_present(qfl, qf_ptr))
3579 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003580 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003581 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003582 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003583 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003584 return QF_ABORT;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003585 }
3586
3587 return retval;
3588}
3589
3590/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003591 * Go to the error line in the current file using either line/column number or
3592 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003593 */
3594 static void
3595qf_jump_goto_line(
3596 linenr_T qf_lnum,
3597 int qf_col,
3598 char_u qf_viscol,
3599 char_u *qf_pattern)
3600{
3601 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003602
3603 if (qf_pattern == NULL)
3604 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003605 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003606 i = qf_lnum;
3607 if (i > 0)
3608 {
3609 if (i > curbuf->b_ml.ml_line_count)
3610 i = curbuf->b_ml.ml_line_count;
3611 curwin->w_cursor.lnum = i;
3612 }
3613 if (qf_col > 0)
3614 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003615 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003616 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003617 coladvance(qf_col - 1);
3618 else
3619 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003620 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003621 check_cursor();
3622 }
3623 else
3624 beginline(BL_WHITE | BL_FIX);
3625 }
3626 else
3627 {
3628 pos_T save_cursor;
3629
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003630 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003631 save_cursor = curwin->w_cursor;
3632 curwin->w_cursor.lnum = 0;
John Marriott8c85a2a2024-05-20 19:18:26 +02003633 if (!do_search(NULL, '/', '/', qf_pattern, STRLEN(qf_pattern), (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003634 curwin->w_cursor = save_cursor;
3635 }
3636}
3637
3638/*
3639 * Display quickfix list index and size message
3640 */
3641 static void
3642qf_jump_print_msg(
3643 qf_info_T *qi,
3644 int qf_index,
3645 qfline_T *qf_ptr,
3646 buf_T *old_curbuf,
3647 linenr_T old_lnum)
3648{
3649 linenr_T i;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003650 garray_T *gap;
3651
3652 gap = qfga_get();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003653
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003654 // Update the screen before showing the message, unless the screen
3655 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003656 if (!msg_scrolled)
3657 update_topline_redraw();
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003658 vim_snprintf((char *)IObuff, IOSIZE, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003659 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003660 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3661 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003662 // Add the message, skipping leading whitespace and newlines.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003663 ga_concat(gap, IObuff);
3664 qf_fmt_text(gap, skipwhite(qf_ptr->qf_text));
Shane Harper5bf04282023-06-07 19:09:57 +01003665 ga_append(gap, NUL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003666
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003667 // Output the message. Overwrite to avoid scrolling when the 'O'
3668 // flag is present in 'shortmess'; But when not jumping, print the
3669 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003670 i = msg_scroll;
3671 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3672 msg_scroll = TRUE;
3673 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3674 msg_scroll = FALSE;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003675 msg_attr_keep((char *)gap->ga_data, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003676 msg_scroll = i;
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01003677
3678 qfga_clear();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003679}
3680
3681/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003682 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003683 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3684 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003685 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3686 * able to jump/open a window. Returns NOTDONE if a file is not associated
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003687 * with the entry. Returns QF_ABORT if the quickfix/location list was modified
3688 * by an autocmd.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003689 */
3690 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003691qf_jump_open_window(
3692 qf_info_T *qi,
3693 qfline_T *qf_ptr,
3694 int newwin,
3695 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003696{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003697 qf_list_T *qfl = qf_get_curlist(qi);
3698 int old_changedtick = qfl->qf_changedtick;
3699 int old_qf_curlist = qi->qf_curlist;
3700 qfltype_T qfl_type = qfl->qfl_type;
3701
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003702 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003703 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3704 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003705 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003706 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003707 if (old_qf_curlist != qi->qf_curlist
3708 || old_changedtick != qfl->qf_changedtick
3709 || !is_qf_entry_present(qfl, qf_ptr))
3710 {
3711 if (qfl_type == QFLT_QUICKFIX)
3712 emsg(_(e_current_quickfix_list_was_changed));
3713 else
3714 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003715 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003716 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003717
3718 // If currently in the quickfix window, find another window to show the
3719 // file in.
3720 if (bt_quickfix(curbuf) && !*opened_window)
3721 {
3722 // If there is no file specified, we don't know where to go.
3723 // But do advance, otherwise ":cn" gets stuck.
3724 if (qf_ptr->qf_fnum == 0)
3725 return NOTDONE;
3726
Bram Moolenaarb2443732018-11-11 22:50:27 +01003727 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3728 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003729 return FAIL;
3730 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003731 if (old_qf_curlist != qi->qf_curlist
3732 || old_changedtick != qfl->qf_changedtick
3733 || !is_qf_entry_present(qfl, qf_ptr))
3734 {
3735 if (qfl_type == QFLT_QUICKFIX)
3736 emsg(_(e_current_quickfix_list_was_changed));
3737 else
3738 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003739 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003740 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003741
3742 return OK;
3743}
3744
3745/*
3746 * Edit a selected file from the quickfix/location list and jump to a
3747 * particular line/column, adjust the folds and display a message about the
3748 * jump.
3749 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003750 * QF_ABORT if the quickfix/location list is freed by an autocmd when opening
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003751 * the file.
3752 */
3753 static int
3754qf_jump_to_buffer(
3755 qf_info_T *qi,
3756 int qf_index,
3757 qfline_T *qf_ptr,
3758 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003759 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003760 int *opened_window,
3761 int openfold,
3762 int print_message)
3763{
3764 buf_T *old_curbuf;
3765 linenr_T old_lnum;
3766 int retval = OK;
3767
3768 // If there is a file name, read the wanted file if needed, and check
3769 // autowrite etc.
3770 old_curbuf = curbuf;
3771 old_lnum = curwin->w_cursor.lnum;
3772
3773 if (qf_ptr->qf_fnum != 0)
3774 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003775 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003776 opened_window);
3777 if (retval != OK)
3778 return retval;
3779 }
3780
3781 // When not switched to another buffer, still need to set pc mark
3782 if (curbuf == old_curbuf)
3783 setpcmark();
3784
3785 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3786 qf_ptr->qf_pattern);
3787
3788#ifdef FEAT_FOLDING
3789 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3790 foldOpenCursor();
3791#endif
3792 if (print_message)
3793 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3794
3795 return retval;
3796}
3797
3798/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003799 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 */
3801 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003802qf_jump(qf_info_T *qi,
3803 int dir,
3804 int errornr,
3805 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003807 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3808}
3809
3810/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003811 * Jump to a quickfix line.
3812 * If dir == 0 go to entry "errornr".
3813 * If dir == FORWARD go "errornr" valid entries forward.
3814 * If dir == BACKWARD go "errornr" valid entries backward.
3815 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3816 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3817 * else if "errornr" is zero, redisplay the same line
3818 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003819 * If 'newwin' is TRUE, then open the file in a new window.
3820 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003821 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003822qf_jump_newwin(qf_info_T *qi,
3823 int dir,
3824 int errornr,
3825 int forceit,
3826 int newwin)
3827{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003828 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003829 qfline_T *qf_ptr;
3830 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003833 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003834 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003835 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003838 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003839 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003841 if (qi == NULL)
64-bitman88d41ab2025-04-06 17:20:39 +02003842 {
3843 if (ql_info == NULL)
3844 {
3845 emsg(_(e_no_quickfix_stack));
3846 return;
3847 }
3848 qi = ql_info;
3849 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003850
Bram Moolenaar0398e002019-03-21 21:12:49 +01003851 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003853 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 return;
3855 }
3856
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003857 incr_quickfix_busy();
3858
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003859 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003860
3861 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003863 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003865
3866 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3867 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003869 qf_ptr = old_qf_ptr;
3870 qf_index = old_qf_index;
3871 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003874 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003875 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003876 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003877 // No need to print the error message if it's visible in the error
3878 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 print_message = FALSE;
3880
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003881 prev_winid = curwin->w_id;
3882
Bram Moolenaarb2443732018-11-11 22:50:27 +01003883 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003884 if (retval == FAIL)
3885 goto failed;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003886 if (retval == QF_ABORT)
3887 {
3888 qi = NULL;
3889 qf_ptr = NULL;
3890 goto theend;
3891 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003892 if (retval == NOTDONE)
3893 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003894
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003895 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003896 &opened_window, old_KeyTyped, print_message);
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003897 if (retval == QF_ABORT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 {
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003899 // Quickfix/location list was modified by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003900 qi = NULL;
3901 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003904 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003907 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003908 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003910 // Couldn't open file, so put index back where it was. This could
3911 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 qf_ptr = old_qf_ptr;
3914 qf_index = old_qf_index;
3915 }
3916 }
3917theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003918 if (qi != NULL)
3919 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003920 qfl->qf_ptr = qf_ptr;
3921 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003922 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003923 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003925 // Restore old 'switchbuf' value, but not when an autocommand or
3926 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003927 p_swb = old_swb;
3928 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003930 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931}
3932
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003933// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003934static int qfFileAttr;
3935static int qfSepAttr;
3936static int qfLineAttr;
3937
3938/*
3939 * Display information about a single entry from the quickfix/location list.
3940 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003941 * 'cursel' will be set to TRUE for the currently selected entry in the
3942 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003943 */
3944 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003945qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003946{
3947 char_u *fname;
3948 buf_T *buf;
3949 int filter_entry;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003950 garray_T *gap;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003951
3952 fname = NULL;
3953 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3954 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3955 (char *)qfp->qf_module);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00003956 else
3957 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003958 if (qfp->qf_fnum != 0
3959 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3960 {
Austin Chang29822992024-10-03 10:50:05 +02003961 if (qfp->qf_fname == NULL)
3962 fname = buf->b_fname;
3963 else
3964 fname = qfp->qf_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003965 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003966 fname = gettail(fname);
3967 }
3968 if (fname == NULL)
3969 sprintf((char *)IObuff, "%2d", qf_idx);
3970 else
3971 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3972 qf_idx, (char *)fname);
3973 }
3974
3975 // Support for filtering entries using :filter /pat/ clist
3976 // Match against the module name, file name, search pattern and
3977 // text of the entry.
3978 filter_entry = TRUE;
3979 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3980 filter_entry &= message_filtered(qfp->qf_module);
3981 if (filter_entry && fname != NULL)
3982 filter_entry &= message_filtered(fname);
3983 if (filter_entry && qfp->qf_pattern != NULL)
3984 filter_entry &= message_filtered(qfp->qf_pattern);
3985 if (filter_entry)
3986 filter_entry &= message_filtered(qfp->qf_text);
3987 if (filter_entry)
3988 return;
3989
3990 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003991 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003992
3993 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003994 msg_puts_attr(":", qfSepAttr);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003995 gap = qfga_get();
Shane Harper5bf04282023-06-07 19:09:57 +01003996 if (qfp->qf_lnum != 0)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003997 qf_range_text(gap, qfp);
3998 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
3999 ga_append(gap, NUL);
4000 msg_puts_attr((char *)gap->ga_data, qfLineAttr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004001 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02004002 if (qfp->qf_pattern != NULL)
4003 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004004 gap = qfga_get();
4005 qf_fmt_text(gap, qfp->qf_pattern);
Shane Harper5bf04282023-06-07 19:09:57 +01004006 ga_append(gap, NUL);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004007 msg_puts((char *)gap->ga_data);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004008 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02004009 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004010 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02004011
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004012 // Remove newlines and leading whitespace from the text. For an
4013 // unrecognized line keep the indent, the compiler may mark a word
4014 // with ^^^^.
4015 gap = qfga_get();
4016 qf_fmt_text(gap, (fname != NULL || qfp->qf_lnum != 0)
Shane Harper5bf04282023-06-07 19:09:57 +01004017 ? skipwhite(qfp->qf_text) : qfp->qf_text);
4018 ga_append(gap, NUL);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004019 msg_prt_line((char_u *)gap->ga_data, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004020 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02004021}
4022
4023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004025 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 */
4027 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004028qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004030 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004031 qfline_T *qfp;
4032 int i;
4033 int idx1 = 1;
4034 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004035 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02004036 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004037 int all = eap->forceit; // if not :cl!, only show
4038 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004039 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004041 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4042 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004043
Bram Moolenaar0398e002019-03-21 21:12:49 +01004044 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004046 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 return;
4048 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02004049 if (*arg == '+')
4050 {
4051 ++arg;
4052 plus = TRUE;
4053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
4055 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004056 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 return;
4058 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004059 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02004060 if (plus)
4061 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004062 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02004063 idx2 = i + idx1;
4064 idx1 = i;
4065 }
4066 else
4067 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004068 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02004069 if (idx1 < 0)
4070 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
4071 if (idx2 < 0)
4072 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
4073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004075 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02004076 shorten_fnames(FALSE);
4077
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004078 // Get the attributes for the different quickfix highlight items. Note
4079 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01004080 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
4081 if (qfFileAttr == 0)
4082 qfFileAttr = HL_ATTR(HLF_D);
4083 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
4084 if (qfSepAttr == 0)
4085 qfSepAttr = HL_ATTR(HLF_D);
4086 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
4087 if (qfLineAttr == 0)
4088 qfLineAttr = HL_ATTR(HLF_N);
4089
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004090 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02004092 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
4094 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004095 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004096
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 ui_breakcheck();
4098 }
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01004099 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100}
4101
4102/*
4103 * Remove newlines and leading whitespace from an error message.
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004104 * Add the result to the grow array "gap".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 */
4106 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004107qf_fmt_text(garray_T *gap, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 char_u *p = text;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004110 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 {
4112 if (*p == '\n')
4113 {
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004114 ga_append(gap, ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004116 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 break;
4118 }
4119 else
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004120 ga_append(gap, *p++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122}
4123
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004124/*
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004125 * Add the range information from the lnum, col, end_lnum, and end_col values
4126 * of a quickfix entry to the grow array "gap".
thinca6864efa2021-06-19 20:45:20 +02004127 */
4128 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004129qf_range_text(garray_T *gap, qfline_T *qfp)
thinca6864efa2021-06-19 20:45:20 +02004130{
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004131 char_u *buf = IObuff;
4132 int bufsize = IOSIZE;
thinca6864efa2021-06-19 20:45:20 +02004133 int len;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004134
thinca6864efa2021-06-19 20:45:20 +02004135 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
4136 len = (int)STRLEN(buf);
4137
4138 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
4139 {
Shane Harper5bf04282023-06-07 19:09:57 +01004140 vim_snprintf((char *)buf + len, bufsize - len, "-%ld",
4141 qfp->qf_end_lnum);
thinca6864efa2021-06-19 20:45:20 +02004142 len += (int)STRLEN(buf + len);
4143 }
4144 if (qfp->qf_col > 0)
4145 {
4146 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
4147 len += (int)STRLEN(buf + len);
4148 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
4149 {
Shane Harper5bf04282023-06-07 19:09:57 +01004150 vim_snprintf((char *)buf + len, bufsize - len, "-%d",
4151 qfp->qf_end_col);
thinca6864efa2021-06-19 20:45:20 +02004152 len += (int)STRLEN(buf + len);
4153 }
4154 }
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004155
4156 ga_concat_len(gap, buf, len);
thinca6864efa2021-06-19 20:45:20 +02004157}
4158
4159/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004160 * Display information (list number, list size and the title) about a
4161 * quickfix/location list.
4162 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004163 static void
4164qf_msg(qf_info_T *qi, int which, char *lead)
4165{
4166 char *title = (char *)qi->qf_lists[which].qf_title;
4167 int count = qi->qf_lists[which].qf_count;
4168 char_u buf[IOSIZE];
4169
4170 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
4171 lead,
4172 which + 1,
4173 qi->qf_listcount,
4174 count);
4175
4176 if (title != NULL)
4177 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02004178 size_t len = STRLEN(buf);
4179
4180 if (len < 34)
4181 {
4182 vim_memset(buf + len, ' ', 34 - len);
4183 buf[34] = NUL;
4184 }
4185 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004186 }
4187 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004188 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004189}
4190
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191/*
4192 * ":colder [count]": Up in the quickfix stack.
4193 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004194 * ":lolder [count]": Up in the location list stack.
4195 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 */
4197 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004198qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004200 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 int count;
4202
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004203 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4204 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004205
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 if (eap->addr_count != 0)
4207 count = eap->line2;
4208 else
4209 count = 1;
4210 while (count--)
4211 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004212 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004214 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004216 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02004217 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004219 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221 else
4222 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004223 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004225 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02004226 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004228 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 }
4230 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004231 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004232 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233}
4234
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004235/*
4236 * Display the information about all the quickfix/location lists in the stack
4237 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004238 void
4239qf_history(exarg_T *eap)
4240{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004241 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004242 int i;
4243
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004244 if (eap->addr_count > 0)
4245 {
4246 if (qi == NULL)
4247 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004248 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004249 return;
4250 }
4251
4252 // Jump to the specified quickfix list
4253 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
4254 {
4255 qi->qf_curlist = eap->line2 - 1;
4256 qf_msg(qi, qi->qf_curlist, "");
4257 qf_update_buffer(qi, NULL);
4258 }
4259 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004260 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004261
4262 return;
4263 }
4264
Bram Moolenaar5b69c222019-01-11 14:50:06 +01004265 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004266 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004267 else
4268 for (i = 0; i < qi->qf_listcount; ++i)
4269 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
4270}
4271
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004273 * Free all the entries in the error list "idx". Note that other information
4274 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 */
4276 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004277qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004279 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004280 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01004281 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004283 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004285 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004286 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004287 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01004288 {
Austin Chang29822992024-10-03 10:50:05 +02004289 vim_free(qfp->qf_fname);
Bram Moolenaard76ce852018-05-01 15:02:04 +02004290 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004291 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004292 vim_free(qfp->qf_pattern);
Tom Praschanca6ac992023-08-11 23:26:12 +02004293 clear_tv(&qfp->qf_user_data);
Bram Moolenaard76ce852018-05-01 15:02:04 +02004294 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004295 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01004296 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004297 // Somehow qf_count may have an incorrect value, set it to 1
4298 // to avoid crashing when it's wrong.
4299 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004300 qfl->qf_count = 1;
Christian Brabandt567cae22023-11-19 16:19:27 +01004301 else
4302 qfl->qf_start = qfpnext;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01004303 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004304 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004306
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004307 qfl->qf_index = 0;
4308 qfl->qf_start = NULL;
4309 qfl->qf_last = NULL;
4310 qfl->qf_ptr = NULL;
4311 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02004312
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004313 qf_clean_dir_stack(&qfl->qf_dir_stack);
4314 qfl->qf_directory = NULL;
4315 qf_clean_dir_stack(&qfl->qf_file_stack);
4316 qfl->qf_currfile = NULL;
4317 qfl->qf_multiline = FALSE;
4318 qfl->qf_multiignore = FALSE;
4319 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320}
4321
4322/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004323 * Free error list "idx". Frees all the entries in the quickfix list,
4324 * associated context information and the title.
4325 */
4326 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004327qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004328{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004329 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004330
Bram Moolenaard23a8232018-02-10 18:45:26 +01004331 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004332 free_tv(qfl->qf_ctx);
4333 qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004334 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004335 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004336 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004337}
4338
4339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 * qf_mark_adjust: adjust marks
4341 */
4342 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004343qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02004344 win_T *wp,
4345 linenr_T line1,
4346 linenr_T line2,
4347 long amount,
4348 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004350 int i;
4351 qfline_T *qfp;
4352 int idx;
64-bitman88d41ab2025-04-06 17:20:39 +02004353 qf_info_T *qi = ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004354 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02004355 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
Bram Moolenaarc1542742016-07-20 21:44:37 +02004357 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004358 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004359 if (wp != NULL)
4360 {
4361 if (wp->w_llist == NULL)
4362 return;
4363 qi = wp->w_llist;
4364 }
64-bitman88d41ab2025-04-06 17:20:39 +02004365 else if (qi == NULL)
4366 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004367
4368 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01004369 {
4370 qf_list_T *qfl = qf_get_list(qi, idx);
4371
4372 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01004373 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 if (qfp->qf_fnum == curbuf->b_fnum)
4375 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004376 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
4378 {
4379 if (amount == MAXLNUM)
4380 qfp->qf_cleared = TRUE;
4381 else
4382 qfp->qf_lnum += amount;
4383 }
4384 else if (amount_after && qfp->qf_lnum > line2)
4385 qfp->qf_lnum += amount_after;
4386 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01004387 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004388
4389 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02004390 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391}
4392
4393/*
4394 * Make a nice message out of the error character and the error number:
4395 * char number message
4396 * e or E 0 " error"
4397 * w or W 0 " warning"
4398 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004399 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 * 0 0 ""
4401 * other 0 " c"
4402 * e or E n " error n"
4403 * w or W n " warning n"
4404 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004405 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 * 0 n " error n"
4407 * other n " c n"
4408 * 1 x "" :helpgrep
4409 */
4410 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004411qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412{
4413 static char_u buf[20];
4414 static char_u cc[3];
4415 char_u *p;
4416
4417 if (c == 'W' || c == 'w')
4418 p = (char_u *)" warning";
4419 else if (c == 'I' || c == 'i')
4420 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004421 else if (c == 'N' || c == 'n')
4422 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4424 p = (char_u *)" error";
4425 else if (c == 0 || c == 1)
4426 p = (char_u *)"";
4427 else
4428 {
4429 cc[0] = ' ';
4430 cc[1] = c;
4431 cc[2] = NUL;
4432 p = cc;
4433 }
4434
4435 if (nr <= 0)
4436 return p;
4437
4438 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4439 return buf;
4440}
4441
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004443 * When "split" is FALSE: Open the entry/result under the cursor.
4444 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4445 */
4446 void
4447qf_view_result(int split)
4448{
64-bitman88d41ab2025-04-06 17:20:39 +02004449 qf_info_T *qi = ql_info;
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004450
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004451 if (IS_LL_WINDOW(curwin))
4452 qi = GET_LOC_LIST(curwin);
64-bitman88d41ab2025-04-06 17:20:39 +02004453 else if (qi == NULL)
4454 {
4455 emsg(_(e_no_quickfix_stack));
4456 return;
4457 }
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004458
Bram Moolenaar0398e002019-03-21 21:12:49 +01004459 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004460 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004461 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004462 return;
4463 }
4464
4465 if (split)
4466 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004467 // Open the selected entry in a new window
4468 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4469 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004470 return;
4471 }
4472
4473 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4474}
4475
4476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 * ":cwindow": open the quickfix window if we have errors to display,
4478 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004479 * ":lwindow": open the location list window if we have locations to display,
4480 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 */
4482 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004483ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004485 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004486 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 win_T *win;
4488
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004489 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4490 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004491
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004492 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004493
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004494 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004495 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004497 // If a quickfix window is open but we have no errors to display,
4498 // close the window. If a quickfix window is not open, then open
4499 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004500 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004501 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004502 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 {
4504 if (win != NULL)
4505 ex_cclose(eap);
4506 }
4507 else if (win == NULL)
4508 ex_copen(eap);
4509}
4510
4511/*
4512 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004513 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004516ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004518 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004519 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004521 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4522 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004524 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004525 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 if (win != NULL)
4527 win_close(win, FALSE);
4528}
4529
4530/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004531 * Set "w:quickfix_title" if "qi" has a title.
4532 */
4533 static void
4534qf_set_title_var(qf_list_T *qfl)
4535{
4536 if (qfl->qf_title != NULL)
4537 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4538}
4539
4540/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004541 * Goto a quickfix or location list window (if present).
4542 * Returns OK if the window is found, FAIL otherwise.
4543 */
4544 static int
4545qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4546{
4547 win_T *win;
4548
4549 win = qf_find_win(qi);
4550 if (win == NULL)
4551 return FAIL;
4552
4553 win_goto(win);
4554 if (resize)
4555 {
4556 if (vertsplit)
4557 {
4558 if (sz != win->w_width)
4559 win_setwidth(sz);
4560 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004561 else if (sz != win->w_height && win->w_height
4562 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004563 win_setheight(sz);
4564 }
4565
4566 return OK;
4567}
4568
4569/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004570 * Set options for the buffer in the quickfix or location list window.
4571 */
4572 static void
4573qf_set_cwindow_options(void)
4574{
4575 // switch off 'swapfile'
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004576 set_option_value_give_err((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4577 set_option_value_give_err((char_u *)"bt",
4578 0L, (char_u *)"quickfix", OPT_LOCAL);
4579 set_option_value_give_err((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004580 RESET_BINDING(curwin);
4581#ifdef FEAT_DIFF
4582 curwin->w_p_diff = FALSE;
4583#endif
4584#ifdef FEAT_FOLDING
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004585 set_option_value_give_err((char_u *)"fdm", 0L, (char_u *)"manual",
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004586 OPT_LOCAL);
4587#endif
4588}
4589
4590/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004591 * Open a new quickfix or location list window, load the quickfix buffer and
4592 * set the appropriate options for the window.
4593 * Returns FAIL if the window could not be opened.
4594 */
4595 static int
4596qf_open_new_cwindow(qf_info_T *qi, int height)
4597{
4598 buf_T *qf_buf;
4599 win_T *oldwin = curwin;
4600 tabpage_T *prevtab = curtab;
4601 int flags = 0;
4602 win_T *win;
4603
4604 qf_buf = qf_find_buf(qi);
4605
4606 // The current window becomes the previous window afterwards.
4607 win = curwin;
4608
Bram Moolenaare1004402020-10-24 20:49:43 +02004609 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004610 // Create the new quickfix window at the very bottom, except when
4611 // :belowright or :aboveleft is used.
4612 win_goto(lastwin);
4613 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004614 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004615 flags = WSP_BELOW;
4616 flags |= WSP_NEWLOC;
4617 if (win_split(height, flags) == FAIL)
4618 return FAIL; // not enough room for window
4619 RESET_BINDING(curwin);
4620
4621 if (IS_LL_STACK(qi))
4622 {
4623 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004624 // location list stack from the window 'win'.
4625 curwin->w_llist_ref = qi;
4626 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004627 }
4628
4629 if (oldwin != curwin)
4630 oldwin = NULL; // don't store info when in another window
4631 if (qf_buf != NULL)
4632 {
4633 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004634 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004635 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004636 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004637 }
4638 else
4639 {
4640 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004641 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4642 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004643 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004644
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004645 // save the number of the new buffer
4646 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004647 }
4648
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004649 // Set the options for the quickfix buffer/window (if not already done)
4650 // Do this even if the quickfix buffer was already present, as an autocmd
4651 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004652 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004653 qf_set_cwindow_options();
4654
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004655 // Only set the height when still in the same tab page and there is no
4656 // window to the side.
4657 if (curtab == prevtab && curwin->w_width == Columns)
4658 win_setheight(height);
4659 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4660 if (win_valid(win))
4661 prevwin = win;
4662
4663 return OK;
4664}
4665
4666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004668 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 */
4670 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004671ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004673 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004674 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004676 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004677 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004679 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4680 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004681
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004682 incr_quickfix_busy();
4683
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 if (eap->addr_count != 0)
4685 height = eap->line2;
4686 else
4687 height = QF_WINHEIGHT;
4688
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004689 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690#ifdef FEAT_GUI
4691 need_mouse_correct = TRUE;
4692#endif
4693
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004694 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004695 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004696 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004697 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004698 if (status == FAIL)
4699 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004700 {
4701 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004702 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004705 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004706 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004707 // Save the current index here, as updating the quickfix buffer may free
4708 // the quickfix list
4709 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004710
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004711 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004712 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004714 decr_quickfix_busy();
4715
4716 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 curwin->w_cursor.col = 0;
4718 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004719 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720}
4721
4722/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004723 * Move the cursor in the quickfix window to "lnum".
4724 */
4725 static void
4726qf_win_goto(win_T *win, linenr_T lnum)
4727{
4728 win_T *old_curwin = curwin;
4729
4730 curwin = win;
4731 curbuf = win->w_buffer;
4732 curwin->w_cursor.lnum = lnum;
4733 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004734 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004735 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004736 update_topline(); // scroll to show the line
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004737 redraw_later(UPD_VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004738 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004739 curwin = old_curwin;
4740 curbuf = curwin->w_buffer;
4741}
4742
4743/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004744 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004745 */
4746 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004747ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004748{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004749 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004750 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004751
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004752 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4753 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004754
4755 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004756 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4757 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4758}
4759
4760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 * Return the number of the current entry (line number in the quickfix
4762 * window).
4763 */
4764 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004765qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766{
64-bitman88d41ab2025-04-06 17:20:39 +02004767 qf_info_T *qi = ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004768
4769 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004770 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004771 qi = wp->w_llist_ref;
64-bitman88d41ab2025-04-06 17:20:39 +02004772 else if (qi == NULL)
4773 return 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004774
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004775 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776}
4777
4778/*
4779 * Update the cursor position in the quickfix window to the current error.
4780 * Return TRUE if there is a quickfix window.
4781 */
4782 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004783qf_win_pos_update(
4784 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004785 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786{
4787 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004788 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004790 // Put the cursor on the current error in the quickfix window, so that
4791 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004792 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 if (win != NULL
4794 && qf_index <= win->w_buffer->b_ml.ml_line_count
4795 && old_qf_index != qf_index)
4796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 if (qf_index > old_qf_index)
4798 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004799 win->w_redraw_top = old_qf_index;
4800 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
4802 else
4803 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004804 win->w_redraw_top = qf_index;
4805 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004807 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809 return win != NULL;
4810}
4811
4812/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004813 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004814 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004815 */
4816 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004817is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004818{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004819 // A window displaying the quickfix buffer will have the w_llist_ref field
4820 // set to NULL.
4821 // A window displaying a location list buffer will have the w_llist_ref
4822 // pointing to the location list.
Christian Brabandtfc682992023-09-03 20:20:52 +02004823 if (buf_valid(win->w_buffer) && bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004824 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4825 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004826 return TRUE;
4827
4828 return FALSE;
4829}
4830
4831/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004832 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4833 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004834 */
4835 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004836qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004837{
4838 win_T *win;
4839
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004840 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004841 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004842 return win;
4843 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004844}
4845
4846/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004847 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004848 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 */
4850 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004851qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004853 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004854 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004856 if (qi->qf_bufnr != INVALID_QFBUFNR)
4857 {
4858 buf_T *qfbuf;
4859 qfbuf = buflist_findnr(qi->qf_bufnr);
4860 if (qfbuf != NULL)
4861 return qfbuf;
4862 // buffer is no longer present
4863 qi->qf_bufnr = INVALID_QFBUFNR;
4864 }
4865
Bram Moolenaar9c102382006-05-03 21:26:49 +00004866 FOR_ALL_TAB_WINDOWS(tp, win)
4867 if (is_qf_win(win, qi))
4868 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004869
Bram Moolenaar9c102382006-05-03 21:26:49 +00004870 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871}
4872
4873/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004874 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004875 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004876 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004877 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004878did_set_quickfixtextfunc(optset_T *args UNUSED)
Bram Moolenaard43906d2020-07-20 21:31:32 +02004879{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004880 if (option_set_callback_func(p_qftf, &qftf_cb) == FAIL)
4881 return e_invalid_argument;
4882
4883 return NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004884}
4885
4886/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004887 * Update the w:quickfix_title variable in the quickfix/location list window in
4888 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004889 */
4890 static void
4891qf_update_win_titlevar(qf_info_T *qi)
4892{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004893 qf_list_T *qfl = qf_get_curlist(qi);
4894 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004895 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004896 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004897
Bram Moolenaar530bed92020-12-16 21:02:56 +01004898 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004899 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004900 if (is_qf_win(win, qi))
4901 {
4902 curwin = win;
4903 qf_set_title_var(qfl);
4904 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004905 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004906 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004907}
4908
4909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 * Find the quickfix buffer. If it exists, update the contents.
4911 */
4912 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004913qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914{
4915 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004916 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004919 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004920 buf = qf_find_buf(qi);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004921 if (buf == NULL)
4922 return;
4923
4924 linenr_T old_line_count = buf->b_ml.ml_line_count;
4925 int qf_winid = 0;
4926
4927 if (IS_LL_STACK(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004929 if (curwin->w_llist == qi)
4930 win = curwin;
4931 else
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004932 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004933 // Find the file window (non-quickfix) with this location list
4934 win = qf_find_win_with_loclist(qi);
4935 if (win == NULL)
4936 // File window is not found. Find the location list window.
4937 win = qf_find_win(qi);
4938 if (win == NULL)
4939 return;
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004940 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004941 qf_winid = win->w_id;
4942 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004943
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004944 // autocommands may cause trouble
4945 incr_quickfix_busy();
Bram Moolenaard0fab102022-10-20 16:03:33 +01004946
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004947 int do_fill = TRUE;
4948 if (old_last == NULL)
4949 {
4950 // set curwin/curbuf to buf and save a few things
4951 aucmd_prepbuf(&aco, buf);
4952 if (curbuf != buf)
4953 do_fill = FALSE; // failed to find a window for "buf"
4954 }
4955
4956 if (do_fill)
4957 {
4958 qf_update_win_titlevar(qi);
4959
4960 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
4961 ++CHANGEDTICK(buf);
4962
Bram Moolenaar864293a2016-06-02 13:40:04 +02004963 if (old_last == NULL)
4964 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004965 (void)qf_win_pos_update(qi, 0);
4966
4967 // restore curwin/curbuf and a few other things
4968 aucmd_restbuf(&aco);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004971
4972 // Only redraw when added lines are visible. This avoids flickering
4973 // when the added lines are not visible.
4974 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4975 redraw_buf_later(buf, UPD_NOT_VALID);
4976
4977 // always called after incr_quickfix_busy()
4978 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979}
4980
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004981/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004982 * Add an error line to the quickfix buffer.
4983 */
4984 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004985qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004986 buf_T *buf, // quickfix window buffer
4987 linenr_T lnum,
4988 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004989 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004990 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004991 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004992{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004993 buf_T *errbuf;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004994 garray_T *gap;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004995
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004996 gap = qfga_get();
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004997
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004998 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004999 // for this entry, then use it.
5000 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00005001 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005002 ga_concat(gap, qftf_str);
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00005003 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005004 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005005 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02005006 if (qfp->qf_module != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005007 ga_concat(gap, qfp->qf_module);
Bram Moolenaar858ba062020-05-31 23:11:59 +02005008 else if (qfp->qf_fnum != 0
5009 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
5010 && errbuf->b_fname != NULL)
5011 {
5012 if (qfp->qf_type == 1) // :helpgrep
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005013 ga_concat(gap, gettail(errbuf->b_fname));
Bram Moolenaar858ba062020-05-31 23:11:59 +02005014 else
5015 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02005016 // Shorten the file name if not done already.
5017 // For optimization, do this only for the first entry in a
5018 // buffer.
5019 if (first_bufline && (errbuf->b_sfname == NULL
5020 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005021 {
5022 if (*dirname == NUL)
5023 mch_dirname(dirname, MAXPATHL);
5024 shorten_buf_fname(errbuf, dirname, FALSE);
5025 }
Austin Chang29822992024-10-03 10:50:05 +02005026 if (qfp->qf_fname == NULL)
5027 ga_concat(gap, errbuf->b_fname);
5028 else
5029 ga_concat(gap, qfp->qf_fname);
Bram Moolenaar858ba062020-05-31 23:11:59 +02005030 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02005031 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02005032
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005033 ga_append(gap, '|');
Bram Moolenaar858ba062020-05-31 23:11:59 +02005034
5035 if (qfp->qf_lnum > 0)
5036 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005037 qf_range_text(gap, qfp);
5038 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005039 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02005040 else if (qfp->qf_pattern != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005041 qf_fmt_text(gap, qfp->qf_pattern);
5042 ga_append(gap, '|');
5043 ga_append(gap, ' ');
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005044
Bram Moolenaar858ba062020-05-31 23:11:59 +02005045 // Remove newlines and leading whitespace from the text.
5046 // For an unrecognized line keep the indent, the compiler may
5047 // mark a word with ^^^^.
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00005048 qf_fmt_text(gap, gap->ga_len > 3 ? skipwhite(qfp->qf_text)
5049 : qfp->qf_text);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005050 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005051
Shane Harper5bf04282023-06-07 19:09:57 +01005052 ga_append(gap, NUL);
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00005053 if (ml_append_buf(buf, lnum, gap->ga_data, gap->ga_len, FALSE) == FAIL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005054 return FAIL;
5055
5056 return OK;
5057}
5058
Bram Moolenaard43906d2020-07-20 21:31:32 +02005059/*
5060 * Call the 'quickfixtextfunc' function to get the list of lines to display in
5061 * the quickfix window for the entries 'start_idx' to 'end_idx'.
5062 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005063 static list_T *
5064call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
5065{
Bram Moolenaard43906d2020-07-20 21:31:32 +02005066 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005067 list_T *qftf_list = NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01005068 static int recursive = FALSE;
5069
5070 if (recursive)
5071 return NULL; // this doesn't work properly recursively
5072 recursive = TRUE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005073
5074 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
5075 // the text to display. Use the local value of 'quickfixtextfunc' if it is
5076 // set.
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01005077 if (qfl->qf_qftf_cb.cb_name != NULL)
5078 cb = &qfl->qf_qftf_cb;
5079 if (cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005080 {
5081 typval_T args[1];
5082 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02005083 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005084
5085 // create the dict argument
5086 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
Bram Moolenaard6c67622022-08-24 20:07:22 +01005087 {
5088 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005089 return NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01005090 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005091 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
5092 dict_add_number(d, "winid", (long)qf_winid);
5093 dict_add_number(d, "id", (long)qfl->qf_id);
5094 dict_add_number(d, "start_idx", start_idx);
5095 dict_add_number(d, "end_idx", end_idx);
5096 ++d->dv_refcount;
5097 args[0].v_type = VAR_DICT;
5098 args[0].vval.v_dict = d;
5099
Bram Moolenaard43906d2020-07-20 21:31:32 +02005100 qftf_list = NULL;
5101 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
5102 {
5103 if (rettv.v_type == VAR_LIST)
5104 {
5105 qftf_list = rettv.vval.v_list;
5106 qftf_list->lv_refcount++;
5107 }
5108 clear_tv(&rettv);
5109 }
5110 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005111 }
5112
Bram Moolenaard6c67622022-08-24 20:07:22 +01005113 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005114 return qftf_list;
5115}
5116
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 * Fill current buffer with quickfix errors, replacing any previous contents.
5119 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02005120 * If "old_last" is not NULL append the items after this one.
5121 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
5122 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 */
5124 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02005125qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005127 linenr_T lnum;
5128 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005129 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005130 list_T *qftf_list = NULL;
5131 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
Bram Moolenaar864293a2016-06-02 13:40:04 +02005133 if (old_last == NULL)
5134 {
zeertzjqc7a8eb52024-05-08 20:22:40 +02005135 win_T *wp;
5136 tabpage_T *tp;
5137
Bram Moolenaar864293a2016-06-02 13:40:04 +02005138 if (buf != curbuf)
5139 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005140 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02005141 return;
5142 }
5143
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005144 // delete all existing lines
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01005145 //
5146 // Note: we cannot store undo information, because
5147 // qf buffer is usually not allowed to be modified.
5148 //
5149 // So we need to clean up undo information
5150 // otherwise autocommands may invalidate the undo stack
Bram Moolenaar864293a2016-06-02 13:40:04 +02005151 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02005152 (void)ml_delete((linenr_T)1);
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01005153
zeertzjqc7a8eb52024-05-08 20:22:40 +02005154 FOR_ALL_TAB_WINDOWS(tp, wp)
5155 if (wp->w_buffer == curbuf)
5156 wp->w_skipcol = 0;
5157
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01005158 // Remove all undo information
Christian Brabandt9071ed82024-02-15 20:17:37 +01005159 u_clearallandblockfree(curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005162 // Check if there is anything to display
Bram Moolenaar4f1b0832022-08-29 20:45:16 +01005163 if (qfl != NULL && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005165 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02005166 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02005167 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02005168
5169 *dirname = NUL;
5170
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005171 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01005172 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02005173 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005174 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005175 lnum = 0;
5176 }
5177 else
5178 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01005179 if (old_last->qf_next != NULL)
5180 qfp = old_last->qf_next;
5181 else
5182 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005183 lnum = buf->b_ml.ml_line_count;
5184 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005185
5186 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
5187 (long)qfl->qf_count);
5188 if (qftf_list != NULL)
5189 qftf_li = qftf_list->lv_first;
5190
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005191 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005193 char_u *qftf_str = NULL;
5194
Bram Moolenaard43906d2020-07-20 21:31:32 +02005195 // Use the text supplied by the user defined function (if any).
5196 // If the returned value is not string, then ignore the rest
5197 // of the returned values and use the default.
5198 if (qftf_li != NULL && !invalid_val)
5199 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005200 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02005201 if (qftf_str == NULL)
5202 invalid_val = TRUE;
5203 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005204
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02005205 if (qf_buf_add_line(buf, lnum, qfp, dirname,
5206 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005208
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02005209 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005210 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005212 if (qfp == NULL)
5213 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005214
5215 if (qftf_li != NULL)
5216 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02005218
5219 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005220 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02005221 (void)ml_delete(lnum + 1);
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01005222
5223 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
5225
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005226 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 check_lnums(TRUE);
5228
Bram Moolenaar864293a2016-06-02 13:40:04 +02005229 if (old_last == NULL)
5230 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005231 // Set the 'filetype' to "qf" each time after filling the buffer.
5232 // This resembles reading a file into a buffer, it's more logical when
5233 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02005234 ++curbuf_lock;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005235 set_option_value_give_err((char_u *)"ft",
5236 0L, (char_u *)"qf", OPT_LOCAL);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005237 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
zeertzjq5bf6c212024-03-31 18:41:27 +02005239 curbuf->b_keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02005240 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005242 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 FALSE, curbuf);
zeertzjq5bf6c212024-03-31 18:41:27 +02005244 curbuf->b_keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02005245 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005246
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005247 // make sure it will be redrawn
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005248 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005251 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 KeyTyped = old_KeyTyped;
5253}
5254
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005255/*
5256 * For every change made to the quickfix list, update the changed tick.
5257 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01005258 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005259qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01005260{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005261 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005262}
5263
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005265 * Return the quickfix/location list number with the given identifier.
5266 * Returns -1 if list is not found.
5267 */
5268 static int
5269qf_id2nr(qf_info_T *qi, int_u qfid)
5270{
5271 int qf_idx;
5272
5273 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
5274 if (qi->qf_lists[qf_idx].qf_id == qfid)
5275 return qf_idx;
5276 return INVALID_QFIDX;
5277}
5278
5279/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005280 * If the current list is not "save_qfid" and we can find the list with that ID
5281 * then make it the current list.
5282 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005283 * Returns OK if successfully restored the list. Returns FAIL if the list with
5284 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005285 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005286 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005287qf_restore_list(qf_info_T *qi, int_u save_qfid)
5288{
5289 int curlist;
5290
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00005291 if (qf_get_curlist(qi)->qf_id == save_qfid)
5292 return OK;
5293
5294 curlist = qf_id2nr(qi, save_qfid);
5295 if (curlist < 0)
5296 // list is not present
5297 return FAIL;
5298 qi->qf_curlist = curlist;
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005299 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005300}
5301
5302/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005303 * Jump to the first entry if there is one.
5304 */
5305 static void
5306qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
5307{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005308 if (qf_restore_list(qi, save_qfid) == FAIL)
5309 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005310
Colin Kennedy21570352024-03-03 16:16:47 +01005311
5312 if (!check_can_set_curbuf_forceit(forceit))
5313 return;
5314
5315
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005316 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01005317 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005318 qf_jump(qi, 0, 0, forceit);
5319}
5320
5321/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005322 * Return TRUE when using ":vimgrep" for ":grep".
5323 */
5324 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005325grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005326{
Bram Moolenaar754b5602006-02-09 23:53:20 +00005327 return ((cmdidx == CMD_grep
5328 || cmdidx == CMD_lgrep
5329 || cmdidx == CMD_grepadd
5330 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005331 && STRCMP("internal",
5332 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
5333}
5334
5335/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005336 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005338 static char_u *
5339make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005341 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02005342 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005343 case CMD_make: return (char_u *)"make";
5344 case CMD_lmake: return (char_u *)"lmake";
5345 case CMD_grep: return (char_u *)"grep";
5346 case CMD_lgrep: return (char_u *)"lgrep";
5347 case CMD_grepadd: return (char_u *)"grepadd";
5348 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5349 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02005350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351}
5352
5353/*
5354 * Return the name for the errorfile, in allocated memory.
5355 * Find a new unique name when 'makeef' contains "##".
5356 * Returns NULL for error.
5357 */
5358 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01005359get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360{
5361 char_u *p;
5362 char_u *name;
5363 static int start = -1;
5364 static int off = 0;
5365#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02005366 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367#endif
5368
5369 if (*p_mef == NUL)
5370 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005371 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005373 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 return name;
5375 }
5376
5377 for (p = p_mef; *p; ++p)
5378 if (p[0] == '#' && p[1] == '#')
5379 break;
5380
5381 if (*p == NUL)
5382 return vim_strsave(p_mef);
5383
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005384 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 for (;;)
5386 {
5387 if (start == -1)
5388 start = mch_get_pid();
5389 else
5390 off += 19;
5391
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005392 name = alloc_id(STRLEN(p_mef) + 30, aid_qf_mef_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 if (name == NULL)
5394 break;
5395 STRCPY(name, p_mef);
5396 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
5397 STRCAT(name, p + 2);
5398 if (mch_getperm(name) < 0
5399#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005400 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 && mch_lstat((char *)name, &sb) < 0
5402#endif
5403 )
5404 break;
5405 vim_free(name);
5406 }
5407 return name;
5408}
5409
5410/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005411 * Form the complete command line to invoke 'make'/'grep'. Quote the command
5412 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
5413 */
5414 static char_u *
5415make_get_fullcmd(char_u *makecmd, char_u *fname)
5416{
5417 char_u *cmd;
5418 unsigned len;
5419
5420 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
5421 if (*p_sp != NUL)
5422 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005423 cmd = alloc_id(len, aid_qf_makecmd);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005424 if (cmd == NULL)
5425 return NULL;
5426 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
5427 (char *)p_shq);
5428
5429 // If 'shellpipe' empty: don't redirect to 'errorfile'.
5430 if (*p_sp != NUL)
5431 append_redir(cmd, len, p_sp, fname);
5432
5433 // Display the fully formed command. Output a newline if there's something
5434 // else than the :make command that was typed (in which case the cursor is
5435 // in column 0).
5436 if (msg_col == 0)
5437 msg_didout = FALSE;
5438 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005439 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005440 msg_outtrans(cmd); // show what we are doing
5441
5442 return cmd;
5443}
5444
5445/*
5446 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5447 */
5448 void
5449ex_make(exarg_T *eap)
5450{
5451 char_u *fname;
5452 char_u *cmd;
5453 char_u *enc = NULL;
5454 win_T *wp = NULL;
64-bitman88d41ab2025-04-06 17:20:39 +02005455 qf_info_T *qi = ql_info;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005456 int res;
5457 char_u *au_name = NULL;
5458 int_u save_qfid;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005459 char_u *errorformat = p_efm;
5460 int newlist = TRUE;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005461
5462 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5463 if (grep_internal(eap->cmdidx))
5464 {
5465 ex_vimgrep(eap);
5466 return;
5467 }
5468
5469 au_name = make_get_auname(eap->cmdidx);
5470 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5471 curbuf->b_fname, TRUE, curbuf))
5472 {
5473#ifdef FEAT_EVAL
5474 if (aborting())
5475 return;
5476#endif
5477 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005478 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005479
5480 if (is_loclist_cmd(eap->cmdidx))
5481 wp = curwin;
5482
5483 autowrite_all();
5484 fname = get_mef_name();
5485 if (fname == NULL)
5486 return;
5487 mch_remove(fname); // in case it's not unique
5488
5489 cmd = make_get_fullcmd(eap->arg, fname);
5490 if (cmd == NULL)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005491 {
5492 vim_free(fname);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005493 return;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005494 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005495
5496 // let the shell know if we are redirecting output or not
5497 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5498
5499#ifdef AMIGA
5500 out_flush();
5501 // read window status report and redraw before message
5502 (void)char_avail();
5503#endif
5504
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005505 incr_quickfix_busy();
5506
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005507 if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake)
5508 errorformat = p_gefm;
5509 if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
5510 newlist = FALSE;
5511
5512 res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep),
5513 enc);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005514 if (wp != NULL)
5515 {
5516 qi = GET_LOC_LIST(wp);
5517 if (qi == NULL)
5518 goto cleanup;
5519 }
64-bitman88d41ab2025-04-06 17:20:39 +02005520 else if (qi == NULL)
5521 goto cleanup;
5522
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005523 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005524 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005525
5526 // Remember the current quickfix list identifier, so that we can
5527 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005528 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005529 if (au_name != NULL)
5530 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5531 curbuf->b_fname, TRUE, curbuf);
5532 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5533 // display the first error
5534 qf_jump_first(qi, save_qfid, FALSE);
5535
5536cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005537 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005538 mch_remove(fname);
5539 vim_free(fname);
5540 vim_free(cmd);
5541}
5542
5543/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005544 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005545 */
5546 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005547qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005548{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005549 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005550
5551 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5552 return 0;
5553 return qf_get_curlist(qi)->qf_count;
5554}
5555
5556/*
5557 * Returns the number of valid entries in the current quickfix/location list.
5558 */
5559 int
5560qf_get_valid_size(exarg_T *eap)
5561{
5562 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005563 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005564 qfline_T *qfp;
5565 int i, sz = 0;
5566 int prev_fnum = 0;
5567
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005568 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5569 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005570
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005571 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005572 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005573 {
5574 if (qfp->qf_valid)
5575 {
5576 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005577 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005578 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5579 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005580 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005581 sz++;
5582 prev_fnum = qfp->qf_fnum;
5583 }
5584 }
5585 }
5586
5587 return sz;
5588}
5589
5590/*
5591 * Returns the current index of the quickfix/location list.
5592 * Returns 0 if there is an error.
5593 */
5594 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005595qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005596{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005597 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005598
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005599 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5600 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005601
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005602 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005603}
5604
5605/*
5606 * Returns the current index in the quickfix/location list (counting only valid
5607 * entries). If no valid entries are in the list, then returns 1.
5608 */
5609 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005610qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005611{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005612 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005613 qf_list_T *qfl;
5614 qfline_T *qfp;
5615 int i, eidx = 0;
5616 int prev_fnum = 0;
5617
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005618 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5619 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005620
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005621 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005622 qfp = qfl->qf_start;
5623
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005624 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005625 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005626 return 1;
5627
5628 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5629 {
5630 if (qfp->qf_valid)
5631 {
5632 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5633 {
5634 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5635 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005636 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005637 eidx++;
5638 prev_fnum = qfp->qf_fnum;
5639 }
5640 }
5641 else
5642 eidx++;
5643 }
5644 }
5645
5646 return eidx ? eidx : 1;
5647}
5648
5649/*
5650 * Get the 'n'th valid error entry in the quickfix or location list.
5651 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5652 * For :cdo and :ldo returns the 'n'th valid error entry.
5653 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5654 */
5655 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005656qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005657{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005658 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005659 int i, eidx;
5660 int prev_fnum = 0;
5661
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005662 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005663 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005664 return 1;
5665
Bram Moolenaar95946f12019-03-31 15:31:59 +02005666 eidx = 0;
5667 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005668 {
5669 if (qfp->qf_valid)
5670 {
5671 if (fdo)
5672 {
5673 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5674 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005675 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005676 eidx++;
5677 prev_fnum = qfp->qf_fnum;
5678 }
5679 }
5680 else
5681 eidx++;
5682 }
5683
5684 if (eidx == n)
5685 break;
5686 }
5687
5688 if (i <= qfl->qf_count)
5689 return i;
5690 else
5691 return 1;
5692}
5693
5694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005696 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005697 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 */
5699 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005700ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005702 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005703 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005704
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005705 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5706 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005707
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005708 if (eap->addr_count > 0)
5709 errornr = (int)eap->line2;
5710 else
5711 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005712 switch (eap->cmdidx)
5713 {
5714 case CMD_cc: case CMD_ll:
5715 errornr = 0;
5716 break;
5717 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5718 case CMD_lfirst:
5719 errornr = 1;
5720 break;
5721 default:
5722 errornr = 32767;
5723 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005724 }
5725
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005726 // For cdo and ldo commands, jump to the nth valid error.
5727 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005728 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5729 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005730 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005731 eap->addr_count > 0 ? (int)eap->line1 : 1,
5732 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5733
5734 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735}
5736
5737/*
5738 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005739 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005740 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 */
5742 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005743ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005745 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005746 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005747 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005748
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005749 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5750 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005751
Bram Moolenaar55b69262017-08-13 13:42:01 +02005752 if (eap->addr_count > 0
5753 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5754 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005755 errornr = (int)eap->line2;
5756 else
5757 errornr = 1;
5758
Bram Moolenaar39665952018-08-15 20:59:48 +02005759 // Depending on the command jump to either next or previous entry/file.
5760 switch (eap->cmdidx)
5761 {
5762 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5763 dir = FORWARD;
5764 break;
5765 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5766 case CMD_lNext:
5767 dir = BACKWARD;
5768 break;
5769 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5770 dir = FORWARD_FILE;
5771 break;
5772 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5773 dir = BACKWARD_FILE;
5774 break;
5775 default:
5776 dir = FORWARD;
5777 break;
5778 }
5779
5780 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781}
5782
5783/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005784 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5785 * The index of the entry is stored in 'errornr'.
5786 * Returns NULL if an entry is not found.
5787 */
5788 static qfline_T *
5789qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5790{
5791 qfline_T *qfp = NULL;
5792 int idx = 0;
5793
5794 // Find the first entry in this file
5795 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5796 if (qfp->qf_fnum == bnr)
5797 break;
5798
5799 *errornr = idx;
5800 return qfp;
5801}
5802
5803/*
5804 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5805 * with the error number for the first entry. Assumes the entries are sorted in
5806 * the quickfix list by line number.
5807 */
5808 static qfline_T *
5809qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5810{
5811 while (!got_int
5812 && entry->qf_prev != NULL
5813 && entry->qf_fnum == entry->qf_prev->qf_fnum
5814 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5815 {
5816 entry = entry->qf_prev;
5817 --*errornr;
5818 }
5819
5820 return entry;
5821}
5822
5823/*
5824 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5825 * with the error number for the last entry. Assumes the entries are sorted in
5826 * the quickfix list by line number.
5827 */
5828 static qfline_T *
5829qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5830{
5831 while (!got_int &&
5832 entry->qf_next != NULL
5833 && entry->qf_fnum == entry->qf_next->qf_fnum
5834 && entry->qf_lnum == entry->qf_next->qf_lnum)
5835 {
5836 entry = entry->qf_next;
5837 ++*errornr;
5838 }
5839
5840 return entry;
5841}
5842
5843/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005844 * Returns TRUE if the specified quickfix entry is
5845 * after the given line (linewise is TRUE)
5846 * or after the line and column.
5847 */
5848 static int
5849qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5850{
5851 if (linewise)
5852 return qfp->qf_lnum > pos->lnum;
5853 else
5854 return (qfp->qf_lnum > pos->lnum ||
5855 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5856}
5857
5858/*
5859 * Returns TRUE if the specified quickfix entry is
5860 * before the given line (linewise is TRUE)
5861 * or before the line and column.
5862 */
5863 static int
5864qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5865{
5866 if (linewise)
5867 return qfp->qf_lnum < pos->lnum;
5868 else
5869 return (qfp->qf_lnum < pos->lnum ||
5870 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5871}
5872
5873/*
5874 * Returns TRUE if the specified quickfix entry is
5875 * on or after the given line (linewise is TRUE)
5876 * or on or after the line and column.
5877 */
5878 static int
5879qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5880{
5881 if (linewise)
5882 return qfp->qf_lnum >= pos->lnum;
5883 else
5884 return (qfp->qf_lnum > pos->lnum ||
5885 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5886}
5887
5888/*
5889 * Returns TRUE if the specified quickfix entry is
5890 * on or before the given line (linewise is TRUE)
5891 * or on or before the line and column.
5892 */
5893 static int
5894qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5895{
5896 if (linewise)
5897 return qfp->qf_lnum <= pos->lnum;
5898 else
5899 return (qfp->qf_lnum < pos->lnum ||
5900 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5901}
5902
5903/*
5904 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5905 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5906 * multiple entries on a single line as one. Otherwise returns the entry after
5907 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005908 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5909 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005910 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005911 */
5912 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005913qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005914 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005915 pos_T *pos,
5916 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005917 qfline_T *qfp,
5918 int *errornr)
5919{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005920 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005921 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005922 return qfp;
5923
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005924 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005925 while (qfp->qf_next != NULL
5926 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005927 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005928 {
5929 qfp = qfp->qf_next;
5930 ++*errornr;
5931 }
5932
5933 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005934 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005935 return NULL;
5936
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005937 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005938 qfp = qfp->qf_next;
5939 ++*errornr;
5940
5941 return qfp;
5942}
5943
5944/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005945 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5946 * If 'linewise' is TRUE, returns the entry before the specified line and
5947 * treats multiple entries on a single line as one. Otherwise returns the entry
5948 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005949 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5950 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005951 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005952 */
5953 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005954qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005955 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005956 pos_T *pos,
5957 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005958 qfline_T *qfp,
5959 int *errornr)
5960{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005961 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005962 while (qfp->qf_next != NULL
5963 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005964 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005965 {
5966 qfp = qfp->qf_next;
5967 ++*errornr;
5968 }
5969
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005970 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005971 return NULL;
5972
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005973 if (linewise)
5974 // If multiple entries are on the same line, then use the first entry
5975 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005976
5977 return qfp;
5978}
5979
5980/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005981 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005982 * the direction 'dir'.
5983 */
5984 static qfline_T *
5985qf_find_closest_entry(
5986 qf_list_T *qfl,
5987 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005988 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005989 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005990 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005991 int *errornr)
5992{
5993 qfline_T *qfp;
5994
5995 *errornr = 0;
5996
5997 // Find the first entry in this file
5998 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5999 if (qfp == NULL)
6000 return NULL; // no entry in this file
6001
6002 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006003 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006004 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006005 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006006
6007 return qfp;
6008}
6009
6010/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006011 * Get the nth quickfix entry below the specified entry. Searches forward in
6012 * the list. If linewise is TRUE, then treat multiple entries on a single line
6013 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006014 */
Bram Moolenaar64416122019-06-07 21:37:13 +02006015 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006016qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006017{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006018 qfline_T *entry = entry_arg;
6019
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006020 while (n-- > 0 && !got_int)
6021 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006022 int first_errornr = *errornr;
6023
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006024 if (linewise)
6025 // Treat all the entries on the same line in this file as one
6026 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006027
6028 if (entry->qf_next == NULL
6029 || entry->qf_next->qf_fnum != entry->qf_fnum)
6030 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006031 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006032 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006033 break;
6034 }
6035
6036 entry = entry->qf_next;
6037 ++*errornr;
6038 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006039}
6040
6041/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006042 * Get the nth quickfix entry above the specified entry. Searches backwards in
6043 * the list. If linewise is TRUE, then treat multiple entries on a single line
6044 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006045 */
Bram Moolenaar64416122019-06-07 21:37:13 +02006046 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006047qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006048{
6049 while (n-- > 0 && !got_int)
6050 {
6051 if (entry->qf_prev == NULL
6052 || entry->qf_prev->qf_fnum != entry->qf_fnum)
6053 break;
6054
6055 entry = entry->qf_prev;
6056 --*errornr;
6057
6058 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006059 if (linewise)
6060 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006061 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006062}
6063
6064/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006065 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
6066 * the specified direction. Returns the error number in the quickfix list or 0
6067 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006068 */
6069 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006070qf_find_nth_adj_entry(
6071 qf_list_T *qfl,
6072 int bnr,
6073 pos_T *pos,
6074 int n,
6075 int dir,
6076 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006077{
6078 qfline_T *adj_entry;
6079 int errornr;
6080
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006081 // Find an entry closest to the specified position
6082 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006083 if (adj_entry == NULL)
6084 return 0;
6085
6086 if (--n > 0)
6087 {
6088 // Go to the n'th entry in the current buffer
6089 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02006090 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006091 else
Bram Moolenaar64416122019-06-07 21:37:13 +02006092 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006093 }
6094
6095 return errornr;
6096}
6097
6098/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006099 * Jump to a quickfix entry in the current file nearest to the current line or
6100 * current line/col.
6101 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
6102 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006103 */
6104 void
6105ex_cbelow(exarg_T *eap)
6106{
6107 qf_info_T *qi;
6108 qf_list_T *qfl;
6109 int dir;
6110 int buf_has_flag;
6111 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006112 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006113
6114 if (eap->addr_count > 0 && eap->line2 <= 0)
6115 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006116 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006117 return;
6118 }
6119
6120 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006121 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
6122 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006123 buf_has_flag = BUF_HAS_QF_ENTRY;
6124 else
6125 buf_has_flag = BUF_HAS_LL_ENTRY;
6126 if (!(curbuf->b_has_qf_entry & buf_has_flag))
6127 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006128 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006129 return;
6130 }
6131
6132 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
6133 return;
6134
6135 qfl = qf_get_curlist(qi);
6136 // check if the list has valid errors
6137 if (!qf_list_has_valid_entries(qfl))
6138 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006139 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006140 return;
6141 }
6142
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006143 if (eap->cmdidx == CMD_cbelow
6144 || eap->cmdidx == CMD_lbelow
6145 || eap->cmdidx == CMD_cafter
6146 || eap->cmdidx == CMD_lafter)
6147 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006148 dir = FORWARD;
6149 else
6150 dir = BACKWARD;
6151
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006152 pos = curwin->w_cursor;
6153 // A quickfix entry column number is 1 based whereas cursor column
6154 // number is 0 based. Adjust the column number.
6155 pos.col++;
6156 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
6157 eap->addr_count > 0 ? eap->line2 : 0, dir,
6158 eap->cmdidx == CMD_cbelow
6159 || eap->cmdidx == CMD_lbelow
6160 || eap->cmdidx == CMD_cabove
6161 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006162
6163 if (errornr > 0)
6164 qf_jump(qi, 0, errornr, FALSE);
6165 else
6166 emsg(_(e_no_more_items));
6167}
6168
6169/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01006170 * Return the autocmd name for the :cfile Ex commands
6171 */
6172 static char_u *
6173cfile_get_auname(cmdidx_T cmdidx)
6174{
6175 switch (cmdidx)
6176 {
6177 case CMD_cfile: return (char_u *)"cfile";
6178 case CMD_cgetfile: return (char_u *)"cgetfile";
6179 case CMD_caddfile: return (char_u *)"caddfile";
6180 case CMD_lfile: return (char_u *)"lfile";
6181 case CMD_lgetfile: return (char_u *)"lgetfile";
6182 case CMD_laddfile: return (char_u *)"laddfile";
6183 default: return NULL;
6184 }
6185}
6186
6187/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006188 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006189 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 */
6191 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006192ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006194 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006195 win_T *wp = NULL;
64-bitman88d41ab2025-04-06 17:20:39 +02006196 qf_info_T *qi = ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01006197 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006198 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006199 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006200
Bram Moolenaara16123a2019-03-28 20:31:07 +01006201 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02006202 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6203 NULL, FALSE, curbuf))
6204 {
6205#ifdef FEAT_EVAL
6206 if (aborting())
6207 return;
6208#endif
6209 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006210
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006211 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02006212#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006213 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02006214 {
6215 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02006216 NULL, NULL,
6217 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02006218 if (browse_file == NULL)
6219 return;
6220 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
6221 vim_free(browse_file);
6222 }
6223 else
6224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006226 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006227
Bram Moolenaar39665952018-08-15 20:59:48 +02006228 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01006229 wp = curwin;
6230
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006231 incr_quickfix_busy();
6232
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006233 // This function is used by the :cfile, :cgetfile and :caddfile
6234 // commands.
Colin Kennedy21570352024-03-03 16:16:47 +01006235 // :cfile always creates a new quickfix list and may jump to the
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006236 // first error.
6237 // :cgetfile creates a new quickfix list but doesn't jump to the
6238 // first error.
6239 // :caddfile adds to an existing quickfix list. If there is no
6240 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006241 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006242 && eap->cmdidx != CMD_laddfile),
6243 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006244 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006245 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01006246 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006247 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006248 {
6249 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006250 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006251 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006252 }
64-bitman88d41ab2025-04-06 17:20:39 +02006253 else if (qi == NULL)
6254 {
6255 decr_quickfix_busy();
6256 return;
6257 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006258 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006259 qf_list_changed(qf_get_curlist(qi));
6260 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006261 if (au_name != NULL)
6262 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01006263
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006264 // Jump to the first error for a new list and if autocmds didn't
6265 // free the list.
6266 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
6267 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006268 // display the first error
6269 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006270
6271 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006272}
6273
6274/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006275 * Return the vimgrep autocmd name.
6276 */
6277 static char_u *
6278vgr_get_auname(cmdidx_T cmdidx)
6279{
6280 switch (cmdidx)
6281 {
6282 case CMD_vimgrep: return (char_u *)"vimgrep";
6283 case CMD_lvimgrep: return (char_u *)"lvimgrep";
6284 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
6285 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
6286 case CMD_grep: return (char_u *)"grep";
6287 case CMD_lgrep: return (char_u *)"lgrep";
6288 case CMD_grepadd: return (char_u *)"grepadd";
6289 case CMD_lgrepadd: return (char_u *)"lgrepadd";
6290 default: return NULL;
6291 }
6292}
6293
6294/*
6295 * Initialize the regmatch used by vimgrep for pattern "s".
6296 */
6297 static void
6298vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
6299{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006300 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006301 regmatch->regprog = NULL;
6302
6303 if (s == NULL || *s == NUL)
6304 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006305 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006306 if (last_search_pat() == NULL)
6307 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006308 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006309 return;
6310 }
6311 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
6312 }
6313 else
6314 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
6315
6316 regmatch->rmm_ic = p_ic;
6317 regmatch->rmm_maxcol = 0;
6318}
6319
6320/*
6321 * Display a file name when vimgrep is running.
6322 */
6323 static void
6324vgr_display_fname(char_u *fname)
6325{
6326 char_u *p;
6327
6328 msg_start();
6329 p = msg_strtrunc(fname, TRUE);
6330 if (p == NULL)
6331 msg_outtrans(fname);
6332 else
6333 {
6334 msg_outtrans(p);
6335 vim_free(p);
6336 }
6337 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006338 msg_didout = FALSE; // overwrite this message
6339 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006340 msg_col = 0;
6341 out_flush();
6342}
6343
6344/*
6345 * Load a dummy buffer to search for a pattern using vimgrep.
6346 */
6347 static buf_T *
6348vgr_load_dummy_buf(
6349 char_u *fname,
6350 char_u *dirname_start,
6351 char_u *dirname_now)
6352{
6353 int save_mls;
6354#if defined(FEAT_SYN_HL)
6355 char_u *save_ei = NULL;
6356#endif
6357 buf_T *buf;
6358
6359#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006360 // Don't do Filetype autocommands to avoid loading syntax and
6361 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006362 save_ei = au_event_disable(",Filetype");
6363#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006364 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006365 save_mls = p_mls;
6366 p_mls = 0;
6367
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006368 // Load file into a buffer, so that 'fileencoding' is detected,
6369 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006370 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
6371
6372 p_mls = save_mls;
6373#if defined(FEAT_SYN_HL)
6374 au_event_restore(save_ei);
6375#endif
6376
6377 return buf;
6378}
6379
6380/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006381 * Check whether a quickfix/location list is valid. Autocmds may remove or
6382 * change a quickfix list when vimgrep is running. If the list is not found,
6383 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006384 */
6385 static int
6386vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006387 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006388 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006389 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006390 char_u *title)
6391{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006392 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006393 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006394 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006395 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006396 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006397 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02006398 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006399 return FALSE;
6400 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006401 else
6402 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006403 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006404 qf_new_list(qi, title);
6405 return TRUE;
6406 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006407 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006408
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02006409 if (qf_restore_list(qi, qfid) == FAIL)
6410 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006411
6412 return TRUE;
6413}
6414
6415/*
6416 * Search for a pattern in all the lines in a buffer and add the matching lines
6417 * to a quickfix list.
6418 */
6419 static int
6420vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006421 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006422 char_u *fname,
6423 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006424 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006425 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01006426 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006427 int duplicate_name,
6428 int flags)
6429{
6430 int found_match = FALSE;
6431 long lnum;
6432 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02006433 int pat_len = (int)STRLEN(spat);
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006434 if (pat_len > MAX_FUZZY_MATCHES)
6435 pat_len = MAX_FUZZY_MATCHES;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006436
Bram Moolenaar1c299432018-10-28 14:36:09 +01006437 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006438 {
6439 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006440 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006441 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006442 // Regular expression match
6443 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006444 col, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006445 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006446 // Pass the buffer number so that it gets used even for a
6447 // dummy buffer, unless duplicate_name is set, then the
6448 // buffer will be wiped out below.
6449 if (qf_add_entry(qfl,
6450 NULL, // dir
6451 fname,
6452 NULL,
6453 duplicate_name ? 0 : buf->b_fnum,
6454 ml_get_buf(buf,
6455 regmatch->startpos[0].lnum + lnum, FALSE),
6456 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02006457 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006458 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02006459 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006460 FALSE, // vis_col
6461 NULL, // search pattern
6462 0, // nr
6463 0, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02006464 NULL, // user_data
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006465 TRUE // valid
6466 ) == QF_FAIL)
6467 {
6468 got_int = TRUE;
6469 break;
6470 }
6471 found_match = TRUE;
6472 if (--*tomatch == 0)
6473 break;
6474 if ((flags & VGR_GLOBAL) == 0
6475 || regmatch->endpos[0].lnum > 0)
6476 break;
6477 col = regmatch->endpos[0].col
6478 + (col == regmatch->endpos[0].col);
zeertzjq94b7c322024-03-12 21:50:32 +01006479 if (col > ml_get_buf_len(buf, lnum))
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006480 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006481 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006482 }
6483 else
6484 {
6485 char_u *str = ml_get_buf(buf, lnum, FALSE);
zeertzjq8c55d602024-03-13 20:42:26 +01006486 colnr_T linelen = ml_get_buf_len(buf, lnum);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006487 int score;
6488 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006489 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006490
6491 // Fuzzy string match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006492 CLEAR_FIELD(matches);
glepnir28e40a72025-03-16 21:24:22 +01006493 while (fuzzy_match(str + col, spat, FALSE, &score,
6494 matches, sz, TRUE) > 0)
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006495 {
6496 // Pass the buffer number so that it gets used even for a
6497 // dummy buffer, unless duplicate_name is set, then the
6498 // buffer will be wiped out below.
6499 if (qf_add_entry(qfl,
6500 NULL, // dir
6501 fname,
6502 NULL,
6503 duplicate_name ? 0 : buf->b_fnum,
6504 str,
6505 lnum,
thinca6864efa2021-06-19 20:45:20 +02006506 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006507 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006508 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006509 FALSE, // vis_col
6510 NULL, // search pattern
6511 0, // nr
6512 0, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02006513 NULL, // user_data
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006514 TRUE // valid
6515 ) == QF_FAIL)
6516 {
6517 got_int = TRUE;
6518 break;
6519 }
6520 found_match = TRUE;
6521 if (--*tomatch == 0)
6522 break;
6523 if ((flags & VGR_GLOBAL) == 0)
6524 break;
6525 col = matches[pat_len - 1] + col + 1;
zeertzjq8c55d602024-03-13 20:42:26 +01006526 if (col > linelen)
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006527 break;
6528 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006529 }
6530 line_breakcheck();
6531 if (got_int)
6532 break;
6533 }
6534
6535 return found_match;
6536}
6537
6538/*
6539 * Jump to the first match and update the directory.
6540 */
6541 static void
6542vgr_jump_to_match(
6543 qf_info_T *qi,
6544 int forceit,
6545 int *redraw_for_dummy,
6546 buf_T *first_match_buf,
6547 char_u *target_dir)
6548{
6549 buf_T *buf;
6550
6551 buf = curbuf;
6552 qf_jump(qi, 0, 0, forceit);
6553 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006554 // If we jumped to another buffer redrawing will already be
6555 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006556 *redraw_for_dummy = FALSE;
6557
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006558 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006559 if (curbuf == first_match_buf && target_dir != NULL)
6560 {
6561 exarg_T ea;
6562
Bram Moolenaara80faa82020-04-12 19:37:17 +02006563 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006564 ea.arg = target_dir;
6565 ea.cmdidx = CMD_lcd;
6566 ex_cd(&ea);
6567 }
6568}
6569
6570/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006571 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006572 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006573typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006574{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006575 long tomatch; // maximum number of matches to find
6576 char_u *spat; // search pattern
6577 int flags; // search modifier
6578 char_u **fnames; // list of files to search
6579 int fcount; // number of files
6580 regmmatch_T regmatch; // compiled search pattern
6581 char_u *qf_title; // quickfix list title
6582} vgr_args_T;
6583
6584/*
6585 * Process :vimgrep command arguments. The command syntax is:
6586 *
6587 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6588 */
6589 static int
6590vgr_process_args(
6591 exarg_T *eap,
6592 vgr_args_T *args)
6593{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006594 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006595
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00006596 CLEAR_POINTER(args);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006597
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006598 args->regmatch.regprog = NULL;
6599 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006600
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006601 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006602 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006603 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006604 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006605
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006606 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006607 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006608 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006609 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006610 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006611 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006612 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006613
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006614 vgr_init_regmatch(&args->regmatch, args->spat);
6615 if (args->regmatch.regprog == NULL)
6616 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006617
6618 p = skipwhite(p);
6619 if (*p == NUL)
6620 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006621 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006622 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006623 }
6624
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006625 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006626 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6627 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006628 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006629 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006630 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006631 }
6632
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006633 return OK;
6634}
6635
6636/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006637 * Return TRUE if "buf" had an existing swap file, the current swap file does
6638 * not end in ".swp".
6639 */
6640 static int
6641existing_swapfile(buf_T *buf)
6642{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006643 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006644 {
6645 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6646 size_t len = STRLEN(fname);
6647
6648 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6649 }
6650 return FALSE;
6651}
6652
6653/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006654 * Search for a pattern in a list of files and populate the quickfix list with
6655 * the matches.
6656 */
6657 static int
6658vgr_process_files(
6659 win_T *wp,
6660 qf_info_T *qi,
6661 vgr_args_T *cmd_args,
6662 int *redraw_for_dummy,
6663 buf_T **first_match_buf,
6664 char_u **target_dir)
6665{
6666 int status = FAIL;
6667 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6668 time_t seconds = 0;
6669 char_u *fname;
6670 int fi;
6671 buf_T *buf;
6672 int duplicate_name = FALSE;
6673 int using_dummy;
6674 char_u *dirname_start = NULL;
6675 char_u *dirname_now = NULL;
6676 int found_match;
6677 aco_save_T aco;
6678
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006679 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6680 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006681 if (dirname_start == NULL || dirname_now == NULL)
6682 goto theend;
6683
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006684 // Remember the current directory, because a BufRead autocommand that does
6685 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006686 mch_dirname(dirname_start, MAXPATHL);
6687
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006688 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006689 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6690 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006691 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006692 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006693 if (time(NULL) > seconds)
6694 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006695 // Display the file name every second or so, show the user we are
6696 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006697 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006698 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006699 }
6700
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006701 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006702 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6703 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006704 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006705 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006706 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006707 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006708
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006709 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006710 }
6711 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006712 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006713 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006714
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006715 // Check whether the quickfix list is still valid. When loading a
6716 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006717 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006718 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006719
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006720 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006721
Bram Moolenaar81695252004-12-29 20:58:21 +00006722 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006723 {
6724 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006725 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006726 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006727 else
6728 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006729 // Try for a match in all lines of the buffer.
6730 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006731 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006732 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006733 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006734
Bram Moolenaar81695252004-12-29 20:58:21 +00006735 if (using_dummy)
6736 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006737 if (found_match && *first_match_buf == NULL)
6738 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006739 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006740 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006741 // Never keep a dummy buffer if there is another buffer
6742 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006743 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006744 buf = NULL;
6745 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006746 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006747 || buf->b_p_bh[0] == 'u' // "unload"
6748 || buf->b_p_bh[0] == 'w' // "wipe"
6749 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006750 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006751 // When no match was found we don't need to remember the
6752 // buffer, wipe it out. If there was a match and it
6753 // wasn't the first one or we won't jump there: only
6754 // unload the buffer.
6755 // Ignore 'hidden' here, because it may lead to having too
6756 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006757 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006758 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006759 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006760 buf = NULL;
6761 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006762 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006763 || (cmd_args->flags & VGR_NOJUMP)
6764 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006765 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006766 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006767 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006768 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006769 buf = NULL;
6770 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006771 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006772
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006773 if (buf != NULL)
6774 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006775 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006776 buf->b_flags &= ~BF_DUMMY;
6777
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006778 // If the buffer is still loaded we need to use the
6779 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006780 if (buf == *first_match_buf
6781 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006782 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006783 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006784
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006785 // The buffer is still loaded, the Filetype autocommands
6786 // need to be done now, in that buffer. And the modelines
6787 // need to be done (again). But not the window-local
6788 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006789 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006790 if (curbuf == buf)
6791 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006792#if defined(FEAT_SYN_HL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00006793 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006794 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006795#endif
Bram Moolenaare76062c2022-11-28 18:51:43 +00006796 do_modelines(OPT_NOWIN);
6797 aucmd_restbuf(&aco);
6798 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006799 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006800 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006801 }
6802 }
6803
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006804 status = OK;
6805
6806theend:
6807 vim_free(dirname_now);
6808 vim_free(dirname_start);
6809 return status;
6810}
6811
6812/*
6813 * ":vimgrep {pattern} file(s)"
6814 * ":vimgrepadd {pattern} file(s)"
6815 * ":lvimgrep {pattern} file(s)"
6816 * ":lvimgrepadd {pattern} file(s)"
6817 */
6818 void
6819ex_vimgrep(exarg_T *eap)
6820{
6821 vgr_args_T args;
6822 qf_info_T *qi;
6823 qf_list_T *qfl;
6824 int_u save_qfid;
6825 win_T *wp = NULL;
6826 int redraw_for_dummy = FALSE;
6827 buf_T *first_match_buf = NULL;
6828 char_u *target_dir = NULL;
6829 char_u *au_name = NULL;
6830 int status;
6831
Colin Kennedy21570352024-03-03 16:16:47 +01006832 if (!check_can_set_curbuf_forceit(eap->forceit))
6833 return;
6834
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006835 au_name = vgr_get_auname(eap->cmdidx);
6836 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6837 curbuf->b_fname, TRUE, curbuf))
6838 {
6839#ifdef FEAT_EVAL
6840 if (aborting())
6841 return;
6842#endif
6843 }
6844
6845 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6846 if (qi == NULL)
6847 return;
6848
6849 if (vgr_process_args(eap, &args) == FAIL)
6850 goto theend;
6851
6852 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6853 && eap->cmdidx != CMD_vimgrepadd
6854 && eap->cmdidx != CMD_lvimgrepadd)
6855 || qf_stack_empty(qi))
6856 // make place for a new list
6857 qf_new_list(qi, args.qf_title);
6858
6859 incr_quickfix_busy();
6860
6861 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6862 &first_match_buf, &target_dir);
6863 if (status != OK)
6864 {
6865 FreeWild(args.fcount, args.fnames);
6866 decr_quickfix_busy();
6867 goto theend;
6868 }
6869
6870 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006871
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006872 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006873 qfl->qf_nonevalid = FALSE;
6874 qfl->qf_ptr = qfl->qf_start;
6875 qfl->qf_index = 1;
6876 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006877
Bram Moolenaar864293a2016-06-02 13:40:04 +02006878 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006879
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006880 // Remember the current quickfix list identifier, so that we can check for
6881 // autocommands changing the current quickfix list.
6882 save_qfid = qf_get_curlist(qi)->qf_id;
6883
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006884 if (au_name != NULL)
6885 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6886 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006887 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6888 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006889 if (!qflist_valid(wp, save_qfid)
6890 || qf_restore_list(qi, save_qfid) == FAIL)
6891 {
6892 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006893 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006894 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006895
zeertzjq8c55d602024-03-13 20:42:26 +01006896 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006897 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006898 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006899 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006900 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6901 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006902 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006903 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006904 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006905
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006906 decr_quickfix_busy();
6907
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006908 // If we loaded a dummy buffer into the current window, the autocommands
6909 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006910 if (redraw_for_dummy)
6911 {
6912#ifdef FEAT_FOLDING
6913 foldUpdateAll(curwin);
6914#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006915 redraw_later(UPD_NOT_VALID);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006916#endif
6917 }
6918
Bram Moolenaar86b68352004-12-27 21:59:20 +00006919theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006920 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006921 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006922 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006923}
6924
6925/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006926 * Restore current working directory to "dirname_start" if they differ, taking
6927 * into account whether it is set locally or globally.
6928 */
6929 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006930restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006931{
6932 char_u *dirname_now = alloc(MAXPATHL);
6933
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006934 if (dirname_now == NULL)
6935 return;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006936
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006937 mch_dirname(dirname_now, MAXPATHL);
6938 if (STRCMP(dirname_start, dirname_now) != 0)
6939 {
6940 // If the directory has changed, change it back by building up an
6941 // appropriate ex command and executing it.
6942 exarg_T ea;
6943
6944 CLEAR_FIELD(ea);
6945 ea.arg = dirname_start;
6946 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6947 ex_cd(&ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006948 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006949 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006950}
6951
6952/*
6953 * Load file "fname" into a dummy buffer and return the buffer pointer,
6954 * placing the directory resulting from the buffer load into the
6955 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6956 * prior to calling this function. Restores directory to "dirname_start" prior
6957 * to returning, if autocmds or the 'autochdir' option have changed it.
6958 *
6959 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6960 * or wipe_dummy_buffer() later!
6961 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006962 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006963 */
6964 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006965load_dummy_buffer(
6966 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006967 char_u *dirname_start, // in: old directory
6968 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006969{
6970 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006971 bufref_T newbufref;
6972 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006973 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006974 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006975 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006976
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006977 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006978 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6979 if (newbuf == NULL)
6980 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006981 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006982
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006983 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006984 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6985
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006986 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006987 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006988 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006989 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006990 ++newbuf->b_locked;
6991
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006992 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006993 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006994 if (curbuf == newbuf)
Bram Moolenaar81695252004-12-29 20:58:21 +00006995 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00006996 // Need to set the filename for autocommands.
6997 (void)setfname(curbuf, fname, NULL, FALSE);
6998
6999 // Create swap file now to avoid the ATTENTION message.
7000 check_need_swap(TRUE);
7001
7002 // Remove the "dummy" flag, otherwise autocommands may not
7003 // work.
7004 curbuf->b_flags &= ~BF_DUMMY;
7005
7006 newbuf_to_wipe.br_buf = NULL;
7007 readfile_result = readfile(fname, NULL,
7008 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
7009 NULL, READ_NEW | READ_DUMMY);
7010 --newbuf->b_locked;
7011 if (readfile_result == OK
7012 && !got_int
7013 && !(curbuf->b_flags & BF_NEW))
Bram Moolenaar81695252004-12-29 20:58:21 +00007014 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00007015 failed = FALSE;
7016 if (curbuf != newbuf)
7017 {
7018 // Bloody autocommands changed the buffer! Can happen when
7019 // using netrw and editing a remote file. Use the current
7020 // buffer instead, delete the dummy one after restoring the
7021 // window stuff.
7022 set_bufref(&newbuf_to_wipe, newbuf);
7023 newbuf = curbuf;
7024 }
Bram Moolenaar81695252004-12-29 20:58:21 +00007025 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00007026
7027 // restore curwin/curbuf and a few other things
7028 aucmd_restbuf(&aco);
Bram Moolenaar81695252004-12-29 20:58:21 +00007029
Bram Moolenaar84497cd2022-11-28 20:34:52 +00007030 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
7031 wipe_buffer(newbuf_to_wipe.br_buf, FALSE);
7032 }
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02007033
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007034 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
7035 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02007036 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00007037 }
Bram Moolenaar81695252004-12-29 20:58:21 +00007038
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007039 // When autocommands/'autochdir' option changed directory: go back.
7040 // Let the caller know what the resulting dir was first, in case it is
7041 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007042 mch_dirname(resulting_dir, MAXPATHL);
7043 restore_start_dir(dirname_start);
7044
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007045 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00007046 return NULL;
7047 if (failed)
7048 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007049 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00007050 return NULL;
7051 }
7052 return newbuf;
7053}
7054
7055/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007056 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
7057 * directory to "dirname_start" prior to returning, if autocmds or the
7058 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00007059 */
7060 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007061wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00007062{
Bram Moolenaar2573af32020-03-14 17:21:34 +01007063 // If any autocommand opened a window on the dummy buffer, close that
7064 // window. If we can't close them all then give up.
7065 while (buf->b_nwindows > 0)
7066 {
7067 int did_one = FALSE;
7068 win_T *wp;
7069
7070 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007071 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01007072 if (wp->w_buffer == buf)
7073 {
7074 if (win_close(wp, FALSE) == OK)
7075 did_one = TRUE;
7076 break;
7077 }
7078 if (!did_one)
7079 return;
7080 }
7081
7082 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00007083 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007084#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00007085 cleanup_T cs;
7086
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007087 // Reset the error/interrupt/exception state here so that aborting()
7088 // returns FALSE when wiping out the buffer. Otherwise it doesn't
7089 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00007090 enter_cleanup(&cs);
7091#endif
7092
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01007093 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00007094
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007095#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007096 // Restore the error/interrupt/exception state if not discarded by a
7097 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00007098 leave_cleanup(&cs);
7099#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007100 // When autocommands/'autochdir' option changed directory: go back.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007101 restore_start_dir(dirname_start);
Bram Moolenaard68071d2006-05-02 22:08:30 +00007102 }
Bram Moolenaar81695252004-12-29 20:58:21 +00007103}
7104
7105/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007106 * Unload the dummy buffer that load_dummy_buffer() created. Restores
7107 * directory to "dirname_start" prior to returning, if autocmds or the
7108 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00007109 */
7110 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007111unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00007112{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007113 if (curbuf == buf) // safety check
7114 return;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007115
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007116 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
7117
7118 // When autocommands/'autochdir' option changed directory: go back.
7119 restore_start_dir(dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00007120}
7121
Bram Moolenaar05159a02005-02-26 23:04:13 +00007122#if defined(FEAT_EVAL) || defined(PROTO)
7123/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007124 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01007125 * to 'list'. Returns OK on success.
7126 */
7127 static int
7128get_qfline_items(qfline_T *qfp, list_T *list)
7129{
7130 int bufnum;
7131 dict_T *dict;
7132 char_u buf[2];
7133
7134 // Handle entries with a non-existing buffer number.
7135 bufnum = qfp->qf_fnum;
7136 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7137 bufnum = 0;
7138
7139 if ((dict = dict_alloc()) == NULL)
7140 return FAIL;
7141 if (list_append_dict(list, dict) == FAIL)
7142 return FAIL;
7143
7144 buf[0] = qfp->qf_type;
7145 buf[1] = NUL;
7146 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02007147 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
7148 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
7149 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
7150 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
7151 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
7152 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01007153 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
7154 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
7155 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
7156 || dict_add_string(dict, "type", buf) == FAIL
Tom Praschanca6ac992023-08-11 23:26:12 +02007157 || (qfp->qf_user_data.v_type != VAR_UNKNOWN
7158 && dict_add_tv(dict, "user_data", &qfp->qf_user_data) == FAIL )
Bram Moolenaara16123a2019-03-28 20:31:07 +01007159 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
7160 return FAIL;
7161
7162 return OK;
7163}
7164
7165/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007166 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007167 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02007168 * If eidx is not 0, then return only the specified entry. Otherwise return
7169 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00007170 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007171 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007172get_errorlist(
7173 qf_info_T *qi_arg,
7174 win_T *wp,
7175 int qf_idx,
7176 int eidx,
7177 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007178{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007179 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007180 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007181 qfline_T *qfp;
7182 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007183
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007184 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007185 {
64-bitman88d41ab2025-04-06 17:20:39 +02007186 qi = ql_info;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007187 if (wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007188 qi = GET_LOC_LIST(wp);
64-bitman88d41ab2025-04-06 17:20:39 +02007189 if (qi == NULL)
7190 return FAIL;
7191
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007192 }
7193
Bram Moolenaar858ba062020-05-31 23:11:59 +02007194 if (eidx < 0)
7195 return OK;
7196
Bram Moolenaar29ce4092018-04-28 21:56:44 +02007197 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007198 qf_idx = qi->qf_curlist;
7199
Bram Moolenaar0398e002019-03-21 21:12:49 +01007200 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007201 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007202
Bram Moolenaar0398e002019-03-21 21:12:49 +01007203 qfl = qf_get_list(qi, qf_idx);
7204 if (qf_list_empty(qfl))
7205 return FAIL;
7206
Bram Moolenaara16123a2019-03-28 20:31:07 +01007207 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007208 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007209 if (eidx > 0)
7210 {
7211 if (eidx == i)
7212 return get_qfline_items(qfp, list);
7213 }
7214 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007215 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007216 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007217
Bram Moolenaar05159a02005-02-26 23:04:13 +00007218 return OK;
7219}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007220
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007221// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007222enum {
7223 QF_GETLIST_NONE = 0x0,
7224 QF_GETLIST_TITLE = 0x1,
7225 QF_GETLIST_ITEMS = 0x2,
7226 QF_GETLIST_NR = 0x4,
7227 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007228 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007229 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007230 QF_GETLIST_IDX = 0x40,
7231 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01007232 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007233 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007234 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02007235 QF_GETLIST_QFTF = 0x800,
7236 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007237};
7238
7239/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007240 * Parse text from 'di' and return the quickfix list items.
7241 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007242 */
7243 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02007244qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007245{
7246 int status = FAIL;
7247 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02007248 char_u *errorformat = p_efm;
7249 dictitem_T *efm_di;
7250 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007251
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007252 // Only a List value is supported
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007253 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7254 return FAIL;
7255
7256 // If errorformat is supplied then use it, otherwise use the 'efm'
7257 // option setting
7258 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007259 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007260 if (efm_di->di_tv.v_type != VAR_STRING ||
7261 efm_di->di_tv.vval.v_string == NULL)
Bram Moolenaarda732532017-08-31 20:58:02 +02007262 return FAIL;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007263 errorformat = efm_di->di_tv.vval.v_string;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007264 }
7265
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007266 l = list_alloc();
7267 if (l == NULL)
7268 return FAIL;
7269
64-bitman88d41ab2025-04-06 17:20:39 +02007270 qi = qf_alloc_stack(QFLT_INTERNAL, 1);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007271 if (qi != NULL)
7272 {
7273 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
7274 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
7275 {
7276 (void)get_errorlist(qi, NULL, 0, 0, l);
7277 qf_free(&qi->qf_lists[0]);
7278 }
64-bitman88d41ab2025-04-06 17:20:39 +02007279
7280 qf_free_lists(qi);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007281 }
7282 dict_add_list(retdict, "items", l);
7283 status = OK;
7284
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007285 return status;
7286}
7287
7288/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01007289 * Return the quickfix/location list window identifier in the current tabpage.
7290 */
7291 static int
7292qf_winid(qf_info_T *qi)
7293{
7294 win_T *win;
7295
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007296 // The quickfix window can be opened even if the quickfix list is not set
7297 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01007298 if (qi == NULL)
7299 return 0;
7300 win = qf_find_win(qi);
7301 if (win != NULL)
7302 return win->w_id;
7303 return 0;
7304}
7305
7306/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007307 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00007308 * window. If there is no buffer associated with the list or the buffer is
7309 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007310 */
7311 static int
7312qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
7313{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00007314 int bufnum = 0;
7315
7316 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
7317 bufnum = qi->qf_bufnr;
7318
7319 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007320}
7321
7322/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007323 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007324 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007325 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007326qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007327{
Bram Moolenaard823fa92016-08-12 16:29:27 +02007328 int flags = QF_GETLIST_NONE;
7329
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007330 if (dict_has_key(what, "all"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007331 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007332 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007333 if (!loclist)
7334 // File window ID is applicable only to location list windows
7335 flags &= ~ QF_GETLIST_FILEWINID;
7336 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007337
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007338 if (dict_has_key(what, "title"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007339 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007340
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007341 if (dict_has_key(what, "nr"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01007342 flags |= QF_GETLIST_NR;
7343
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007344 if (dict_has_key(what, "winid"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007345 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007346
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007347 if (dict_has_key(what, "context"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007348 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007349
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007350 if (dict_has_key(what, "id"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01007351 flags |= QF_GETLIST_ID;
7352
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007353 if (dict_has_key(what, "items"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007354 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007355
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007356 if (dict_has_key(what, "idx"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007357 flags |= QF_GETLIST_IDX;
7358
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007359 if (dict_has_key(what, "size"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007360 flags |= QF_GETLIST_SIZE;
7361
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007362 if (dict_has_key(what, "changedtick"))
Bram Moolenaarb254af32017-12-18 19:48:58 +01007363 flags |= QF_GETLIST_TICK;
7364
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007365 if (loclist && dict_has_key(what, "filewinid"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007366 flags |= QF_GETLIST_FILEWINID;
7367
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007368 if (dict_has_key(what, "qfbufnr"))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007369 flags |= QF_GETLIST_QFBUFNR;
7370
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007371 if (dict_has_key(what, "quickfixtextfunc"))
Bram Moolenaard43906d2020-07-20 21:31:32 +02007372 flags |= QF_GETLIST_QFTF;
7373
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007374 return flags;
7375}
Bram Moolenaara6d48492017-12-12 22:45:31 +01007376
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007377/*
7378 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
7379 * If 'nr' and 'id' are not present in 'what' then return the current
7380 * quickfix list index.
7381 * If 'nr' is zero then return the current quickfix list index.
7382 * If 'nr' is '$' then return the last quickfix list index.
7383 * If 'id' is present then return the index of the quickfix list with that id.
7384 * If 'id' is zero then return the quickfix list index specified by 'nr'.
7385 * Return -1, if quickfix list is not present or if the stack is empty.
7386 */
7387 static int
7388qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
7389{
7390 int qf_idx;
7391 dictitem_T *di;
7392
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007393 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007394 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7395 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007396 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007397 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01007398 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007399 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007400 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01007401 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007402 qf_idx = di->di_tv.vval.v_number - 1;
7403 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007404 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007405 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01007406 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007407 else if (di->di_tv.v_type == VAR_STRING
7408 && di->di_tv.vval.v_string != NULL
7409 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007410 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007411 qf_idx = qi->qf_listcount - 1;
7412 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007413 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007414 }
7415
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007416 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
7417 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007418 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007419 if (di->di_tv.v_type == VAR_NUMBER)
7420 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007421 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007422 if (di->di_tv.vval.v_number != 0)
7423 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
7424 }
7425 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007426 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007427 }
7428
7429 return qf_idx;
7430}
7431
7432/*
7433 * Return default values for quickfix list properties in retdict.
7434 */
7435 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007436qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007437{
7438 int status = OK;
7439
7440 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007441 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007442 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
7443 {
7444 list_T *l = list_alloc();
7445 if (l != NULL)
7446 status = dict_add_list(retdict, "items", l);
7447 else
7448 status = FAIL;
7449 }
7450 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007451 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007452 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007453 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007454 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007455 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007456 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007457 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007458 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007459 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007460 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007461 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007462 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007463 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007464 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007465 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007466 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7467 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007468 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7469 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007470
7471 return status;
7472}
7473
7474/*
7475 * Return the quickfix list title as 'title' in retdict
7476 */
7477 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007478qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007479{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007480 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007481}
7482
7483/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007484 * Returns the identifier of the window used to display files from a location
7485 * list. If there is no associated window, then returns 0. Useful only when
7486 * called from a location list window.
7487 */
7488 static int
7489qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7490{
7491 int winid = 0;
7492
7493 if (wp != NULL && IS_LL_WINDOW(wp))
7494 {
7495 win_T *ll_wp = qf_find_win_with_loclist(qi);
7496 if (ll_wp != NULL)
7497 winid = ll_wp->w_id;
7498 }
7499
7500 return dict_add_number(retdict, "filewinid", winid);
7501}
7502
7503/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007504 * Return the quickfix list items/entries as 'items' in retdict.
7505 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007506 */
7507 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007508qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007509{
7510 int status = OK;
7511 list_T *l = list_alloc();
7512 if (l != NULL)
7513 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007514 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007515 dict_add_list(retdict, "items", l);
7516 }
7517 else
7518 status = FAIL;
7519
7520 return status;
7521}
7522
7523/*
7524 * Return the quickfix list context (if any) as 'context' in retdict.
7525 */
7526 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007527qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007528{
7529 int status;
7530 dictitem_T *di;
7531
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007532 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007533 {
7534 di = dictitem_alloc((char_u *)"context");
7535 if (di != NULL)
7536 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007537 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007538 status = dict_add(retdict, di);
7539 if (status == FAIL)
7540 dictitem_free(di);
7541 }
7542 else
7543 status = FAIL;
7544 }
7545 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007546 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007547
7548 return status;
7549}
7550
7551/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007552 * Return the current quickfix list index as 'idx' in retdict.
7553 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007554 */
7555 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007556qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007557{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007558 if (eidx == 0)
7559 {
7560 eidx = qfl->qf_index;
7561 if (qf_list_empty(qfl))
7562 // For empty lists, current index is set to 0
7563 eidx = 0;
7564 }
7565 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007566}
7567
7568/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007569 * Return the 'quickfixtextfunc' function of a quickfix/location list
7570 */
7571 static int
7572qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7573{
7574 int status;
7575
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007576 if (qfl->qf_qftf_cb.cb_name != NULL)
Bram Moolenaard43906d2020-07-20 21:31:32 +02007577 {
7578 typval_T tv;
7579
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007580 put_callback(&qfl->qf_qftf_cb, &tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007581 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7582 clear_tv(&tv);
7583 }
7584 else
7585 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7586
7587 return status;
7588}
7589
7590/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007591 * Return quickfix/location list details (title) as a
7592 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7593 * then current list is used. Otherwise the specified list is used.
7594 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007595 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007596qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7597{
64-bitman88d41ab2025-04-06 17:20:39 +02007598 qf_info_T *qi = ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007599 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007600 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007601 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007602 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007603 dictitem_T *di;
7604 int flags = QF_GETLIST_NONE;
7605
7606 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7607 return qf_get_list_from_lines(what, di, retdict);
7608
7609 if (wp != NULL)
7610 qi = GET_LOC_LIST(wp);
64-bitman88d41ab2025-04-06 17:20:39 +02007611 else if (qi == NULL)
7612 return FAIL;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007613
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007614 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007615
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007616 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007617 qf_idx = qf_getprop_qfidx(qi, what);
7618
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007619 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007620 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007621 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007622
Bram Moolenaar0398e002019-03-21 21:12:49 +01007623 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007624
Bram Moolenaar858ba062020-05-31 23:11:59 +02007625 // If an entry index is specified, use that
7626 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7627 {
7628 if (di->di_tv.v_type != VAR_NUMBER)
7629 return FAIL;
7630 eidx = di->di_tv.vval.v_number;
7631 }
7632
Bram Moolenaard823fa92016-08-12 16:29:27 +02007633 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007634 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007635 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007636 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007637 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007638 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007639 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007640 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007641 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007642 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007643 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007644 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007645 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007646 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007647 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007648 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007649 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007650 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007651 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7652 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007653 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7654 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007655 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7656 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007657
Bram Moolenaard823fa92016-08-12 16:29:27 +02007658 return status;
7659}
7660
7661/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007662 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007663 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7664 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007665 */
7666 static int
7667qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007668 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007669 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007670 int first_entry,
7671 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007672{
7673 static int did_bufnr_emsg;
7674 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007675 int bufnum, valid, status, col, end_col, vcol, nr;
7676 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007677
7678 if (first_entry)
7679 did_bufnr_emsg = FALSE;
7680
Bram Moolenaard61efa52022-07-23 09:52:04 +01007681 filename = dict_get_string(d, "filename", TRUE);
7682 module = dict_get_string(d, "module", TRUE);
7683 bufnum = (int)dict_get_number(d, "bufnr");
7684 lnum = (int)dict_get_number(d, "lnum");
7685 end_lnum = (int)dict_get_number(d, "end_lnum");
7686 col = (int)dict_get_number(d, "col");
7687 end_col = (int)dict_get_number(d, "end_col");
7688 vcol = (int)dict_get_number(d, "vcol");
7689 nr = (int)dict_get_number(d, "nr");
7690 type = dict_get_string(d, "type", TRUE);
7691 pattern = dict_get_string(d, "pattern", TRUE);
7692 text = dict_get_string(d, "text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007693 if (text == NULL)
7694 text = vim_strsave((char_u *)"");
Tom Praschanca6ac992023-08-11 23:26:12 +02007695 typval_T user_data;
7696 user_data.v_type = VAR_UNKNOWN;
7697 dict_get_tv(d, "user_data", &user_data);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007698
7699 valid = TRUE;
7700 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7701 valid = FALSE;
7702
7703 // Mark entries with non-existing buffer number as not valid. Give the
7704 // error message only once.
7705 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7706 {
7707 if (!did_bufnr_emsg)
7708 {
7709 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007710 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007711 }
7712 valid = FALSE;
7713 bufnum = 0;
7714 }
7715
7716 // If the 'valid' field is present it overrules the detected value.
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007717 if (dict_has_key(d, "valid"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01007718 valid = (int)dict_get_bool(d, "valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007719
Bram Moolenaar0398e002019-03-21 21:12:49 +01007720 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007721 NULL, // dir
7722 filename,
7723 module,
7724 bufnum,
7725 text,
7726 lnum,
thinca6864efa2021-06-19 20:45:20 +02007727 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007728 col,
thinca6864efa2021-06-19 20:45:20 +02007729 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007730 vcol, // vis_col
7731 pattern, // search pattern
7732 nr,
7733 type == NULL ? NUL : *type,
Tom Praschanca6ac992023-08-11 23:26:12 +02007734 &user_data,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007735 valid);
7736
7737 vim_free(filename);
7738 vim_free(module);
7739 vim_free(pattern);
7740 vim_free(text);
7741 vim_free(type);
Tom Praschanca6ac992023-08-11 23:26:12 +02007742 clear_tv(&user_data);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007743
Bram Moolenaar9752c722018-12-22 16:49:34 +01007744 if (valid)
7745 *valid_entry = TRUE;
7746
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007747 return status;
7748}
7749
7750/*
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007751 * Check if `entry` is closer to the target than `other_entry`.
7752 *
7753 * Only returns TRUE if `entry` is definitively closer. If it's further
7754 * away, or there's not enough information to tell, return FALSE.
7755 */
7756 static int
7757entry_is_closer_to_target(
7758 qfline_T *entry,
7759 qfline_T *other_entry,
7760 int target_fnum,
7761 int target_lnum,
7762 int target_col)
7763{
7764 // First, compare entries to target file.
7765 if (!target_fnum)
7766 // Without a target file, we can't know which is closer.
7767 return FALSE;
7768
7769 int is_target_file = entry->qf_fnum && entry->qf_fnum == target_fnum;
7770 int other_is_target_file = other_entry->qf_fnum && other_entry->qf_fnum == target_fnum;
7771 if (!is_target_file && other_is_target_file)
7772 return FALSE;
7773 else if (is_target_file && !other_is_target_file)
7774 return TRUE;
7775
7776 // Both entries are pointing at the exact same file. Now compare line
7777 // numbers.
7778 if (!target_lnum)
7779 // Without a target line number, we can't know which is closer.
7780 return FALSE;
7781
7782 int line_distance = entry->qf_lnum ? labs(entry->qf_lnum - target_lnum) : INT_MAX;
7783 int other_line_distance = other_entry->qf_lnum ? labs(other_entry->qf_lnum - target_lnum) : INT_MAX;
7784 if (line_distance > other_line_distance)
7785 return FALSE;
7786 else if (line_distance < other_line_distance)
7787 return TRUE;
7788
7789 // Both entries are pointing at the exact same line number (or no line
7790 // number at all). Now compare columns.
7791 if (!target_col)
7792 // Without a target column, we can't know which is closer.
7793 return FALSE;
7794
7795 int column_distance = entry->qf_col ? abs(entry->qf_col - target_col) : INT_MAX;
7796 int other_column_distance = other_entry->qf_col ? abs(other_entry->qf_col - target_col): INT_MAX;
7797 if (column_distance > other_column_distance)
7798 return FALSE;
7799 else if (column_distance < other_column_distance)
7800 return TRUE;
7801
7802 // It's a complete tie! The exact same file, line, and column.
7803 return FALSE;
7804}
7805
7806/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007807 * Add list of entries to quickfix/location list. Each list entry is
7808 * a dictionary with item information.
7809 */
7810 static int
7811qf_add_entries(
7812 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007813 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007814 list_T *list,
7815 char_u *title,
7816 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007817{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007818 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007819 listitem_T *li;
7820 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007821 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007822 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007823 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007824
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007825 // If there's an entry selected in the quickfix list, remember its location
7826 // (file, line, column), so we can select the nearest entry in the updated
7827 // quickfix list.
7828 int prev_fnum = 0;
7829 int prev_lnum = 0;
7830 int prev_col = 0;
7831 if (qfl->qf_ptr)
7832 {
7833 prev_fnum = qfl->qf_ptr->qf_fnum;
7834 prev_lnum = qfl->qf_ptr->qf_lnum;
7835 prev_col = qfl->qf_ptr->qf_col;
7836 }
7837
7838 int select_first_entry = FALSE;
7839 int select_nearest_entry = FALSE;
7840
Bram Moolenaara3921f42017-06-04 15:30:34 +02007841 if (action == ' ' || qf_idx == qi->qf_listcount)
7842 {
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007843 select_first_entry = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007844 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007845 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007846 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007847 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007848 }
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007849 else if (action == 'a')
7850 {
7851 if (qf_list_empty(qfl))
7852 // Appending to empty list, select first entry.
7853 select_first_entry = TRUE;
7854 else
7855 // Adding to existing list, use last entry.
7856 old_last = qfl->qf_last;
7857 }
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007858 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007859 {
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007860 select_first_entry = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007861 qf_free_items(qfl);
7862 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007863 }
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007864 else if (action == 'u')
7865 {
7866 select_nearest_entry = TRUE;
7867 qf_free_items(qfl);
7868 qf_store_title(qfl, title);
7869 }
7870
7871 qfline_T *entry_to_select = NULL;
7872 int entry_to_select_index = 0;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007873
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007874 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007875 {
7876 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007877 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007878
7879 d = li->li_tv.vval.v_dict;
7880 if (d == NULL)
7881 continue;
7882
Bram Moolenaar0398e002019-03-21 21:12:49 +01007883 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007884 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007885 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007886 break;
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007887
7888 qfline_T *entry = qfl->qf_last;
7889 if (
7890 (select_first_entry && entry_to_select == NULL) ||
7891 (select_nearest_entry &&
7892 (entry_to_select == NULL ||
7893 entry_is_closer_to_target(entry, entry_to_select, prev_fnum,
7894 prev_lnum, prev_col))))
7895 {
7896 entry_to_select = entry;
7897 entry_to_select_index = qfl->qf_count;
7898 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007899 }
7900
Bram Moolenaar9752c722018-12-22 16:49:34 +01007901 // Check if any valid error entries are added to the list.
7902 if (valid_entry)
7903 qfl->qf_nonevalid = FALSE;
7904 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007905 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007906 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007907
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007908 // Set the current error.
7909 if (entry_to_select)
7910 {
7911 qfl->qf_ptr = entry_to_select;
7912 qfl->qf_index = entry_to_select_index;
7913 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007914
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007915 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007916 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007917
7918 return retval;
7919}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007920
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007921/*
7922 * Get the quickfix list index from 'nr' or 'id'
7923 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007924 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007925qf_setprop_get_qfidx(
7926 qf_info_T *qi,
7927 dict_T *what,
7928 int action,
7929 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007930{
7931 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007932 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007933
Bram Moolenaard823fa92016-08-12 16:29:27 +02007934 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7935 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007936 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007937 if (di->di_tv.v_type == VAR_NUMBER)
7938 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007939 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007940 if (di->di_tv.vval.v_number != 0)
7941 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007942
Bram Moolenaar55b69262017-08-13 13:42:01 +02007943 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7944 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007945 // When creating a new list, accept qf_idx pointing to the next
7946 // non-available list and add the new list at the end of the
7947 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007948 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007949 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007950 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007951 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007952 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007953 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007954 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007955 }
7956 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007957 && di->di_tv.vval.v_string != NULL
7958 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007959 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007960 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007961 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007962 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007963 qf_idx = 0;
7964 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007965 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007966 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007967 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007968 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007969 }
7970
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007971 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007972 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007973 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007974 if (di->di_tv.v_type != VAR_NUMBER)
7975 return INVALID_QFIDX;
7976
7977 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007978 }
7979
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007980 return qf_idx;
7981}
7982
7983/*
7984 * Set the quickfix list title.
7985 */
7986 static int
7987qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7988{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007989 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007990
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007991 if (di->di_tv.v_type != VAR_STRING)
7992 return FAIL;
7993
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007994 vim_free(qfl->qf_title);
Bram Moolenaard61efa52022-07-23 09:52:04 +01007995 qfl->qf_title = dict_get_string(what, "title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007996 if (qf_idx == qi->qf_curlist)
7997 qf_update_win_titlevar(qi);
7998
7999 return OK;
8000}
8001
8002/*
8003 * Set quickfix list items/entries.
8004 */
8005 static int
8006qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
8007{
8008 int retval = FAIL;
8009 char_u *title_save;
8010
8011 if (di->di_tv.v_type != VAR_LIST)
8012 return FAIL;
8013
8014 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
8015 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
8016 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008017 vim_free(title_save);
8018
8019 return retval;
8020}
8021
8022/*
8023 * Set quickfix list items/entries from a list of lines.
8024 */
8025 static int
8026qf_setprop_items_from_lines(
8027 qf_info_T *qi,
8028 int qf_idx,
8029 dict_T *what,
8030 dictitem_T *di,
8031 int action)
8032{
8033 char_u *errorformat = p_efm;
8034 dictitem_T *efm_di;
8035 int retval = FAIL;
8036
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008037 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008038 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
8039 {
8040 if (efm_di->di_tv.v_type != VAR_STRING ||
8041 efm_di->di_tv.vval.v_string == NULL)
8042 return FAIL;
8043 errorformat = efm_di->di_tv.vval.v_string;
8044 }
8045
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008046 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008047 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
8048 return FAIL;
8049
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02008050 if (action == 'r' || action == 'u')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02008051 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008052 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01008053 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008054 retval = OK;
8055
8056 return retval;
8057}
8058
8059/*
8060 * Set quickfix list context.
8061 */
8062 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02008063qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008064{
8065 typval_T *ctx;
8066
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02008067 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008068 ctx = alloc_tv();
8069 if (ctx != NULL)
8070 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02008071 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008072
8073 return OK;
8074}
8075
8076/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01008077 * Set the current index in the specified quickfix list
8078 */
8079 static int
8080qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
8081{
8082 int denote = FALSE;
8083 int newidx;
8084 int old_qfidx;
8085 qfline_T *qf_ptr;
8086
8087 // If the specified index is '$', then use the last entry
8088 if (di->di_tv.v_type == VAR_STRING
8089 && di->di_tv.vval.v_string != NULL
8090 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
8091 newidx = qfl->qf_count;
8092 else
8093 {
8094 // Otherwise use the specified index
8095 newidx = tv_get_number_chk(&di->di_tv, &denote);
8096 if (denote)
8097 return FAIL;
8098 }
8099
8100 if (newidx < 1) // sanity check
8101 return FAIL;
8102 if (newidx > qfl->qf_count)
8103 newidx = qfl->qf_count;
8104
8105 old_qfidx = qfl->qf_index;
8106 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
8107 if (qf_ptr == NULL)
8108 return FAIL;
8109 qfl->qf_ptr = qf_ptr;
8110 qfl->qf_index = newidx;
8111
8112 // If the current list is modified and it is displayed in the quickfix
8113 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01008114 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01008115 qf_win_pos_update(qi, old_qfidx);
8116
8117 return OK;
8118}
8119
8120/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02008121 * Set the current index in the specified quickfix list
8122 */
8123 static int
8124qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
8125{
Bram Moolenaard43906d2020-07-20 21:31:32 +02008126 callback_T cb;
8127
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01008128 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02008129 cb = get_callback(&di->di_tv);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00008130 if (cb.cb_name == NULL || *cb.cb_name == NUL)
8131 return OK;
8132
8133 set_callback(&qfl->qf_qftf_cb, &cb);
8134 if (cb.cb_free_name)
8135 vim_free(cb.cb_name);
Bram Moolenaar858ba062020-05-31 23:11:59 +02008136
8137 return OK;
8138}
8139
8140/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008141 * Set quickfix/location list properties (title, items, context).
8142 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008143 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008144 */
8145 static int
8146qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
8147{
8148 dictitem_T *di;
8149 int retval = FAIL;
8150 int qf_idx;
8151 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008152 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008153
Bram Moolenaar019dfe62018-10-07 14:38:49 +02008154 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008155 newlist = TRUE;
8156
8157 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008158 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008159 return FAIL;
8160
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02008161 if (newlist)
8162 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02008163 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02008164 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02008165 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02008166 }
8167
Bram Moolenaar0398e002019-03-21 21:12:49 +01008168 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02008169 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008170 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02008171 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008172 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02008173 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008174 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008175 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008176 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01008177 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
8178 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02008179 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
8180 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008181
Bram Moolenaar287153c2020-11-29 14:20:27 +01008182 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008183 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01008184 if (newlist)
8185 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01008186
Bram Moolenaard823fa92016-08-12 16:29:27 +02008187 return retval;
8188}
8189
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008190/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008191 * Free the entire quickfix/location list stack.
8192 * If the quickfix/location list window is open, then clear it.
8193 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008194 static void
8195qf_free_stack(win_T *wp, qf_info_T *qi)
8196{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008197 win_T *qfwin = qf_find_win(qi);
8198 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008199
8200 if (qfwin != NULL)
8201 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008202 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008203 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01008204 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008205 qf_update_buffer(qi, NULL);
8206 }
8207
8208 if (wp != NULL && IS_LL_WINDOW(wp))
8209 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008210 // If in the location list window, then use the non-location list
8211 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01008212 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008213 if (llwin != NULL)
8214 wp = llwin;
8215 }
8216
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008217 qf_free_all(wp);
8218 if (wp == NULL)
8219 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008220 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008221 qi->qf_curlist = 0;
8222 qi->qf_listcount = 0;
8223 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01008224 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008225 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008226 // If the location list window is open, then create a new empty
8227 // location list
64-bitman88d41ab2025-04-06 17:20:39 +02008228 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION, wp->w_p_lhi);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02008229
Bram Moolenaar95946f12019-03-31 15:31:59 +02008230 if (new_ll != NULL)
8231 {
8232 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02008233
Bram Moolenaar95946f12019-03-31 15:31:59 +02008234 // first free the list reference in the location list window
8235 ll_free_all(&qfwin->w_llist_ref);
8236
8237 qfwin->w_llist_ref = new_ll;
8238 if (wp != qfwin)
8239 win_set_loclist(wp, new_ll);
8240 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008241 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008242}
8243
Bram Moolenaard823fa92016-08-12 16:29:27 +02008244/*
8245 * Populate the quickfix list with the items supplied in the list
8246 * of dictionaries. "title" will be copied to w:quickfix_title.
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02008247 * "action" is 'a' for add, 'r' for replace, 'u' for update. Otherwise
8248 * create a new list. When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02008249 */
8250 int
8251set_errorlist(
8252 win_T *wp,
8253 list_T *list,
8254 int action,
8255 char_u *title,
8256 dict_T *what)
8257{
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02008258 qf_info_T *qi;
Bram Moolenaard823fa92016-08-12 16:29:27 +02008259 int retval = OK;
8260
8261 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02008262 qi = ll_get_or_alloc_list(wp);
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02008263 else
8264 qi = ql_info;
8265 if (qi == NULL)
64-bitman88d41ab2025-04-06 17:20:39 +02008266 return FAIL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02008267
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008268 if (action == 'f')
8269 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008270 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008271 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008272 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008273 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008274
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008275 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02008276 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008277 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008278 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008279 _("cannot have both a list and a \"what\" argument"));
8280 return FAIL;
8281 }
8282
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008283 incr_quickfix_busy();
8284
8285 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02008286 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02008287 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01008288 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02008289 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01008290 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01008291 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01008292 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02008293
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008294 decr_quickfix_busy();
8295
Bram Moolenaard823fa92016-08-12 16:29:27 +02008296 return retval;
8297}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008298
Tom Praschanca6ac992023-08-11 23:26:12 +02008299static int mark_quickfix_user_data(qf_info_T *qi, int copyID)
8300{
8301 int abort = FALSE;
64-bitman88d41ab2025-04-06 17:20:39 +02008302 for (int i = 0; i < qi->qf_maxcount && !abort; ++i)
Tom Praschanca6ac992023-08-11 23:26:12 +02008303 {
8304 qf_list_T *qfl = &qi->qf_lists[i];
8305 if (!qfl->qf_has_user_data)
8306 continue;
8307 qfline_T *qfp;
8308 int j;
8309 FOR_ALL_QFL_ITEMS(qfl, qfp, j)
8310 {
8311 typval_T* user_data = &qfp->qf_user_data;
8312 if (user_data != NULL && user_data->v_type != VAR_NUMBER
8313 && user_data->v_type != VAR_STRING && user_data->v_type != VAR_FLOAT)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01008314 abort = abort || set_ref_in_item(user_data, copyID, NULL, NULL, NULL);
Tom Praschanca6ac992023-08-11 23:26:12 +02008315 }
8316 }
8317 return abort;
8318}
8319
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008320/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008321 * Mark the quickfix context and callback function as in use for all the lists
8322 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008323 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008324 static int
8325mark_quickfix_ctx(qf_info_T *qi, int copyID)
8326{
8327 int i;
8328 int abort = FALSE;
8329 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008330 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008331
64-bitman88d41ab2025-04-06 17:20:39 +02008332 for (i = 0; i < qi->qf_maxcount && !abort; ++i)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008333 {
8334 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02008335 if (ctx != NULL && ctx->v_type != VAR_NUMBER
8336 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01008337 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL, NULL);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008338
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01008339 cb = &qi->qf_lists[i].qf_qftf_cb;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008340 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008341 }
8342
8343 return abort;
8344}
8345
8346/*
8347 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02008348 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008349 */
8350 int
8351set_ref_in_quickfix(int copyID)
8352{
8353 int abort = FALSE;
8354 tabpage_T *tp;
8355 win_T *win;
8356
64-bitman88d41ab2025-04-06 17:20:39 +02008357 if (ql_info == NULL)
8358 return TRUE;
8359
8360 abort = mark_quickfix_ctx(ql_info, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008361 if (abort)
8362 return abort;
8363
64-bitman88d41ab2025-04-06 17:20:39 +02008364 abort = mark_quickfix_user_data(ql_info, copyID);
Tom Praschanca6ac992023-08-11 23:26:12 +02008365 if (abort)
8366 return abort;
8367
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008368 abort = set_ref_in_callback(&qftf_cb, copyID);
8369 if (abort)
8370 return abort;
8371
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008372 FOR_ALL_TAB_WINDOWS(tp, win)
8373 {
8374 if (win->w_llist != NULL)
8375 {
8376 abort = mark_quickfix_ctx(win->w_llist, copyID);
8377 if (abort)
8378 return abort;
Tom Praschanca6ac992023-08-11 23:26:12 +02008379
8380 abort = mark_quickfix_user_data(win->w_llist, copyID);
8381 if (abort)
8382 return abort;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008383 }
Bram Moolenaar12237442017-12-19 12:38:52 +01008384 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
8385 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008386 // In a location list window and none of the other windows is
8387 // referring to this location list. Mark the location list
8388 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01008389 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
8390 if (abort)
8391 return abort;
zeertzjqbe4bd182024-09-14 10:32:31 +02008392
8393 abort = mark_quickfix_user_data(win->w_llist_ref, copyID);
8394 if (abort)
8395 return abort;
Bram Moolenaar12237442017-12-19 12:38:52 +01008396 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008397 }
8398
8399 return abort;
8400}
Bram Moolenaar05159a02005-02-26 23:04:13 +00008401#endif
8402
Bram Moolenaar81695252004-12-29 20:58:21 +00008403/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01008404 * Return the autocmd name for the :cbuffer Ex commands
8405 */
8406 static char_u *
8407cbuffer_get_auname(cmdidx_T cmdidx)
8408{
8409 switch (cmdidx)
8410 {
8411 case CMD_cbuffer: return (char_u *)"cbuffer";
8412 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
8413 case CMD_caddbuffer: return (char_u *)"caddbuffer";
8414 case CMD_lbuffer: return (char_u *)"lbuffer";
8415 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
8416 case CMD_laddbuffer: return (char_u *)"laddbuffer";
8417 default: return NULL;
8418 }
8419}
8420
8421/*
8422 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
8423 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
8424 */
8425 static int
8426cbuffer_process_args(
8427 exarg_T *eap,
8428 buf_T **bufp,
8429 linenr_T *line1,
8430 linenr_T *line2)
8431{
8432 buf_T *buf = NULL;
8433
8434 if (*eap->arg == NUL)
8435 buf = curbuf;
8436 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
8437 buf = buflist_findnr(atoi((char *)eap->arg));
8438
8439 if (buf == NULL)
8440 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008441 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008442 return FAIL;
8443 }
8444
8445 if (buf->b_ml.ml_mfp == NULL)
8446 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008447 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008448 return FAIL;
8449 }
8450
8451 if (eap->addr_count == 0)
8452 {
8453 eap->line1 = 1;
8454 eap->line2 = buf->b_ml.ml_line_count;
8455 }
8456
8457 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
8458 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
8459 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008460 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008461 return FAIL;
8462 }
8463
8464 *line1 = eap->line1;
8465 *line2 = eap->line2;
8466 *bufp = buf;
8467
8468 return OK;
8469}
8470
8471/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00008472 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00008473 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00008474 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008475 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00008476 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00008477 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008478 */
8479 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008480ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008481{
8482 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008483 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008484 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01008485 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02008486 int_u save_qfid;
8487 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01008488 char_u *qf_title;
8489 linenr_T line1;
8490 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008491
Bram Moolenaara16123a2019-03-28 20:31:07 +01008492 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01008493 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01008494 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008495 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008496#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008497 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008498 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008499#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008500 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008501
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008502 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008503 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8504 if (qi == NULL)
8505 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01008506
Bram Moolenaara16123a2019-03-28 20:31:07 +01008507 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
8508 return;
8509
8510 qf_title = qf_cmdtitle(*eap->cmdlinep);
8511
8512 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008513 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01008514 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
8515 (char *)qf_title, (char *)buf->b_sfname);
8516 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008517 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01008518
8519 incr_quickfix_busy();
8520
8521 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
8522 (eap->cmdidx != CMD_caddbuffer
8523 && eap->cmdidx != CMD_laddbuffer),
8524 line1, line2,
8525 qf_title, NULL);
8526 if (qf_stack_empty(qi))
8527 {
8528 decr_quickfix_busy();
8529 return;
8530 }
8531 if (res >= 0)
8532 qf_list_changed(qf_get_curlist(qi));
8533
8534 // Remember the current quickfix list identifier, so that we can
8535 // check for autocommands changing the current quickfix list.
8536 save_qfid = qf_get_curlist(qi)->qf_id;
8537 if (au_name != NULL)
8538 {
8539 buf_T *curbuf_old = curbuf;
8540
8541 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
8542 TRUE, curbuf);
8543 if (curbuf != curbuf_old)
8544 // Autocommands changed buffer, don't jump now, "qi" may
8545 // be invalid.
8546 res = 0;
8547 }
8548 // Jump to the first error for a new list and if autocmds didn't
8549 // free the list.
8550 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
8551 eap->cmdidx == CMD_lbuffer)
8552 && qflist_valid(wp, save_qfid))
8553 // display the first error
8554 qf_jump_first(qi, save_qfid, eap->forceit);
8555
8556 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00008557}
8558
Bram Moolenaar1e015462005-09-25 22:16:38 +00008559#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008560/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01008561 * Return the autocmd name for the :cexpr Ex commands.
8562 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008563 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01008564cexpr_get_auname(cmdidx_T cmdidx)
8565{
8566 switch (cmdidx)
8567 {
8568 case CMD_cexpr: return (char_u *)"cexpr";
8569 case CMD_cgetexpr: return (char_u *)"cgetexpr";
8570 case CMD_caddexpr: return (char_u *)"caddexpr";
8571 case CMD_lexpr: return (char_u *)"lexpr";
8572 case CMD_lgetexpr: return (char_u *)"lgetexpr";
8573 case CMD_laddexpr: return (char_u *)"laddexpr";
8574 default: return NULL;
8575 }
8576}
8577
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008578 int
8579trigger_cexpr_autocmd(int cmdidx)
8580{
8581 char_u *au_name = cexpr_get_auname(cmdidx);
8582
8583 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8584 curbuf->b_fname, TRUE, curbuf))
8585 {
8586 if (aborting())
8587 return FAIL;
8588 }
8589 return OK;
8590}
8591
8592 int
8593cexpr_core(exarg_T *eap, typval_T *tv)
8594{
8595 qf_info_T *qi;
8596 win_T *wp = NULL;
8597
8598 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8599 if (qi == NULL)
8600 return FAIL;
8601
8602 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
8603 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
8604 {
8605 int res;
8606 int_u save_qfid;
8607 char_u *au_name = cexpr_get_auname(eap->cmdidx);
8608
8609 incr_quickfix_busy();
8610 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
8611 (eap->cmdidx != CMD_caddexpr
8612 && eap->cmdidx != CMD_laddexpr),
8613 (linenr_T)0, (linenr_T)0,
8614 qf_cmdtitle(*eap->cmdlinep), NULL);
8615 if (qf_stack_empty(qi))
8616 {
8617 decr_quickfix_busy();
8618 return FAIL;
8619 }
8620 if (res >= 0)
8621 qf_list_changed(qf_get_curlist(qi));
8622
8623 // Remember the current quickfix list identifier, so that we can
8624 // check for autocommands changing the current quickfix list.
8625 save_qfid = qf_get_curlist(qi)->qf_id;
8626 if (au_name != NULL)
8627 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8628 curbuf->b_fname, TRUE, curbuf);
8629
8630 // Jump to the first error for a new list and if autocmds didn't
8631 // free the list.
8632 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8633 && qflist_valid(wp, save_qfid))
8634 // display the first error
8635 qf_jump_first(qi, save_qfid, eap->forceit);
8636 decr_quickfix_busy();
8637 return OK;
8638 }
8639
Bram Moolenaar677658a2022-01-05 16:09:06 +00008640 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008641 return FAIL;
8642}
8643
Bram Moolenaara16123a2019-03-28 20:31:07 +01008644/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008645 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8646 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008647 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008648 */
8649 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008650ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008651{
8652 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008653
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008654 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008655 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008656
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008657 // Evaluate the expression. When the result is a string or a list we can
8658 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008659 tv = eval_expr(eap->arg, eap);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00008660 if (tv == NULL)
8661 return;
8662
8663 (void)cexpr_core(eap, tv);
8664 free_tv(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008665}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008666#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008667
8668/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008669 * Get the location list for ":lhelpgrep"
8670 */
8671 static qf_info_T *
8672hgr_get_ll(int *new_ll)
8673{
8674 win_T *wp;
8675 qf_info_T *qi;
8676
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008677 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008678 if (bt_help(curwin->w_buffer))
8679 wp = curwin;
8680 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008681 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008682 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008683
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008684 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008685 qi = NULL;
8686 else
8687 qi = wp->w_llist;
8688
8689 if (qi == NULL)
8690 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008691 // Allocate a new location list for help text matches
64-bitman88d41ab2025-04-06 17:20:39 +02008692 if ((qi = qf_alloc_stack(QFLT_LOCATION, 1)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008693 return NULL;
8694 *new_ll = TRUE;
8695 }
8696
8697 return qi;
8698}
8699
8700/*
8701 * Search for a pattern in a help file.
8702 */
8703 static void
8704hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008705 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008706 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008707 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008708 regmatch_T *p_regmatch)
8709{
8710 FILE *fd;
8711 long lnum;
8712
8713 fd = mch_fopen((char *)fname, "r");
8714 if (fd == NULL)
8715 return;
8716
8717 lnum = 1;
8718 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8719 {
8720 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008721
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008722 // Convert a line if 'encoding' is not utf-8 and
8723 // the line contains a non-ASCII character.
Yegappan Lakshmanandf1bbea2022-03-05 14:35:12 +00008724 if (p_vc->vc_type != CONV_NONE && has_non_ascii(IObuff))
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008725 {
8726 line = string_convert(p_vc, IObuff, NULL);
8727 if (line == NULL)
8728 line = IObuff;
8729 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008730
8731 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8732 {
8733 int l = (int)STRLEN(line);
8734
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008735 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008736 while (l > 0 && line[l - 1] <= ' ')
8737 line[--l] = NUL;
8738
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008739 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008740 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008741 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008742 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008743 0,
8744 line,
8745 lnum,
thinca6864efa2021-06-19 20:45:20 +02008746 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008747 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008748 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008749 (int)(p_regmatch->endp[0] - line)
8750 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008751 FALSE, // vis_col
8752 NULL, // search pattern
8753 0, // nr
8754 1, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02008755 NULL, // user_data
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008756 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008757 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008758 {
8759 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008760 if (line != IObuff)
8761 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008762 break;
8763 }
8764 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008765 if (line != IObuff)
8766 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008767 ++lnum;
8768 line_breakcheck();
8769 }
8770 fclose(fd);
8771}
8772
8773/*
8774 * Search for a pattern in all the help files in the doc directory under
8775 * the given directory.
8776 */
8777 static void
8778hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008779 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008780 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008781 regmatch_T *p_regmatch,
8782 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008783#ifdef FEAT_MULTI_LANG
8784 , char_u *lang
8785#endif
8786 )
8787{
8788 int fcount;
8789 char_u **fnames;
8790 int fi;
8791
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008792 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008793 add_pathsep(dirname);
8794 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8795 if (gen_expand_wildcards(1, &dirname, &fcount,
8796 &fnames, EW_FILE|EW_SILENT) == OK
8797 && fcount > 0)
8798 {
8799 for (fi = 0; fi < fcount && !got_int; ++fi)
8800 {
8801#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008802 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008803 if (lang != NULL
8804 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008805 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008806 && !(STRNICMP(lang, "en", 2) == 0
8807 && STRNICMP("txt", fnames[fi]
8808 + STRLEN(fnames[fi]) - 3, 3) == 0))
8809 continue;
8810#endif
8811
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008812 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008813 }
8814 FreeWild(fcount, fnames);
8815 }
8816}
8817
8818/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008819 * Search for a pattern in all the help files in the 'runtimepath'
8820 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008821 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008822 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008823 */
8824 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008825hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008826{
8827 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008828
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008829 vimconv_T vc;
8830
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008831 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8832 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008833 vc.vc_type = CONV_NONE;
8834 if (!enc_utf8)
8835 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008836
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008837 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008838 p = p_rtp;
8839 while (*p != NUL && !got_int)
8840 {
8841 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8842
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008843 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008844#ifdef FEAT_MULTI_LANG
8845 , lang
8846#endif
8847 );
8848 }
8849
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008850 if (vc.vc_type != CONV_NONE)
8851 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008852}
8853
8854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 * ":helpgrep {pattern}"
8856 */
8857 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008858ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
8860 regmatch_T regmatch;
8861 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008862 int save_cpo_allocated;
64-bitman88d41ab2025-04-06 17:20:39 +02008863 qf_info_T *qi = ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008864 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008865 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008866 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008867 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868
Bram Moolenaar73633f82012-01-20 13:39:07 +01008869 switch (eap->cmdidx)
8870 {
8871 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8872 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8873 default: break;
8874 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008875 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8876 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008877 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008878#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008879 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008880 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008881#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008882 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008883
Bram Moolenaar39665952018-08-15 20:59:48 +02008884 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008885 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008886 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008887 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008888 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008889 }
64-bitman88d41ab2025-04-06 17:20:39 +02008890 else if (qi == NULL)
8891 {
8892 emsg(_(e_no_quickfix_stack));
8893 return;
8894 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008895
Bram Moolenaara16123a2019-03-28 20:31:07 +01008896 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8897 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008898 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008899 p_cpo = empty_option;
8900
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008901 incr_quickfix_busy();
8902
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008903#ifdef FEAT_MULTI_LANG
8904 // Check for a specified language
8905 lang = check_help_lang(eap->arg);
8906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8908 regmatch.rm_ic = FALSE;
8909 if (regmatch.regprog != NULL)
8910 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008911 qf_list_T *qfl;
8912
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008913 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008914 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008915 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008917 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008918
Bram Moolenaar473de612013-06-08 18:19:48 +02008919 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008921 qfl->qf_nonevalid = FALSE;
8922 qfl->qf_ptr = qfl->qf_start;
8923 qfl->qf_index = 1;
8924 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008925 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 }
8927
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008928 if (p_cpo == empty_option)
8929 p_cpo = save_cpo;
8930 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008931 {
8932 // Darn, some plugin changed the value. If it's still empty it was
8933 // changed and restored, need to restore in the complicated way.
8934 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008935 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008936 if (save_cpo_allocated)
8937 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008938 }
8939
8940 if (updated)
8941 // This may open a window and source scripts, do this after 'cpo' was
8942 // restored.
8943 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
Bram Moolenaar73633f82012-01-20 13:39:07 +01008945 if (au_name != NULL)
8946 {
8947 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8948 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008949 // When adding a location list to an existing location list stack,
8950 // if the autocmd made the stack invalid, then just return.
8951 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008952 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008953 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008954 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008955 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008956 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008957
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008958 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008959 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008960 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008961 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008962 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008963
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008964 decr_quickfix_busy();
8965
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008966 if (eap->cmdidx == CMD_lhelpgrep)
8967 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008968 // If the help window is not opened or if it already points to the
8969 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008970 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008971 {
8972 if (new_qi)
8973 ll_free_all(&qi);
8974 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008975 else if (curwin->w_llist == NULL && new_qi)
8976 // current window didn't have a location list associated with it
8977 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008978 curwin->w_llist = qi;
8979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980}
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008981
8982# if defined(EXITFREE) || defined(PROTO)
8983 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00008984free_quickfix(void)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008985{
8986 win_T *win;
8987 tabpage_T *tab;
8988
8989 qf_free_all(NULL);
8990 // Free all location lists
8991 FOR_ALL_TAB_WINDOWS(tab, win)
8992 qf_free_all(win);
8993
8994 ga_clear(&qfga);
8995}
8996# endif
8997
Bram Moolenaar63d9e732019-12-05 21:10:38 +01008998#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02008999
9000#if defined(FEAT_EVAL) || defined(PROTO)
9001# ifdef FEAT_QUICKFIX
9002 static void
9003get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
9004{
9005 if (what_arg->v_type == VAR_UNKNOWN)
9006 {
9007 if (rettv_list_alloc(rettv) == OK)
9008 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02009009 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02009010 }
9011 else
9012 {
9013 if (rettv_dict_alloc(rettv) == OK)
9014 if (is_qf || (wp != NULL))
9015 {
9016 if (what_arg->v_type == VAR_DICT)
9017 {
9018 dict_T *d = what_arg->vval.v_dict;
9019
9020 if (d != NULL)
9021 qf_get_properties(wp, d, rettv->vval.v_dict);
9022 }
9023 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00009024 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02009025 }
9026 }
9027}
9028# endif
9029
9030/*
9031 * "getloclist()" function
9032 */
9033 void
9034f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9035{
9036# ifdef FEAT_QUICKFIX
9037 win_T *wp;
9038
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02009039 if (in_vim9script()
9040 && (check_for_number_arg(argvars, 0) == FAIL
9041 || check_for_opt_dict_arg(argvars, 1) == FAIL))
9042 return;
9043
Bram Moolenaare677df82019-09-02 22:31:11 +02009044 wp = find_win_by_nr_or_id(&argvars[0]);
9045 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
9046# endif
9047}
9048
9049/*
9050 * "getqflist()" function
9051 */
9052 void
9053f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9054{
9055# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02009056 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
9057 return;
9058
Bram Moolenaare677df82019-09-02 22:31:11 +02009059 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
9060# endif
9061}
9062
9063/*
9064 * Used by "setqflist()" and "setloclist()" functions
9065 */
9066 static void
9067set_qf_ll_list(
9068 win_T *wp UNUSED,
9069 typval_T *list_arg UNUSED,
9070 typval_T *action_arg UNUSED,
9071 typval_T *what_arg UNUSED,
9072 typval_T *rettv)
9073{
9074# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02009075 char_u *act;
9076 int action = 0;
9077 static int recursive = 0;
9078# endif
9079
9080 rettv->vval.v_number = -1;
9081
9082# ifdef FEAT_QUICKFIX
9083 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00009084 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02009085 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00009086 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02009087 else
9088 {
9089 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02009090 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02009091 int valid_dict = TRUE;
9092
9093 if (action_arg->v_type == VAR_STRING)
9094 {
9095 act = tv_get_string_chk(action_arg);
9096 if (act == NULL)
9097 return; // type error; errmsg already given
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02009098 if ((*act == 'a' || *act == 'r' || *act == 'u' || *act == ' ' || *act == 'f') &&
Bram Moolenaare677df82019-09-02 22:31:11 +02009099 act[1] == NUL)
9100 action = *act;
9101 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009102 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02009103 }
9104 else if (action_arg->v_type == VAR_UNKNOWN)
9105 action = ' ';
9106 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00009107 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02009108
9109 if (action_arg->v_type != VAR_UNKNOWN
9110 && what_arg->v_type != VAR_UNKNOWN)
9111 {
Bram Moolenaar90049492020-07-01 13:04:05 +02009112 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
9113 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02009114 else
9115 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00009116 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02009117 valid_dict = FALSE;
9118 }
9119 }
9120
9121 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02009122 if (l != NULL && action && valid_dict
9123 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02009124 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02009125 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02009126 rettv->vval.v_number = 0;
9127 --recursive;
9128 }
9129# endif
9130}
9131
9132/*
9133 * "setloclist()" function
9134 */
9135 void
9136f_setloclist(typval_T *argvars, typval_T *rettv)
9137{
9138 win_T *win;
9139
9140 rettv->vval.v_number = -1;
9141
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02009142 if (in_vim9script()
9143 && (check_for_number_arg(argvars, 0) == FAIL
9144 || check_for_list_arg(argvars, 1) == FAIL
9145 || check_for_opt_string_arg(argvars, 2) == FAIL
9146 || (argvars[2].v_type != VAR_UNKNOWN
9147 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
9148 return;
9149
Bram Moolenaare677df82019-09-02 22:31:11 +02009150 win = find_win_by_nr_or_id(&argvars[0]);
9151 if (win != NULL)
9152 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
9153}
9154
9155/*
9156 * "setqflist()" function
9157 */
9158 void
9159f_setqflist(typval_T *argvars, typval_T *rettv)
9160{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02009161 if (in_vim9script()
9162 && (check_for_list_arg(argvars, 0) == FAIL
9163 || check_for_opt_string_arg(argvars, 1) == FAIL
9164 || (argvars[1].v_type != VAR_UNKNOWN
9165 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
9166 return;
9167
Bram Moolenaare677df82019-09-02 22:31:11 +02009168 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
9169}
9170#endif