blob: ab595d7bbc2b1459a9c610a83839cb816bb32dac [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
zeertzjq98800972025-04-18 10:57:33 +0200122static qf_info_T *ql_info; // points to ql_info_actual if memory allocation is successful.
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/*
zeertzjqb71f1302025-04-08 20:29:40 +02002105 * Free a qf_info_T struct completely
64-bitman88d41ab2025-04-06 17:20:39 +02002106 */
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 {
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +02002271 if (fnamecmp(fullname, buf->b_ffname) != 0)
2272 {
2273 p = shorten_fname1(fullname);
2274 if (p != NULL)
Austin Chang29822992024-10-03 10:50:05 +02002275 qfp->qf_fname = vim_strsave(p);
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +02002276 }
Austin Chang29822992024-10-03 10:50:05 +02002277 }
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{
zeertzjqb71f1302025-04-08 20:29:40 +02002368 // check if given window is a location list window;
64-bitman88d41ab2025-04-06 17:20:39 +02002369 // if so then sync its 'lhistory' to the parent window or vice versa
zeertzjqb71f1302025-04-08 20:29:40 +02002370 if (IS_LL_WINDOW(wp))
64-bitman88d41ab2025-04-06 17:20:39 +02002371 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.
64-bitman88d41ab2025-04-06 17:20:39 +02002502 */
2503 static qf_list_T *
2504qf_alloc_list_stack(int n)
2505{
2506 return ALLOC_CLEAR_MULT(qf_list_T, n);
2507}
2508
2509/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002510 * Return the location list stack for window 'wp'.
2511 * If not present, allocate a location list stack
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002512 */
2513 static qf_info_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01002514ll_get_or_alloc_list(win_T *wp)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002515{
2516 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002517 // For a location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002518 return wp->w_llist_ref;
2519
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002520 // For a non-location list window, w_llist_ref should not point to a
2521 // location list.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002522 ll_free_all(&wp->w_llist_ref);
2523
2524 if (wp->w_llist == NULL)
64-bitman88d41ab2025-04-06 17:20:39 +02002525 // new location list
2526 wp->w_llist = qf_alloc_stack(QFLT_LOCATION, wp->w_p_lhi);
2527
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002528 return wp->w_llist;
2529}
2530
2531/*
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002532 * Get the quickfix/location list stack to use for the specified Ex command.
2533 * For a location list command, returns the stack for the current window. If
2534 * the location list is not found, then returns NULL and prints an error
2535 * message if 'print_emsg' is TRUE.
2536 */
2537 static qf_info_T *
2538qf_cmd_get_stack(exarg_T *eap, int print_emsg)
2539{
64-bitman88d41ab2025-04-06 17:20:39 +02002540 qf_info_T *qi = ql_info;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002541
2542 if (is_loclist_cmd(eap->cmdidx))
2543 {
2544 qi = GET_LOC_LIST(curwin);
2545 if (qi == NULL)
2546 {
2547 if (print_emsg)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002548 emsg(_(e_no_location_list));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002549 return NULL;
2550 }
2551 }
64-bitman88d41ab2025-04-06 17:20:39 +02002552 if (qi == NULL && print_emsg)
2553 emsg(_(e_no_quickfix_stack));
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002554
2555 return qi;
2556}
2557
2558/*
2559 * Get the quickfix/location list stack to use for the specified Ex command.
2560 * For a location list command, returns the stack for the current window.
2561 * If the location list is not present, then allocates a new one.
2562 * Returns NULL if the allocation fails. For a location list command, sets
2563 * 'pwinp' to curwin.
2564 */
2565 static qf_info_T *
2566qf_cmd_get_or_alloc_stack(exarg_T *eap, win_T **pwinp)
2567{
64-bitman88d41ab2025-04-06 17:20:39 +02002568 qf_info_T *qi = ql_info;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02002569
2570 if (is_loclist_cmd(eap->cmdidx))
2571 {
2572 qi = ll_get_or_alloc_list(curwin);
2573 if (qi == NULL)
2574 return NULL;
2575 *pwinp = curwin;
2576 }
2577
2578 return qi;
2579}
2580
2581/*
Bram Moolenaar09037502018-09-25 22:08:14 +02002582 * Copy location list entries from 'from_qfl' to 'to_qfl'.
2583 */
2584 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002585copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002586{
2587 int i;
2588 qfline_T *from_qfp;
2589 qfline_T *prevp;
2590
2591 // copy all the location entries in this list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002592 FOR_ALL_QFL_ITEMS(from_qfl, from_qfp, i)
Bram Moolenaar09037502018-09-25 22:08:14 +02002593 {
Bram Moolenaar0398e002019-03-21 21:12:49 +01002594 if (qf_add_entry(to_qfl,
Bram Moolenaar09037502018-09-25 22:08:14 +02002595 NULL,
2596 NULL,
2597 from_qfp->qf_module,
2598 0,
2599 from_qfp->qf_text,
2600 from_qfp->qf_lnum,
thinca6864efa2021-06-19 20:45:20 +02002601 from_qfp->qf_end_lnum,
Bram Moolenaar09037502018-09-25 22:08:14 +02002602 from_qfp->qf_col,
thinca6864efa2021-06-19 20:45:20 +02002603 from_qfp->qf_end_col,
Bram Moolenaar09037502018-09-25 22:08:14 +02002604 from_qfp->qf_viscol,
2605 from_qfp->qf_pattern,
2606 from_qfp->qf_nr,
2607 0,
Tom Praschanca6ac992023-08-11 23:26:12 +02002608 &from_qfp->qf_user_data,
Bram Moolenaar95946f12019-03-31 15:31:59 +02002609 from_qfp->qf_valid) == QF_FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002610 return FAIL;
2611
2612 // qf_add_entry() will not set the qf_num field, as the
2613 // directory and file names are not supplied. So the qf_fnum
2614 // field is copied here.
2615 prevp = to_qfl->qf_last;
2616 prevp->qf_fnum = from_qfp->qf_fnum; // file number
2617 prevp->qf_type = from_qfp->qf_type; // error type
2618 if (from_qfl->qf_ptr == from_qfp)
2619 to_qfl->qf_ptr = prevp; // current location
2620 }
2621
2622 return OK;
2623}
2624
2625/*
2626 * Copy the specified location list 'from_qfl' to 'to_qfl'.
2627 */
2628 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002629copy_loclist(qf_list_T *from_qfl, qf_list_T *to_qfl)
Bram Moolenaar09037502018-09-25 22:08:14 +02002630{
2631 // Some of the fields are populated by qf_add_entry()
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002632 to_qfl->qfl_type = from_qfl->qfl_type;
Bram Moolenaar09037502018-09-25 22:08:14 +02002633 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
Tom Praschanca6ac992023-08-11 23:26:12 +02002634 to_qfl->qf_has_user_data = from_qfl->qf_has_user_data;
Bram Moolenaar09037502018-09-25 22:08:14 +02002635 to_qfl->qf_count = 0;
2636 to_qfl->qf_index = 0;
2637 to_qfl->qf_start = NULL;
2638 to_qfl->qf_last = NULL;
2639 to_qfl->qf_ptr = NULL;
2640 if (from_qfl->qf_title != NULL)
2641 to_qfl->qf_title = vim_strsave(from_qfl->qf_title);
2642 else
2643 to_qfl->qf_title = NULL;
2644 if (from_qfl->qf_ctx != NULL)
2645 {
2646 to_qfl->qf_ctx = alloc_tv();
2647 if (to_qfl->qf_ctx != NULL)
2648 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx);
2649 }
2650 else
2651 to_qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002652 if (from_qfl->qf_qftf_cb.cb_name != NULL)
2653 copy_callback(&to_qfl->qf_qftf_cb, &from_qfl->qf_qftf_cb);
Bram Moolenaar858ba062020-05-31 23:11:59 +02002654 else
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01002655 to_qfl->qf_qftf_cb.cb_name = NULL;
Bram Moolenaar09037502018-09-25 22:08:14 +02002656
2657 if (from_qfl->qf_count)
Bram Moolenaar0398e002019-03-21 21:12:49 +01002658 if (copy_loclist_entries(from_qfl, to_qfl) == FAIL)
Bram Moolenaar09037502018-09-25 22:08:14 +02002659 return FAIL;
2660
2661 to_qfl->qf_index = from_qfl->qf_index; // current index in the list
2662
2663 // Assign a new ID for the location list
2664 to_qfl->qf_id = ++last_qf_id;
2665 to_qfl->qf_changedtick = 0L;
2666
2667 // When no valid entries are present in the list, qf_ptr points to
2668 // the first item in the list
2669 if (to_qfl->qf_nonevalid)
2670 {
2671 to_qfl->qf_ptr = to_qfl->qf_start;
2672 to_qfl->qf_index = 1;
2673 }
2674
2675 return OK;
2676}
2677
2678/*
2679 * Copy the location list stack 'from' window to 'to' window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002680 */
2681 void
Bram Moolenaar09037502018-09-25 22:08:14 +02002682copy_loclist_stack(win_T *from, win_T *to)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002683{
2684 qf_info_T *qi;
2685 int idx;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002686
Bram Moolenaar09037502018-09-25 22:08:14 +02002687 // When copying from a location list window, copy the referenced
2688 // location list. For other windows, copy the location list for
2689 // that window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002690 if (IS_LL_WINDOW(from))
2691 qi = from->w_llist_ref;
2692 else
2693 qi = from->w_llist;
2694
Bram Moolenaar09037502018-09-25 22:08:14 +02002695 if (qi == NULL) // no location list to copy
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002696 return;
2697
64-bitman88d41ab2025-04-06 17:20:39 +02002698 // allocate a new location list, set size of stack to 'from' window value
2699 if ((to->w_llist = qf_alloc_stack(QFLT_LOCATION, from->w_p_lhi)) == NULL)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002700 return;
64-bitman88d41ab2025-04-06 17:20:39 +02002701 else
2702 // set 'to' lhi to reflect new value
2703 to->w_p_lhi = to->w_llist->qf_maxcount;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002704
2705 to->w_llist->qf_listcount = qi->qf_listcount;
2706
Bram Moolenaar09037502018-09-25 22:08:14 +02002707 // Copy the location lists one at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02002708 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002709 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002710 to->w_llist->qf_curlist = idx;
2711
Bram Moolenaar0398e002019-03-21 21:12:49 +01002712 if (copy_loclist(qf_get_list(qi, idx),
2713 qf_get_list(to->w_llist, idx)) == FAIL)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002714 {
Bram Moolenaar09037502018-09-25 22:08:14 +02002715 qf_free_all(to);
2716 return;
Bram Moolenaar730d2c02013-06-30 13:33:58 +02002717 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002718 }
2719
Bram Moolenaar09037502018-09-25 22:08:14 +02002720 to->w_llist->qf_curlist = qi->qf_curlist; // current list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002721}
2722
2723/*
Bram Moolenaar7618e002016-11-13 15:09:26 +01002724 * Get buffer number for file "directory/fname".
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002725 * Also sets the b_has_qf_entry flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 */
2727 static int
Bram Moolenaar0398e002019-03-21 21:12:49 +01002728qf_get_fnum(qf_list_T *qfl, char_u *directory, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729{
Bram Moolenaar82404332016-07-10 17:00:38 +02002730 char_u *ptr = NULL;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002731 buf_T *buf;
Bram Moolenaar82404332016-07-10 17:00:38 +02002732 char_u *bufname;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002733
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002734 if (fname == NULL || *fname == NUL) // no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
Bram Moolenaare60acc12011-05-10 16:41:25 +02002737#ifdef VMS
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002738 vms_remove_version(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002739#endif
2740#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002741 if (directory != NULL)
2742 slash_adjust(directory);
2743 slash_adjust(fname);
Bram Moolenaare60acc12011-05-10 16:41:25 +02002744#endif
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002745 if (directory != NULL && !vim_isAbsName(fname)
2746 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
2747 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002748 // Here we check if the file really exists.
2749 // This should normally be true, but if make works without
2750 // "leaving directory"-messages we might have missed a
2751 // directory change.
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002752 if (mch_getperm(ptr) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 vim_free(ptr);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02002755 directory = qf_guess_filepath(qfl, fname);
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002756 if (directory)
2757 ptr = concat_fnames(directory, fname, TRUE);
2758 else
2759 ptr = vim_strsave(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002761 // Use concatenated directory name and file name
Bram Moolenaar82404332016-07-10 17:00:38 +02002762 bufname = ptr;
2763 }
2764 else
2765 bufname = fname;
2766
2767 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002768 && bufref_valid(&qf_last_bufref))
Bram Moolenaar82404332016-07-10 17:00:38 +02002769 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002770 buf = qf_last_bufref.br_buf;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002771 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002773 else
Bram Moolenaar82404332016-07-10 17:00:38 +02002774 {
2775 vim_free(qf_last_bufname);
2776 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT);
2777 if (bufname == ptr)
2778 qf_last_bufname = bufname;
2779 else
2780 qf_last_bufname = vim_strsave(bufname);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002781 set_bufref(&qf_last_bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002782 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002783 if (buf == NULL)
2784 return 0;
Bram Moolenaar82404332016-07-10 17:00:38 +02002785
Bram Moolenaarc1542742016-07-20 21:44:37 +02002786 buf->b_has_qf_entry =
Bram Moolenaar2d67d302018-11-16 18:46:02 +01002787 IS_QF_LIST(qfl) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02002788 return buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789}
2790
2791/*
Bram Moolenaar38df43b2016-06-20 21:41:12 +02002792 * Push dirbuf onto the directory stack and return pointer to actual dir or
2793 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 */
2795 static char_u *
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002796qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 struct dir_stack_T *ds_new;
2799 struct dir_stack_T *ds_ptr;
2800
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002801 // allocate new stack element and hook it in
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00002802 ds_new = ALLOC_ONE_ID(struct dir_stack_T, aid_qf_dirstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 if (ds_new == NULL)
2804 return NULL;
2805
2806 ds_new->next = *stackptr;
2807 *stackptr = ds_new;
2808
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002809 // store directory on the stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 if (vim_isAbsName(dirbuf)
2811 || (*stackptr)->next == NULL
=?UTF-8?q?Dundar=20G=C3=B6c?=dd410372022-05-15 13:59:11 +01002812 || is_file_stack)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 (*stackptr)->dirname = vim_strsave(dirbuf);
2814 else
2815 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002816 // Okay we don't have an absolute path.
2817 // dirbuf must be a subdir of one of the directories on the stack.
2818 // Let's search...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 ds_new = (*stackptr)->next;
2820 (*stackptr)->dirname = NULL;
2821 while (ds_new)
2822 {
2823 vim_free((*stackptr)->dirname);
2824 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
2825 TRUE);
2826 if (mch_isdir((*stackptr)->dirname) == TRUE)
2827 break;
2828
2829 ds_new = ds_new->next;
2830 }
2831
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002832 // clean up all dirs we already left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 while ((*stackptr)->next != ds_new)
2834 {
2835 ds_ptr = (*stackptr)->next;
2836 (*stackptr)->next = (*stackptr)->next->next;
2837 vim_free(ds_ptr->dirname);
2838 vim_free(ds_ptr);
2839 }
2840
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002841 // Nothing found -> it must be on top level
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 if (ds_new == NULL)
2843 {
2844 vim_free((*stackptr)->dirname);
2845 (*stackptr)->dirname = vim_strsave(dirbuf);
2846 }
2847 }
2848
2849 if ((*stackptr)->dirname != NULL)
2850 return (*stackptr)->dirname;
2851 else
2852 {
2853 ds_ptr = *stackptr;
2854 *stackptr = (*stackptr)->next;
2855 vim_free(ds_ptr);
2856 return NULL;
2857 }
2858}
2859
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860/*
2861 * pop dirbuf from the directory stack and return previous directory or NULL if
2862 * stack is empty
2863 */
2864 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002865qf_pop_dir(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
2867 struct dir_stack_T *ds_ptr;
2868
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002869 // TODO: Should we check if dirbuf is the directory on top of the stack?
2870 // What to do if it isn't?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002872 // pop top element and free it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 if (*stackptr != NULL)
2874 {
2875 ds_ptr = *stackptr;
2876 *stackptr = (*stackptr)->next;
2877 vim_free(ds_ptr->dirname);
2878 vim_free(ds_ptr);
2879 }
2880
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002881 // return NEW top element as current dir or NULL if stack is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 return *stackptr ? (*stackptr)->dirname : NULL;
2883}
2884
2885/*
2886 * clean up directory stack
2887 */
2888 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002889qf_clean_dir_stack(struct dir_stack_T **stackptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
2891 struct dir_stack_T *ds_ptr;
2892
2893 while ((ds_ptr = *stackptr) != NULL)
2894 {
2895 *stackptr = (*stackptr)->next;
2896 vim_free(ds_ptr->dirname);
2897 vim_free(ds_ptr);
2898 }
2899}
2900
2901/*
2902 * Check in which directory of the directory stack the given file can be
2903 * found.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002904 * Returns a pointer to the directory name or NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 * Cleans up intermediate directory entries.
2906 *
2907 * TODO: How to solve the following problem?
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002908 * If we have this directory tree:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 * ./
2910 * ./aa
2911 * ./aa/bb
2912 * ./bb
2913 * ./bb/x.c
2914 * and make says:
2915 * making all in aa
2916 * making all in bb
2917 * x.c:9: Error
2918 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
2919 * qf_guess_filepath will return NULL.
2920 */
2921 static char_u *
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002922qf_guess_filepath(qf_list_T *qfl, char_u *filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
2924 struct dir_stack_T *ds_ptr;
2925 struct dir_stack_T *ds_tmp;
2926 char_u *fullname;
2927
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002928 // no dirs on the stack - there's nothing we can do
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002929 if (qfl->qf_dir_stack == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 return NULL;
2931
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002932 ds_ptr = qfl->qf_dir_stack->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 fullname = NULL;
2934 while (ds_ptr)
2935 {
2936 vim_free(fullname);
2937 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
2938
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002939 // If concat_fnames failed, just go on. The worst thing that can happen
2940 // is that we delete the entire stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
2942 break;
2943
2944 ds_ptr = ds_ptr->next;
2945 }
2946
2947 vim_free(fullname);
2948
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002949 // clean up all dirs we already left
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002950 while (qfl->qf_dir_stack->next != ds_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02002952 ds_tmp = qfl->qf_dir_stack->next;
2953 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 vim_free(ds_tmp->dirname);
2955 vim_free(ds_tmp);
2956 }
2957
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002958 return ds_ptr == NULL ? NULL : ds_ptr->dirname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959}
2960
2961/*
Bram Moolenaar3c097222017-12-21 20:54:49 +01002962 * Returns TRUE if a quickfix/location list with the given identifier exists.
2963 */
2964 static int
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02002965qflist_valid(win_T *wp, int_u qf_id)
Bram Moolenaar3c097222017-12-21 20:54:49 +01002966{
64-bitman88d41ab2025-04-06 17:20:39 +02002967 qf_info_T *qi = ql_info;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002968 int i;
2969
2970 if (wp != NULL)
2971 {
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002972 if (!win_valid(wp))
2973 return FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002974 qi = GET_LOC_LIST(wp); // Location list
Bram Moolenaar3c097222017-12-21 20:54:49 +01002975 }
64-bitman88d41ab2025-04-06 17:20:39 +02002976 if (qi == NULL)
2977 return FALSE;
Bram Moolenaar3c097222017-12-21 20:54:49 +01002978
2979 for (i = 0; i < qi->qf_listcount; ++i)
2980 if (qi->qf_lists[i].qf_id == qf_id)
2981 return TRUE;
2982
2983 return FALSE;
2984}
2985
2986/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02002987 * When loading a file from the quickfix, the autocommands may modify it.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002988 * This may invalidate the current quickfix entry. This function checks
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02002989 * whether an entry is still present in the quickfix list.
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002990 * Similar to location list.
2991 */
2992 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02002993is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002994{
Bram Moolenaarffec3c52016-03-23 20:55:42 +01002995 qfline_T *qfp;
2996 int i;
2997
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02002998 // Search for the entry in the current list
Bram Moolenaara16123a2019-03-28 20:31:07 +01002999 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
3000 if (qfp == qf_ptr)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003001 break;
3002
Bram Moolenaar95946f12019-03-31 15:31:59 +02003003 if (i > qfl->qf_count) // Entry is not found
Bram Moolenaarffec3c52016-03-23 20:55:42 +01003004 return FALSE;
3005
3006 return TRUE;
3007}
3008
3009/*
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003010 * Get the next valid entry in the current quickfix/location list. The search
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003011 * starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003012 */
3013 static qfline_T *
3014get_next_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003015 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003016 qfline_T *qf_ptr,
3017 int *qf_index,
3018 int dir)
3019{
3020 int idx;
3021 int old_qf_fnum;
3022
3023 idx = *qf_index;
3024 old_qf_fnum = qf_ptr->qf_fnum;
3025
3026 do
3027 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003028 if (idx == qfl->qf_count || qf_ptr->qf_next == NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003029 return NULL;
3030 ++idx;
3031 qf_ptr = qf_ptr->qf_next;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003032 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003033 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
3034
3035 *qf_index = idx;
3036 return qf_ptr;
3037}
3038
3039/*
3040 * Get the previous valid entry in the current quickfix/location list. The
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003041 * search starts from the current entry. Returns NULL on failure.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003042 */
3043 static qfline_T *
3044get_prev_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003045 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003046 qfline_T *qf_ptr,
3047 int *qf_index,
3048 int dir)
3049{
3050 int idx;
3051 int old_qf_fnum;
3052
3053 idx = *qf_index;
3054 old_qf_fnum = qf_ptr->qf_fnum;
3055
3056 do
3057 {
3058 if (idx == 1 || qf_ptr->qf_prev == NULL)
3059 return NULL;
3060 --idx;
3061 qf_ptr = qf_ptr->qf_prev;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003062 } while ((!qfl->qf_nonevalid && !qf_ptr->qf_valid)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003063 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
3064
3065 *qf_index = idx;
3066 return qf_ptr;
3067}
3068
3069/*
3070 * Get the n'th (errornr) previous/next valid entry from the current entry in
3071 * the quickfix list.
3072 * dir == FORWARD or FORWARD_FILE: next valid entry
3073 * dir == BACKWARD or BACKWARD_FILE: previous valid entry
3074 */
3075 static qfline_T *
3076get_nth_valid_entry(
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003077 qf_list_T *qfl,
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003078 int errornr,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003079 int dir,
3080 int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003081{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003082 qfline_T *qf_ptr = qfl->qf_ptr;
3083 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003084 qfline_T *prev_qf_ptr;
3085 int prev_index;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003086 char *err = e_no_more_items;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003087
3088 while (errornr--)
3089 {
3090 prev_qf_ptr = qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003091 prev_index = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003092
3093 if (dir == FORWARD || dir == FORWARD_FILE)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003094 qf_ptr = get_next_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003095 else
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003096 qf_ptr = get_prev_valid_entry(qfl, qf_ptr, &qf_idx, dir);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003097 if (qf_ptr == NULL)
3098 {
3099 qf_ptr = prev_qf_ptr;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003100 qf_idx = prev_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003101 if (err != NULL)
3102 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003103 emsg(_(err));
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003104 return NULL;
3105 }
3106 break;
3107 }
3108
3109 err = NULL;
3110 }
3111
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003112 *new_qfidx = qf_idx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003113 return qf_ptr;
3114}
3115
3116/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003117 * Get n'th (errornr) quickfix entry from the current entry in the quickfix
3118 * list 'qfl'. Returns a pointer to the new entry and the index in 'new_qfidx'
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003119 */
3120 static qfline_T *
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003121get_nth_entry(qf_list_T *qfl, int errornr, int *new_qfidx)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003122{
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003123 qfline_T *qf_ptr = qfl->qf_ptr;
3124 int qf_idx = qfl->qf_index;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003125
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003126 // New error number is less than the current error number
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003127 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL)
3128 {
3129 --qf_idx;
3130 qf_ptr = qf_ptr->qf_prev;
3131 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003132 // New error number is greater than the current error number
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003133 while (errornr > qf_idx && qf_idx < qfl->qf_count &&
3134 qf_ptr->qf_next != NULL)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003135 {
3136 ++qf_idx;
3137 qf_ptr = qf_ptr->qf_next;
3138 }
3139
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003140 *new_qfidx = qf_idx;
3141 return qf_ptr;
3142}
3143
3144/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003145 * Get a entry specified by 'errornr' and 'dir' from the current
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003146 * quickfix/location list. 'errornr' specifies the index of the entry and 'dir'
3147 * specifies the direction (FORWARD/BACKWARD/FORWARD_FILE/BACKWARD_FILE).
3148 * Returns a pointer to the entry and the index of the new entry is stored in
3149 * 'new_qfidx'.
3150 */
3151 static qfline_T *
3152qf_get_entry(
3153 qf_list_T *qfl,
3154 int errornr,
3155 int dir,
3156 int *new_qfidx)
3157{
3158 qfline_T *qf_ptr = qfl->qf_ptr;
3159 int qfidx = qfl->qf_index;
3160
3161 if (dir != 0) // next/prev valid entry
3162 qf_ptr = get_nth_valid_entry(qfl, errornr, dir, &qfidx);
3163 else if (errornr != 0) // go to specified number
3164 qf_ptr = get_nth_entry(qfl, errornr, &qfidx);
3165
3166 *new_qfidx = qfidx;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003167 return qf_ptr;
3168}
3169
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003170/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003171 * Find a window displaying a Vim help file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003172 */
3173 static win_T *
3174qf_find_help_win(void)
3175{
3176 win_T *wp;
3177
3178 FOR_ALL_WINDOWS(wp)
3179 if (bt_help(wp->w_buffer))
3180 return wp;
3181
3182 return NULL;
3183}
3184
3185/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003186 * Set the location list for the specified window to 'qi'.
3187 */
3188 static void
3189win_set_loclist(win_T *wp, qf_info_T *qi)
3190{
3191 wp->w_llist = qi;
3192 qi->qf_refcount++;
3193}
3194
3195/*
Bram Moolenaarb2443732018-11-11 22:50:27 +01003196 * Find a help window or open one. If 'newwin' is TRUE, then open a new help
3197 * window.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003198 */
3199 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003200jump_to_help_window(qf_info_T *qi, int newwin, int *opened_window)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003201{
3202 win_T *wp;
3203 int flags;
3204
Bram Moolenaare1004402020-10-24 20:49:43 +02003205 if (cmdmod.cmod_tab != 0 || newwin)
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003206 wp = NULL;
3207 else
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003208 wp = qf_find_help_win();
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003209 if (wp != NULL && wp->w_buffer->b_nwindows > 0)
3210 win_enter(wp, TRUE);
3211 else
3212 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003213 // Split off help window; put it at far top if no position
3214 // specified, the current window is vertically split and narrow.
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003215 flags = WSP_HELP;
Bram Moolenaare1004402020-10-24 20:49:43 +02003216 if (cmdmod.cmod_split == 0 && curwin->w_width != Columns
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003217 && curwin->w_width < 80)
3218 flags |= WSP_TOP;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003219 // If the user asks to open a new window, then copy the location list.
3220 // Otherwise, don't copy the location list.
3221 if (IS_LL_STACK(qi) && !newwin)
3222 flags |= WSP_NEWLOC;
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003223
3224 if (win_split(0, flags) == FAIL)
3225 return FAIL;
3226
3227 *opened_window = TRUE;
3228
3229 if (curwin->w_height < p_hh)
3230 win_setheight((int)p_hh);
3231
Bram Moolenaarb2443732018-11-11 22:50:27 +01003232 // When using location list, the new window should use the supplied
3233 // location list. If the user asks to open a new window, then the new
3234 // window will get a copy of the location list.
3235 if (IS_LL_STACK(qi) && !newwin)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003236 win_set_loclist(curwin, qi);
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003237 }
3238
3239 if (!p_im)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003240 restart_edit = 0; // don't want insert mode in help file
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003241
3242 return OK;
3243}
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003244
3245/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003246 * Find a non-quickfix window using the given location list stack in the
3247 * current tabpage.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003248 * Returns NULL if a matching window is not found.
3249 */
3250 static win_T *
3251qf_find_win_with_loclist(qf_info_T *ll)
3252{
3253 win_T *wp;
3254
3255 FOR_ALL_WINDOWS(wp)
3256 if (wp->w_llist == ll && !bt_quickfix(wp->w_buffer))
3257 return wp;
3258
3259 return NULL;
3260}
3261
3262/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003263 * Find a window containing a normal buffer in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003264 */
3265 static win_T *
3266qf_find_win_with_normal_buf(void)
3267{
3268 win_T *wp;
3269
3270 FOR_ALL_WINDOWS(wp)
Bram Moolenaar91335e52018-08-01 17:53:12 +02003271 if (bt_normal(wp->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003272 return wp;
3273
3274 return NULL;
3275}
3276
3277/*
3278 * Go to a window in any tabpage containing the specified file. Returns TRUE
3279 * if successfully jumped to the window. Otherwise returns FALSE.
3280 */
3281 static int
3282qf_goto_tabwin_with_file(int fnum)
3283{
3284 tabpage_T *tp;
3285 win_T *wp;
3286
3287 FOR_ALL_TAB_WINDOWS(tp, wp)
3288 if (wp->w_buffer->b_fnum == fnum)
3289 {
3290 goto_tabpage_win(tp, wp);
3291 return TRUE;
3292 }
3293
3294 return FALSE;
3295}
3296
3297/*
3298 * Create a new window to show a file above the quickfix window. Called when
3299 * only the quickfix window is present.
3300 */
3301 static int
3302qf_open_new_file_win(qf_info_T *ll_ref)
3303{
3304 int flags;
3305
3306 flags = WSP_ABOVE;
3307 if (ll_ref != NULL)
3308 flags |= WSP_NEWLOC;
3309 if (win_split(0, flags) == FAIL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003310 return FAIL; // not enough room for window
3311 p_swb = empty_option; // don't split again
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003312 swb_flags = 0;
3313 RESET_BINDING(curwin);
3314 if (ll_ref != NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003315 // The new window should use the location list from the
3316 // location list window
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003317 win_set_loclist(curwin, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003318 return OK;
3319}
3320
3321/*
3322 * Go to a window that shows the right buffer. If the window is not found, go
3323 * to the window just above the location list window. This is used for opening
3324 * a file from a location window and not from a quickfix window. If some usable
3325 * window is previously found, then it is supplied in 'use_win'.
3326 */
3327 static void
3328qf_goto_win_with_ll_file(win_T *use_win, int qf_fnum, qf_info_T *ll_ref)
3329{
3330 win_T *win = use_win;
3331
3332 if (win == NULL)
3333 {
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00003334 // Find the window showing the selected file in the current tab page.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003335 FOR_ALL_WINDOWS(win)
3336 if (win->w_buffer->b_fnum == qf_fnum)
3337 break;
3338 if (win == NULL)
3339 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003340 // Find a previous usable window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003341 win = curwin;
3342 do
3343 {
Bram Moolenaar91335e52018-08-01 17:53:12 +02003344 if (bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003345 break;
3346 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003347 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003348 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003349 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003350 } while (win != curwin);
3351 }
3352 }
3353 win_goto(win);
3354
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003355 // If the location list for the window is not set, then set it
3356 // to the location list from the location window
Bram Moolenaar0398e002019-03-21 21:12:49 +01003357 if (win->w_llist == NULL && ll_ref != NULL)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003358 win_set_loclist(win, ll_ref);
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003359}
3360
3361/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003362 * Go to a window that contains the specified buffer 'qf_fnum'. If a window is
3363 * not found, then go to the window just above the quickfix window. This is
3364 * used for opening a file from a quickfix window and not from a location
3365 * window.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003366 */
3367 static void
3368qf_goto_win_with_qfl_file(int qf_fnum)
3369{
3370 win_T *win;
3371 win_T *altwin;
3372
3373 win = curwin;
3374 altwin = NULL;
3375 for (;;)
3376 {
3377 if (win->w_buffer->b_fnum == qf_fnum)
3378 break;
3379 if (win->w_prev == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003380 win = lastwin; // wrap around the top
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003381 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003382 win = win->w_prev; // go to previous window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003383
3384 if (IS_QF_WINDOW(win))
3385 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003386 // Didn't find it, go to the window before the quickfix
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003387 // window, unless 'switchbuf' contains 'uselast': in this case we
3388 // try to jump to the previously used window first.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003389 if ((swb_flags & SWB_USELAST) && win_valid(prevwin) &&
3390 !prevwin->w_p_wfb)
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01003391 win = prevwin;
3392 else if (altwin != NULL)
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003393 win = altwin;
3394 else if (curwin->w_prev != NULL)
3395 win = curwin->w_prev;
3396 else
3397 win = curwin->w_next;
3398 break;
3399 }
3400
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003401 // Remember a usable window.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003402 if (altwin == NULL && !win->w_p_pvw && !win->w_p_wfb &&
3403 bt_normal(win->w_buffer))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003404 altwin = win;
3405 }
3406
3407 win_goto(win);
3408}
3409
3410/*
3411 * Find a suitable window for opening a file (qf_fnum) from the
3412 * quickfix/location list and jump to it. If the file is already opened in a
Bram Moolenaarb2443732018-11-11 22:50:27 +01003413 * window, jump to it. Otherwise open a new window to display the file. If
3414 * 'newwin' is TRUE, then always open a new window. This is called from either
3415 * a quickfix or a location list window.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003416 */
3417 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003418qf_jump_to_usable_window(int qf_fnum, int newwin, int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003419{
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003420 win_T *usable_wp = NULL;
3421 int usable_win = FALSE;
Bram Moolenaarb2443732018-11-11 22:50:27 +01003422 qf_info_T *ll_ref = NULL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003423
Bram Moolenaarb2443732018-11-11 22:50:27 +01003424 // If opening a new window, then don't use the location list referred by
3425 // the current window. Otherwise two windows will refer to the same
3426 // location list.
3427 if (!newwin)
3428 ll_ref = curwin->w_llist_ref;
3429
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003430 if (ll_ref != NULL)
3431 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003432 // Find a non-quickfix window with this location list
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003433 usable_wp = qf_find_win_with_loclist(ll_ref);
3434 if (usable_wp != NULL)
3435 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003436 }
3437
3438 if (!usable_win)
3439 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003440 // Locate a window showing a normal buffer
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003441 win_T *win = qf_find_win_with_normal_buf();
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003442 if (win != NULL)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003443 usable_win = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003444 }
3445
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003446 // If no usable window is found and 'switchbuf' contains "usetab"
3447 // then search in other tabs.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003448 if (!usable_win && (swb_flags & SWB_USETAB))
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003449 usable_win = qf_goto_tabwin_with_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003450
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003451 // If there is only one window and it is the quickfix window, create a
3452 // new one above the quickfix window.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003453 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win || newwin)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003454 {
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003455 if (qf_open_new_file_win(ll_ref) != OK)
3456 return FAIL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003457 *opened_window = TRUE; // close it when fail
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003458 }
3459 else
3460 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003461 if (curwin->w_llist_ref != NULL) // In a location window
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003462 qf_goto_win_with_ll_file(usable_wp, qf_fnum, ll_ref);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003463 else // In a quickfix window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003464 qf_goto_win_with_qfl_file(qf_fnum);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003465 }
3466
3467 return OK;
3468}
3469
3470/*
3471 * Edit the selected file or help file.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003472 * Returns OK if successfully edited the file, FAIL on failing to open the
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003473 * buffer and QF_ABORT if the quickfix/location list was freed by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003474 * when opening the buffer.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003475 */
3476 static int
3477qf_jump_edit_buffer(
3478 qf_info_T *qi,
3479 qfline_T *qf_ptr,
3480 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003481 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003482 int *opened_window)
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003483{
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003484 qf_list_T *qfl = qf_get_curlist(qi);
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003485 int old_changedtick = qfl->qf_changedtick;
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003486 qfltype_T qfl_type = qfl->qfl_type;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003487 int retval = OK;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003488 int old_qf_curlist = qi->qf_curlist;
3489 int save_qfid = qfl->qf_id;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003490
3491 if (qf_ptr->qf_type == 1)
3492 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003493 // Open help file (do_ecmd() will set b_help flag, readfile() will
3494 // set b_p_ro flag).
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003495 if (!can_abandon(curbuf, forceit))
3496 {
3497 no_write_message();
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003498 return FAIL;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003499 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003500
3501 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
3502 ECMD_HIDE + ECMD_SET_HELP,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003503 prev_winid == curwin->w_id ? curwin : NULL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003504 }
3505 else
Colin Kennedy21570352024-03-03 16:16:47 +01003506 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003507 int fnum = qf_ptr->qf_fnum;
3508
3509 if (!forceit && curwin->w_p_wfb && curbuf->b_fnum != fnum)
Colin Kennedy21570352024-03-03 16:16:47 +01003510 {
3511 if (qi->qfl_type == QFLT_LOCATION)
3512 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003513 // Location lists cannot split or reassign their window
3514 // so 'winfixbuf' windows must fail
3515 emsg(_(e_winfixbuf_cannot_go_to_buffer));
3516 return FAIL;
Colin Kennedy21570352024-03-03 16:16:47 +01003517 }
3518
Sean Dewar4bb505e2024-03-05 20:39:07 +01003519 if (win_valid(prevwin) && !prevwin->w_p_wfb &&
3520 !bt_quickfix(prevwin->w_buffer))
Colin Kennedy21570352024-03-03 16:16:47 +01003521 {
Sean Dewar4bb505e2024-03-05 20:39:07 +01003522 // 'winfixbuf' is set; attempt to change to a window without it
3523 // that isn't a quickfix/location list window.
3524 win_goto(prevwin);
3525 }
3526 if (curwin->w_p_wfb)
3527 {
3528 // Split the window, which will be 'nowinfixbuf', and set curwin
3529 // to that
3530 if (win_split(0, 0) == OK)
3531 *opened_window = TRUE;
3532
3533 if (curwin->w_p_wfb)
3534 {
3535 // Autocommands set 'winfixbuf' or sent us to another window
Sean Dewar769eb2d2024-03-07 21:37:50 +01003536 // with it set, or we failed to split the window. Give up,
3537 // but don't return immediately, as they may have messed
3538 // with the list.
Sean Dewar4bb505e2024-03-05 20:39:07 +01003539 emsg(_(e_winfixbuf_cannot_go_to_buffer));
3540 retval = FAIL;
3541 }
Colin Kennedy21570352024-03-03 16:16:47 +01003542 }
3543 }
3544
Sean Dewar4bb505e2024-03-05 20:39:07 +01003545 if (retval == OK)
3546 {
3547 retval = buflist_getfile(fnum,
3548 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
3549 }
Colin Kennedy21570352024-03-03 16:16:47 +01003550 }
Bram Moolenaar3c097222017-12-21 20:54:49 +01003551
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003552 // If a location list, check whether the associated window is still
3553 // present.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003554 if (qfl_type == QFLT_LOCATION)
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003555 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003556 win_T *wp = win_id2wp(prev_winid);
Bram Moolenaarb4ad3b02022-03-30 10:57:45 +01003557
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003558 if (wp == NULL && curwin->w_llist != qi)
3559 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003560 emsg(_(e_current_window_was_closed));
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003561 *opened_window = FALSE;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003562 return QF_ABORT;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003563 }
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003564 }
3565
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003566 if (qfl_type == QFLT_QUICKFIX && !qflist_valid(NULL, save_qfid))
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003567 {
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003568 emsg(_(e_current_quickfix_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003569 return QF_ABORT;
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003570 }
3571
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003572 // Check if the list was changed. The pointers may happen to be identical,
3573 // thus also check qf_changedtick.
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003574 if (old_qf_curlist != qi->qf_curlist
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003575 || old_changedtick != qfl->qf_changedtick
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003576 || !is_qf_entry_present(qfl, qf_ptr))
3577 {
Bram Moolenaar2d67d302018-11-16 18:46:02 +01003578 if (qfl_type == QFLT_QUICKFIX)
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003579 emsg(_(e_current_quickfix_list_was_changed));
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003580 else
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003581 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003582 return QF_ABORT;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003583 }
3584
3585 return retval;
3586}
3587
3588/*
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02003589 * Go to the error line in the current file using either line/column number or
3590 * a search pattern.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003591 */
3592 static void
3593qf_jump_goto_line(
3594 linenr_T qf_lnum,
3595 int qf_col,
3596 char_u qf_viscol,
3597 char_u *qf_pattern)
3598{
3599 linenr_T i;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003600
3601 if (qf_pattern == NULL)
3602 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003603 // Go to line with error, unless qf_lnum is 0.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003604 i = qf_lnum;
3605 if (i > 0)
3606 {
3607 if (i > curbuf->b_ml.ml_line_count)
3608 i = curbuf->b_ml.ml_line_count;
3609 curwin->w_cursor.lnum = i;
3610 }
3611 if (qf_col > 0)
3612 {
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003613 curwin->w_cursor.coladd = 0;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003614 if (qf_viscol == TRUE)
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003615 coladvance(qf_col - 1);
3616 else
3617 curwin->w_cursor.col = qf_col - 1;
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003618 curwin->w_set_curswant = TRUE;
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003619 check_cursor();
3620 }
3621 else
3622 beginline(BL_WHITE | BL_FIX);
3623 }
3624 else
3625 {
3626 pos_T save_cursor;
3627
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003628 // Move the cursor to the first line in the buffer
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003629 save_cursor = curwin->w_cursor;
3630 curwin->w_cursor.lnum = 0;
John Marriott8c85a2a2024-05-20 19:18:26 +02003631 if (!do_search(NULL, '/', '/', qf_pattern, STRLEN(qf_pattern), (long)1, SEARCH_KEEP, NULL))
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003632 curwin->w_cursor = save_cursor;
3633 }
3634}
3635
3636/*
3637 * Display quickfix list index and size message
3638 */
3639 static void
3640qf_jump_print_msg(
3641 qf_info_T *qi,
3642 int qf_index,
3643 qfline_T *qf_ptr,
3644 buf_T *old_curbuf,
3645 linenr_T old_lnum)
3646{
3647 linenr_T i;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003648 garray_T *gap;
3649
3650 gap = qfga_get();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003651
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003652 // Update the screen before showing the message, unless the screen
3653 // scrolled up.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003654 if (!msg_scrolled)
3655 update_topline_redraw();
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01003656 vim_snprintf((char *)IObuff, IOSIZE, _("(%d of %d)%s%s: "), qf_index,
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003657 qf_get_curlist(qi)->qf_count,
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003658 qf_ptr->qf_cleared ? _(" (line deleted)") : "",
3659 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003660 // Add the message, skipping leading whitespace and newlines.
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003661 ga_concat(gap, IObuff);
3662 qf_fmt_text(gap, skipwhite(qf_ptr->qf_text));
Shane Harper5bf04282023-06-07 19:09:57 +01003663 ga_append(gap, NUL);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003664
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003665 // Output the message. Overwrite to avoid scrolling when the 'O'
3666 // flag is present in 'shortmess'; But when not jumping, print the
3667 // whole message.
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003668 i = msg_scroll;
3669 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
3670 msg_scroll = TRUE;
3671 else if (!msg_scrolled && shortmess(SHM_OVERALL))
3672 msg_scroll = FALSE;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003673 msg_attr_keep((char *)gap->ga_data, 0, TRUE);
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003674 msg_scroll = i;
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01003675
3676 qfga_clear();
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003677}
3678
3679/*
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003680 * Find a usable window for opening a file from the quickfix/location list. If
Bram Moolenaarb2443732018-11-11 22:50:27 +01003681 * a window is not found then open a new window. If 'newwin' is TRUE, then open
3682 * a new window.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003683 * Returns OK if successfully jumped or opened a window. Returns FAIL if not
3684 * able to jump/open a window. Returns NOTDONE if a file is not associated
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003685 * with the entry. Returns QF_ABORT if the quickfix/location list was modified
3686 * by an autocmd.
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003687 */
3688 static int
Bram Moolenaarb2443732018-11-11 22:50:27 +01003689qf_jump_open_window(
3690 qf_info_T *qi,
3691 qfline_T *qf_ptr,
3692 int newwin,
3693 int *opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003694{
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003695 qf_list_T *qfl = qf_get_curlist(qi);
3696 int old_changedtick = qfl->qf_changedtick;
3697 int old_qf_curlist = qi->qf_curlist;
3698 qfltype_T qfl_type = qfl->qfl_type;
3699
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003700 // For ":helpgrep" find a help window or open one.
Bram Moolenaare1004402020-10-24 20:49:43 +02003701 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer)
3702 || cmdmod.cmod_tab != 0))
Bram Moolenaarb2443732018-11-11 22:50:27 +01003703 if (jump_to_help_window(qi, newwin, opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003704 return FAIL;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003705 if (old_qf_curlist != qi->qf_curlist
3706 || old_changedtick != qfl->qf_changedtick
3707 || !is_qf_entry_present(qfl, qf_ptr))
3708 {
3709 if (qfl_type == QFLT_QUICKFIX)
3710 emsg(_(e_current_quickfix_list_was_changed));
3711 else
3712 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003713 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003714 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003715
3716 // If currently in the quickfix window, find another window to show the
3717 // file in.
3718 if (bt_quickfix(curbuf) && !*opened_window)
3719 {
3720 // If there is no file specified, we don't know where to go.
3721 // But do advance, otherwise ":cn" gets stuck.
3722 if (qf_ptr->qf_fnum == 0)
3723 return NOTDONE;
3724
Bram Moolenaarb2443732018-11-11 22:50:27 +01003725 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, newwin,
3726 opened_window) == FAIL)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003727 return FAIL;
3728 }
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003729 if (old_qf_curlist != qi->qf_curlist
3730 || old_changedtick != qfl->qf_changedtick
3731 || !is_qf_entry_present(qfl, qf_ptr))
3732 {
3733 if (qfl_type == QFLT_QUICKFIX)
3734 emsg(_(e_current_quickfix_list_was_changed));
3735 else
3736 emsg(_(e_current_location_list_was_changed));
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003737 return QF_ABORT;
Bram Moolenaar4d170af2020-09-13 22:21:22 +02003738 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003739
3740 return OK;
3741}
3742
3743/*
3744 * Edit a selected file from the quickfix/location list and jump to a
3745 * particular line/column, adjust the folds and display a message about the
3746 * jump.
3747 * Returns OK on success and FAIL on failing to open the file/buffer. Returns
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003748 * QF_ABORT if the quickfix/location list is freed by an autocmd when opening
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003749 * the file.
3750 */
3751 static int
3752qf_jump_to_buffer(
3753 qf_info_T *qi,
3754 int qf_index,
3755 qfline_T *qf_ptr,
3756 int forceit,
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003757 int prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003758 int *opened_window,
3759 int openfold,
3760 int print_message)
3761{
3762 buf_T *old_curbuf;
3763 linenr_T old_lnum;
3764 int retval = OK;
3765
3766 // If there is a file name, read the wanted file if needed, and check
3767 // autowrite etc.
3768 old_curbuf = curbuf;
3769 old_lnum = curwin->w_cursor.lnum;
3770
3771 if (qf_ptr->qf_fnum != 0)
3772 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003773 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, prev_winid,
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003774 opened_window);
3775 if (retval != OK)
3776 return retval;
3777 }
3778
3779 // When not switched to another buffer, still need to set pc mark
3780 if (curbuf == old_curbuf)
3781 setpcmark();
3782
3783 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol,
3784 qf_ptr->qf_pattern);
3785
3786#ifdef FEAT_FOLDING
3787 if ((fdo_flags & FDO_QUICKFIX) && openfold)
3788 foldOpenCursor();
3789#endif
3790 if (print_message)
3791 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum);
3792
3793 return retval;
3794}
3795
3796/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003797 * Jump to a quickfix line and try to use an existing window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 */
3799 void
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02003800qf_jump(qf_info_T *qi,
3801 int dir,
3802 int errornr,
3803 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
Bram Moolenaarb2443732018-11-11 22:50:27 +01003805 qf_jump_newwin(qi, dir, errornr, forceit, FALSE);
3806}
3807
3808/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01003809 * Jump to a quickfix line.
3810 * If dir == 0 go to entry "errornr".
3811 * If dir == FORWARD go "errornr" valid entries forward.
3812 * If dir == BACKWARD go "errornr" valid entries backward.
3813 * If dir == FORWARD_FILE go "errornr" valid entries files backward.
3814 * If dir == BACKWARD_FILE go "errornr" valid entries files backward
3815 * else if "errornr" is zero, redisplay the same line
3816 * If 'forceit' is TRUE, then can discard changes to the current buffer.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003817 * If 'newwin' is TRUE, then open the file in a new window.
3818 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003819 static void
Bram Moolenaarb2443732018-11-11 22:50:27 +01003820qf_jump_newwin(qf_info_T *qi,
3821 int dir,
3822 int errornr,
3823 int forceit,
3824 int newwin)
3825{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003826 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003827 qfline_T *qf_ptr;
3828 qfline_T *old_qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 int qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 int old_qf_index;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003831 char_u *old_swb = p_swb;
Bram Moolenaar446cb832008-06-24 21:56:24 +00003832 unsigned old_swb_flags = swb_flags;
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003833 int prev_winid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 int opened_window = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 int print_message = TRUE;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003836 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar9cb03712017-09-20 22:43:02 +02003837 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003839 if (qi == NULL)
64-bitman88d41ab2025-04-06 17:20:39 +02003840 {
3841 if (ql_info == NULL)
3842 {
3843 emsg(_(e_no_quickfix_stack));
3844 return;
3845 }
3846 qi = ql_info;
3847 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003848
Bram Moolenaar0398e002019-03-21 21:12:49 +01003849 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003851 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 return;
3853 }
3854
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003855 incr_quickfix_busy();
3856
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01003857 qfl = qf_get_curlist(qi);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003858
3859 qf_ptr = qfl->qf_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 old_qf_ptr = qf_ptr;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003861 qf_index = qfl->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 old_qf_index = qf_index;
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003863
3864 qf_ptr = qf_get_entry(qfl, errornr, dir, &qf_index);
3865 if (qf_ptr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003867 qf_ptr = old_qf_ptr;
3868 qf_index = old_qf_index;
3869 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003872 qfl->qf_index = qf_index;
Wei-Chung Wen1557b162021-07-15 13:13:39 +02003873 qfl->qf_ptr = qf_ptr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003874 if (qf_win_pos_update(qi, old_qf_index))
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003875 // No need to print the error message if it's visible in the error
3876 // window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 print_message = FALSE;
3878
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003879 prev_winid = curwin->w_id;
3880
Bram Moolenaarb2443732018-11-11 22:50:27 +01003881 retval = qf_jump_open_window(qi, qf_ptr, newwin, &opened_window);
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003882 if (retval == FAIL)
3883 goto failed;
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003884 if (retval == QF_ABORT)
3885 {
3886 qi = NULL;
3887 qf_ptr = NULL;
3888 goto theend;
3889 }
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003890 if (retval == NOTDONE)
3891 goto theend;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003892
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003893 retval = qf_jump_to_buffer(qi, qf_index, qf_ptr, forceit, prev_winid,
Bram Moolenaard570ab92019-09-03 23:20:05 +02003894 &opened_window, old_KeyTyped, print_message);
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003895 if (retval == QF_ABORT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01003897 // Quickfix/location list was modified by an autocmd
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003898 qi = NULL;
3899 qf_ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003902 if (retval != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 if (opened_window)
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003905 win_close(curwin, TRUE); // Close opened window
Bram Moolenaar0899d692016-03-19 13:35:03 +01003906 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003908 // Couldn't open file, so put index back where it was. This could
3909 // happen if the file was readonly and we changed something.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910failed:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 qf_ptr = old_qf_ptr;
3912 qf_index = old_qf_index;
3913 }
3914 }
3915theend:
Bram Moolenaar0899d692016-03-19 13:35:03 +01003916 if (qi != NULL)
3917 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003918 qfl->qf_ptr = qf_ptr;
3919 qfl->qf_index = qf_index;
Bram Moolenaar0899d692016-03-19 13:35:03 +01003920 }
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003921 if (p_swb != old_swb && p_swb == empty_option)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 {
Bram Moolenaar6dae96e2018-09-24 21:50:12 +02003923 // Restore old 'switchbuf' value, but not when an autocommand or
3924 // modeline has changed the value.
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003925 p_swb = old_swb;
3926 swb_flags = old_swb_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003928 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929}
3930
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003931// Highlight attributes used for displaying entries from the quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003932static int qfFileAttr;
3933static int qfSepAttr;
3934static int qfLineAttr;
3935
3936/*
3937 * Display information about a single entry from the quickfix/location list.
3938 * Used by ":clist/:llist" commands.
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003939 * 'cursel' will be set to TRUE for the currently selected entry in the
3940 * quickfix list.
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003941 */
3942 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003943qf_list_entry(qfline_T *qfp, int qf_idx, int cursel)
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003944{
3945 char_u *fname;
3946 buf_T *buf;
3947 int filter_entry;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003948 garray_T *gap;
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003949
3950 fname = NULL;
3951 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3952 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", qf_idx,
3953 (char *)qfp->qf_module);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00003954 else
3955 {
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003956 if (qfp->qf_fnum != 0
3957 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
3958 {
Austin Chang29822992024-10-03 10:50:05 +02003959 if (qfp->qf_fname == NULL)
3960 fname = buf->b_fname;
3961 else
3962 fname = qfp->qf_fname;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02003963 if (qfp->qf_type == 1) // :helpgrep
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003964 fname = gettail(fname);
3965 }
3966 if (fname == NULL)
3967 sprintf((char *)IObuff, "%2d", qf_idx);
3968 else
3969 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
3970 qf_idx, (char *)fname);
3971 }
3972
3973 // Support for filtering entries using :filter /pat/ clist
3974 // Match against the module name, file name, search pattern and
3975 // text of the entry.
3976 filter_entry = TRUE;
3977 if (qfp->qf_module != NULL && *qfp->qf_module != NUL)
3978 filter_entry &= message_filtered(qfp->qf_module);
3979 if (filter_entry && fname != NULL)
3980 filter_entry &= message_filtered(fname);
3981 if (filter_entry && qfp->qf_pattern != NULL)
3982 filter_entry &= message_filtered(qfp->qf_pattern);
3983 if (filter_entry)
3984 filter_entry &= message_filtered(qfp->qf_text);
3985 if (filter_entry)
3986 return;
3987
3988 msg_putchar('\n');
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02003989 msg_outtrans_attr(IObuff, cursel ? HL_ATTR(HLF_QFL) : qfFileAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02003990
3991 if (qfp->qf_lnum != 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003992 msg_puts_attr(":", qfSepAttr);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003993 gap = qfga_get();
Shane Harper5bf04282023-06-07 19:09:57 +01003994 if (qfp->qf_lnum != 0)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01003995 qf_range_text(gap, qfp);
3996 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
3997 ga_append(gap, NUL);
3998 msg_puts_attr((char *)gap->ga_data, qfLineAttr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003999 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02004000 if (qfp->qf_pattern != NULL)
4001 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004002 gap = qfga_get();
4003 qf_fmt_text(gap, qfp->qf_pattern);
Shane Harper5bf04282023-06-07 19:09:57 +01004004 ga_append(gap, NUL);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004005 msg_puts((char *)gap->ga_data);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004006 msg_puts_attr(":", qfSepAttr);
Bram Moolenaarde3b3672018-08-07 21:54:41 +02004007 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01004008 msg_puts(" ");
Bram Moolenaarde3b3672018-08-07 21:54:41 +02004009
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004010 // Remove newlines and leading whitespace from the text. For an
4011 // unrecognized line keep the indent, the compiler may mark a word
4012 // with ^^^^.
4013 gap = qfga_get();
4014 qf_fmt_text(gap, (fname != NULL || qfp->qf_lnum != 0)
Shane Harper5bf04282023-06-07 19:09:57 +01004015 ? skipwhite(qfp->qf_text) : qfp->qf_text);
4016 ga_append(gap, NUL);
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004017 msg_prt_line((char_u *)gap->ga_data, FALSE);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004018 out_flush(); // show one line at a time
Bram Moolenaarde3b3672018-08-07 21:54:41 +02004019}
4020
4021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 * ":clist": list all errors
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004023 * ":llist": list all locations
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 */
4025 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004026qf_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004028 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004029 qfline_T *qfp;
4030 int i;
4031 int idx1 = 1;
4032 int idx2 = -1;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004033 char_u *arg = eap->arg;
Bram Moolenaare8fea072016-07-01 14:48:27 +02004034 int plus = FALSE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004035 int all = eap->forceit; // if not :cl!, only show
4036 // recognised errors
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004037 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004039 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4040 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004041
Bram Moolenaar0398e002019-03-21 21:12:49 +01004042 if (qf_stack_empty(qi) || qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004044 emsg(_(e_no_errors));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 return;
4046 }
Bram Moolenaare8fea072016-07-01 14:48:27 +02004047 if (*arg == '+')
4048 {
4049 ++arg;
4050 plus = TRUE;
4051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
4053 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004054 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 return;
4056 }
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004057 qfl = qf_get_curlist(qi);
Bram Moolenaare8fea072016-07-01 14:48:27 +02004058 if (plus)
4059 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004060 i = qfl->qf_index;
Bram Moolenaare8fea072016-07-01 14:48:27 +02004061 idx2 = i + idx1;
4062 idx1 = i;
4063 }
4064 else
4065 {
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004066 i = qfl->qf_count;
Bram Moolenaare8fea072016-07-01 14:48:27 +02004067 if (idx1 < 0)
4068 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
4069 if (idx2 < 0)
4070 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
4071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004073 // Shorten all the file names, so that it is easy to read
Bram Moolenaara796d462018-05-01 14:30:36 +02004074 shorten_fnames(FALSE);
4075
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004076 // Get the attributes for the different quickfix highlight items. Note
4077 // that this depends on syntax items defined in the qf.vim syntax file
Bram Moolenaar93a32e22017-11-23 22:05:45 +01004078 qfFileAttr = syn_name2attr((char_u *)"qfFileName");
4079 if (qfFileAttr == 0)
4080 qfFileAttr = HL_ATTR(HLF_D);
4081 qfSepAttr = syn_name2attr((char_u *)"qfSeparator");
4082 if (qfSepAttr == 0)
4083 qfSepAttr = HL_ATTR(HLF_D);
4084 qfLineAttr = syn_name2attr((char_u *)"qfLineNr");
4085 if (qfLineAttr == 0)
4086 qfLineAttr = HL_ATTR(HLF_N);
4087
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004088 if (qfl->qf_nonevalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 all = TRUE;
Bram Moolenaar95946f12019-03-31 15:31:59 +02004090 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
4092 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004093 qf_list_entry(qfp, i, i == qfl->qf_index);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00004094
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 ui_breakcheck();
4096 }
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01004097 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098}
4099
4100/*
4101 * Remove newlines and leading whitespace from an error message.
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004102 * Add the result to the grow array "gap".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 */
4104 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004105qf_fmt_text(garray_T *gap, char_u *text)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 char_u *p = text;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004108 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
4110 if (*p == '\n')
4111 {
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004112 ga_append(gap, ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 while (*++p != NUL)
Bram Moolenaar1c465442017-03-12 20:10:05 +01004114 if (!VIM_ISWHITE(*p) && *p != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 break;
4116 }
4117 else
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004118 ga_append(gap, *p++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120}
4121
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004122/*
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004123 * Add the range information from the lnum, col, end_lnum, and end_col values
4124 * of a quickfix entry to the grow array "gap".
thinca6864efa2021-06-19 20:45:20 +02004125 */
4126 static void
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004127qf_range_text(garray_T *gap, qfline_T *qfp)
thinca6864efa2021-06-19 20:45:20 +02004128{
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004129 char_u *buf = IObuff;
4130 int bufsize = IOSIZE;
thinca6864efa2021-06-19 20:45:20 +02004131 int len;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004132
thinca6864efa2021-06-19 20:45:20 +02004133 vim_snprintf((char *)buf, bufsize, "%ld", qfp->qf_lnum);
4134 len = (int)STRLEN(buf);
4135
4136 if (qfp->qf_end_lnum > 0 && qfp->qf_lnum != qfp->qf_end_lnum)
4137 {
Shane Harper5bf04282023-06-07 19:09:57 +01004138 vim_snprintf((char *)buf + len, bufsize - len, "-%ld",
4139 qfp->qf_end_lnum);
thinca6864efa2021-06-19 20:45:20 +02004140 len += (int)STRLEN(buf + len);
4141 }
4142 if (qfp->qf_col > 0)
4143 {
4144 vim_snprintf((char *)buf + len, bufsize - len, " col %d", qfp->qf_col);
4145 len += (int)STRLEN(buf + len);
4146 if (qfp->qf_end_col > 0 && qfp->qf_col != qfp->qf_end_col)
4147 {
Shane Harper5bf04282023-06-07 19:09:57 +01004148 vim_snprintf((char *)buf + len, bufsize - len, "-%d",
4149 qfp->qf_end_col);
thinca6864efa2021-06-19 20:45:20 +02004150 len += (int)STRLEN(buf + len);
4151 }
4152 }
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004153
4154 ga_concat_len(gap, buf, len);
thinca6864efa2021-06-19 20:45:20 +02004155}
4156
4157/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004158 * Display information (list number, list size and the title) about a
4159 * quickfix/location list.
4160 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004161 static void
4162qf_msg(qf_info_T *qi, int which, char *lead)
4163{
4164 char *title = (char *)qi->qf_lists[which].qf_title;
4165 int count = qi->qf_lists[which].qf_count;
4166 char_u buf[IOSIZE];
4167
4168 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "),
4169 lead,
4170 which + 1,
4171 qi->qf_listcount,
4172 count);
4173
4174 if (title != NULL)
4175 {
Bram Moolenaar16ec3c92016-07-18 22:22:39 +02004176 size_t len = STRLEN(buf);
4177
4178 if (len < 34)
4179 {
4180 vim_memset(buf + len, ' ', 34 - len);
4181 buf[34] = NUL;
4182 }
4183 vim_strcat(buf, (char_u *)title, IOSIZE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004184 }
4185 trunc_string(buf, buf, Columns - 1, IOSIZE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004186 msg((char *)buf);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004187}
4188
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189/*
4190 * ":colder [count]": Up in the quickfix stack.
4191 * ":cnewer [count]": Down in the quickfix stack.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004192 * ":lolder [count]": Up in the location list stack.
4193 * ":lnewer [count]": Down in the location list stack.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 */
4195 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004196qf_age(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004198 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 int count;
4200
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004201 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4202 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004203
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 if (eap->addr_count != 0)
4205 count = eap->line2;
4206 else
4207 count = 1;
4208 while (count--)
4209 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004210 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004212 if (qi->qf_curlist == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004214 emsg(_(e_at_bottom_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02004215 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004217 --qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219 else
4220 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004221 if (qi->qf_curlist >= qi->qf_listcount - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 {
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004223 emsg(_(e_at_top_of_quickfix_stack));
Bram Moolenaar82e803b2013-05-11 15:50:33 +02004224 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004226 ++qi->qf_curlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228 }
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004229 qf_msg(qi, qi->qf_curlist, "");
Bram Moolenaar864293a2016-06-02 13:40:04 +02004230 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231}
4232
Bram Moolenaar18cebf42018-05-08 22:31:37 +02004233/*
4234 * Display the information about all the quickfix/location lists in the stack
4235 */
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004236 void
4237qf_history(exarg_T *eap)
4238{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004239 qf_info_T *qi = qf_cmd_get_stack(eap, FALSE);
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004240 int i;
4241
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004242 if (eap->addr_count > 0)
4243 {
4244 if (qi == NULL)
4245 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004246 emsg(_(e_no_location_list));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004247 return;
4248 }
4249
4250 // Jump to the specified quickfix list
4251 if (eap->line2 > 0 && eap->line2 <= qi->qf_listcount)
4252 {
4253 qi->qf_curlist = eap->line2 - 1;
4254 qf_msg(qi, qi->qf_curlist, "");
4255 qf_update_buffer(qi, NULL);
4256 }
4257 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004258 emsg(_(e_invalid_range));
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02004259
4260 return;
4261 }
4262
Bram Moolenaar5b69c222019-01-11 14:50:06 +01004263 if (qf_stack_empty(qi))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004264 msg(_("No entries"));
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02004265 else
4266 for (i = 0; i < qi->qf_listcount; ++i)
4267 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " ");
4268}
4269
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004271 * Free all the entries in the error list "idx". Note that other information
4272 * associated with the list like context and title are not freed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 */
4274 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004275qf_free_items(qf_list_T *qfl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004277 qfline_T *qfp;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004278 qfline_T *qfpnext;
Bram Moolenaar81484f42012-12-05 15:16:47 +01004279 int stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004281 while (qfl->qf_count && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 {
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004283 qfp = qfl->qf_start;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004284 qfpnext = qfp->qf_next;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004285 if (!stop)
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01004286 {
Austin Chang29822992024-10-03 10:50:05 +02004287 vim_free(qfp->qf_fname);
Bram Moolenaard76ce852018-05-01 15:02:04 +02004288 vim_free(qfp->qf_module);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004289 vim_free(qfp->qf_text);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004290 vim_free(qfp->qf_pattern);
Tom Praschanca6ac992023-08-11 23:26:12 +02004291 clear_tv(&qfp->qf_user_data);
Bram Moolenaard76ce852018-05-01 15:02:04 +02004292 stop = (qfp == qfpnext);
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02004293 vim_free(qfp);
Bram Moolenaar81484f42012-12-05 15:16:47 +01004294 if (stop)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004295 // Somehow qf_count may have an incorrect value, set it to 1
4296 // to avoid crashing when it's wrong.
4297 // TODO: Avoid qf_count being incorrect.
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004298 qfl->qf_count = 1;
Christian Brabandt567cae22023-11-19 16:19:27 +01004299 else
4300 qfl->qf_start = qfpnext;
Bram Moolenaarc83a44b2012-11-28 15:25:34 +01004301 }
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004302 --qfl->qf_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004304
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004305 qfl->qf_index = 0;
4306 qfl->qf_start = NULL;
4307 qfl->qf_last = NULL;
4308 qfl->qf_ptr = NULL;
4309 qfl->qf_nonevalid = TRUE;
Bram Moolenaar361c8f02016-07-02 15:41:47 +02004310
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004311 qf_clean_dir_stack(&qfl->qf_dir_stack);
4312 qfl->qf_directory = NULL;
4313 qf_clean_dir_stack(&qfl->qf_file_stack);
4314 qfl->qf_currfile = NULL;
4315 qfl->qf_multiline = FALSE;
4316 qfl->qf_multiignore = FALSE;
4317 qfl->qf_multiscan = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318}
4319
4320/*
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004321 * Free error list "idx". Frees all the entries in the quickfix list,
4322 * associated context information and the title.
4323 */
4324 static void
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004325qf_free(qf_list_T *qfl)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004326{
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004327 qf_free_items(qfl);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004328
Bram Moolenaard23a8232018-02-10 18:45:26 +01004329 VIM_CLEAR(qfl->qf_title);
Bram Moolenaara7df8c72017-07-19 13:23:06 +02004330 free_tv(qfl->qf_ctx);
4331 qfl->qf_ctx = NULL;
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01004332 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02004333 qfl->qf_id = 0;
Bram Moolenaarb254af32017-12-18 19:48:58 +01004334 qfl->qf_changedtick = 0L;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02004335}
4336
4337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 * qf_mark_adjust: adjust marks
4339 */
4340 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004341qf_mark_adjust(
Bram Moolenaaref6b8de2017-09-14 13:57:37 +02004342 win_T *wp,
4343 linenr_T line1,
4344 linenr_T line2,
4345 long amount,
4346 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004348 int i;
4349 qfline_T *qfp;
4350 int idx;
64-bitman88d41ab2025-04-06 17:20:39 +02004351 qf_info_T *qi = ql_info;
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004352 int found_one = FALSE;
Bram Moolenaarc1542742016-07-20 21:44:37 +02004353 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
Bram Moolenaarc1542742016-07-20 21:44:37 +02004355 if (!(curbuf->b_has_qf_entry & buf_has_flag))
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004356 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004357 if (wp != NULL)
4358 {
4359 if (wp->w_llist == NULL)
4360 return;
4361 qi = wp->w_llist;
4362 }
64-bitman88d41ab2025-04-06 17:20:39 +02004363 else if (qi == NULL)
4364 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004365
4366 for (idx = 0; idx < qi->qf_listcount; ++idx)
Bram Moolenaar0398e002019-03-21 21:12:49 +01004367 {
4368 qf_list_T *qfl = qf_get_list(qi, idx);
4369
4370 if (!qf_list_empty(qfl))
Bram Moolenaara16123a2019-03-28 20:31:07 +01004371 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 if (qfp->qf_fnum == curbuf->b_fnum)
4373 {
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004374 found_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
4376 {
4377 if (amount == MAXLNUM)
4378 qfp->qf_cleared = TRUE;
4379 else
4380 qfp->qf_lnum += amount;
4381 }
4382 else if (amount_after && qfp->qf_lnum > line2)
4383 qfp->qf_lnum += amount_after;
4384 }
Bram Moolenaar0398e002019-03-21 21:12:49 +01004385 }
Bram Moolenaar2f095a42016-06-03 19:05:49 +02004386
4387 if (!found_one)
Bram Moolenaarc1542742016-07-20 21:44:37 +02004388 curbuf->b_has_qf_entry &= ~buf_has_flag;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389}
4390
4391/*
4392 * Make a nice message out of the error character and the error number:
4393 * char number message
4394 * e or E 0 " error"
4395 * w or W 0 " warning"
4396 * i or I 0 " info"
Bram Moolenaare9283662020-06-07 14:10:47 +02004397 * n or N 0 " note"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 * 0 0 ""
4399 * other 0 " c"
4400 * e or E n " error n"
4401 * w or W n " warning n"
4402 * i or I n " info n"
Bram Moolenaare9283662020-06-07 14:10:47 +02004403 * n or N n " note n"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 * 0 n " error n"
4405 * other n " c n"
4406 * 1 x "" :helpgrep
4407 */
4408 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01004409qf_types(int c, int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410{
4411 static char_u buf[20];
4412 static char_u cc[3];
4413 char_u *p;
4414
4415 if (c == 'W' || c == 'w')
4416 p = (char_u *)" warning";
4417 else if (c == 'I' || c == 'i')
4418 p = (char_u *)" info";
Bram Moolenaare9283662020-06-07 14:10:47 +02004419 else if (c == 'N' || c == 'n')
4420 p = (char_u *)" note";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
4422 p = (char_u *)" error";
4423 else if (c == 0 || c == 1)
4424 p = (char_u *)"";
4425 else
4426 {
4427 cc[0] = ' ';
4428 cc[1] = c;
4429 cc[2] = NUL;
4430 p = cc;
4431 }
4432
4433 if (nr <= 0)
4434 return p;
4435
4436 sprintf((char *)buf, "%s %3d", (char *)p, nr);
4437 return buf;
4438}
4439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440/*
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004441 * When "split" is FALSE: Open the entry/result under the cursor.
4442 * When "split" is TRUE: Open the entry/result under the cursor in a new window.
4443 */
4444 void
4445qf_view_result(int split)
4446{
64-bitman88d41ab2025-04-06 17:20:39 +02004447 qf_info_T *qi = ql_info;
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004448
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004449 if (IS_LL_WINDOW(curwin))
4450 qi = GET_LOC_LIST(curwin);
64-bitman88d41ab2025-04-06 17:20:39 +02004451 else if (qi == NULL)
4452 {
4453 emsg(_(e_no_quickfix_stack));
4454 return;
4455 }
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004456
Bram Moolenaar0398e002019-03-21 21:12:49 +01004457 if (qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004458 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004459 emsg(_(e_no_errors));
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004460 return;
4461 }
4462
4463 if (split)
4464 {
Bram Moolenaarb2443732018-11-11 22:50:27 +01004465 // Open the selected entry in a new window
4466 qf_jump_newwin(qi, 0, (long)curwin->w_cursor.lnum, FALSE, TRUE);
4467 do_cmdline_cmd((char_u *) "clearjumps");
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004468 return;
4469 }
4470
4471 do_cmdline_cmd((char_u *)(IS_LL_WINDOW(curwin) ? ".ll" : ".cc"));
4472}
4473
4474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 * ":cwindow": open the quickfix window if we have errors to display,
4476 * close it if not.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004477 * ":lwindow": open the location list window if we have locations to display,
4478 * close it if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 */
4480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004481ex_cwindow(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004483 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004484 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 win_T *win;
4486
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004487 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4488 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004489
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004490 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004491
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004492 // Look for an existing quickfix window.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004493 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004495 // If a quickfix window is open but we have no errors to display,
4496 // close the window. If a quickfix window is not open, then open
4497 // it if we have errors; otherwise, leave it closed.
Bram Moolenaar019dfe62018-10-07 14:38:49 +02004498 if (qf_stack_empty(qi)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004499 || qfl->qf_nonevalid
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01004500 || qf_list_empty(qfl))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 {
4502 if (win != NULL)
4503 ex_cclose(eap);
4504 }
4505 else if (win == NULL)
4506 ex_copen(eap);
4507}
4508
4509/*
4510 * ":cclose": close the window showing the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004511 * ":lclose": close the window showing the location list
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004514ex_cclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515{
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004516 win_T *win = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004517 qf_info_T *qi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004519 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
4520 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004522 // Find existing quickfix window and close it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004523 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 if (win != NULL)
4525 win_close(win, FALSE);
4526}
4527
4528/*
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004529 * Set "w:quickfix_title" if "qi" has a title.
4530 */
4531 static void
4532qf_set_title_var(qf_list_T *qfl)
4533{
4534 if (qfl->qf_title != NULL)
4535 set_internal_string_var((char_u *)"w:quickfix_title", qfl->qf_title);
4536}
4537
4538/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004539 * Goto a quickfix or location list window (if present).
4540 * Returns OK if the window is found, FAIL otherwise.
4541 */
4542 static int
4543qf_goto_cwindow(qf_info_T *qi, int resize, int sz, int vertsplit)
4544{
4545 win_T *win;
4546
4547 win = qf_find_win(qi);
4548 if (win == NULL)
4549 return FAIL;
4550
4551 win_goto(win);
4552 if (resize)
4553 {
4554 if (vertsplit)
4555 {
4556 if (sz != win->w_width)
4557 win_setwidth(sz);
4558 }
Bram Moolenaar1142a312019-10-16 14:51:39 +02004559 else if (sz != win->w_height && win->w_height
4560 + win->w_status_height + tabline_height() < cmdline_row)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004561 win_setheight(sz);
4562 }
4563
4564 return OK;
4565}
4566
4567/*
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004568 * Set options for the buffer in the quickfix or location list window.
4569 */
4570 static void
4571qf_set_cwindow_options(void)
4572{
4573 // switch off 'swapfile'
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004574 set_option_value_give_err((char_u *)"swf", 0L, NULL, OPT_LOCAL);
4575 set_option_value_give_err((char_u *)"bt",
4576 0L, (char_u *)"quickfix", OPT_LOCAL);
4577 set_option_value_give_err((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004578 RESET_BINDING(curwin);
4579#ifdef FEAT_DIFF
4580 curwin->w_p_diff = FALSE;
4581#endif
4582#ifdef FEAT_FOLDING
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004583 set_option_value_give_err((char_u *)"fdm", 0L, (char_u *)"manual",
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004584 OPT_LOCAL);
4585#endif
4586}
4587
4588/*
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004589 * Open a new quickfix or location list window, load the quickfix buffer and
4590 * set the appropriate options for the window.
4591 * Returns FAIL if the window could not be opened.
4592 */
4593 static int
4594qf_open_new_cwindow(qf_info_T *qi, int height)
4595{
4596 buf_T *qf_buf;
4597 win_T *oldwin = curwin;
4598 tabpage_T *prevtab = curtab;
4599 int flags = 0;
4600 win_T *win;
4601
4602 qf_buf = qf_find_buf(qi);
4603
4604 // The current window becomes the previous window afterwards.
4605 win = curwin;
4606
Bram Moolenaare1004402020-10-24 20:49:43 +02004607 if (IS_QF_STACK(qi) && cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004608 // Create the new quickfix window at the very bottom, except when
4609 // :belowright or :aboveleft is used.
4610 win_goto(lastwin);
4611 // Default is to open the window below the current window
Bram Moolenaare1004402020-10-24 20:49:43 +02004612 if (cmdmod.cmod_split == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004613 flags = WSP_BELOW;
4614 flags |= WSP_NEWLOC;
4615 if (win_split(height, flags) == FAIL)
4616 return FAIL; // not enough room for window
4617 RESET_BINDING(curwin);
4618
4619 if (IS_LL_STACK(qi))
4620 {
4621 // For the location list window, create a reference to the
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004622 // location list stack from the window 'win'.
4623 curwin->w_llist_ref = qi;
4624 qi->qf_refcount++;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004625 }
4626
4627 if (oldwin != curwin)
4628 oldwin = NULL; // don't store info when in another window
4629 if (qf_buf != NULL)
4630 {
4631 // Use the existing quickfix buffer
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004632 if (do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004633 ECMD_HIDE + ECMD_OLDBUF + ECMD_NOWINENTER, oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004634 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004635 }
4636 else
4637 {
4638 // Create a new quickfix buffer
Bram Moolenaar1d30fde2021-10-20 21:58:42 +01004639 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE + ECMD_NOWINENTER,
4640 oldwin) == FAIL)
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01004641 return FAIL;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004642
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004643 // save the number of the new buffer
4644 qi->qf_bufnr = curbuf->b_fnum;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004645 }
4646
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004647 // Set the options for the quickfix buffer/window (if not already done)
4648 // Do this even if the quickfix buffer was already present, as an autocmd
4649 // might have previously deleted (:bdelete) the quickfix buffer.
Bram Moolenaar61eeeea2019-06-15 21:56:17 +02004650 if (!bt_quickfix(curbuf))
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004651 qf_set_cwindow_options();
4652
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004653 // Only set the height when still in the same tab page and there is no
4654 // window to the side.
4655 if (curtab == prevtab && curwin->w_width == Columns)
4656 win_setheight(height);
4657 curwin->w_p_wfh = TRUE; // set 'winfixheight'
4658 if (win_valid(win))
4659 prevwin = win;
4660
4661 return OK;
4662}
4663
4664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 * ":copen": open a window that shows the list of errors.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004666 * ":lopen": open a window that shows the location list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 */
4668 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004669ex_copen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004671 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004672 qf_list_T *qfl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 int height;
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004674 int status = FAIL;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004675 int lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004677 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4678 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004679
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004680 incr_quickfix_busy();
4681
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 if (eap->addr_count != 0)
4683 height = eap->line2;
4684 else
4685 height = QF_WINHEIGHT;
4686
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004687 reset_VIsual_and_resel(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688#ifdef FEAT_GUI
4689 need_mouse_correct = TRUE;
4690#endif
4691
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004692 // Find an existing quickfix window, or open a new one.
Bram Moolenaare1004402020-10-24 20:49:43 +02004693 if (cmdmod.cmod_tab == 0)
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004694 status = qf_goto_cwindow(qi, eap->addr_count != 0, height,
Bram Moolenaare1004402020-10-24 20:49:43 +02004695 cmdmod.cmod_split & WSP_VERT);
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004696 if (status == FAIL)
4697 if (qf_open_new_cwindow(qi, height) == FAIL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004698 {
4699 decr_quickfix_busy();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004700 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004703 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02004704 qf_set_title_var(qfl);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004705 // Save the current index here, as updating the quickfix buffer may free
4706 // the quickfix list
4707 lnum = qfl->qf_index;
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004708
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004709 // Fill the buffer with the quickfix list.
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004710 qf_fill_buffer(qfl, curbuf, NULL, curwin->w_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004712 decr_quickfix_busy();
4713
4714 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 curwin->w_cursor.col = 0;
4716 check_cursor();
Bram Moolenaar476c0db2018-09-19 21:56:02 +02004717 update_topline(); // scroll to show the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718}
4719
4720/*
Bram Moolenaardcb17002016-07-07 18:58:59 +02004721 * Move the cursor in the quickfix window to "lnum".
4722 */
4723 static void
4724qf_win_goto(win_T *win, linenr_T lnum)
4725{
4726 win_T *old_curwin = curwin;
4727
4728 curwin = win;
4729 curbuf = win->w_buffer;
4730 curwin->w_cursor.lnum = lnum;
4731 curwin->w_cursor.col = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004732 curwin->w_cursor.coladd = 0;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004733 curwin->w_curswant = 0;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004734 update_topline(); // scroll to show the line
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004735 redraw_later(UPD_VALID);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004736 curwin->w_redr_status = TRUE; // update ruler
Bram Moolenaardcb17002016-07-07 18:58:59 +02004737 curwin = old_curwin;
4738 curbuf = curwin->w_buffer;
4739}
4740
4741/*
Bram Moolenaar537ef082016-07-09 17:56:19 +02004742 * :cbottom/:lbottom commands.
Bram Moolenaardcb17002016-07-07 18:58:59 +02004743 */
4744 void
Bram Moolenaar39665952018-08-15 20:59:48 +02004745ex_cbottom(exarg_T *eap)
Bram Moolenaardcb17002016-07-07 18:58:59 +02004746{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004747 qf_info_T *qi;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004748 win_T *win;
Bram Moolenaardcb17002016-07-07 18:58:59 +02004749
Bram Moolenaar87f59b02019-04-04 14:04:11 +02004750 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
4751 return;
Bram Moolenaar537ef082016-07-09 17:56:19 +02004752
4753 win = qf_find_win(qi);
Bram Moolenaardcb17002016-07-07 18:58:59 +02004754 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count)
4755 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count);
4756}
4757
4758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 * Return the number of the current entry (line number in the quickfix
4760 * window).
4761 */
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02004762 linenr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01004763qf_current_entry(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764{
64-bitman88d41ab2025-04-06 17:20:39 +02004765 qf_info_T *qi = ql_info;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004766
4767 if (IS_LL_WINDOW(wp))
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004768 // In the location list window, use the referenced location list
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004769 qi = wp->w_llist_ref;
64-bitman88d41ab2025-04-06 17:20:39 +02004770 else if (qi == NULL)
4771 return 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004772
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004773 return qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774}
4775
4776/*
4777 * Update the cursor position in the quickfix window to the current error.
4778 * Return TRUE if there is a quickfix window.
4779 */
4780 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004781qf_win_pos_update(
4782 qf_info_T *qi,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004783 int old_qf_index) // previous qf_index or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784{
4785 win_T *win;
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01004786 int qf_index = qf_get_curlist(qi)->qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004788 // Put the cursor on the current error in the quickfix window, so that
4789 // it's viewable.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004790 win = qf_find_win(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 if (win != NULL
4792 && qf_index <= win->w_buffer->b_ml.ml_line_count
4793 && old_qf_index != qf_index)
4794 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 if (qf_index > old_qf_index)
4796 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004797 win->w_redraw_top = old_qf_index;
4798 win->w_redraw_bot = qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 }
4800 else
4801 {
Bram Moolenaardcb17002016-07-07 18:58:59 +02004802 win->w_redraw_top = qf_index;
4803 win->w_redraw_bot = old_qf_index;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
Bram Moolenaardcb17002016-07-07 18:58:59 +02004805 qf_win_goto(win, qf_index);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807 return win != NULL;
4808}
4809
4810/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004811 * Check whether the given window is displaying the specified quickfix/location
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02004812 * stack.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004813 */
4814 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004815is_qf_win(win_T *win, qf_info_T *qi)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004816{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004817 // A window displaying the quickfix buffer will have the w_llist_ref field
4818 // set to NULL.
4819 // A window displaying a location list buffer will have the w_llist_ref
4820 // pointing to the location list.
Christian Brabandtfc682992023-09-03 20:20:52 +02004821 if (buf_valid(win->w_buffer) && bt_quickfix(win->w_buffer))
Bram Moolenaar4d77c652018-08-18 19:59:54 +02004822 if ((IS_QF_STACK(qi) && win->w_llist_ref == NULL)
4823 || (IS_LL_STACK(qi) && win->w_llist_ref == qi))
Bram Moolenaar9c102382006-05-03 21:26:49 +00004824 return TRUE;
4825
4826 return FALSE;
4827}
4828
4829/*
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004830 * Find a window displaying the quickfix/location stack 'qi' in the current tab
4831 * page.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004832 */
4833 static win_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004834qf_find_win(qf_info_T *qi)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004835{
4836 win_T *win;
4837
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004838 FOR_ALL_WINDOWS(win)
Bram Moolenaar9c102382006-05-03 21:26:49 +00004839 if (is_qf_win(win, qi))
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004840 return win;
4841 return NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004842}
4843
4844/*
Bram Moolenaar9c102382006-05-03 21:26:49 +00004845 * Find a quickfix buffer.
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00004846 * Searches in windows opened in all the tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 */
4848 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004849qf_find_buf(qf_info_T *qi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850{
Bram Moolenaar9c102382006-05-03 21:26:49 +00004851 tabpage_T *tp;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004852 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004854 if (qi->qf_bufnr != INVALID_QFBUFNR)
4855 {
4856 buf_T *qfbuf;
4857 qfbuf = buflist_findnr(qi->qf_bufnr);
4858 if (qfbuf != NULL)
4859 return qfbuf;
4860 // buffer is no longer present
4861 qi->qf_bufnr = INVALID_QFBUFNR;
4862 }
4863
Bram Moolenaar9c102382006-05-03 21:26:49 +00004864 FOR_ALL_TAB_WINDOWS(tp, win)
4865 if (is_qf_win(win, qi))
4866 return win->w_buffer;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004867
Bram Moolenaar9c102382006-05-03 21:26:49 +00004868 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869}
4870
4871/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004872 * Process the 'quickfixtextfunc' option value.
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00004873 * Returns OK or FAIL.
Bram Moolenaard43906d2020-07-20 21:31:32 +02004874 */
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004875 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004876did_set_quickfixtextfunc(optset_T *args UNUSED)
Bram Moolenaard43906d2020-07-20 21:31:32 +02004877{
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004878 if (option_set_callback_func(p_qftf, &qftf_cb) == FAIL)
4879 return e_invalid_argument;
4880
4881 return NULL;
Bram Moolenaard43906d2020-07-20 21:31:32 +02004882}
4883
4884/*
Bram Moolenaar530bed92020-12-16 21:02:56 +01004885 * Update the w:quickfix_title variable in the quickfix/location list window in
4886 * all the tab pages.
Bram Moolenaard823fa92016-08-12 16:29:27 +02004887 */
4888 static void
4889qf_update_win_titlevar(qf_info_T *qi)
4890{
Bram Moolenaar530bed92020-12-16 21:02:56 +01004891 qf_list_T *qfl = qf_get_curlist(qi);
4892 tabpage_T *tp;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004893 win_T *win;
Bram Moolenaar530bed92020-12-16 21:02:56 +01004894 win_T *save_curwin = curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004895
Bram Moolenaar530bed92020-12-16 21:02:56 +01004896 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaard823fa92016-08-12 16:29:27 +02004897 {
Bram Moolenaar530bed92020-12-16 21:02:56 +01004898 if (is_qf_win(win, qi))
4899 {
4900 curwin = win;
4901 qf_set_title_var(qfl);
4902 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004903 }
Bram Moolenaar530bed92020-12-16 21:02:56 +01004904 curwin = save_curwin;
Bram Moolenaard823fa92016-08-12 16:29:27 +02004905}
4906
4907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 * Find the quickfix buffer. If it exists, update the contents.
4909 */
4910 static void
Bram Moolenaar864293a2016-06-02 13:40:04 +02004911qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912{
4913 buf_T *buf;
Bram Moolenaarc95e3262011-08-10 18:36:54 +02004914 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02004917 // Check if a buffer for the quickfix list exists. Update it.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004918 buf = qf_find_buf(qi);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004919 if (buf == NULL)
4920 return;
4921
4922 linenr_T old_line_count = buf->b_ml.ml_line_count;
4923 int qf_winid = 0;
4924
4925 if (IS_LL_STACK(qi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004927 if (curwin->w_llist == qi)
4928 win = curwin;
4929 else
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004930 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004931 // Find the file window (non-quickfix) with this location list
4932 win = qf_find_win_with_loclist(qi);
4933 if (win == NULL)
4934 // File window is not found. Find the location list window.
4935 win = qf_find_win(qi);
4936 if (win == NULL)
4937 return;
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02004938 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004939 qf_winid = win->w_id;
4940 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02004941
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004942 // autocommands may cause trouble
4943 incr_quickfix_busy();
Bram Moolenaard0fab102022-10-20 16:03:33 +01004944
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004945 int do_fill = TRUE;
4946 if (old_last == NULL)
4947 {
4948 // set curwin/curbuf to buf and save a few things
4949 aucmd_prepbuf(&aco, buf);
4950 if (curbuf != buf)
4951 do_fill = FALSE; // failed to find a window for "buf"
4952 }
4953
4954 if (do_fill)
4955 {
4956 qf_update_win_titlevar(qi);
4957
4958 qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);
4959 ++CHANGEDTICK(buf);
4960
Bram Moolenaar864293a2016-06-02 13:40:04 +02004961 if (old_last == NULL)
4962 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004963 (void)qf_win_pos_update(qi, 0);
4964
4965 // restore curwin/curbuf and a few other things
4966 aucmd_restbuf(&aco);
Bram Moolenaare76062c2022-11-28 18:51:43 +00004967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00004969
4970 // Only redraw when added lines are visible. This avoids flickering
4971 // when the added lines are not visible.
4972 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline)
4973 redraw_buf_later(buf, UPD_NOT_VALID);
4974
4975 // always called after incr_quickfix_busy()
4976 decr_quickfix_busy();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977}
4978
Bram Moolenaar81278ef2015-05-04 12:34:22 +02004979/*
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004980 * Add an error line to the quickfix buffer.
4981 */
4982 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02004983qf_buf_add_line(
Bram Moolenaar858ba062020-05-31 23:11:59 +02004984 buf_T *buf, // quickfix window buffer
4985 linenr_T lnum,
4986 qfline_T *qfp,
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02004987 char_u *dirname,
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004988 int first_bufline,
Bram Moolenaar00e260b2020-06-11 19:35:52 +02004989 char_u *qftf_str)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004990{
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004991 buf_T *errbuf;
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004992 garray_T *gap;
Yegappan Lakshmananf8412c92022-10-13 11:59:22 +01004993
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01004994 gap = qfga_get();
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02004995
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004996 // If the 'quickfixtextfunc' function returned a non-empty custom string
Bram Moolenaard43906d2020-07-20 21:31:32 +02004997 // for this entry, then use it.
4998 if (qftf_str != NULL && *qftf_str != NUL)
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00004999 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005000 ga_concat(gap, qftf_str);
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00005001 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005002 else
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005003 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02005004 if (qfp->qf_module != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005005 ga_concat(gap, qfp->qf_module);
Bram Moolenaar858ba062020-05-31 23:11:59 +02005006 else if (qfp->qf_fnum != 0
5007 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
5008 && errbuf->b_fname != NULL)
5009 {
5010 if (qfp->qf_type == 1) // :helpgrep
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005011 ga_concat(gap, gettail(errbuf->b_fname));
Bram Moolenaar858ba062020-05-31 23:11:59 +02005012 else
5013 {
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02005014 // Shorten the file name if not done already.
5015 // For optimization, do this only for the first entry in a
5016 // buffer.
5017 if (first_bufline && (errbuf->b_sfname == NULL
5018 || mch_isFullName(errbuf->b_sfname)))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005019 {
5020 if (*dirname == NUL)
5021 mch_dirname(dirname, MAXPATHL);
5022 shorten_buf_fname(errbuf, dirname, FALSE);
5023 }
Austin Chang29822992024-10-03 10:50:05 +02005024 if (qfp->qf_fname == NULL)
5025 ga_concat(gap, errbuf->b_fname);
5026 else
5027 ga_concat(gap, qfp->qf_fname);
Bram Moolenaar858ba062020-05-31 23:11:59 +02005028 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02005029 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02005030
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005031 ga_append(gap, '|');
Bram Moolenaar858ba062020-05-31 23:11:59 +02005032
5033 if (qfp->qf_lnum > 0)
5034 {
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005035 qf_range_text(gap, qfp);
5036 ga_concat(gap, qf_types(qfp->qf_type, qfp->qf_nr));
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005037 }
Bram Moolenaar858ba062020-05-31 23:11:59 +02005038 else if (qfp->qf_pattern != NULL)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01005039 qf_fmt_text(gap, qfp->qf_pattern);
5040 ga_append(gap, '|');
5041 ga_append(gap, ' ');
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005042
Bram Moolenaar858ba062020-05-31 23:11:59 +02005043 // Remove newlines and leading whitespace from the text.
5044 // For an unrecognized line keep the indent, the compiler may
5045 // mark a word with ^^^^.
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00005046 qf_fmt_text(gap, gap->ga_len > 3 ? skipwhite(qfp->qf_text)
5047 : qfp->qf_text);
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005048 }
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005049
Shane Harper5bf04282023-06-07 19:09:57 +01005050 ga_append(gap, NUL);
Bram Moolenaar2f7bfe62022-11-13 12:54:50 +00005051 if (ml_append_buf(buf, lnum, gap->ga_data, gap->ga_len, FALSE) == FAIL)
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005052 return FAIL;
5053
5054 return OK;
5055}
5056
Bram Moolenaard43906d2020-07-20 21:31:32 +02005057/*
5058 * Call the 'quickfixtextfunc' function to get the list of lines to display in
5059 * the quickfix window for the entries 'start_idx' to 'end_idx'.
5060 */
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005061 static list_T *
5062call_qftf_func(qf_list_T *qfl, int qf_winid, long start_idx, long end_idx)
5063{
Bram Moolenaard43906d2020-07-20 21:31:32 +02005064 callback_T *cb = &qftf_cb;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005065 list_T *qftf_list = NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01005066 static int recursive = FALSE;
5067
5068 if (recursive)
5069 return NULL; // this doesn't work properly recursively
5070 recursive = TRUE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005071
5072 // If 'quickfixtextfunc' is set, then use the user-supplied function to get
5073 // the text to display. Use the local value of 'quickfixtextfunc' if it is
5074 // set.
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01005075 if (qfl->qf_qftf_cb.cb_name != NULL)
5076 cb = &qfl->qf_qftf_cb;
5077 if (cb->cb_name != NULL)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005078 {
5079 typval_T args[1];
5080 dict_T *d;
Bram Moolenaard43906d2020-07-20 21:31:32 +02005081 typval_T rettv;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005082
5083 // create the dict argument
5084 if ((d = dict_alloc_lock(VAR_FIXED)) == NULL)
Bram Moolenaard6c67622022-08-24 20:07:22 +01005085 {
5086 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005087 return NULL;
Bram Moolenaard6c67622022-08-24 20:07:22 +01005088 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005089 dict_add_number(d, "quickfix", (long)IS_QF_LIST(qfl));
5090 dict_add_number(d, "winid", (long)qf_winid);
5091 dict_add_number(d, "id", (long)qfl->qf_id);
5092 dict_add_number(d, "start_idx", start_idx);
5093 dict_add_number(d, "end_idx", end_idx);
5094 ++d->dv_refcount;
5095 args[0].v_type = VAR_DICT;
5096 args[0].vval.v_dict = d;
5097
Bram Moolenaard43906d2020-07-20 21:31:32 +02005098 qftf_list = NULL;
5099 if (call_callback(cb, 0, &rettv, 1, args) != FAIL)
5100 {
5101 if (rettv.v_type == VAR_LIST)
5102 {
5103 qftf_list = rettv.vval.v_list;
5104 qftf_list->lv_refcount++;
5105 }
5106 clear_tv(&rettv);
5107 }
5108 dict_unref(d);
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005109 }
5110
Bram Moolenaard6c67622022-08-24 20:07:22 +01005111 recursive = FALSE;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005112 return qftf_list;
5113}
5114
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 * Fill current buffer with quickfix errors, replacing any previous contents.
5117 * curbuf must be the quickfix buffer!
Bram Moolenaar864293a2016-06-02 13:40:04 +02005118 * If "old_last" is not NULL append the items after this one.
5119 * When "old_last" is NULL then "buf" must equal "curbuf"! Because
5120 * ml_delete() is used and autocommands will be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 */
5122 static void
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02005123qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int qf_winid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124{
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005125 linenr_T lnum;
5126 qfline_T *qfp;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005127 int old_KeyTyped = KeyTyped;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005128 list_T *qftf_list = NULL;
5129 listitem_T *qftf_li = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130
Bram Moolenaar864293a2016-06-02 13:40:04 +02005131 if (old_last == NULL)
5132 {
zeertzjqc7a8eb52024-05-08 20:22:40 +02005133 win_T *wp;
5134 tabpage_T *tp;
5135
Bram Moolenaar864293a2016-06-02 13:40:04 +02005136 if (buf != curbuf)
5137 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005138 internal_error("qf_fill_buffer()");
Bram Moolenaar864293a2016-06-02 13:40:04 +02005139 return;
5140 }
5141
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005142 // delete all existing lines
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01005143 //
5144 // Note: we cannot store undo information, because
5145 // qf buffer is usually not allowed to be modified.
5146 //
5147 // So we need to clean up undo information
5148 // otherwise autocommands may invalidate the undo stack
Bram Moolenaar864293a2016-06-02 13:40:04 +02005149 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +02005150 (void)ml_delete((linenr_T)1);
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01005151
zeertzjqc7a8eb52024-05-08 20:22:40 +02005152 FOR_ALL_TAB_WINDOWS(tp, wp)
5153 if (wp->w_buffer == curbuf)
5154 wp->w_skipcol = 0;
5155
Christian Brabandtf0d3d4a2024-02-15 20:15:04 +01005156 // Remove all undo information
Christian Brabandt9071ed82024-02-15 20:17:37 +01005157 u_clearallandblockfree(curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005160 // Check if there is anything to display
Bram Moolenaar4f1b0832022-08-29 20:45:16 +01005161 if (qfl != NULL && qfl->qf_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005163 char_u dirname[MAXPATHL];
Bram Moolenaard43906d2020-07-20 21:31:32 +02005164 int invalid_val = FALSE;
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02005165 int prev_bufnr = -1;
Bram Moolenaara796d462018-05-01 14:30:36 +02005166
5167 *dirname = NUL;
5168
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005169 // Add one line for each error
Bram Moolenaar2ce77902020-11-14 13:15:24 +01005170 if (old_last == NULL)
Bram Moolenaar864293a2016-06-02 13:40:04 +02005171 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005172 qfp = qfl->qf_start;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005173 lnum = 0;
5174 }
5175 else
5176 {
Bram Moolenaar2ce77902020-11-14 13:15:24 +01005177 if (old_last->qf_next != NULL)
5178 qfp = old_last->qf_next;
5179 else
5180 qfp = old_last;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005181 lnum = buf->b_ml.ml_line_count;
5182 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005183
5184 qftf_list = call_qftf_func(qfl, qf_winid, (long)(lnum + 1),
5185 (long)qfl->qf_count);
5186 if (qftf_list != NULL)
5187 qftf_li = qftf_list->lv_first;
5188
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005189 while (lnum < qfl->qf_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005191 char_u *qftf_str = NULL;
5192
Bram Moolenaard43906d2020-07-20 21:31:32 +02005193 // Use the text supplied by the user defined function (if any).
5194 // If the returned value is not string, then ignore the rest
5195 // of the returned values and use the default.
5196 if (qftf_li != NULL && !invalid_val)
5197 {
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005198 qftf_str = tv_get_string_chk(&qftf_li->li_tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02005199 if (qftf_str == NULL)
5200 invalid_val = TRUE;
5201 }
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005202
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02005203 if (qf_buf_add_line(buf, lnum, qfp, dirname,
5204 prev_bufnr != qfp->qf_fnum, qftf_str) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 break;
Bram Moolenaar6053f2d2018-05-21 16:56:38 +02005206
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02005207 prev_bufnr = qfp->qf_fnum;
Bram Moolenaar864293a2016-06-02 13:40:04 +02005208 ++lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 qfp = qfp->qf_next;
Bram Moolenaar83e6d7a2016-06-02 22:08:05 +02005210 if (qfp == NULL)
5211 break;
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005212
5213 if (qftf_li != NULL)
5214 qftf_li = qftf_li->li_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
Bram Moolenaar864293a2016-06-02 13:40:04 +02005216
5217 if (old_last == NULL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005218 // Delete the empty line which is now at the end
Bram Moolenaarca70c072020-05-30 20:30:46 +02005219 (void)ml_delete(lnum + 1);
Yegappan Lakshmanand8cd6f72022-10-16 11:30:48 +01005220
5221 qfga_clear();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
5223
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005224 // correct cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 check_lnums(TRUE);
5226
Bram Moolenaar864293a2016-06-02 13:40:04 +02005227 if (old_last == NULL)
5228 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005229 // Set the 'filetype' to "qf" each time after filling the buffer.
5230 // This resembles reading a file into a buffer, it's more logical when
5231 // using autocommands.
Bram Moolenaar18141832017-06-25 21:17:25 +02005232 ++curbuf_lock;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005233 set_option_value_give_err((char_u *)"ft",
5234 0L, (char_u *)"qf", OPT_LOCAL);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005235 curbuf->b_p_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
zeertzjq5bf6c212024-03-31 18:41:27 +02005237 curbuf->b_keep_filetype = TRUE; // don't detect 'filetype'
Bram Moolenaar864293a2016-06-02 13:40:04 +02005238 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 FALSE, curbuf);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005240 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 FALSE, curbuf);
zeertzjq5bf6c212024-03-31 18:41:27 +02005242 curbuf->b_keep_filetype = FALSE;
Bram Moolenaar18141832017-06-25 21:17:25 +02005243 --curbuf_lock;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005244
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005245 // make sure it will be redrawn
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005246 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar864293a2016-06-02 13:40:04 +02005247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005249 // Restore KeyTyped, setting 'filetype' may reset it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 KeyTyped = old_KeyTyped;
5251}
5252
Bram Moolenaar18cebf42018-05-08 22:31:37 +02005253/*
5254 * For every change made to the quickfix list, update the changed tick.
5255 */
Bram Moolenaarb254af32017-12-18 19:48:58 +01005256 static void
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005257qf_list_changed(qf_list_T *qfl)
Bram Moolenaarb254af32017-12-18 19:48:58 +01005258{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005259 qfl->qf_changedtick++;
Bram Moolenaarb254af32017-12-18 19:48:58 +01005260}
5261
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262/*
Bram Moolenaar531b9a32018-07-03 16:54:23 +02005263 * Return the quickfix/location list number with the given identifier.
5264 * Returns -1 if list is not found.
5265 */
5266 static int
5267qf_id2nr(qf_info_T *qi, int_u qfid)
5268{
5269 int qf_idx;
5270
5271 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
5272 if (qi->qf_lists[qf_idx].qf_id == qfid)
5273 return qf_idx;
5274 return INVALID_QFIDX;
5275}
5276
5277/*
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005278 * If the current list is not "save_qfid" and we can find the list with that ID
5279 * then make it the current list.
5280 * This is used when autocommands may have changed the current list.
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005281 * Returns OK if successfully restored the list. Returns FAIL if the list with
5282 * the specified identifier (save_qfid) is not found in the stack.
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005283 */
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005284 static int
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005285qf_restore_list(qf_info_T *qi, int_u save_qfid)
5286{
5287 int curlist;
5288
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00005289 if (qf_get_curlist(qi)->qf_id == save_qfid)
5290 return OK;
5291
5292 curlist = qf_id2nr(qi, save_qfid);
5293 if (curlist < 0)
5294 // list is not present
5295 return FAIL;
5296 qi->qf_curlist = curlist;
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005297 return OK;
Bram Moolenaar38efd1d2018-08-09 21:52:24 +02005298}
5299
5300/*
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005301 * Jump to the first entry if there is one.
5302 */
5303 static void
5304qf_jump_first(qf_info_T *qi, int_u save_qfid, int forceit)
5305{
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005306 if (qf_restore_list(qi, save_qfid) == FAIL)
5307 return;
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005308
Colin Kennedy21570352024-03-03 16:16:47 +01005309
5310 if (!check_can_set_curbuf_forceit(forceit))
5311 return;
5312
5313
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02005314 // Autocommands might have cleared the list, check for that.
Bram Moolenaar0398e002019-03-21 21:12:49 +01005315 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02005316 qf_jump(qi, 0, 0, forceit);
5317}
5318
5319/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00005320 * Return TRUE when using ":vimgrep" for ":grep".
5321 */
5322 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005323grep_internal(cmdidx_T cmdidx)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005324{
Bram Moolenaar754b5602006-02-09 23:53:20 +00005325 return ((cmdidx == CMD_grep
5326 || cmdidx == CMD_lgrep
5327 || cmdidx == CMD_grepadd
5328 || cmdidx == CMD_lgrepadd)
Bram Moolenaar86b68352004-12-27 21:59:20 +00005329 && STRCMP("internal",
5330 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0);
5331}
5332
5333/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005334 * Return the make/grep autocmd name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 */
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005336 static char_u *
5337make_get_auname(cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005339 switch (cmdidx)
Bram Moolenaard88e02d2011-04-28 17:27:09 +02005340 {
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005341 case CMD_make: return (char_u *)"make";
5342 case CMD_lmake: return (char_u *)"lmake";
5343 case CMD_grep: return (char_u *)"grep";
5344 case CMD_lgrep: return (char_u *)"lgrep";
5345 case CMD_grepadd: return (char_u *)"grepadd";
5346 case CMD_lgrepadd: return (char_u *)"lgrepadd";
5347 default: return NULL;
Bram Moolenaard88e02d2011-04-28 17:27:09 +02005348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349}
5350
5351/*
5352 * Return the name for the errorfile, in allocated memory.
5353 * Find a new unique name when 'makeef' contains "##".
5354 * Returns NULL for error.
5355 */
5356 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01005357get_mef_name(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358{
5359 char_u *p;
5360 char_u *name;
5361 static int start = -1;
5362 static int off = 0;
5363#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02005364 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365#endif
5366
5367 if (*p_mef == NUL)
5368 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02005369 name = vim_tempname('e', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 if (name == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005371 emsg(_(e_cant_get_temp_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 return name;
5373 }
5374
5375 for (p = p_mef; *p; ++p)
5376 if (p[0] == '#' && p[1] == '#')
5377 break;
5378
5379 if (*p == NUL)
5380 return vim_strsave(p_mef);
5381
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005382 // Keep trying until the name doesn't exist yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 for (;;)
5384 {
5385 if (start == -1)
5386 start = mch_get_pid();
5387 else
5388 off += 19;
5389
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005390 name = alloc_id(STRLEN(p_mef) + 30, aid_qf_mef_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 if (name == NULL)
5392 break;
5393 STRCPY(name, p_mef);
5394 sprintf((char *)name + (p - p_mef), "%d%d", start, off);
5395 STRCAT(name, p + 2);
5396 if (mch_getperm(name) < 0
5397#ifdef HAVE_LSTAT
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005398 // Don't accept a symbolic link, it's a security risk.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 && mch_lstat((char *)name, &sb) < 0
5400#endif
5401 )
5402 break;
5403 vim_free(name);
5404 }
5405 return name;
5406}
5407
5408/*
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005409 * Form the complete command line to invoke 'make'/'grep'. Quote the command
5410 * using 'shellquote' and append 'shellpipe'. Echo the fully formed command.
5411 */
5412 static char_u *
5413make_get_fullcmd(char_u *makecmd, char_u *fname)
5414{
5415 char_u *cmd;
5416 unsigned len;
5417
5418 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(makecmd) + 1;
5419 if (*p_sp != NUL)
5420 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005421 cmd = alloc_id(len, aid_qf_makecmd);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005422 if (cmd == NULL)
5423 return NULL;
5424 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)makecmd,
5425 (char *)p_shq);
5426
5427 // If 'shellpipe' empty: don't redirect to 'errorfile'.
5428 if (*p_sp != NUL)
5429 append_redir(cmd, len, p_sp, fname);
5430
5431 // Display the fully formed command. Output a newline if there's something
5432 // else than the :make command that was typed (in which case the cursor is
5433 // in column 0).
5434 if (msg_col == 0)
5435 msg_didout = FALSE;
5436 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005437 msg_puts(":!");
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005438 msg_outtrans(cmd); // show what we are doing
5439
5440 return cmd;
5441}
5442
5443/*
5444 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd"
5445 */
5446 void
5447ex_make(exarg_T *eap)
5448{
5449 char_u *fname;
5450 char_u *cmd;
5451 char_u *enc = NULL;
5452 win_T *wp = NULL;
64-bitman88d41ab2025-04-06 17:20:39 +02005453 qf_info_T *qi = ql_info;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005454 int res;
5455 char_u *au_name = NULL;
5456 int_u save_qfid;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005457 char_u *errorformat = p_efm;
5458 int newlist = TRUE;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005459
5460 // Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal".
5461 if (grep_internal(eap->cmdidx))
5462 {
5463 ex_vimgrep(eap);
5464 return;
5465 }
5466
5467 au_name = make_get_auname(eap->cmdidx);
5468 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
5469 curbuf->b_fname, TRUE, curbuf))
5470 {
5471#ifdef FEAT_EVAL
5472 if (aborting())
5473 return;
5474#endif
5475 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005476 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005477
5478 if (is_loclist_cmd(eap->cmdidx))
5479 wp = curwin;
5480
5481 autowrite_all();
5482 fname = get_mef_name();
5483 if (fname == NULL)
5484 return;
5485 mch_remove(fname); // in case it's not unique
5486
5487 cmd = make_get_fullcmd(eap->arg, fname);
5488 if (cmd == NULL)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005489 {
5490 vim_free(fname);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005491 return;
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +00005492 }
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005493
5494 // let the shell know if we are redirecting output or not
5495 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0);
5496
5497#ifdef AMIGA
5498 out_flush();
5499 // read window status report and redraw before message
5500 (void)char_avail();
5501#endif
5502
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005503 incr_quickfix_busy();
5504
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005505 if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake)
glepnir7b9eb632025-05-16 19:49:23 +02005506 errorformat = *curbuf->b_p_gefm != NUL ? curbuf->b_p_gefm : p_gefm;
Yegappan Lakshmanan2f87a992022-03-02 20:29:35 +00005507 if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
5508 newlist = FALSE;
5509
5510 res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep),
5511 enc);
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005512 if (wp != NULL)
5513 {
5514 qi = GET_LOC_LIST(wp);
5515 if (qi == NULL)
5516 goto cleanup;
5517 }
64-bitman88d41ab2025-04-06 17:20:39 +02005518 else if (qi == NULL)
5519 goto cleanup;
5520
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005521 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005522 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005523
5524 // Remember the current quickfix list identifier, so that we can
5525 // check for autocommands changing the current quickfix list.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005526 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005527 if (au_name != NULL)
5528 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
5529 curbuf->b_fname, TRUE, curbuf);
5530 if (res > 0 && !eap->forceit && qflist_valid(wp, save_qfid))
5531 // display the first error
5532 qf_jump_first(qi, save_qfid, FALSE);
5533
5534cleanup:
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02005535 decr_quickfix_busy();
Bram Moolenaarb434ae22018-09-28 23:09:55 +02005536 mch_remove(fname);
5537 vim_free(fname);
5538 vim_free(cmd);
5539}
5540
5541/*
Bram Moolenaar25190db2019-05-04 15:05:28 +02005542 * Returns the number of entries in the current quickfix/location list.
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005543 */
5544 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005545qf_get_size(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005546{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005547 qf_info_T *qi;
Bram Moolenaar25190db2019-05-04 15:05:28 +02005548
5549 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5550 return 0;
5551 return qf_get_curlist(qi)->qf_count;
5552}
5553
5554/*
5555 * Returns the number of valid entries in the current quickfix/location list.
5556 */
5557 int
5558qf_get_valid_size(exarg_T *eap)
5559{
5560 qf_info_T *qi;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02005561 qf_list_T *qfl;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005562 qfline_T *qfp;
5563 int i, sz = 0;
5564 int prev_fnum = 0;
5565
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005566 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5567 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005568
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005569 qfl = qf_get_curlist(qi);
Bram Moolenaara16123a2019-03-28 20:31:07 +01005570 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005571 {
5572 if (qfp->qf_valid)
5573 {
5574 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005575 sz++; // Count all valid entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005576 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5577 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005578 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005579 sz++;
5580 prev_fnum = qfp->qf_fnum;
5581 }
5582 }
5583 }
5584
5585 return sz;
5586}
5587
5588/*
5589 * Returns the current index of the quickfix/location list.
5590 * Returns 0 if there is an error.
5591 */
5592 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005593qf_get_cur_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005594{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005595 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005596
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005597 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5598 return 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005599
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005600 return qf_get_curlist(qi)->qf_index;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005601}
5602
5603/*
5604 * Returns the current index in the quickfix/location list (counting only valid
5605 * entries). If no valid entries are in the list, then returns 1.
5606 */
5607 int
Bram Moolenaar05540972016-01-30 20:31:25 +01005608qf_get_cur_valid_idx(exarg_T *eap)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005609{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005610 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005611 qf_list_T *qfl;
5612 qfline_T *qfp;
5613 int i, eidx = 0;
5614 int prev_fnum = 0;
5615
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005616 if ((qi = qf_cmd_get_stack(eap, FALSE)) == NULL)
5617 return 1;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005618
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005619 qfl = qf_get_curlist(qi);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005620 qfp = qfl->qf_start;
5621
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005622 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005623 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005624 return 1;
5625
5626 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next)
5627 {
5628 if (qfp->qf_valid)
5629 {
5630 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
5631 {
5632 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5633 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005634 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005635 eidx++;
5636 prev_fnum = qfp->qf_fnum;
5637 }
5638 }
5639 else
5640 eidx++;
5641 }
5642 }
5643
5644 return eidx ? eidx : 1;
5645}
5646
5647/*
5648 * Get the 'n'th valid error entry in the quickfix or location list.
5649 * Used by :cdo, :ldo, :cfdo and :lfdo commands.
5650 * For :cdo and :ldo returns the 'n'th valid error entry.
5651 * For :cfdo and :lfdo returns the 'n'th valid file entry.
5652 */
5653 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02005654qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005655{
Bram Moolenaar95946f12019-03-31 15:31:59 +02005656 qfline_T *qfp;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005657 int i, eidx;
5658 int prev_fnum = 0;
5659
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005660 // check if the list has valid errors
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005661 if (!qf_list_has_valid_entries(qfl))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005662 return 1;
5663
Bram Moolenaar95946f12019-03-31 15:31:59 +02005664 eidx = 0;
5665 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005666 {
5667 if (qfp->qf_valid)
5668 {
5669 if (fdo)
5670 {
5671 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum)
5672 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005673 // Count the number of files
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005674 eidx++;
5675 prev_fnum = qfp->qf_fnum;
5676 }
5677 }
5678 else
5679 eidx++;
5680 }
5681
5682 if (eidx == n)
5683 break;
5684 }
5685
5686 if (i <= qfl->qf_count)
5687 return i;
5688 else
5689 return 1;
5690}
5691
5692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 * ":cc", ":crewind", ":cfirst" and ":clast".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005694 * ":ll", ":lrewind", ":lfirst" and ":llast".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005695 * ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 */
5697 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005698ex_cc(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005700 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005701 int errornr;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005702
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005703 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5704 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005705
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005706 if (eap->addr_count > 0)
5707 errornr = (int)eap->line2;
5708 else
5709 {
Bram Moolenaar39665952018-08-15 20:59:48 +02005710 switch (eap->cmdidx)
5711 {
5712 case CMD_cc: case CMD_ll:
5713 errornr = 0;
5714 break;
5715 case CMD_crewind: case CMD_lrewind: case CMD_cfirst:
5716 case CMD_lfirst:
5717 errornr = 1;
5718 break;
5719 default:
5720 errornr = 32767;
5721 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005722 }
5723
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02005724 // For cdo and ldo commands, jump to the nth valid error.
5725 // For cfdo and lfdo commands, jump to the nth valid file entry.
Bram Moolenaar55b69262017-08-13 13:42:01 +02005726 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
5727 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01005728 errornr = qf_get_nth_valid_entry(qf_get_curlist(qi),
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005729 eap->addr_count > 0 ? (int)eap->line1 : 1,
5730 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo);
5731
5732 qf_jump(qi, 0, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733}
5734
5735/*
5736 * ":cnext", ":cnfile", ":cNext" and ":cprevious".
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005737 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile".
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005738 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 */
5740 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005741ex_cnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742{
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005743 qf_info_T *qi;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005744 int errornr;
Bram Moolenaar39665952018-08-15 20:59:48 +02005745 int dir;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005746
Bram Moolenaar87f59b02019-04-04 14:04:11 +02005747 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
5748 return;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005749
Bram Moolenaar55b69262017-08-13 13:42:01 +02005750 if (eap->addr_count > 0
5751 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo
5752 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo))
Bram Moolenaaraa23b372015-09-08 18:46:31 +02005753 errornr = (int)eap->line2;
5754 else
5755 errornr = 1;
5756
Bram Moolenaar39665952018-08-15 20:59:48 +02005757 // Depending on the command jump to either next or previous entry/file.
5758 switch (eap->cmdidx)
5759 {
5760 case CMD_cnext: case CMD_lnext: case CMD_cdo: case CMD_ldo:
5761 dir = FORWARD;
5762 break;
5763 case CMD_cprevious: case CMD_lprevious: case CMD_cNext:
5764 case CMD_lNext:
5765 dir = BACKWARD;
5766 break;
5767 case CMD_cnfile: case CMD_lnfile: case CMD_cfdo: case CMD_lfdo:
5768 dir = FORWARD_FILE;
5769 break;
5770 case CMD_cpfile: case CMD_lpfile: case CMD_cNfile: case CMD_lNfile:
5771 dir = BACKWARD_FILE;
5772 break;
5773 default:
5774 dir = FORWARD;
5775 break;
5776 }
5777
5778 qf_jump(qi, dir, errornr, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779}
5780
5781/*
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005782 * Find the first entry in the quickfix list 'qfl' from buffer 'bnr'.
5783 * The index of the entry is stored in 'errornr'.
5784 * Returns NULL if an entry is not found.
5785 */
5786 static qfline_T *
5787qf_find_first_entry_in_buf(qf_list_T *qfl, int bnr, int *errornr)
5788{
5789 qfline_T *qfp = NULL;
5790 int idx = 0;
5791
5792 // Find the first entry in this file
5793 FOR_ALL_QFL_ITEMS(qfl, qfp, idx)
5794 if (qfp->qf_fnum == bnr)
5795 break;
5796
5797 *errornr = idx;
5798 return qfp;
5799}
5800
5801/*
5802 * Find the first quickfix entry on the same line as 'entry'. Updates 'errornr'
5803 * with the error number for the first entry. Assumes the entries are sorted in
5804 * the quickfix list by line number.
5805 */
5806 static qfline_T *
5807qf_find_first_entry_on_line(qfline_T *entry, int *errornr)
5808{
5809 while (!got_int
5810 && entry->qf_prev != NULL
5811 && entry->qf_fnum == entry->qf_prev->qf_fnum
5812 && entry->qf_lnum == entry->qf_prev->qf_lnum)
5813 {
5814 entry = entry->qf_prev;
5815 --*errornr;
5816 }
5817
5818 return entry;
5819}
5820
5821/*
5822 * Find the last quickfix entry on the same line as 'entry'. Updates 'errornr'
5823 * with the error number for the last entry. Assumes the entries are sorted in
5824 * the quickfix list by line number.
5825 */
5826 static qfline_T *
5827qf_find_last_entry_on_line(qfline_T *entry, int *errornr)
5828{
5829 while (!got_int &&
5830 entry->qf_next != NULL
5831 && entry->qf_fnum == entry->qf_next->qf_fnum
5832 && entry->qf_lnum == entry->qf_next->qf_lnum)
5833 {
5834 entry = entry->qf_next;
5835 ++*errornr;
5836 }
5837
5838 return entry;
5839}
5840
5841/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005842 * Returns TRUE if the specified quickfix entry is
5843 * after the given line (linewise is TRUE)
5844 * or after the line and column.
5845 */
5846 static int
5847qf_entry_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5848{
5849 if (linewise)
5850 return qfp->qf_lnum > pos->lnum;
5851 else
5852 return (qfp->qf_lnum > pos->lnum ||
5853 (qfp->qf_lnum == pos->lnum && qfp->qf_col > pos->col));
5854}
5855
5856/*
5857 * Returns TRUE if the specified quickfix entry is
5858 * before the given line (linewise is TRUE)
5859 * or before the line and column.
5860 */
5861 static int
5862qf_entry_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5863{
5864 if (linewise)
5865 return qfp->qf_lnum < pos->lnum;
5866 else
5867 return (qfp->qf_lnum < pos->lnum ||
5868 (qfp->qf_lnum == pos->lnum && qfp->qf_col < pos->col));
5869}
5870
5871/*
5872 * Returns TRUE if the specified quickfix entry is
5873 * on or after the given line (linewise is TRUE)
5874 * or on or after the line and column.
5875 */
5876 static int
5877qf_entry_on_or_after_pos(qfline_T *qfp, pos_T *pos, int linewise)
5878{
5879 if (linewise)
5880 return qfp->qf_lnum >= pos->lnum;
5881 else
5882 return (qfp->qf_lnum > pos->lnum ||
5883 (qfp->qf_lnum == pos->lnum && qfp->qf_col >= pos->col));
5884}
5885
5886/*
5887 * Returns TRUE if the specified quickfix entry is
5888 * on or before the given line (linewise is TRUE)
5889 * or on or before the line and column.
5890 */
5891 static int
5892qf_entry_on_or_before_pos(qfline_T *qfp, pos_T *pos, int linewise)
5893{
5894 if (linewise)
5895 return qfp->qf_lnum <= pos->lnum;
5896 else
5897 return (qfp->qf_lnum < pos->lnum ||
5898 (qfp->qf_lnum == pos->lnum && qfp->qf_col <= pos->col));
5899}
5900
5901/*
5902 * Find the first quickfix entry after position 'pos' in buffer 'bnr'.
5903 * If 'linewise' is TRUE, returns the entry after the specified line and treats
5904 * multiple entries on a single line as one. Otherwise returns the entry after
5905 * the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005906 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5907 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005908 * Returns NULL if an entry is not found after 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005909 */
5910 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005911qf_find_entry_after_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005912 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005913 pos_T *pos,
5914 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005915 qfline_T *qfp,
5916 int *errornr)
5917{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005918 if (qf_entry_after_pos(qfp, pos, linewise))
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005919 // First entry is after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005920 return qfp;
5921
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005922 // Find the entry just before or at the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005923 while (qfp->qf_next != NULL
5924 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005925 && qf_entry_on_or_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005926 {
5927 qfp = qfp->qf_next;
5928 ++*errornr;
5929 }
5930
5931 if (qfp->qf_next == NULL || qfp->qf_next->qf_fnum != bnr)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005932 // No entries found after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005933 return NULL;
5934
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005935 // Use the entry just after position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005936 qfp = qfp->qf_next;
5937 ++*errornr;
5938
5939 return qfp;
5940}
5941
5942/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005943 * Find the first quickfix entry before position 'pos' in buffer 'bnr'.
5944 * If 'linewise' is TRUE, returns the entry before the specified line and
5945 * treats multiple entries on a single line as one. Otherwise returns the entry
5946 * before the specified line and column.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005947 * 'qfp' points to the very first entry in the buffer and 'errornr' is the
5948 * index of the very first entry in the quickfix list.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005949 * Returns NULL if an entry is not found before 'pos'.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005950 */
5951 static qfline_T *
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005952qf_find_entry_before_pos(
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005953 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005954 pos_T *pos,
5955 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005956 qfline_T *qfp,
5957 int *errornr)
5958{
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005959 // Find the entry just before the position 'pos'
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005960 while (qfp->qf_next != NULL
5961 && qfp->qf_next->qf_fnum == bnr
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005962 && qf_entry_before_pos(qfp->qf_next, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005963 {
5964 qfp = qfp->qf_next;
5965 ++*errornr;
5966 }
5967
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005968 if (qf_entry_on_or_after_pos(qfp, pos, linewise))
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005969 return NULL;
5970
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005971 if (linewise)
5972 // If multiple entries are on the same line, then use the first entry
5973 qfp = qf_find_first_entry_on_line(qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005974
5975 return qfp;
5976}
5977
5978/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005979 * Find a quickfix entry in 'qfl' closest to position 'pos' in buffer 'bnr' in
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005980 * the direction 'dir'.
5981 */
5982 static qfline_T *
5983qf_find_closest_entry(
5984 qf_list_T *qfl,
5985 int bnr,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005986 pos_T *pos,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005987 int dir,
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005988 int linewise,
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005989 int *errornr)
5990{
5991 qfline_T *qfp;
5992
5993 *errornr = 0;
5994
5995 // Find the first entry in this file
5996 qfp = qf_find_first_entry_in_buf(qfl, bnr, errornr);
5997 if (qfp == NULL)
5998 return NULL; // no entry in this file
5999
6000 if (dir == FORWARD)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006001 qfp = qf_find_entry_after_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006002 else
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006003 qfp = qf_find_entry_before_pos(bnr, pos, linewise, qfp, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006004
6005 return qfp;
6006}
6007
6008/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006009 * Get the nth quickfix entry below the specified entry. Searches forward in
6010 * the list. If linewise is TRUE, then treat multiple entries on a single line
6011 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006012 */
Bram Moolenaar64416122019-06-07 21:37:13 +02006013 static void
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006014qf_get_nth_below_entry(qfline_T *entry_arg, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006015{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006016 qfline_T *entry = entry_arg;
6017
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006018 while (n-- > 0 && !got_int)
6019 {
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006020 int first_errornr = *errornr;
6021
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006022 if (linewise)
6023 // Treat all the entries on the same line in this file as one
6024 entry = qf_find_last_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006025
6026 if (entry->qf_next == NULL
6027 || entry->qf_next->qf_fnum != entry->qf_fnum)
6028 {
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006029 if (linewise)
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006030 *errornr = first_errornr;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006031 break;
6032 }
6033
6034 entry = entry->qf_next;
6035 ++*errornr;
6036 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006037}
6038
6039/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006040 * Get the nth quickfix entry above the specified entry. Searches backwards in
6041 * the list. If linewise is TRUE, then treat multiple entries on a single line
6042 * as one.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006043 */
Bram Moolenaar64416122019-06-07 21:37:13 +02006044 static void
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006045qf_get_nth_above_entry(qfline_T *entry, int n, int linewise, int *errornr)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006046{
6047 while (n-- > 0 && !got_int)
6048 {
6049 if (entry->qf_prev == NULL
6050 || entry->qf_prev->qf_fnum != entry->qf_fnum)
6051 break;
6052
6053 entry = entry->qf_prev;
6054 --*errornr;
6055
6056 // If multiple entries are on the same line, then use the first entry
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006057 if (linewise)
6058 entry = qf_find_first_entry_on_line(entry, errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006059 }
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006060}
6061
6062/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006063 * Find the n'th quickfix entry adjacent to position 'pos' in buffer 'bnr' in
6064 * the specified direction. Returns the error number in the quickfix list or 0
6065 * if an entry is not found.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006066 */
6067 static int
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006068qf_find_nth_adj_entry(
6069 qf_list_T *qfl,
6070 int bnr,
6071 pos_T *pos,
6072 int n,
6073 int dir,
6074 int linewise)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006075{
6076 qfline_T *adj_entry;
6077 int errornr;
6078
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006079 // Find an entry closest to the specified position
6080 adj_entry = qf_find_closest_entry(qfl, bnr, pos, dir, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006081 if (adj_entry == NULL)
6082 return 0;
6083
6084 if (--n > 0)
6085 {
6086 // Go to the n'th entry in the current buffer
6087 if (dir == FORWARD)
Bram Moolenaar64416122019-06-07 21:37:13 +02006088 qf_get_nth_below_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006089 else
Bram Moolenaar64416122019-06-07 21:37:13 +02006090 qf_get_nth_above_entry(adj_entry, n, linewise, &errornr);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006091 }
6092
6093 return errornr;
6094}
6095
6096/*
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006097 * Jump to a quickfix entry in the current file nearest to the current line or
6098 * current line/col.
6099 * ":cabove", ":cbelow", ":labove", ":lbelow", ":cafter", ":cbefore",
6100 * ":lafter" and ":lbefore" commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006101 */
6102 void
6103ex_cbelow(exarg_T *eap)
6104{
6105 qf_info_T *qi;
6106 qf_list_T *qfl;
6107 int dir;
6108 int buf_has_flag;
6109 int errornr = 0;
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006110 pos_T pos;
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006111
6112 if (eap->addr_count > 0 && eap->line2 <= 0)
6113 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006114 emsg(_(e_invalid_range));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006115 return;
6116 }
6117
6118 // Check whether the current buffer has any quickfix entries
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006119 if (eap->cmdidx == CMD_cabove || eap->cmdidx == CMD_cbelow
6120 || eap->cmdidx == CMD_cbefore || eap->cmdidx == CMD_cafter)
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006121 buf_has_flag = BUF_HAS_QF_ENTRY;
6122 else
6123 buf_has_flag = BUF_HAS_LL_ENTRY;
6124 if (!(curbuf->b_has_qf_entry & buf_has_flag))
6125 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006126 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006127 return;
6128 }
6129
6130 if ((qi = qf_cmd_get_stack(eap, TRUE)) == NULL)
6131 return;
6132
6133 qfl = qf_get_curlist(qi);
6134 // check if the list has valid errors
6135 if (!qf_list_has_valid_entries(qfl))
6136 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006137 emsg(_(e_no_errors));
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006138 return;
6139 }
6140
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006141 if (eap->cmdidx == CMD_cbelow
6142 || eap->cmdidx == CMD_lbelow
6143 || eap->cmdidx == CMD_cafter
6144 || eap->cmdidx == CMD_lafter)
6145 // Forward motion commands
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006146 dir = FORWARD;
6147 else
6148 dir = BACKWARD;
6149
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02006150 pos = curwin->w_cursor;
6151 // A quickfix entry column number is 1 based whereas cursor column
6152 // number is 0 based. Adjust the column number.
6153 pos.col++;
6154 errornr = qf_find_nth_adj_entry(qfl, curbuf->b_fnum, &pos,
6155 eap->addr_count > 0 ? eap->line2 : 0, dir,
6156 eap->cmdidx == CMD_cbelow
6157 || eap->cmdidx == CMD_lbelow
6158 || eap->cmdidx == CMD_cabove
6159 || eap->cmdidx == CMD_labove);
Bram Moolenaar3ff33112019-05-03 21:56:35 +02006160
6161 if (errornr > 0)
6162 qf_jump(qi, 0, errornr, FALSE);
6163 else
6164 emsg(_(e_no_more_items));
6165}
6166
6167/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01006168 * Return the autocmd name for the :cfile Ex commands
6169 */
6170 static char_u *
6171cfile_get_auname(cmdidx_T cmdidx)
6172{
6173 switch (cmdidx)
6174 {
6175 case CMD_cfile: return (char_u *)"cfile";
6176 case CMD_cgetfile: return (char_u *)"cgetfile";
6177 case CMD_caddfile: return (char_u *)"caddfile";
6178 case CMD_lfile: return (char_u *)"lfile";
6179 case CMD_lgetfile: return (char_u *)"lgetfile";
6180 case CMD_laddfile: return (char_u *)"laddfile";
6181 default: return NULL;
6182 }
6183}
6184
6185/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006186 * ":cfile"/":cgetfile"/":caddfile" commands.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006187 * ":lfile"/":lgetfile"/":laddfile" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 */
6189 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006190ex_cfile(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191{
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006192 char_u *enc = NULL;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006193 win_T *wp = NULL;
64-bitman88d41ab2025-04-06 17:20:39 +02006194 qf_info_T *qi = ql_info;
Bram Moolenaar8ec1f852012-03-07 20:13:49 +01006195 char_u *au_name = NULL;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006196 int_u save_qfid = 0; // init for gcc
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006197 int res;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006198
Bram Moolenaara16123a2019-03-28 20:31:07 +01006199 au_name = cfile_get_auname(eap->cmdidx);
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02006200 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6201 NULL, FALSE, curbuf))
6202 {
6203#ifdef FEAT_EVAL
6204 if (aborting())
6205 return;
6206#endif
6207 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01006208
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006209 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc;
Bram Moolenaar9028b102010-07-11 16:58:51 +02006210#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006211 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar9028b102010-07-11 16:58:51 +02006212 {
6213 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg,
Bram Moolenaarc36651b2018-04-29 12:22:56 +02006214 NULL, NULL,
6215 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar9028b102010-07-11 16:58:51 +02006216 if (browse_file == NULL)
6217 return;
6218 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0);
6219 vim_free(browse_file);
6220 }
6221 else
6222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (*eap->arg != NUL)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006224 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006225
Bram Moolenaar39665952018-08-15 20:59:48 +02006226 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01006227 wp = curwin;
6228
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006229 incr_quickfix_busy();
6230
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006231 // This function is used by the :cfile, :cgetfile and :caddfile
6232 // commands.
Colin Kennedy21570352024-03-03 16:16:47 +01006233 // :cfile always creates a new quickfix list and may jump to the
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006234 // first error.
6235 // :cgetfile creates a new quickfix list but doesn't jump to the
6236 // first error.
6237 // :caddfile adds to an existing quickfix list. If there is no
6238 // quickfix list then a new list is created.
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006239 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile
Bram Moolenaar8b62e312018-05-13 15:29:04 +02006240 && eap->cmdidx != CMD_laddfile),
6241 qf_cmdtitle(*eap->cmdlinep), enc);
Bram Moolenaarb254af32017-12-18 19:48:58 +01006242 if (wp != NULL)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006243 {
Bram Moolenaarb254af32017-12-18 19:48:58 +01006244 qi = GET_LOC_LIST(wp);
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006245 if (qi == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006246 {
6247 decr_quickfix_busy();
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006248 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006249 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006250 }
64-bitman88d41ab2025-04-06 17:20:39 +02006251 else if (qi == NULL)
6252 {
6253 decr_quickfix_busy();
6254 return;
6255 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006256 if (res >= 0)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006257 qf_list_changed(qf_get_curlist(qi));
6258 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01006259 if (au_name != NULL)
6260 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf);
Bram Moolenaar0549a1e2018-02-11 15:02:48 +01006261
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006262 // Jump to the first error for a new list and if autocmds didn't
6263 // free the list.
6264 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)
6265 && qflist_valid(wp, save_qfid))
Bram Moolenaar8d8a65e2018-08-07 19:48:08 +02006266 // display the first error
6267 qf_jump_first(qi, save_qfid, eap->forceit);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006268
6269 decr_quickfix_busy();
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006270}
6271
6272/*
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006273 * Return the vimgrep autocmd name.
6274 */
6275 static char_u *
6276vgr_get_auname(cmdidx_T cmdidx)
6277{
6278 switch (cmdidx)
6279 {
6280 case CMD_vimgrep: return (char_u *)"vimgrep";
6281 case CMD_lvimgrep: return (char_u *)"lvimgrep";
6282 case CMD_vimgrepadd: return (char_u *)"vimgrepadd";
6283 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd";
6284 case CMD_grep: return (char_u *)"grep";
6285 case CMD_lgrep: return (char_u *)"lgrep";
6286 case CMD_grepadd: return (char_u *)"grepadd";
6287 case CMD_lgrepadd: return (char_u *)"lgrepadd";
6288 default: return NULL;
6289 }
6290}
6291
6292/*
6293 * Initialize the regmatch used by vimgrep for pattern "s".
6294 */
6295 static void
6296vgr_init_regmatch(regmmatch_T *regmatch, char_u *s)
6297{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006298 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006299 regmatch->regprog = NULL;
6300
6301 if (s == NULL || *s == NUL)
6302 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006303 // Pattern is empty, use last search pattern.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006304 if (last_search_pat() == NULL)
6305 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006306 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006307 return;
6308 }
6309 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
6310 }
6311 else
6312 regmatch->regprog = vim_regcomp(s, RE_MAGIC);
6313
6314 regmatch->rmm_ic = p_ic;
6315 regmatch->rmm_maxcol = 0;
6316}
6317
6318/*
6319 * Display a file name when vimgrep is running.
6320 */
6321 static void
6322vgr_display_fname(char_u *fname)
6323{
6324 char_u *p;
6325
6326 msg_start();
6327 p = msg_strtrunc(fname, TRUE);
6328 if (p == NULL)
6329 msg_outtrans(fname);
6330 else
6331 {
6332 msg_outtrans(p);
6333 vim_free(p);
6334 }
6335 msg_clr_eos();
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006336 msg_didout = FALSE; // overwrite this message
6337 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006338 msg_col = 0;
6339 out_flush();
6340}
6341
6342/*
6343 * Load a dummy buffer to search for a pattern using vimgrep.
6344 */
6345 static buf_T *
6346vgr_load_dummy_buf(
6347 char_u *fname,
6348 char_u *dirname_start,
6349 char_u *dirname_now)
6350{
6351 int save_mls;
6352#if defined(FEAT_SYN_HL)
6353 char_u *save_ei = NULL;
6354#endif
6355 buf_T *buf;
6356
6357#if defined(FEAT_SYN_HL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006358 // Don't do Filetype autocommands to avoid loading syntax and
6359 // indent scripts, a great speed improvement.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006360 save_ei = au_event_disable(",Filetype");
6361#endif
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006362 // Don't use modelines here, it's useless.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006363 save_mls = p_mls;
6364 p_mls = 0;
6365
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006366 // Load file into a buffer, so that 'fileencoding' is detected,
6367 // autocommands applied, etc.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006368 buf = load_dummy_buffer(fname, dirname_start, dirname_now);
6369
6370 p_mls = save_mls;
6371#if defined(FEAT_SYN_HL)
6372 au_event_restore(save_ei);
6373#endif
6374
6375 return buf;
6376}
6377
6378/*
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006379 * Check whether a quickfix/location list is valid. Autocmds may remove or
6380 * change a quickfix list when vimgrep is running. If the list is not found,
6381 * create a new list.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006382 */
6383 static int
6384vgr_qflist_valid(
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006385 win_T *wp,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006386 qf_info_T *qi,
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006387 int_u qfid,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006388 char_u *title)
6389{
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006390 // Verify that the quickfix/location list was not freed by an autocmd
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006391 if (!qflist_valid(wp, qfid))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006392 {
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006393 if (wp != NULL)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006394 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006395 // An autocmd has freed the location list.
Bram Moolenaar4d170af2020-09-13 22:21:22 +02006396 emsg(_(e_current_location_list_was_changed));
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006397 return FALSE;
6398 }
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006399 else
6400 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006401 // Quickfix list is not found, create a new one.
Bram Moolenaare1bb8792018-04-06 22:58:23 +02006402 qf_new_list(qi, title);
6403 return TRUE;
6404 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006405 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006406
Bram Moolenaar90f1e2b2018-08-11 13:36:56 +02006407 if (qf_restore_list(qi, qfid) == FAIL)
6408 return FALSE;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006409
6410 return TRUE;
6411}
6412
6413/*
6414 * Search for a pattern in all the lines in a buffer and add the matching lines
6415 * to a quickfix list.
6416 */
6417 static int
6418vgr_match_buflines(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006419 qf_list_T *qfl,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006420 char_u *fname,
6421 buf_T *buf,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006422 char_u *spat,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006423 regmmatch_T *regmatch,
Bram Moolenaar1c299432018-10-28 14:36:09 +01006424 long *tomatch,
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006425 int duplicate_name,
6426 int flags)
6427{
6428 int found_match = FALSE;
6429 long lnum;
6430 colnr_T col;
Bram Moolenaar551c1ae2021-05-03 18:57:05 +02006431 int pat_len = (int)STRLEN(spat);
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006432 if (pat_len > MAX_FUZZY_MATCHES)
6433 pat_len = MAX_FUZZY_MATCHES;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006434
Bram Moolenaar1c299432018-10-28 14:36:09 +01006435 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; ++lnum)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006436 {
6437 col = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006438 if (!(flags & VGR_FUZZY))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006439 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006440 // Regular expression match
6441 while (vim_regexec_multi(regmatch, curwin, buf, lnum,
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006442 col, NULL) > 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006443 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006444 // Pass the buffer number so that it gets used even for a
6445 // dummy buffer, unless duplicate_name is set, then the
6446 // buffer will be wiped out below.
6447 if (qf_add_entry(qfl,
6448 NULL, // dir
6449 fname,
6450 NULL,
6451 duplicate_name ? 0 : buf->b_fnum,
6452 ml_get_buf(buf,
6453 regmatch->startpos[0].lnum + lnum, FALSE),
6454 regmatch->startpos[0].lnum + lnum,
thinca6864efa2021-06-19 20:45:20 +02006455 regmatch->endpos[0].lnum + lnum,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006456 regmatch->startpos[0].col + 1,
thinca6864efa2021-06-19 20:45:20 +02006457 regmatch->endpos[0].col + 1,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006458 FALSE, // vis_col
6459 NULL, // search pattern
6460 0, // nr
6461 0, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02006462 NULL, // user_data
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006463 TRUE // valid
6464 ) == QF_FAIL)
6465 {
6466 got_int = TRUE;
6467 break;
6468 }
6469 found_match = TRUE;
6470 if (--*tomatch == 0)
6471 break;
6472 if ((flags & VGR_GLOBAL) == 0
6473 || regmatch->endpos[0].lnum > 0)
6474 break;
6475 col = regmatch->endpos[0].col
6476 + (col == regmatch->endpos[0].col);
zeertzjq94b7c322024-03-12 21:50:32 +01006477 if (col > ml_get_buf_len(buf, lnum))
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006478 break;
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006479 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006480 }
6481 else
6482 {
6483 char_u *str = ml_get_buf(buf, lnum, FALSE);
zeertzjq8c55d602024-03-13 20:42:26 +01006484 colnr_T linelen = ml_get_buf_len(buf, lnum);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006485 int score;
6486 int_u matches[MAX_FUZZY_MATCHES];
K.Takataeeec2542021-06-02 13:28:16 +02006487 int_u sz = ARRAY_LENGTH(matches);
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006488
6489 // Fuzzy string match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01006490 CLEAR_FIELD(matches);
glepnir28e40a72025-03-16 21:24:22 +01006491 while (fuzzy_match(str + col, spat, FALSE, &score,
6492 matches, sz, TRUE) > 0)
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006493 {
6494 // Pass the buffer number so that it gets used even for a
6495 // dummy buffer, unless duplicate_name is set, then the
6496 // buffer will be wiped out below.
6497 if (qf_add_entry(qfl,
6498 NULL, // dir
6499 fname,
6500 NULL,
6501 duplicate_name ? 0 : buf->b_fnum,
6502 str,
6503 lnum,
thinca6864efa2021-06-19 20:45:20 +02006504 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006505 matches[0] + col + 1,
thinca6864efa2021-06-19 20:45:20 +02006506 0,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006507 FALSE, // vis_col
6508 NULL, // search pattern
6509 0, // nr
6510 0, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02006511 NULL, // user_data
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006512 TRUE // valid
6513 ) == QF_FAIL)
6514 {
6515 got_int = TRUE;
6516 break;
6517 }
6518 found_match = TRUE;
6519 if (--*tomatch == 0)
6520 break;
6521 if ((flags & VGR_GLOBAL) == 0)
6522 break;
6523 col = matches[pat_len - 1] + col + 1;
zeertzjq8c55d602024-03-13 20:42:26 +01006524 if (col > linelen)
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006525 break;
6526 }
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006527 }
6528 line_breakcheck();
6529 if (got_int)
6530 break;
6531 }
6532
6533 return found_match;
6534}
6535
6536/*
6537 * Jump to the first match and update the directory.
6538 */
6539 static void
6540vgr_jump_to_match(
6541 qf_info_T *qi,
6542 int forceit,
6543 int *redraw_for_dummy,
6544 buf_T *first_match_buf,
6545 char_u *target_dir)
6546{
6547 buf_T *buf;
6548
6549 buf = curbuf;
6550 qf_jump(qi, 0, 0, forceit);
6551 if (buf != curbuf)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006552 // If we jumped to another buffer redrawing will already be
6553 // taken care of.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006554 *redraw_for_dummy = FALSE;
6555
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006556 // Jump to the directory used after loading the buffer.
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006557 if (curbuf == first_match_buf && target_dir != NULL)
6558 {
6559 exarg_T ea;
6560
Bram Moolenaara80faa82020-04-12 19:37:17 +02006561 CLEAR_FIELD(ea);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006562 ea.arg = target_dir;
6563 ea.cmdidx = CMD_lcd;
6564 ex_cd(&ea);
6565 }
6566}
6567
6568/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006569 * :vimgrep command arguments
Bram Moolenaar86b68352004-12-27 21:59:20 +00006570 */
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006571typedef struct
Bram Moolenaar86b68352004-12-27 21:59:20 +00006572{
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006573 long tomatch; // maximum number of matches to find
6574 char_u *spat; // search pattern
6575 int flags; // search modifier
6576 char_u **fnames; // list of files to search
6577 int fcount; // number of files
6578 regmmatch_T regmatch; // compiled search pattern
6579 char_u *qf_title; // quickfix list title
6580} vgr_args_T;
6581
6582/*
6583 * Process :vimgrep command arguments. The command syntax is:
6584 *
6585 * :{count}vimgrep /{pattern}/[g][j] {file} ...
6586 */
6587 static int
6588vgr_process_args(
6589 exarg_T *eap,
6590 vgr_args_T *args)
6591{
Bram Moolenaar748bf032005-02-02 23:04:36 +00006592 char_u *p;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006593
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00006594 CLEAR_POINTER(args);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006595
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006596 args->regmatch.regprog = NULL;
6597 args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaara6557602006-02-04 22:43:20 +00006598
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006599 if (eap->addr_count > 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006600 args->tomatch = eap->line2;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006601 else
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006602 args->tomatch = MAXLNUM;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00006603
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006604 // Get the search pattern: either white-separated or enclosed in //
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006605 p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006606 if (p == NULL)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006607 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006608 emsg(_(e_invalid_search_pattern_or_delimiter));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006609 return FAIL;
Bram Moolenaar81695252004-12-29 20:58:21 +00006610 }
Bram Moolenaar60abe752013-03-07 16:32:54 +01006611
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006612 vgr_init_regmatch(&args->regmatch, args->spat);
6613 if (args->regmatch.regprog == NULL)
6614 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006615
6616 p = skipwhite(p);
6617 if (*p == NUL)
6618 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006619 emsg(_(e_file_name_missing_or_invalid_pattern));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006620 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006621 }
6622
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01006623 // Parse the list of arguments, wildcards have already been expanded.
Christian Brabandt0b226f62021-12-01 10:54:24 +00006624 if ((get_arglist_exp(p, &args->fcount, &args->fnames, TRUE) == FAIL) ||
6625 args->fcount == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006626 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006627 emsg(_(e_no_match));
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006628 return FAIL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006629 }
6630
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006631 return OK;
6632}
6633
6634/*
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006635 * Return TRUE if "buf" had an existing swap file, the current swap file does
6636 * not end in ".swp".
6637 */
6638 static int
6639existing_swapfile(buf_T *buf)
6640{
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02006641 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006642 {
6643 char_u *fname = buf->b_ml.ml_mfp->mf_fname;
6644 size_t len = STRLEN(fname);
6645
6646 return fname[len - 1] != 'p' || fname[len - 2] != 'w';
6647 }
6648 return FALSE;
6649}
6650
6651/*
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006652 * Search for a pattern in a list of files and populate the quickfix list with
6653 * the matches.
6654 */
6655 static int
6656vgr_process_files(
6657 win_T *wp,
6658 qf_info_T *qi,
6659 vgr_args_T *cmd_args,
6660 int *redraw_for_dummy,
6661 buf_T **first_match_buf,
6662 char_u **target_dir)
6663{
6664 int status = FAIL;
6665 int_u save_qfid = qf_get_curlist(qi)->qf_id;
6666 time_t seconds = 0;
6667 char_u *fname;
6668 int fi;
6669 buf_T *buf;
6670 int duplicate_name = FALSE;
6671 int using_dummy;
6672 char_u *dirname_start = NULL;
6673 char_u *dirname_now = NULL;
6674 int found_match;
6675 aco_save_T aco;
6676
Bram Moolenaarb86a3432016-01-10 16:00:53 +01006677 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start);
6678 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now);
Bram Moolenaard9462e32011-04-11 21:35:11 +02006679 if (dirname_start == NULL || dirname_now == NULL)
6680 goto theend;
6681
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006682 // Remember the current directory, because a BufRead autocommand that does
6683 // ":lcd %:p:h" changes the meaning of short path names.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006684 mch_dirname(dirname_start, MAXPATHL);
6685
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006686 seconds = (time_t)0;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006687 for (fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0;
6688 ++fi)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006689 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006690 fname = shorten_fname1(cmd_args->fnames[fi]);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006691 if (time(NULL) > seconds)
6692 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006693 // Display the file name every second or so, show the user we are
6694 // working on it.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006695 seconds = time(NULL);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006696 vgr_display_fname(fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006697 }
6698
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006699 buf = buflist_findname_exp(cmd_args->fnames[fi]);
Bram Moolenaar81695252004-12-29 20:58:21 +00006700 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6701 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006702 // Remember that a buffer with this name already exists.
Bram Moolenaar81695252004-12-29 20:58:21 +00006703 duplicate_name = (buf != NULL);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006704 using_dummy = TRUE;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006705 *redraw_for_dummy = TRUE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006706
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006707 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now);
Bram Moolenaar81695252004-12-29 20:58:21 +00006708 }
6709 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006710 // Use existing, loaded buffer.
Bram Moolenaar81695252004-12-29 20:58:21 +00006711 using_dummy = FALSE;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006712
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006713 // Check whether the quickfix list is still valid. When loading a
6714 // buffer above, autocommands might have changed the quickfix list.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006715 if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title))
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006716 goto theend;
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006717
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006718 save_qfid = qf_get_curlist(qi)->qf_id;
Bram Moolenaar321a9ec2012-12-12 15:55:20 +01006719
Bram Moolenaar81695252004-12-29 20:58:21 +00006720 if (buf == NULL)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006721 {
6722 if (!got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006723 smsg(_("Cannot open file \"%s\""), fname);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006724 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006725 else
6726 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006727 // Try for a match in all lines of the buffer.
6728 // For ":1vimgrep" look for first match only.
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01006729 found_match = vgr_match_buflines(qf_get_curlist(qi),
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006730 fname, buf, cmd_args->spat, &cmd_args->regmatch,
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006731 &cmd_args->tomatch, duplicate_name, cmd_args->flags);
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006732
Bram Moolenaar81695252004-12-29 20:58:21 +00006733 if (using_dummy)
6734 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006735 if (found_match && *first_match_buf == NULL)
6736 *first_match_buf = buf;
Bram Moolenaar81695252004-12-29 20:58:21 +00006737 if (duplicate_name)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006738 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006739 // Never keep a dummy buffer if there is another buffer
6740 // with the same name.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006741 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006742 buf = NULL;
6743 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006744 else if ((cmdmod.cmod_flags & CMOD_HIDE) == 0
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006745 || buf->b_p_bh[0] == 'u' // "unload"
6746 || buf->b_p_bh[0] == 'w' // "wipe"
6747 || buf->b_p_bh[0] == 'd') // "delete"
Bram Moolenaar81695252004-12-29 20:58:21 +00006748 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006749 // When no match was found we don't need to remember the
6750 // buffer, wipe it out. If there was a match and it
6751 // wasn't the first one or we won't jump there: only
6752 // unload the buffer.
6753 // Ignore 'hidden' here, because it may lead to having too
6754 // many swap files.
Bram Moolenaar81695252004-12-29 20:58:21 +00006755 if (!found_match)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006756 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006757 wipe_dummy_buffer(buf, dirname_start);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006758 buf = NULL;
6759 }
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006760 else if (buf != *first_match_buf
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02006761 || (cmd_args->flags & VGR_NOJUMP)
6762 || existing_swapfile(buf))
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006763 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006764 unload_dummy_buffer(buf, dirname_start);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006765 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006766 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006767 buf = NULL;
6768 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006769 }
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006770
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006771 if (buf != NULL)
6772 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006773 // Keeping the buffer, remove the dummy flag.
Bram Moolenaar015102e2016-07-16 18:24:56 +02006774 buf->b_flags &= ~BF_DUMMY;
6775
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006776 // If the buffer is still loaded we need to use the
6777 // directory we jumped to below.
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006778 if (buf == *first_match_buf
6779 && *target_dir == NULL
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006780 && STRCMP(dirname_start, dirname_now) != 0)
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006781 *target_dir = vim_strsave(dirname_now);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006782
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006783 // The buffer is still loaded, the Filetype autocommands
6784 // need to be done now, in that buffer. And the modelines
6785 // need to be done (again). But not the window-local
6786 // options!
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006787 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006788 if (curbuf == buf)
6789 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006790#if defined(FEAT_SYN_HL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00006791 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft,
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006792 buf->b_fname, TRUE, buf);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00006793#endif
Bram Moolenaare76062c2022-11-28 18:51:43 +00006794 do_modelines(OPT_NOWIN);
6795 aucmd_restbuf(&aco);
6796 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006797 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006798 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006799 }
6800 }
6801
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006802 status = OK;
6803
6804theend:
6805 vim_free(dirname_now);
6806 vim_free(dirname_start);
6807 return status;
6808}
6809
6810/*
6811 * ":vimgrep {pattern} file(s)"
6812 * ":vimgrepadd {pattern} file(s)"
6813 * ":lvimgrep {pattern} file(s)"
6814 * ":lvimgrepadd {pattern} file(s)"
6815 */
6816 void
6817ex_vimgrep(exarg_T *eap)
6818{
6819 vgr_args_T args;
6820 qf_info_T *qi;
6821 qf_list_T *qfl;
6822 int_u save_qfid;
6823 win_T *wp = NULL;
6824 int redraw_for_dummy = FALSE;
6825 buf_T *first_match_buf = NULL;
6826 char_u *target_dir = NULL;
6827 char_u *au_name = NULL;
6828 int status;
6829
Colin Kennedy21570352024-03-03 16:16:47 +01006830 if (!check_can_set_curbuf_forceit(eap->forceit))
6831 return;
6832
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006833 au_name = vgr_get_auname(eap->cmdidx);
6834 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
6835 curbuf->b_fname, TRUE, curbuf))
6836 {
6837#ifdef FEAT_EVAL
6838 if (aborting())
6839 return;
6840#endif
6841 }
6842
6843 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
6844 if (qi == NULL)
6845 return;
6846
6847 if (vgr_process_args(eap, &args) == FAIL)
6848 goto theend;
6849
6850 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd
6851 && eap->cmdidx != CMD_vimgrepadd
6852 && eap->cmdidx != CMD_lvimgrepadd)
6853 || qf_stack_empty(qi))
6854 // make place for a new list
6855 qf_new_list(qi, args.qf_title);
6856
6857 incr_quickfix_busy();
6858
6859 status = vgr_process_files(wp, qi, &args, &redraw_for_dummy,
6860 &first_match_buf, &target_dir);
6861 if (status != OK)
6862 {
6863 FreeWild(args.fcount, args.fnames);
6864 decr_quickfix_busy();
6865 goto theend;
6866 }
6867
6868 FreeWild(args.fcount, args.fnames);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006869
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01006870 qfl = qf_get_curlist(qi);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02006871 qfl->qf_nonevalid = FALSE;
6872 qfl->qf_ptr = qfl->qf_start;
6873 qfl->qf_index = 1;
6874 qf_list_changed(qfl);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006875
Bram Moolenaar864293a2016-06-02 13:40:04 +02006876 qf_update_buffer(qi, NULL);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006877
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006878 // Remember the current quickfix list identifier, so that we can check for
6879 // autocommands changing the current quickfix list.
6880 save_qfid = qf_get_curlist(qi)->qf_id;
6881
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006882 if (au_name != NULL)
6883 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
6884 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006885 // The QuickFixCmdPost autocmd may free the quickfix list. Check the list
6886 // is still valid.
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006887 if (!qflist_valid(wp, save_qfid)
6888 || qf_restore_list(qi, save_qfid) == FAIL)
6889 {
6890 decr_quickfix_busy();
Bram Moolenaar3c097222017-12-21 20:54:49 +01006891 goto theend;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006892 }
Bram Moolenaar531b9a32018-07-03 16:54:23 +02006893
zeertzjq8c55d602024-03-13 20:42:26 +01006894 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01006895 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar05159a02005-02-26 23:04:13 +00006896 {
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006897 if ((args.flags & VGR_NOJUMP) == 0)
Bram Moolenaar75b0a882018-03-24 14:01:56 +01006898 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy,
6899 first_match_buf, target_dir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00006900 }
Bram Moolenaar81695252004-12-29 20:58:21 +00006901 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006902 semsg(_(e_no_match_str_2), args.spat);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006903
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02006904 decr_quickfix_busy();
6905
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006906 // If we loaded a dummy buffer into the current window, the autocommands
6907 // may have messed up things, need to redraw and recompute folds.
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006908 if (redraw_for_dummy)
6909 {
6910#ifdef FEAT_FOLDING
6911 foldUpdateAll(curwin);
6912#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006913 redraw_later(UPD_NOT_VALID);
Bram Moolenaar1042fa32007-09-16 11:27:42 +00006914#endif
6915 }
6916
Bram Moolenaar86b68352004-12-27 21:59:20 +00006917theend:
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006918 vim_free(args.qf_title);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006919 vim_free(target_dir);
Bram Moolenaard6a98a32019-11-12 22:59:51 +01006920 vim_regfree(args.regmatch.regprog);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006921}
6922
6923/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006924 * Restore current working directory to "dirname_start" if they differ, taking
6925 * into account whether it is set locally or globally.
6926 */
6927 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006928restore_start_dir(char_u *dirname_start)
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006929{
6930 char_u *dirname_now = alloc(MAXPATHL);
6931
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006932 if (dirname_now == NULL)
6933 return;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006934
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006935 mch_dirname(dirname_now, MAXPATHL);
6936 if (STRCMP(dirname_start, dirname_now) != 0)
6937 {
6938 // If the directory has changed, change it back by building up an
6939 // appropriate ex command and executing it.
6940 exarg_T ea;
6941
6942 CLEAR_FIELD(ea);
6943 ea.arg = dirname_start;
6944 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd;
6945 ex_cd(&ea);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006946 }
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00006947 vim_free(dirname_now);
Bram Moolenaar7f51a822012-04-25 18:57:21 +02006948}
6949
6950/*
6951 * Load file "fname" into a dummy buffer and return the buffer pointer,
6952 * placing the directory resulting from the buffer load into the
6953 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller
6954 * prior to calling this function. Restores directory to "dirname_start" prior
6955 * to returning, if autocmds or the 'autochdir' option have changed it.
6956 *
6957 * If creating the dummy buffer does not fail, must call unload_dummy_buffer()
6958 * or wipe_dummy_buffer() later!
6959 *
Bram Moolenaar81695252004-12-29 20:58:21 +00006960 * Returns NULL if it fails.
Bram Moolenaar81695252004-12-29 20:58:21 +00006961 */
6962 static buf_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01006963load_dummy_buffer(
6964 char_u *fname,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006965 char_u *dirname_start, // in: old directory
6966 char_u *resulting_dir) // out: new directory
Bram Moolenaar81695252004-12-29 20:58:21 +00006967{
6968 buf_T *newbuf;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006969 bufref_T newbufref;
6970 bufref_T newbuf_to_wipe;
Bram Moolenaar81695252004-12-29 20:58:21 +00006971 int failed = TRUE;
Bram Moolenaar81695252004-12-29 20:58:21 +00006972 aco_save_T aco;
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006973 int readfile_result;
Bram Moolenaar81695252004-12-29 20:58:21 +00006974
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006975 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar81695252004-12-29 20:58:21 +00006976 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6977 if (newbuf == NULL)
6978 return NULL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006979 set_bufref(&newbufref, newbuf);
Bram Moolenaar81695252004-12-29 20:58:21 +00006980
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006981 // Init the options.
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00006982 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP);
6983
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006984 // need to open the memfile before putting the buffer in a window
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006985 if (ml_open(newbuf) == OK)
Bram Moolenaar81695252004-12-29 20:58:21 +00006986 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006987 // Make sure this buffer isn't wiped out by autocommands.
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01006988 ++newbuf->b_locked;
6989
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02006990 // set curwin/curbuf to buf and save a few things
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00006991 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006992 if (curbuf == newbuf)
Bram Moolenaar81695252004-12-29 20:58:21 +00006993 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00006994 // Need to set the filename for autocommands.
6995 (void)setfname(curbuf, fname, NULL, FALSE);
6996
6997 // Create swap file now to avoid the ATTENTION message.
6998 check_need_swap(TRUE);
6999
7000 // Remove the "dummy" flag, otherwise autocommands may not
7001 // work.
7002 curbuf->b_flags &= ~BF_DUMMY;
7003
7004 newbuf_to_wipe.br_buf = NULL;
7005 readfile_result = readfile(fname, NULL,
7006 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
7007 NULL, READ_NEW | READ_DUMMY);
7008 --newbuf->b_locked;
7009 if (readfile_result == OK
7010 && !got_int
7011 && !(curbuf->b_flags & BF_NEW))
Bram Moolenaar81695252004-12-29 20:58:21 +00007012 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00007013 failed = FALSE;
7014 if (curbuf != newbuf)
7015 {
7016 // Bloody autocommands changed the buffer! Can happen when
7017 // using netrw and editing a remote file. Use the current
7018 // buffer instead, delete the dummy one after restoring the
7019 // window stuff.
7020 set_bufref(&newbuf_to_wipe, newbuf);
7021 newbuf = curbuf;
7022 }
Bram Moolenaar81695252004-12-29 20:58:21 +00007023 }
Bram Moolenaare76062c2022-11-28 18:51:43 +00007024
7025 // restore curwin/curbuf and a few other things
7026 aucmd_restbuf(&aco);
Bram Moolenaar81695252004-12-29 20:58:21 +00007027
Bram Moolenaar84497cd2022-11-28 20:34:52 +00007028 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe))
Sean Dewarb4074ea2025-05-10 14:30:36 +02007029 {
7030 block_autocmds();
7031 wipe_dummy_buffer(newbuf_to_wipe.br_buf, NULL);
7032 unblock_autocmds();
7033 }
Bram Moolenaar84497cd2022-11-28 20:34:52 +00007034 }
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02007035
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007036 // Add back the "dummy" flag, otherwise buflist_findname_stat() won't
7037 // skip it.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02007038 newbuf->b_flags |= BF_DUMMY;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00007039 }
Bram Moolenaar81695252004-12-29 20:58:21 +00007040
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007041 // When autocommands/'autochdir' option changed directory: go back.
7042 // Let the caller know what the resulting dir was first, in case it is
7043 // important.
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007044 mch_dirname(resulting_dir, MAXPATHL);
7045 restore_start_dir(dirname_start);
7046
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007047 if (!bufref_valid(&newbufref))
Bram Moolenaar81695252004-12-29 20:58:21 +00007048 return NULL;
7049 if (failed)
7050 {
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007051 wipe_dummy_buffer(newbuf, dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00007052 return NULL;
7053 }
7054 return newbuf;
7055}
7056
7057/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007058 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores
Sean Dewarb4074ea2025-05-10 14:30:36 +02007059 * directory to "dirname_start" if not NULL prior to returning, if autocmds or
7060 * the 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00007061 */
7062 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007063wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00007064{
Bram Moolenaar2573af32020-03-14 17:21:34 +01007065 // If any autocommand opened a window on the dummy buffer, close that
7066 // window. If we can't close them all then give up.
7067 while (buf->b_nwindows > 0)
7068 {
7069 int did_one = FALSE;
7070 win_T *wp;
7071
7072 if (firstwin->w_next != NULL)
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007073 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2573af32020-03-14 17:21:34 +01007074 if (wp->w_buffer == buf)
7075 {
7076 if (win_close(wp, FALSE) == OK)
7077 did_one = TRUE;
7078 break;
7079 }
7080 if (!did_one)
Sean Dewar270124f2025-05-10 14:33:28 +02007081 goto fail;
Bram Moolenaar2573af32020-03-14 17:21:34 +01007082 }
7083
7084 if (curbuf != buf && buf->b_nwindows == 0) // safety check
Bram Moolenaard68071d2006-05-02 22:08:30 +00007085 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007086#if defined(FEAT_EVAL)
Bram Moolenaard68071d2006-05-02 22:08:30 +00007087 cleanup_T cs;
7088
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007089 // Reset the error/interrupt/exception state here so that aborting()
7090 // returns FALSE when wiping out the buffer. Otherwise it doesn't
7091 // work when got_int is set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00007092 enter_cleanup(&cs);
7093#endif
7094
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01007095 wipe_buffer(buf, TRUE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00007096
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007097#if defined(FEAT_EVAL)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007098 // Restore the error/interrupt/exception state if not discarded by a
7099 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaard68071d2006-05-02 22:08:30 +00007100 leave_cleanup(&cs);
7101#endif
Sean Dewarb4074ea2025-05-10 14:30:36 +02007102 if (dirname_start != NULL)
7103 // When autocommands/'autochdir' option changed directory: go back.
7104 restore_start_dir(dirname_start);
Sean Dewar270124f2025-05-10 14:33:28 +02007105
7106 return;
Bram Moolenaard68071d2006-05-02 22:08:30 +00007107 }
Sean Dewar270124f2025-05-10 14:33:28 +02007108
7109fail:
7110 // Keeping the buffer, remove the dummy flag.
7111 buf->b_flags &= ~BF_DUMMY;
Bram Moolenaar81695252004-12-29 20:58:21 +00007112}
7113
7114/*
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007115 * Unload the dummy buffer that load_dummy_buffer() created. Restores
7116 * directory to "dirname_start" prior to returning, if autocmds or the
7117 * 'autochdir' option have changed it.
Bram Moolenaar81695252004-12-29 20:58:21 +00007118 */
7119 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007120unload_dummy_buffer(buf_T *buf, char_u *dirname_start)
Bram Moolenaar81695252004-12-29 20:58:21 +00007121{
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007122 if (curbuf == buf) // safety check
7123 return;
Bram Moolenaar7f51a822012-04-25 18:57:21 +02007124
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007125 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE, TRUE);
7126
7127 // When autocommands/'autochdir' option changed directory: go back.
7128 restore_start_dir(dirname_start);
Bram Moolenaar81695252004-12-29 20:58:21 +00007129}
7130
Bram Moolenaar05159a02005-02-26 23:04:13 +00007131#if defined(FEAT_EVAL) || defined(PROTO)
7132/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01007133 * Copy the specified quickfix entry items into a new dict and append the dict
Bram Moolenaara16123a2019-03-28 20:31:07 +01007134 * to 'list'. Returns OK on success.
7135 */
7136 static int
7137get_qfline_items(qfline_T *qfp, list_T *list)
7138{
7139 int bufnum;
7140 dict_T *dict;
7141 char_u buf[2];
7142
7143 // Handle entries with a non-existing buffer number.
7144 bufnum = qfp->qf_fnum;
7145 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7146 bufnum = 0;
7147
7148 if ((dict = dict_alloc()) == NULL)
7149 return FAIL;
7150 if (list_append_dict(list, dict) == FAIL)
7151 return FAIL;
7152
7153 buf[0] = qfp->qf_type;
7154 buf[1] = NUL;
7155 if (dict_add_number(dict, "bufnr", (long)bufnum) == FAIL
thinca6864efa2021-06-19 20:45:20 +02007156 || dict_add_number(dict, "lnum", (long)qfp->qf_lnum) == FAIL
7157 || dict_add_number(dict, "end_lnum", (long)qfp->qf_end_lnum) == FAIL
7158 || dict_add_number(dict, "col", (long)qfp->qf_col) == FAIL
7159 || dict_add_number(dict, "end_col", (long)qfp->qf_end_col) == FAIL
7160 || dict_add_number(dict, "vcol", (long)qfp->qf_viscol) == FAIL
7161 || dict_add_number(dict, "nr", (long)qfp->qf_nr) == FAIL
Bram Moolenaara16123a2019-03-28 20:31:07 +01007162 || dict_add_string(dict, "module", qfp->qf_module) == FAIL
7163 || dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
7164 || dict_add_string(dict, "text", qfp->qf_text) == FAIL
7165 || dict_add_string(dict, "type", buf) == FAIL
Tom Praschanca6ac992023-08-11 23:26:12 +02007166 || (qfp->qf_user_data.v_type != VAR_UNKNOWN
7167 && dict_add_tv(dict, "user_data", &qfp->qf_user_data) == FAIL )
Bram Moolenaara16123a2019-03-28 20:31:07 +01007168 || dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
7169 return FAIL;
7170
7171 return OK;
7172}
7173
7174/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007175 * Add each quickfix error to list "list" as a dictionary.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007176 * If qf_idx is -1, use the current list. Otherwise, use the specified list.
Bram Moolenaar858ba062020-05-31 23:11:59 +02007177 * If eidx is not 0, then return only the specified entry. Otherwise return
7178 * all the entries.
Bram Moolenaar05159a02005-02-26 23:04:13 +00007179 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007180 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007181get_errorlist(
7182 qf_info_T *qi_arg,
7183 win_T *wp,
7184 int qf_idx,
7185 int eidx,
7186 list_T *list)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007187{
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007188 qf_info_T *qi = qi_arg;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007189 qf_list_T *qfl;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007190 qfline_T *qfp;
7191 int i;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007192
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007193 if (qi == NULL)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007194 {
64-bitman88d41ab2025-04-06 17:20:39 +02007195 qi = ql_info;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007196 if (wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007197 qi = GET_LOC_LIST(wp);
64-bitman88d41ab2025-04-06 17:20:39 +02007198 if (qi == NULL)
7199 return FAIL;
7200
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007201 }
7202
Bram Moolenaar858ba062020-05-31 23:11:59 +02007203 if (eidx < 0)
7204 return OK;
7205
Bram Moolenaar29ce4092018-04-28 21:56:44 +02007206 if (qf_idx == INVALID_QFIDX)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007207 qf_idx = qi->qf_curlist;
7208
Bram Moolenaar0398e002019-03-21 21:12:49 +01007209 if (qf_idx >= qi->qf_listcount)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007210 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007211
Bram Moolenaar0398e002019-03-21 21:12:49 +01007212 qfl = qf_get_list(qi, qf_idx);
7213 if (qf_list_empty(qfl))
7214 return FAIL;
7215
Bram Moolenaara16123a2019-03-28 20:31:07 +01007216 FOR_ALL_QFL_ITEMS(qfl, qfp, i)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007217 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007218 if (eidx > 0)
7219 {
7220 if (eidx == i)
7221 return get_qfline_items(qfp, list);
7222 }
7223 else if (get_qfline_items(qfp, list) == FAIL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007224 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007225 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01007226
Bram Moolenaar05159a02005-02-26 23:04:13 +00007227 return OK;
7228}
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007229
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007230// Flags used by getqflist()/getloclist() to determine which fields to return.
Bram Moolenaard823fa92016-08-12 16:29:27 +02007231enum {
7232 QF_GETLIST_NONE = 0x0,
7233 QF_GETLIST_TITLE = 0x1,
7234 QF_GETLIST_ITEMS = 0x2,
7235 QF_GETLIST_NR = 0x4,
7236 QF_GETLIST_WINID = 0x8,
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007237 QF_GETLIST_CONTEXT = 0x10,
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007238 QF_GETLIST_ID = 0x20,
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007239 QF_GETLIST_IDX = 0x40,
7240 QF_GETLIST_SIZE = 0x80,
Bram Moolenaarb254af32017-12-18 19:48:58 +01007241 QF_GETLIST_TICK = 0x100,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007242 QF_GETLIST_FILEWINID = 0x200,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007243 QF_GETLIST_QFBUFNR = 0x400,
Bram Moolenaard43906d2020-07-20 21:31:32 +02007244 QF_GETLIST_QFTF = 0x800,
7245 QF_GETLIST_ALL = 0xFFF,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007246};
7247
7248/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007249 * Parse text from 'di' and return the quickfix list items.
7250 * Existing quickfix lists are not modified.
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007251 */
7252 static int
Bram Moolenaar36538222017-09-02 19:51:44 +02007253qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007254{
7255 int status = FAIL;
7256 qf_info_T *qi;
Bram Moolenaar36538222017-09-02 19:51:44 +02007257 char_u *errorformat = p_efm;
7258 dictitem_T *efm_di;
7259 list_T *l;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007260
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007261 // Only a List value is supported
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007262 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
7263 return FAIL;
7264
7265 // If errorformat is supplied then use it, otherwise use the 'efm'
7266 // option setting
7267 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007268 {
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007269 if (efm_di->di_tv.v_type != VAR_STRING ||
7270 efm_di->di_tv.vval.v_string == NULL)
Bram Moolenaarda732532017-08-31 20:58:02 +02007271 return FAIL;
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007272 errorformat = efm_di->di_tv.vval.v_string;
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007273 }
7274
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007275 l = list_alloc();
7276 if (l == NULL)
7277 return FAIL;
7278
64-bitman88d41ab2025-04-06 17:20:39 +02007279 qi = qf_alloc_stack(QFLT_INTERNAL, 1);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007280 if (qi != NULL)
7281 {
7282 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat,
7283 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0)
7284 {
7285 (void)get_errorlist(qi, NULL, 0, 0, l);
7286 qf_free(&qi->qf_lists[0]);
7287 }
64-bitman88d41ab2025-04-06 17:20:39 +02007288
7289 qf_free_lists(qi);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00007290 }
7291 dict_add_list(retdict, "items", l);
7292 status = OK;
7293
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02007294 return status;
7295}
7296
7297/*
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01007298 * Return the quickfix/location list window identifier in the current tabpage.
7299 */
7300 static int
7301qf_winid(qf_info_T *qi)
7302{
7303 win_T *win;
7304
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007305 // The quickfix window can be opened even if the quickfix list is not set
7306 // using ":copen". This is not true for location lists.
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01007307 if (qi == NULL)
7308 return 0;
7309 win = qf_find_win(qi);
7310 if (win != NULL)
7311 return win->w_id;
7312 return 0;
7313}
7314
7315/*
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007316 * Returns the number of the buffer displayed in the quickfix/location list
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00007317 * window. If there is no buffer associated with the list or the buffer is
7318 * wiped out, then returns 0.
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007319 */
7320 static int
7321qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
7322{
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00007323 int bufnum = 0;
7324
7325 if (qi != NULL && buflist_findnr(qi->qf_bufnr) != NULL)
7326 bufnum = qi->qf_bufnr;
7327
7328 return dict_add_number(retdict, "qfbufnr", bufnum);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007329}
7330
7331/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007332 * Convert the keys in 'what' to quickfix list property flags.
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007333 */
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007334 static int
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007335qf_getprop_keys2flags(dict_T *what, int loclist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007336{
Bram Moolenaard823fa92016-08-12 16:29:27 +02007337 int flags = QF_GETLIST_NONE;
7338
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007339 if (dict_has_key(what, "all"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007340 {
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007341 flags |= QF_GETLIST_ALL;
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007342 if (!loclist)
7343 // File window ID is applicable only to location list windows
7344 flags &= ~ QF_GETLIST_FILEWINID;
7345 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007346
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007347 if (dict_has_key(what, "title"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007348 flags |= QF_GETLIST_TITLE;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007349
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007350 if (dict_has_key(what, "nr"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01007351 flags |= QF_GETLIST_NR;
7352
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007353 if (dict_has_key(what, "winid"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007354 flags |= QF_GETLIST_WINID;
Bram Moolenaard823fa92016-08-12 16:29:27 +02007355
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007356 if (dict_has_key(what, "context"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007357 flags |= QF_GETLIST_CONTEXT;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007358
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007359 if (dict_has_key(what, "id"))
Bram Moolenaara6d48492017-12-12 22:45:31 +01007360 flags |= QF_GETLIST_ID;
7361
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007362 if (dict_has_key(what, "items"))
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007363 flags |= QF_GETLIST_ITEMS;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007364
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007365 if (dict_has_key(what, "idx"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007366 flags |= QF_GETLIST_IDX;
7367
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007368 if (dict_has_key(what, "size"))
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007369 flags |= QF_GETLIST_SIZE;
7370
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007371 if (dict_has_key(what, "changedtick"))
Bram Moolenaarb254af32017-12-18 19:48:58 +01007372 flags |= QF_GETLIST_TICK;
7373
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007374 if (loclist && dict_has_key(what, "filewinid"))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007375 flags |= QF_GETLIST_FILEWINID;
7376
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007377 if (dict_has_key(what, "qfbufnr"))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007378 flags |= QF_GETLIST_QFBUFNR;
7379
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007380 if (dict_has_key(what, "quickfixtextfunc"))
Bram Moolenaard43906d2020-07-20 21:31:32 +02007381 flags |= QF_GETLIST_QFTF;
7382
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007383 return flags;
7384}
Bram Moolenaara6d48492017-12-12 22:45:31 +01007385
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007386/*
7387 * Return the quickfix list index based on 'nr' or 'id' in 'what'.
7388 * If 'nr' and 'id' are not present in 'what' then return the current
7389 * quickfix list index.
7390 * If 'nr' is zero then return the current quickfix list index.
7391 * If 'nr' is '$' then return the last quickfix list index.
7392 * If 'id' is present then return the index of the quickfix list with that id.
7393 * If 'id' is zero then return the quickfix list index specified by 'nr'.
7394 * Return -1, if quickfix list is not present or if the stack is empty.
7395 */
7396 static int
7397qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
7398{
7399 int qf_idx;
7400 dictitem_T *di;
7401
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007402 qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007403 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7404 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007405 // Use the specified quickfix/location list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007406 if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaara6d48492017-12-12 22:45:31 +01007407 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007408 // for zero use the current list
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007409 if (di->di_tv.vval.v_number != 0)
Bram Moolenaara6d48492017-12-12 22:45:31 +01007410 {
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007411 qf_idx = di->di_tv.vval.v_number - 1;
7412 if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007413 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007414 }
Bram Moolenaara6d48492017-12-12 22:45:31 +01007415 }
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007416 else if (di->di_tv.v_type == VAR_STRING
7417 && di->di_tv.vval.v_string != NULL
7418 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007419 // Get the last quickfix list number
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007420 qf_idx = qi->qf_listcount - 1;
7421 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007422 qf_idx = INVALID_QFIDX;
Bram Moolenaara6d48492017-12-12 22:45:31 +01007423 }
7424
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007425 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
7426 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007427 // Look for a list with the specified id
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007428 if (di->di_tv.v_type == VAR_NUMBER)
7429 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007430 // For zero, use the current list or the list specified by 'nr'
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007431 if (di->di_tv.vval.v_number != 0)
7432 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
7433 }
7434 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007435 qf_idx = INVALID_QFIDX;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007436 }
7437
7438 return qf_idx;
7439}
7440
7441/*
7442 * Return default values for quickfix list properties in retdict.
7443 */
7444 static int
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007445qf_getprop_defaults(qf_info_T *qi, int flags, int locstack, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007446{
7447 int status = OK;
7448
7449 if (flags & QF_GETLIST_TITLE)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007450 status = dict_add_string(retdict, "title", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007451 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
7452 {
7453 list_T *l = list_alloc();
7454 if (l != NULL)
7455 status = dict_add_list(retdict, "items", l);
7456 else
7457 status = FAIL;
7458 }
7459 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007460 status = dict_add_number(retdict, "nr", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007461 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007462 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007463 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007464 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007465 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007466 status = dict_add_number(retdict, "id", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007467 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007468 status = dict_add_number(retdict, "idx", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007469 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007470 status = dict_add_number(retdict, "size", 0);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007471 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007472 status = dict_add_number(retdict, "changedtick", 0);
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007473 if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007474 status = dict_add_number(retdict, "filewinid", 0);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007475 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7476 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007477 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7478 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007479
7480 return status;
7481}
7482
7483/*
7484 * Return the quickfix list title as 'title' in retdict
7485 */
7486 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007487qf_getprop_title(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007488{
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007489 return dict_add_string(retdict, "title", qfl->qf_title);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007490}
7491
7492/*
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007493 * Returns the identifier of the window used to display files from a location
7494 * list. If there is no associated window, then returns 0. Useful only when
7495 * called from a location list window.
7496 */
7497 static int
7498qf_getprop_filewinid(win_T *wp, qf_info_T *qi, dict_T *retdict)
7499{
7500 int winid = 0;
7501
7502 if (wp != NULL && IS_LL_WINDOW(wp))
7503 {
7504 win_T *ll_wp = qf_find_win_with_loclist(qi);
7505 if (ll_wp != NULL)
7506 winid = ll_wp->w_id;
7507 }
7508
7509 return dict_add_number(retdict, "filewinid", winid);
7510}
7511
7512/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007513 * Return the quickfix list items/entries as 'items' in retdict.
7514 * If eidx is not 0, then return the item at the specified index.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007515 */
7516 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007517qf_getprop_items(qf_info_T *qi, int qf_idx, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007518{
7519 int status = OK;
7520 list_T *l = list_alloc();
7521 if (l != NULL)
7522 {
Bram Moolenaar858ba062020-05-31 23:11:59 +02007523 (void)get_errorlist(qi, NULL, qf_idx, eidx, l);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007524 dict_add_list(retdict, "items", l);
7525 }
7526 else
7527 status = FAIL;
7528
7529 return status;
7530}
7531
7532/*
7533 * Return the quickfix list context (if any) as 'context' in retdict.
7534 */
7535 static int
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007536qf_getprop_ctx(qf_list_T *qfl, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007537{
7538 int status;
7539 dictitem_T *di;
7540
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007541 if (qfl->qf_ctx != NULL)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007542 {
7543 di = dictitem_alloc((char_u *)"context");
7544 if (di != NULL)
7545 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007546 copy_tv(qfl->qf_ctx, &di->di_tv);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007547 status = dict_add(retdict, di);
7548 if (status == FAIL)
7549 dictitem_free(di);
7550 }
7551 else
7552 status = FAIL;
7553 }
7554 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007555 status = dict_add_string(retdict, "context", (char_u *)"");
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007556
7557 return status;
7558}
7559
7560/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02007561 * Return the current quickfix list index as 'idx' in retdict.
7562 * If a specific entry index (eidx) is supplied, then use that.
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007563 */
7564 static int
Bram Moolenaar858ba062020-05-31 23:11:59 +02007565qf_getprop_idx(qf_list_T *qfl, int eidx, dict_T *retdict)
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007566{
Bram Moolenaar858ba062020-05-31 23:11:59 +02007567 if (eidx == 0)
7568 {
7569 eidx = qfl->qf_index;
7570 if (qf_list_empty(qfl))
7571 // For empty lists, current index is set to 0
7572 eidx = 0;
7573 }
7574 return dict_add_number(retdict, "idx", eidx);
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007575}
7576
7577/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02007578 * Return the 'quickfixtextfunc' function of a quickfix/location list
7579 */
7580 static int
7581qf_getprop_qftf(qf_list_T *qfl, dict_T *retdict)
7582{
7583 int status;
7584
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007585 if (qfl->qf_qftf_cb.cb_name != NULL)
Bram Moolenaard43906d2020-07-20 21:31:32 +02007586 {
7587 typval_T tv;
7588
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01007589 put_callback(&qfl->qf_qftf_cb, &tv);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007590 status = dict_add_tv(retdict, "quickfixtextfunc", &tv);
7591 clear_tv(&tv);
7592 }
7593 else
7594 status = dict_add_string(retdict, "quickfixtextfunc", (char_u *)"");
7595
7596 return status;
7597}
7598
7599/*
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007600 * Return quickfix/location list details (title) as a
7601 * dictionary. 'what' contains the details to return. If 'list_idx' is -1,
7602 * then current list is used. Otherwise the specified list is used.
7603 */
Bram Moolenaare677df82019-09-02 22:31:11 +02007604 static int
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007605qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
7606{
64-bitman88d41ab2025-04-06 17:20:39 +02007607 qf_info_T *qi = ql_info;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007608 qf_list_T *qfl;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007609 int status = OK;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007610 int qf_idx = INVALID_QFIDX;
Bram Moolenaar858ba062020-05-31 23:11:59 +02007611 int eidx = 0;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007612 dictitem_T *di;
7613 int flags = QF_GETLIST_NONE;
7614
7615 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
7616 return qf_get_list_from_lines(what, di, retdict);
7617
7618 if (wp != NULL)
7619 qi = GET_LOC_LIST(wp);
64-bitman88d41ab2025-04-06 17:20:39 +02007620 else if (qi == NULL)
7621 return FAIL;
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007622
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007623 flags = qf_getprop_keys2flags(what, (wp != NULL));
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007624
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007625 if (!qf_stack_empty(qi))
Bram Moolenaar353eeea2018-04-16 18:04:57 +02007626 qf_idx = qf_getprop_qfidx(qi, what);
7627
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007628 // List is not present or is empty
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007629 if (qf_stack_empty(qi) || qf_idx == INVALID_QFIDX)
Bram Moolenaar2d67d302018-11-16 18:46:02 +01007630 return qf_getprop_defaults(qi, flags, wp != NULL, retdict);
Bram Moolenaara6d48492017-12-12 22:45:31 +01007631
Bram Moolenaar0398e002019-03-21 21:12:49 +01007632 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007633
Bram Moolenaar858ba062020-05-31 23:11:59 +02007634 // If an entry index is specified, use that
7635 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
7636 {
7637 if (di->di_tv.v_type != VAR_NUMBER)
7638 return FAIL;
7639 eidx = di->di_tv.vval.v_number;
7640 }
7641
Bram Moolenaard823fa92016-08-12 16:29:27 +02007642 if (flags & QF_GETLIST_TITLE)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007643 status = qf_getprop_title(qfl, retdict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007644 if ((status == OK) && (flags & QF_GETLIST_NR))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007645 status = dict_add_number(retdict, "nr", qf_idx + 1);
Bram Moolenaard823fa92016-08-12 16:29:27 +02007646 if ((status == OK) && (flags & QF_GETLIST_WINID))
Bram Moolenaare0be1672018-07-08 16:50:37 +02007647 status = dict_add_number(retdict, "winid", qf_winid(qi));
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007648 if ((status == OK) && (flags & QF_GETLIST_ITEMS))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007649 status = qf_getprop_items(qi, qf_idx, eidx, retdict);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02007650 if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007651 status = qf_getprop_ctx(qfl, retdict);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007652 if ((status == OK) && (flags & QF_GETLIST_ID))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007653 status = dict_add_number(retdict, "id", qfl->qf_id);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007654 if ((status == OK) && (flags & QF_GETLIST_IDX))
Bram Moolenaar858ba062020-05-31 23:11:59 +02007655 status = qf_getprop_idx(qfl, eidx, retdict);
Bram Moolenaarfc2b2702017-09-15 22:43:07 +02007656 if ((status == OK) && (flags & QF_GETLIST_SIZE))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007657 status = dict_add_number(retdict, "size", qfl->qf_count);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007658 if ((status == OK) && (flags & QF_GETLIST_TICK))
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007659 status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02007660 if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
7661 status = qf_getprop_filewinid(wp, qi, retdict);
Bram Moolenaar647e24b2019-03-17 16:39:46 +01007662 if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
7663 status = qf_getprop_qfbufnr(qi, retdict);
Bram Moolenaard43906d2020-07-20 21:31:32 +02007664 if ((status == OK) && (flags & QF_GETLIST_QFTF))
7665 status = qf_getprop_qftf(qfl, retdict);
Bram Moolenaarb254af32017-12-18 19:48:58 +01007666
Bram Moolenaard823fa92016-08-12 16:29:27 +02007667 return status;
7668}
7669
7670/*
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007671 * Add a new quickfix entry to list at 'qf_idx' in the stack 'qi' from the
Bram Moolenaar9752c722018-12-22 16:49:34 +01007672 * items in the dict 'd'. If it is a valid error entry, then set 'valid_entry'
7673 * to TRUE.
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007674 */
7675 static int
7676qf_add_entry_from_dict(
Bram Moolenaar0398e002019-03-21 21:12:49 +01007677 qf_list_T *qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007678 dict_T *d,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007679 int first_entry,
7680 int *valid_entry)
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007681{
7682 static int did_bufnr_emsg;
7683 char_u *filename, *module, *pattern, *text, *type;
thinca6864efa2021-06-19 20:45:20 +02007684 int bufnum, valid, status, col, end_col, vcol, nr;
7685 long lnum, end_lnum;
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007686
7687 if (first_entry)
7688 did_bufnr_emsg = FALSE;
7689
Bram Moolenaard61efa52022-07-23 09:52:04 +01007690 filename = dict_get_string(d, "filename", TRUE);
7691 module = dict_get_string(d, "module", TRUE);
7692 bufnum = (int)dict_get_number(d, "bufnr");
7693 lnum = (int)dict_get_number(d, "lnum");
7694 end_lnum = (int)dict_get_number(d, "end_lnum");
7695 col = (int)dict_get_number(d, "col");
7696 end_col = (int)dict_get_number(d, "end_col");
7697 vcol = (int)dict_get_number(d, "vcol");
7698 nr = (int)dict_get_number(d, "nr");
7699 type = dict_get_string(d, "type", TRUE);
7700 pattern = dict_get_string(d, "pattern", TRUE);
7701 text = dict_get_string(d, "text", TRUE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007702 if (text == NULL)
7703 text = vim_strsave((char_u *)"");
Tom Praschanca6ac992023-08-11 23:26:12 +02007704 typval_T user_data;
7705 user_data.v_type = VAR_UNKNOWN;
7706 dict_get_tv(d, "user_data", &user_data);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007707
7708 valid = TRUE;
7709 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
7710 valid = FALSE;
7711
7712 // Mark entries with non-existing buffer number as not valid. Give the
7713 // error message only once.
7714 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL))
7715 {
7716 if (!did_bufnr_emsg)
7717 {
7718 did_bufnr_emsg = TRUE;
Bram Moolenaare1242042021-12-16 20:56:57 +00007719 semsg(_(e_buffer_nr_not_found), bufnum);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007720 }
7721 valid = FALSE;
7722 bufnum = 0;
7723 }
7724
7725 // If the 'valid' field is present it overrules the detected value.
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01007726 if (dict_has_key(d, "valid"))
Bram Moolenaard61efa52022-07-23 09:52:04 +01007727 valid = (int)dict_get_bool(d, "valid", FALSE);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007728
Bram Moolenaar0398e002019-03-21 21:12:49 +01007729 status = qf_add_entry(qfl,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007730 NULL, // dir
7731 filename,
7732 module,
7733 bufnum,
7734 text,
7735 lnum,
thinca6864efa2021-06-19 20:45:20 +02007736 end_lnum,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007737 col,
thinca6864efa2021-06-19 20:45:20 +02007738 end_col,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007739 vcol, // vis_col
7740 pattern, // search pattern
7741 nr,
7742 type == NULL ? NUL : *type,
Tom Praschanca6ac992023-08-11 23:26:12 +02007743 &user_data,
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007744 valid);
7745
7746 vim_free(filename);
7747 vim_free(module);
7748 vim_free(pattern);
7749 vim_free(text);
7750 vim_free(type);
Tom Praschanca6ac992023-08-11 23:26:12 +02007751 clear_tv(&user_data);
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007752
Bram Moolenaar9752c722018-12-22 16:49:34 +01007753 if (valid)
7754 *valid_entry = TRUE;
7755
Bram Moolenaar6f6ef7c2018-08-28 22:07:44 +02007756 return status;
7757}
7758
7759/*
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007760 * Check if `entry` is closer to the target than `other_entry`.
7761 *
7762 * Only returns TRUE if `entry` is definitively closer. If it's further
7763 * away, or there's not enough information to tell, return FALSE.
7764 */
7765 static int
7766entry_is_closer_to_target(
7767 qfline_T *entry,
7768 qfline_T *other_entry,
7769 int target_fnum,
7770 int target_lnum,
7771 int target_col)
7772{
7773 // First, compare entries to target file.
7774 if (!target_fnum)
7775 // Without a target file, we can't know which is closer.
7776 return FALSE;
7777
7778 int is_target_file = entry->qf_fnum && entry->qf_fnum == target_fnum;
7779 int other_is_target_file = other_entry->qf_fnum && other_entry->qf_fnum == target_fnum;
7780 if (!is_target_file && other_is_target_file)
7781 return FALSE;
7782 else if (is_target_file && !other_is_target_file)
7783 return TRUE;
7784
7785 // Both entries are pointing at the exact same file. Now compare line
7786 // numbers.
7787 if (!target_lnum)
7788 // Without a target line number, we can't know which is closer.
7789 return FALSE;
7790
7791 int line_distance = entry->qf_lnum ? labs(entry->qf_lnum - target_lnum) : INT_MAX;
7792 int other_line_distance = other_entry->qf_lnum ? labs(other_entry->qf_lnum - target_lnum) : INT_MAX;
7793 if (line_distance > other_line_distance)
7794 return FALSE;
7795 else if (line_distance < other_line_distance)
7796 return TRUE;
7797
7798 // Both entries are pointing at the exact same line number (or no line
7799 // number at all). Now compare columns.
7800 if (!target_col)
7801 // Without a target column, we can't know which is closer.
7802 return FALSE;
7803
7804 int column_distance = entry->qf_col ? abs(entry->qf_col - target_col) : INT_MAX;
7805 int other_column_distance = other_entry->qf_col ? abs(other_entry->qf_col - target_col): INT_MAX;
7806 if (column_distance > other_column_distance)
7807 return FALSE;
7808 else if (column_distance < other_column_distance)
7809 return TRUE;
7810
7811 // It's a complete tie! The exact same file, line, and column.
7812 return FALSE;
7813}
7814
7815/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02007816 * Add list of entries to quickfix/location list. Each list entry is
7817 * a dictionary with item information.
7818 */
7819 static int
7820qf_add_entries(
7821 qf_info_T *qi,
Bram Moolenaara3921f42017-06-04 15:30:34 +02007822 int qf_idx,
Bram Moolenaard823fa92016-08-12 16:29:27 +02007823 list_T *list,
7824 char_u *title,
7825 int action)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007826{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007827 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007828 listitem_T *li;
7829 dict_T *d;
Bram Moolenaar864293a2016-06-02 13:40:04 +02007830 qfline_T *old_last = NULL;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007831 int retval = OK;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007832 int valid_entry = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007833
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007834 // If there's an entry selected in the quickfix list, remember its location
7835 // (file, line, column), so we can select the nearest entry in the updated
7836 // quickfix list.
7837 int prev_fnum = 0;
7838 int prev_lnum = 0;
7839 int prev_col = 0;
7840 if (qfl->qf_ptr)
7841 {
7842 prev_fnum = qfl->qf_ptr->qf_fnum;
7843 prev_lnum = qfl->qf_ptr->qf_lnum;
7844 prev_col = qfl->qf_ptr->qf_col;
7845 }
7846
7847 int select_first_entry = FALSE;
7848 int select_nearest_entry = FALSE;
7849
Bram Moolenaara3921f42017-06-04 15:30:34 +02007850 if (action == ' ' || qf_idx == qi->qf_listcount)
7851 {
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007852 select_first_entry = TRUE;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007853 // make place for a new list
Bram Moolenaar94116152012-11-28 17:41:59 +01007854 qf_new_list(qi, title);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007855 qf_idx = qi->qf_curlist;
Bram Moolenaar0398e002019-03-21 21:12:49 +01007856 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaara3921f42017-06-04 15:30:34 +02007857 }
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007858 else if (action == 'a')
7859 {
7860 if (qf_list_empty(qfl))
7861 // Appending to empty list, select first entry.
7862 select_first_entry = TRUE;
7863 else
7864 // Adding to existing list, use last entry.
7865 old_last = qfl->qf_last;
7866 }
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007867 else if (action == 'r')
Bram Moolenaarfb604092014-07-23 15:55:00 +02007868 {
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007869 select_first_entry = TRUE;
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007870 qf_free_items(qfl);
7871 qf_store_title(qfl, title);
Bram Moolenaarfb604092014-07-23 15:55:00 +02007872 }
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007873 else if (action == 'u')
7874 {
7875 select_nearest_entry = TRUE;
7876 qf_free_items(qfl);
7877 qf_store_title(qfl, title);
7878 }
7879
7880 qfline_T *entry_to_select = NULL;
7881 int entry_to_select_index = 0;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007882
Bram Moolenaaraeea7212020-04-02 18:50:46 +02007883 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007884 {
7885 if (li->li_tv.v_type != VAR_DICT)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007886 continue; // Skip non-dict items
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007887
7888 d = li->li_tv.vval.v_dict;
7889 if (d == NULL)
7890 continue;
7891
Bram Moolenaar0398e002019-03-21 21:12:49 +01007892 retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
Bram Moolenaar9752c722018-12-22 16:49:34 +01007893 &valid_entry);
Bram Moolenaar95946f12019-03-31 15:31:59 +02007894 if (retval == QF_FAIL)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007895 break;
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007896
7897 qfline_T *entry = qfl->qf_last;
7898 if (
7899 (select_first_entry && entry_to_select == NULL) ||
7900 (select_nearest_entry &&
7901 (entry_to_select == NULL ||
7902 entry_is_closer_to_target(entry, entry_to_select, prev_fnum,
7903 prev_lnum, prev_col))))
7904 {
7905 entry_to_select = entry;
7906 entry_to_select_index = qfl->qf_count;
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +02007907 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007908 }
7909
Bram Moolenaar9752c722018-12-22 16:49:34 +01007910 // Check if any valid error entries are added to the list.
7911 if (valid_entry)
7912 qfl->qf_nonevalid = FALSE;
7913 else if (qfl->qf_index == 0)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007914 // no valid entry
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02007915 qfl->qf_nonevalid = TRUE;
Bram Moolenaar9752c722018-12-22 16:49:34 +01007916
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02007917 // Set the current error.
7918 if (entry_to_select)
7919 {
7920 qfl->qf_ptr = entry_to_select;
7921 qfl->qf_index = entry_to_select_index;
7922 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007923
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007924 // Don't update the cursor in quickfix window when appending entries
Bram Moolenaar864293a2016-06-02 13:40:04 +02007925 qf_update_buffer(qi, old_last);
Bram Moolenaar68b76a62005-03-25 21:53:48 +00007926
7927 return retval;
7928}
Bram Moolenaard823fa92016-08-12 16:29:27 +02007929
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007930/*
7931 * Get the quickfix list index from 'nr' or 'id'
7932 */
Bram Moolenaard823fa92016-08-12 16:29:27 +02007933 static int
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007934qf_setprop_get_qfidx(
7935 qf_info_T *qi,
7936 dict_T *what,
7937 int action,
7938 int *newlist)
Bram Moolenaard823fa92016-08-12 16:29:27 +02007939{
7940 dictitem_T *di;
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007941 int qf_idx = qi->qf_curlist; // default is the current list
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007942
Bram Moolenaard823fa92016-08-12 16:29:27 +02007943 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
7944 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007945 // Use the specified quickfix/location list
Bram Moolenaard823fa92016-08-12 16:29:27 +02007946 if (di->di_tv.v_type == VAR_NUMBER)
7947 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007948 // for zero use the current list
Bram Moolenaar6e62da32017-05-28 08:16:25 +02007949 if (di->di_tv.vval.v_number != 0)
7950 qf_idx = di->di_tv.vval.v_number - 1;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007951
Bram Moolenaar55b69262017-08-13 13:42:01 +02007952 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount)
7953 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007954 // When creating a new list, accept qf_idx pointing to the next
7955 // non-available list and add the new list at the end of the
7956 // stack.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007957 *newlist = TRUE;
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007958 qf_idx = qf_stack_empty(qi) ? 0 : qi->qf_listcount - 1;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007959 }
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007960 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007961 return INVALID_QFIDX;
Bram Moolenaar55b69262017-08-13 13:42:01 +02007962 else if (action != ' ')
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007963 *newlist = FALSE; // use the specified list
Bram Moolenaar55b69262017-08-13 13:42:01 +02007964 }
7965 else if (di->di_tv.v_type == VAR_STRING
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007966 && di->di_tv.vval.v_string != NULL
7967 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007968 {
Bram Moolenaar019dfe62018-10-07 14:38:49 +02007969 if (!qf_stack_empty(qi))
Bram Moolenaar55b69262017-08-13 13:42:01 +02007970 qf_idx = qi->qf_listcount - 1;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007971 else if (*newlist)
Bram Moolenaar55b69262017-08-13 13:42:01 +02007972 qf_idx = 0;
7973 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007974 return INVALID_QFIDX;
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02007975 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02007976 else
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007977 return INVALID_QFIDX;
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02007978 }
7979
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007980 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007981 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02007982 // Use the quickfix/location list with the specified id
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007983 if (di->di_tv.v_type != VAR_NUMBER)
7984 return INVALID_QFIDX;
7985
7986 return qf_id2nr(qi, di->di_tv.vval.v_number);
Bram Moolenaara539f4f2017-08-30 20:33:55 +02007987 }
7988
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02007989 return qf_idx;
7990}
7991
7992/*
7993 * Set the quickfix list title.
7994 */
7995 static int
7996qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di)
7997{
Bram Moolenaar0398e002019-03-21 21:12:49 +01007998 qf_list_T *qfl = qf_get_list(qi, qf_idx);
Bram Moolenaar108e7b42018-10-11 17:39:12 +02007999
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008000 if (di->di_tv.v_type != VAR_STRING)
8001 return FAIL;
8002
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008003 vim_free(qfl->qf_title);
Bram Moolenaard61efa52022-07-23 09:52:04 +01008004 qfl->qf_title = dict_get_string(what, "title", TRUE);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008005 if (qf_idx == qi->qf_curlist)
8006 qf_update_win_titlevar(qi);
8007
8008 return OK;
8009}
8010
8011/*
8012 * Set quickfix list items/entries.
8013 */
8014 static int
8015qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action)
8016{
8017 int retval = FAIL;
8018 char_u *title_save;
8019
8020 if (di->di_tv.v_type != VAR_LIST)
8021 return FAIL;
8022
8023 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title);
8024 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list,
8025 title_save, action == ' ' ? 'a' : action);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008026 vim_free(title_save);
8027
8028 return retval;
8029}
8030
8031/*
8032 * Set quickfix list items/entries from a list of lines.
8033 */
8034 static int
8035qf_setprop_items_from_lines(
8036 qf_info_T *qi,
8037 int qf_idx,
8038 dict_T *what,
8039 dictitem_T *di,
8040 int action)
8041{
8042 char_u *errorformat = p_efm;
8043 dictitem_T *efm_di;
8044 int retval = FAIL;
8045
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008046 // Use the user supplied errorformat settings (if present)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008047 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL)
8048 {
8049 if (efm_di->di_tv.v_type != VAR_STRING ||
8050 efm_di->di_tv.vval.v_string == NULL)
8051 return FAIL;
8052 errorformat = efm_di->di_tv.vval.v_string;
8053 }
8054
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008055 // Only a List value is supported
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008056 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL)
8057 return FAIL;
8058
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02008059 if (action == 'r' || action == 'u')
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02008060 qf_free_items(&qi->qf_lists[qf_idx]);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008061 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat,
Bram Moolenaar287153c2020-11-29 14:20:27 +01008062 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) >= 0)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008063 retval = OK;
8064
8065 return retval;
8066}
8067
8068/*
8069 * Set quickfix list context.
8070 */
8071 static int
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02008072qf_setprop_context(qf_list_T *qfl, dictitem_T *di)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008073{
8074 typval_T *ctx;
8075
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02008076 free_tv(qfl->qf_ctx);
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008077 ctx = alloc_tv();
8078 if (ctx != NULL)
8079 copy_tv(&di->di_tv, ctx);
Bram Moolenaarfe15b7d2018-09-18 22:50:06 +02008080 qfl->qf_ctx = ctx;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008081
8082 return OK;
8083}
8084
8085/*
Bram Moolenaar5b69c222019-01-11 14:50:06 +01008086 * Set the current index in the specified quickfix list
8087 */
8088 static int
8089qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, dictitem_T *di)
8090{
8091 int denote = FALSE;
8092 int newidx;
8093 int old_qfidx;
8094 qfline_T *qf_ptr;
8095
8096 // If the specified index is '$', then use the last entry
8097 if (di->di_tv.v_type == VAR_STRING
8098 && di->di_tv.vval.v_string != NULL
8099 && STRCMP(di->di_tv.vval.v_string, "$") == 0)
8100 newidx = qfl->qf_count;
8101 else
8102 {
8103 // Otherwise use the specified index
8104 newidx = tv_get_number_chk(&di->di_tv, &denote);
8105 if (denote)
8106 return FAIL;
8107 }
8108
8109 if (newidx < 1) // sanity check
8110 return FAIL;
8111 if (newidx > qfl->qf_count)
8112 newidx = qfl->qf_count;
8113
8114 old_qfidx = qfl->qf_index;
8115 qf_ptr = get_nth_entry(qfl, newidx, &newidx);
8116 if (qf_ptr == NULL)
8117 return FAIL;
8118 qfl->qf_ptr = qf_ptr;
8119 qfl->qf_index = newidx;
8120
8121 // If the current list is modified and it is displayed in the quickfix
8122 // window, then Update it.
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01008123 if (qf_get_curlist(qi)->qf_id == qfl->qf_id)
Bram Moolenaar5b69c222019-01-11 14:50:06 +01008124 qf_win_pos_update(qi, old_qfidx);
8125
8126 return OK;
8127}
8128
8129/*
Bram Moolenaar858ba062020-05-31 23:11:59 +02008130 * Set the current index in the specified quickfix list
8131 */
8132 static int
8133qf_setprop_qftf(qf_info_T *qi UNUSED, qf_list_T *qfl, dictitem_T *di)
8134{
Bram Moolenaard43906d2020-07-20 21:31:32 +02008135 callback_T cb;
8136
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01008137 free_callback(&qfl->qf_qftf_cb);
Bram Moolenaard43906d2020-07-20 21:31:32 +02008138 cb = get_callback(&di->di_tv);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00008139 if (cb.cb_name == NULL || *cb.cb_name == NUL)
8140 return OK;
8141
8142 set_callback(&qfl->qf_qftf_cb, &cb);
8143 if (cb.cb_free_name)
8144 vim_free(cb.cb_name);
Bram Moolenaar858ba062020-05-31 23:11:59 +02008145
8146 return OK;
8147}
8148
8149/*
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008150 * Set quickfix/location list properties (title, items, context).
8151 * Also used to add items from parsing a list of lines.
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008152 * Used by the setqflist() and setloclist() Vim script functions.
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008153 */
8154 static int
8155qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title)
8156{
8157 dictitem_T *di;
8158 int retval = FAIL;
8159 int qf_idx;
8160 int newlist = FALSE;
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008161 qf_list_T *qfl;
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008162
Bram Moolenaar019dfe62018-10-07 14:38:49 +02008163 if (action == ' ' || qf_stack_empty(qi))
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008164 newlist = TRUE;
8165
8166 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist);
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008167 if (qf_idx == INVALID_QFIDX) // List not found
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008168 return FAIL;
8169
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02008170 if (newlist)
8171 {
Bram Moolenaar55b69262017-08-13 13:42:01 +02008172 qi->qf_curlist = qf_idx;
Bram Moolenaarae338332017-08-11 20:25:26 +02008173 qf_new_list(qi, title);
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02008174 qf_idx = qi->qf_curlist;
Bram Moolenaard823fa92016-08-12 16:29:27 +02008175 }
8176
Bram Moolenaar0398e002019-03-21 21:12:49 +01008177 qfl = qf_get_list(qi, qf_idx);
Bram Moolenaard823fa92016-08-12 16:29:27 +02008178 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008179 retval = qf_setprop_title(qi, qf_idx, what, di);
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02008180 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008181 retval = qf_setprop_items(qi, qf_idx, di, action);
Bram Moolenaar2c809b72017-09-01 18:34:02 +02008182 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02008183 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008184 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008185 retval = qf_setprop_context(qfl, di);
Bram Moolenaar5b69c222019-01-11 14:50:06 +01008186 if ((di = dict_find(what, (char_u *)"idx", -1)) != NULL)
8187 retval = qf_setprop_curidx(qi, qfl, di);
Bram Moolenaar858ba062020-05-31 23:11:59 +02008188 if ((di = dict_find(what, (char_u *)"quickfixtextfunc", -1)) != NULL)
8189 retval = qf_setprop_qftf(qi, qfl, di);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008190
Bram Moolenaar287153c2020-11-29 14:20:27 +01008191 if (newlist || retval == OK)
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008192 qf_list_changed(qfl);
Bram Moolenaar287153c2020-11-29 14:20:27 +01008193 if (newlist)
8194 qf_update_buffer(qi, NULL);
Bram Moolenaarb254af32017-12-18 19:48:58 +01008195
Bram Moolenaard823fa92016-08-12 16:29:27 +02008196 return retval;
8197}
8198
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008199/*
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008200 * Free the entire quickfix/location list stack.
8201 * If the quickfix/location list window is open, then clear it.
8202 */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008203 static void
8204qf_free_stack(win_T *wp, qf_info_T *qi)
8205{
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008206 win_T *qfwin = qf_find_win(qi);
8207 win_T *llwin = NULL;
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008208
8209 if (qfwin != NULL)
8210 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008211 // If the quickfix/location list window is open, then clear it
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008212 if (qi->qf_curlist < qi->qf_listcount)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01008213 qf_free(qf_get_curlist(qi));
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008214 qf_update_buffer(qi, NULL);
8215 }
8216
8217 if (wp != NULL && IS_LL_WINDOW(wp))
8218 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008219 // If in the location list window, then use the non-location list
8220 // window with this location list (if present)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01008221 llwin = qf_find_win_with_loclist(qi);
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008222 if (llwin != NULL)
8223 wp = llwin;
8224 }
8225
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008226 qf_free_all(wp);
8227 if (wp == NULL)
8228 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008229 // quickfix list
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008230 qi->qf_curlist = 0;
8231 qi->qf_listcount = 0;
8232 }
Bram Moolenaaree8188f2019-02-05 21:23:04 +01008233 else if (qfwin != NULL)
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008234 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008235 // If the location list window is open, then create a new empty
8236 // location list
64-bitman88d41ab2025-04-06 17:20:39 +02008237 qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION, wp->w_p_lhi);
Bram Moolenaar99895ea2017-04-20 22:44:47 +02008238
Bram Moolenaar95946f12019-03-31 15:31:59 +02008239 if (new_ll != NULL)
8240 {
8241 new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
Bram Moolenaard788f6f2017-04-23 17:19:43 +02008242
Bram Moolenaar95946f12019-03-31 15:31:59 +02008243 // first free the list reference in the location list window
8244 ll_free_all(&qfwin->w_llist_ref);
8245
8246 qfwin->w_llist_ref = new_ll;
8247 if (wp != qfwin)
8248 win_set_loclist(wp, new_ll);
8249 }
Bram Moolenaar69f40be2017-04-02 15:15:49 +02008250 }
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008251}
8252
Bram Moolenaard823fa92016-08-12 16:29:27 +02008253/*
8254 * Populate the quickfix list with the items supplied in the list
8255 * of dictionaries. "title" will be copied to w:quickfix_title.
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02008256 * "action" is 'a' for add, 'r' for replace, 'u' for update. Otherwise
8257 * create a new list. When "what" is not NULL then only set some properties.
Bram Moolenaard823fa92016-08-12 16:29:27 +02008258 */
8259 int
8260set_errorlist(
8261 win_T *wp,
8262 list_T *list,
8263 int action,
8264 char_u *title,
8265 dict_T *what)
8266{
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02008267 qf_info_T *qi;
Bram Moolenaard823fa92016-08-12 16:29:27 +02008268 int retval = OK;
8269
8270 if (wp != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +02008271 qi = ll_get_or_alloc_list(wp);
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02008272 else
8273 qi = ql_info;
8274 if (qi == NULL)
64-bitman88d41ab2025-04-06 17:20:39 +02008275 return FAIL;
Bram Moolenaard823fa92016-08-12 16:29:27 +02008276
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008277 if (action == 'f')
8278 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008279 // Free the entire quickfix or location list stack
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008280 qf_free_stack(wp, qi);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008281 return OK;
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02008282 }
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008283
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008284 // A dict argument cannot be specified with a non-empty list argument
Bram Moolenaar90049492020-07-01 13:04:05 +02008285 if (list->lv_len != 0 && what != NULL)
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008286 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008287 semsg(_(e_invalid_argument_str),
Bram Moolenaarbe7a50c2020-06-30 22:11:44 +02008288 _("cannot have both a list and a \"what\" argument"));
8289 return FAIL;
8290 }
8291
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008292 incr_quickfix_busy();
8293
8294 if (what != NULL)
Bram Moolenaarae338332017-08-11 20:25:26 +02008295 retval = qf_set_properties(qi, what, action, title);
Bram Moolenaard823fa92016-08-12 16:29:27 +02008296 else
Bram Moolenaarb254af32017-12-18 19:48:58 +01008297 {
Bram Moolenaara3921f42017-06-04 15:30:34 +02008298 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action);
Bram Moolenaarb254af32017-12-18 19:48:58 +01008299 if (retval == OK)
Bram Moolenaar4aa47b22019-03-13 06:51:53 +01008300 qf_list_changed(qf_get_curlist(qi));
Bram Moolenaarb254af32017-12-18 19:48:58 +01008301 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02008302
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008303 decr_quickfix_busy();
8304
Bram Moolenaard823fa92016-08-12 16:29:27 +02008305 return retval;
8306}
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008307
Tom Praschanca6ac992023-08-11 23:26:12 +02008308static int mark_quickfix_user_data(qf_info_T *qi, int copyID)
8309{
8310 int abort = FALSE;
64-bitman88d41ab2025-04-06 17:20:39 +02008311 for (int i = 0; i < qi->qf_maxcount && !abort; ++i)
Tom Praschanca6ac992023-08-11 23:26:12 +02008312 {
8313 qf_list_T *qfl = &qi->qf_lists[i];
8314 if (!qfl->qf_has_user_data)
8315 continue;
8316 qfline_T *qfp;
8317 int j;
8318 FOR_ALL_QFL_ITEMS(qfl, qfp, j)
8319 {
8320 typval_T* user_data = &qfp->qf_user_data;
8321 if (user_data != NULL && user_data->v_type != VAR_NUMBER
8322 && user_data->v_type != VAR_STRING && user_data->v_type != VAR_FLOAT)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01008323 abort = abort || set_ref_in_item(user_data, copyID, NULL, NULL, NULL);
Tom Praschanca6ac992023-08-11 23:26:12 +02008324 }
8325 }
8326 return abort;
8327}
8328
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008329/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008330 * Mark the quickfix context and callback function as in use for all the lists
8331 * in a quickfix stack.
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008332 */
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008333 static int
8334mark_quickfix_ctx(qf_info_T *qi, int copyID)
8335{
8336 int i;
8337 int abort = FALSE;
8338 typval_T *ctx;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008339 callback_T *cb;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008340
64-bitman88d41ab2025-04-06 17:20:39 +02008341 for (i = 0; i < qi->qf_maxcount && !abort; ++i)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008342 {
8343 ctx = qi->qf_lists[i].qf_ctx;
Bram Moolenaar55b69262017-08-13 13:42:01 +02008344 if (ctx != NULL && ctx->v_type != VAR_NUMBER
8345 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01008346 abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL, NULL);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008347
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01008348 cb = &qi->qf_lists[i].qf_qftf_cb;
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008349 abort = abort || set_ref_in_callback(cb, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008350 }
8351
8352 return abort;
8353}
8354
8355/*
8356 * Mark the context of the quickfix list and the location lists (if present) as
Bram Moolenaar353eeea2018-04-16 18:04:57 +02008357 * "in use". So that garbage collection doesn't free the context.
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008358 */
8359 int
8360set_ref_in_quickfix(int copyID)
8361{
8362 int abort = FALSE;
8363 tabpage_T *tp;
8364 win_T *win;
8365
64-bitman88d41ab2025-04-06 17:20:39 +02008366 if (ql_info == NULL)
8367 return TRUE;
8368
8369 abort = mark_quickfix_ctx(ql_info, copyID);
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008370 if (abort)
8371 return abort;
8372
64-bitman88d41ab2025-04-06 17:20:39 +02008373 abort = mark_quickfix_user_data(ql_info, copyID);
Tom Praschanca6ac992023-08-11 23:26:12 +02008374 if (abort)
8375 return abort;
8376
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00008377 abort = set_ref_in_callback(&qftf_cb, copyID);
8378 if (abort)
8379 return abort;
8380
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008381 FOR_ALL_TAB_WINDOWS(tp, win)
8382 {
8383 if (win->w_llist != NULL)
8384 {
8385 abort = mark_quickfix_ctx(win->w_llist, copyID);
8386 if (abort)
8387 return abort;
Tom Praschanca6ac992023-08-11 23:26:12 +02008388
8389 abort = mark_quickfix_user_data(win->w_llist, copyID);
8390 if (abort)
8391 return abort;
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008392 }
Bram Moolenaar12237442017-12-19 12:38:52 +01008393 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1))
8394 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008395 // In a location list window and none of the other windows is
8396 // referring to this location list. Mark the location list
8397 // context as still in use.
Bram Moolenaar12237442017-12-19 12:38:52 +01008398 abort = mark_quickfix_ctx(win->w_llist_ref, copyID);
8399 if (abort)
8400 return abort;
zeertzjqbe4bd182024-09-14 10:32:31 +02008401
8402 abort = mark_quickfix_user_data(win->w_llist_ref, copyID);
8403 if (abort)
8404 return abort;
Bram Moolenaar12237442017-12-19 12:38:52 +01008405 }
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02008406 }
8407
8408 return abort;
8409}
Bram Moolenaar05159a02005-02-26 23:04:13 +00008410#endif
8411
Bram Moolenaar81695252004-12-29 20:58:21 +00008412/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01008413 * Return the autocmd name for the :cbuffer Ex commands
8414 */
8415 static char_u *
8416cbuffer_get_auname(cmdidx_T cmdidx)
8417{
8418 switch (cmdidx)
8419 {
8420 case CMD_cbuffer: return (char_u *)"cbuffer";
8421 case CMD_cgetbuffer: return (char_u *)"cgetbuffer";
8422 case CMD_caddbuffer: return (char_u *)"caddbuffer";
8423 case CMD_lbuffer: return (char_u *)"lbuffer";
8424 case CMD_lgetbuffer: return (char_u *)"lgetbuffer";
8425 case CMD_laddbuffer: return (char_u *)"laddbuffer";
8426 default: return NULL;
8427 }
8428}
8429
8430/*
8431 * Process and validate the arguments passed to the :cbuffer, :caddbuffer,
8432 * :cgetbuffer, :lbuffer, :laddbuffer, :lgetbuffer Ex commands.
8433 */
8434 static int
8435cbuffer_process_args(
8436 exarg_T *eap,
8437 buf_T **bufp,
8438 linenr_T *line1,
8439 linenr_T *line2)
8440{
8441 buf_T *buf = NULL;
8442
8443 if (*eap->arg == NUL)
8444 buf = curbuf;
8445 else if (*skipwhite(skipdigits(eap->arg)) == NUL)
8446 buf = buflist_findnr(atoi((char *)eap->arg));
8447
8448 if (buf == NULL)
8449 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008450 emsg(_(e_invalid_argument));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008451 return FAIL;
8452 }
8453
8454 if (buf->b_ml.ml_mfp == NULL)
8455 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008456 emsg(_(e_buffer_is_not_loaded));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008457 return FAIL;
8458 }
8459
8460 if (eap->addr_count == 0)
8461 {
8462 eap->line1 = 1;
8463 eap->line2 = buf->b_ml.ml_line_count;
8464 }
8465
8466 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count
8467 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count)
8468 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008469 emsg(_(e_invalid_range));
Bram Moolenaara16123a2019-03-28 20:31:07 +01008470 return FAIL;
8471 }
8472
8473 *line1 = eap->line1;
8474 *line2 = eap->line2;
8475 *bufp = buf;
8476
8477 return OK;
8478}
8479
8480/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00008481 * ":[range]cbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00008482 * ":[range]caddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00008483 * ":[range]cgetbuffer [bufnr]" command.
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008484 * ":[range]lbuffer [bufnr]" command.
Bram Moolenaara6557602006-02-04 22:43:20 +00008485 * ":[range]laddbuffer [bufnr]" command.
Bram Moolenaardb552d602006-03-23 22:59:57 +00008486 * ":[range]lgetbuffer [bufnr]" command.
Bram Moolenaar86b68352004-12-27 21:59:20 +00008487 */
8488 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008489ex_cbuffer(exarg_T *eap)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008490{
8491 buf_T *buf = NULL;
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008492 qf_info_T *qi;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008493 char_u *au_name = NULL;
Bram Moolenaar1ed22762017-11-28 18:03:44 +01008494 int res;
Bram Moolenaar531b9a32018-07-03 16:54:23 +02008495 int_u save_qfid;
8496 win_T *wp = NULL;
Bram Moolenaara16123a2019-03-28 20:31:07 +01008497 char_u *qf_title;
8498 linenr_T line1;
8499 linenr_T line2;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008500
Bram Moolenaara16123a2019-03-28 20:31:07 +01008501 au_name = cbuffer_get_auname(eap->cmdidx);
Bram Moolenaar21662be2016-11-06 14:46:44 +01008502 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
Bram Moolenaara16123a2019-03-28 20:31:07 +01008503 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008504 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008505#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008506 if (aborting())
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008507 return;
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008508#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008509 }
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02008510
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008511 // Must come after autocommands.
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008512 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8513 if (qi == NULL)
8514 return;
Bram Moolenaaraaf6e432017-12-19 16:41:14 +01008515
Bram Moolenaara16123a2019-03-28 20:31:07 +01008516 if (cbuffer_process_args(eap, &buf, &line1, &line2) == FAIL)
8517 return;
8518
8519 qf_title = qf_cmdtitle(*eap->cmdlinep);
8520
8521 if (buf->b_sfname)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008522 {
Bram Moolenaara16123a2019-03-28 20:31:07 +01008523 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)",
8524 (char *)qf_title, (char *)buf->b_sfname);
8525 qf_title = IObuff;
Bram Moolenaar86b68352004-12-27 21:59:20 +00008526 }
Bram Moolenaara16123a2019-03-28 20:31:07 +01008527
8528 incr_quickfix_busy();
8529
8530 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm,
8531 (eap->cmdidx != CMD_caddbuffer
8532 && eap->cmdidx != CMD_laddbuffer),
8533 line1, line2,
8534 qf_title, NULL);
8535 if (qf_stack_empty(qi))
8536 {
8537 decr_quickfix_busy();
8538 return;
8539 }
8540 if (res >= 0)
8541 qf_list_changed(qf_get_curlist(qi));
8542
8543 // Remember the current quickfix list identifier, so that we can
8544 // check for autocommands changing the current quickfix list.
8545 save_qfid = qf_get_curlist(qi)->qf_id;
8546 if (au_name != NULL)
8547 {
8548 buf_T *curbuf_old = curbuf;
8549
8550 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname,
8551 TRUE, curbuf);
8552 if (curbuf != curbuf_old)
8553 // Autocommands changed buffer, don't jump now, "qi" may
8554 // be invalid.
8555 res = 0;
8556 }
8557 // Jump to the first error for a new list and if autocmds didn't
8558 // free the list.
8559 if (res > 0 && (eap->cmdidx == CMD_cbuffer ||
8560 eap->cmdidx == CMD_lbuffer)
8561 && qflist_valid(wp, save_qfid))
8562 // display the first error
8563 qf_jump_first(qi, save_qfid, eap->forceit);
8564
8565 decr_quickfix_busy();
Bram Moolenaar86b68352004-12-27 21:59:20 +00008566}
8567
Bram Moolenaar1e015462005-09-25 22:16:38 +00008568#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar86b68352004-12-27 21:59:20 +00008569/*
Bram Moolenaara16123a2019-03-28 20:31:07 +01008570 * Return the autocmd name for the :cexpr Ex commands.
8571 */
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008572 char_u *
Bram Moolenaara16123a2019-03-28 20:31:07 +01008573cexpr_get_auname(cmdidx_T cmdidx)
8574{
8575 switch (cmdidx)
8576 {
8577 case CMD_cexpr: return (char_u *)"cexpr";
8578 case CMD_cgetexpr: return (char_u *)"cgetexpr";
8579 case CMD_caddexpr: return (char_u *)"caddexpr";
8580 case CMD_lexpr: return (char_u *)"lexpr";
8581 case CMD_lgetexpr: return (char_u *)"lgetexpr";
8582 case CMD_laddexpr: return (char_u *)"laddexpr";
8583 default: return NULL;
8584 }
8585}
8586
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008587 int
8588trigger_cexpr_autocmd(int cmdidx)
8589{
8590 char_u *au_name = cexpr_get_auname(cmdidx);
8591
8592 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8593 curbuf->b_fname, TRUE, curbuf))
8594 {
8595 if (aborting())
8596 return FAIL;
8597 }
8598 return OK;
8599}
8600
8601 int
8602cexpr_core(exarg_T *eap, typval_T *tv)
8603{
8604 qf_info_T *qi;
8605 win_T *wp = NULL;
8606
8607 qi = qf_cmd_get_or_alloc_stack(eap, &wp);
8608 if (qi == NULL)
8609 return FAIL;
8610
8611 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
8612 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL))
8613 {
8614 int res;
8615 int_u save_qfid;
8616 char_u *au_name = cexpr_get_auname(eap->cmdidx);
8617
8618 incr_quickfix_busy();
8619 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm,
8620 (eap->cmdidx != CMD_caddexpr
8621 && eap->cmdidx != CMD_laddexpr),
8622 (linenr_T)0, (linenr_T)0,
8623 qf_cmdtitle(*eap->cmdlinep), NULL);
8624 if (qf_stack_empty(qi))
8625 {
8626 decr_quickfix_busy();
8627 return FAIL;
8628 }
8629 if (res >= 0)
8630 qf_list_changed(qf_get_curlist(qi));
8631
8632 // Remember the current quickfix list identifier, so that we can
8633 // check for autocommands changing the current quickfix list.
8634 save_qfid = qf_get_curlist(qi)->qf_id;
8635 if (au_name != NULL)
8636 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8637 curbuf->b_fname, TRUE, curbuf);
8638
8639 // Jump to the first error for a new list and if autocmds didn't
8640 // free the list.
8641 if (res > 0 && (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr)
8642 && qflist_valid(wp, save_qfid))
8643 // display the first error
8644 qf_jump_first(qi, save_qfid, eap->forceit);
8645 decr_quickfix_busy();
8646 return OK;
8647 }
8648
Bram Moolenaar677658a2022-01-05 16:09:06 +00008649 emsg(_(e_string_or_list_expected));
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008650 return FAIL;
8651}
8652
Bram Moolenaara16123a2019-03-28 20:31:07 +01008653/*
Bram Moolenaardb552d602006-03-23 22:59:57 +00008654 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command.
8655 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command.
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008656 * Also: ":caddexpr", ":cgetexpr", "laddexpr" and "laddexpr".
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008657 */
8658 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008659ex_cexpr(exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008660{
8661 typval_T *tv;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008662
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02008663 if (trigger_cexpr_autocmd(eap->cmdidx) == FAIL)
Bram Moolenaar87f59b02019-04-04 14:04:11 +02008664 return;
Bram Moolenaar3c097222017-12-21 20:54:49 +01008665
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008666 // Evaluate the expression. When the result is a string or a list we can
8667 // use it to fill the errorlist.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02008668 tv = eval_expr(eap->arg, eap);
Yegappan Lakshmananf97a2952023-01-18 18:17:48 +00008669 if (tv == NULL)
8670 return;
8671
8672 (void)cexpr_core(eap, tv);
8673 free_tv(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008674}
Bram Moolenaar1e015462005-09-25 22:16:38 +00008675#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008676
8677/*
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008678 * Get the location list for ":lhelpgrep"
8679 */
8680 static qf_info_T *
8681hgr_get_ll(int *new_ll)
8682{
8683 win_T *wp;
8684 qf_info_T *qi;
8685
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008686 // If the current window is a help window, then use it
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008687 if (bt_help(curwin->w_buffer))
8688 wp = curwin;
8689 else
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008690 // Find an existing help window
Bram Moolenaar7a2b0e52018-05-10 18:55:28 +02008691 wp = qf_find_help_win();
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008692
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008693 if (wp == NULL) // Help window not found
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008694 qi = NULL;
8695 else
8696 qi = wp->w_llist;
8697
8698 if (qi == NULL)
8699 {
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008700 // Allocate a new location list for help text matches
64-bitman88d41ab2025-04-06 17:20:39 +02008701 if ((qi = qf_alloc_stack(QFLT_LOCATION, 1)) == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008702 return NULL;
8703 *new_ll = TRUE;
8704 }
8705
8706 return qi;
8707}
8708
8709/*
8710 * Search for a pattern in a help file.
8711 */
8712 static void
8713hgr_search_file(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008714 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008715 char_u *fname,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008716 vimconv_T *p_vc,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008717 regmatch_T *p_regmatch)
8718{
8719 FILE *fd;
8720 long lnum;
8721
8722 fd = mch_fopen((char *)fname, "r");
8723 if (fd == NULL)
8724 return;
8725
8726 lnum = 1;
8727 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int)
8728 {
8729 char_u *line = IObuff;
Bram Moolenaara12a1612019-01-24 16:39:02 +01008730
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008731 // Convert a line if 'encoding' is not utf-8 and
8732 // the line contains a non-ASCII character.
Yegappan Lakshmanandf1bbea2022-03-05 14:35:12 +00008733 if (p_vc->vc_type != CONV_NONE && has_non_ascii(IObuff))
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008734 {
8735 line = string_convert(p_vc, IObuff, NULL);
8736 if (line == NULL)
8737 line = IObuff;
8738 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008739
8740 if (vim_regexec(p_regmatch, line, (colnr_T)0))
8741 {
8742 int l = (int)STRLEN(line);
8743
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008744 // remove trailing CR, LF, spaces, etc.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008745 while (l > 0 && line[l - 1] <= ' ')
8746 line[--l] = NUL;
8747
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008748 if (qf_add_entry(qfl,
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008749 NULL, // dir
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008750 fname,
Bram Moolenaard76ce852018-05-01 15:02:04 +02008751 NULL,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008752 0,
8753 line,
8754 lnum,
thinca6864efa2021-06-19 20:45:20 +02008755 0,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008756 (int)(p_regmatch->startp[0] - line)
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008757 + 1, // col
thinca6864efa2021-06-19 20:45:20 +02008758 (int)(p_regmatch->endp[0] - line)
8759 + 1, // end_col
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008760 FALSE, // vis_col
8761 NULL, // search pattern
8762 0, // nr
8763 1, // type
Tom Praschanca6ac992023-08-11 23:26:12 +02008764 NULL, // user_data
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008765 TRUE // valid
Bram Moolenaar95946f12019-03-31 15:31:59 +02008766 ) == QF_FAIL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008767 {
8768 got_int = TRUE;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008769 if (line != IObuff)
8770 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008771 break;
8772 }
8773 }
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008774 if (line != IObuff)
8775 vim_free(line);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008776 ++lnum;
8777 line_breakcheck();
8778 }
8779 fclose(fd);
8780}
8781
8782/*
8783 * Search for a pattern in all the help files in the doc directory under
8784 * the given directory.
8785 */
8786 static void
8787hgr_search_files_in_dir(
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008788 qf_list_T *qfl,
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008789 char_u *dirname,
Bram Moolenaara12a1612019-01-24 16:39:02 +01008790 regmatch_T *p_regmatch,
8791 vimconv_T *p_vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008792#ifdef FEAT_MULTI_LANG
8793 , char_u *lang
8794#endif
8795 )
8796{
8797 int fcount;
8798 char_u **fnames;
8799 int fi;
8800
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008801 // Find all "*.txt" and "*.??x" files in the "doc" directory.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008802 add_pathsep(dirname);
8803 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)");
8804 if (gen_expand_wildcards(1, &dirname, &fcount,
8805 &fnames, EW_FILE|EW_SILENT) == OK
8806 && fcount > 0)
8807 {
8808 for (fi = 0; fi < fcount && !got_int; ++fi)
8809 {
8810#ifdef FEAT_MULTI_LANG
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008811 // Skip files for a different language.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008812 if (lang != NULL
8813 && STRNICMP(lang, fnames[fi]
Bram Moolenaard76ce852018-05-01 15:02:04 +02008814 + STRLEN(fnames[fi]) - 3, 2) != 0
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008815 && !(STRNICMP(lang, "en", 2) == 0
8816 && STRNICMP("txt", fnames[fi]
8817 + STRLEN(fnames[fi]) - 3, 3) == 0))
8818 continue;
8819#endif
8820
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008821 hgr_search_file(qfl, fnames[fi], p_vc, p_regmatch);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008822 }
8823 FreeWild(fcount, fnames);
8824 }
8825}
8826
8827/*
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008828 * Search for a pattern in all the help files in the 'runtimepath'
8829 * and add the matches to a quickfix list.
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008830 * 'lang' is the language specifier. If supplied, then only matches in the
Bram Moolenaar18cebf42018-05-08 22:31:37 +02008831 * specified language are found.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008832 */
8833 static void
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008834hgr_search_in_rtp(qf_list_T *qfl, regmatch_T *p_regmatch, char_u *lang)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008835{
8836 char_u *p;
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008837
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008838 vimconv_T vc;
8839
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008840 // Help files are in utf-8 or latin1, convert lines when 'encoding'
8841 // differs.
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008842 vc.vc_type = CONV_NONE;
8843 if (!enc_utf8)
8844 convert_setup(&vc, (char_u *)"utf-8", p_enc);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008845
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008846 // Go through all the directories in 'runtimepath'
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008847 p = p_rtp;
8848 while (*p != NUL && !got_int)
8849 {
8850 copy_option_part(&p, NameBuff, MAXPATHL, ",");
8851
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008852 hgr_search_files_in_dir(qfl, NameBuff, p_regmatch, &vc
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008853#ifdef FEAT_MULTI_LANG
8854 , lang
8855#endif
8856 );
8857 }
8858
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008859 if (vc.vc_type != CONV_NONE)
8860 convert_setup(&vc, NULL, NULL);
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008861}
8862
8863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 * ":helpgrep {pattern}"
8865 */
8866 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008867ex_helpgrep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868{
8869 regmatch_T regmatch;
8870 char_u *save_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008871 int save_cpo_allocated;
64-bitman88d41ab2025-04-06 17:20:39 +02008872 qf_info_T *qi = ql_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008873 int new_qi = FALSE;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008874 char_u *au_name = NULL;
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008875 char_u *lang = NULL;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008876 int updated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
Bram Moolenaar73633f82012-01-20 13:39:07 +01008878 switch (eap->cmdidx)
8879 {
8880 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break;
8881 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break;
8882 default: break;
8883 }
Bram Moolenaar21662be2016-11-06 14:46:44 +01008884 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name,
8885 curbuf->b_fname, TRUE, curbuf))
Bram Moolenaar73633f82012-01-20 13:39:07 +01008886 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008887#ifdef FEAT_EVAL
Bram Moolenaar21662be2016-11-06 14:46:44 +01008888 if (aborting())
Bram Moolenaar73633f82012-01-20 13:39:07 +01008889 return;
Bram Moolenaar73633f82012-01-20 13:39:07 +01008890#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008891 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008892
Bram Moolenaar39665952018-08-15 20:59:48 +02008893 if (is_loclist_cmd(eap->cmdidx))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008894 {
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008895 qi = hgr_get_ll(&new_qi);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008896 if (qi == NULL)
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02008897 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008898 }
64-bitman88d41ab2025-04-06 17:20:39 +02008899 else if (qi == NULL)
8900 {
8901 emsg(_(e_no_quickfix_stack));
8902 return;
8903 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008904
Bram Moolenaara16123a2019-03-28 20:31:07 +01008905 // Make 'cpoptions' empty, the 'l' flag should not be used here.
8906 save_cpo = p_cpo;
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008907 save_cpo_allocated = is_option_allocated("cpo");
Bram Moolenaara16123a2019-03-28 20:31:07 +01008908 p_cpo = empty_option;
8909
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008910 incr_quickfix_busy();
8911
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008912#ifdef FEAT_MULTI_LANG
8913 // Check for a specified language
8914 lang = check_help_lang(eap->arg);
8915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING);
8917 regmatch.rm_ic = FALSE;
8918 if (regmatch.regprog != NULL)
8919 {
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008920 qf_list_T *qfl;
8921
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008922 // create a new quickfix list
Bram Moolenaar8b62e312018-05-13 15:29:04 +02008923 qf_new_list(qi, qf_cmdtitle(*eap->cmdlinep));
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008924 qfl = qf_get_curlist(qi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925
Bram Moolenaar9afe5e92019-03-22 14:16:06 +01008926 hgr_search_in_rtp(qfl, &regmatch, lang);
Bram Moolenaar10b7b392012-01-10 16:28:45 +01008927
Bram Moolenaar473de612013-06-08 18:19:48 +02008928 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929
Bram Moolenaar108e7b42018-10-11 17:39:12 +02008930 qfl->qf_nonevalid = FALSE;
8931 qfl->qf_ptr = qfl->qf_start;
8932 qfl->qf_index = 1;
8933 qf_list_changed(qfl);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008934 updated = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 }
8936
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00008937 if (p_cpo == empty_option)
8938 p_cpo = save_cpo;
8939 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008940 {
8941 // Darn, some plugin changed the value. If it's still empty it was
8942 // changed and restored, need to restore in the complicated way.
8943 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01008944 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00008945 if (save_cpo_allocated)
8946 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008947 }
8948
8949 if (updated)
8950 // This may open a window and source scripts, do this after 'cpo' was
8951 // restored.
8952 qf_update_buffer(qi, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
Bram Moolenaar73633f82012-01-20 13:39:07 +01008954 if (au_name != NULL)
8955 {
8956 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name,
8957 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarec98e932020-06-08 19:35:59 +02008958 // When adding a location list to an existing location list stack,
8959 // if the autocmd made the stack invalid, then just return.
8960 if (!new_qi && IS_LL_STACK(qi) && qf_find_win_with_loclist(qi) == NULL)
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008961 {
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008962 decr_quickfix_busy();
Bram Moolenaar73633f82012-01-20 13:39:07 +01008963 return;
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008964 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008965 }
Bram Moolenaar73633f82012-01-20 13:39:07 +01008966
Bram Moolenaar00bf8cd2018-10-07 20:26:20 +02008967 // Jump to first match.
Bram Moolenaar0398e002019-03-21 21:12:49 +01008968 if (!qf_list_empty(qf_get_curlist(qi)))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008969 qf_jump(qi, 0, 0, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008970 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00008971 semsg(_(e_no_match_str_2), eap->arg);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008972
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02008973 decr_quickfix_busy();
8974
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008975 if (eap->cmdidx == CMD_lhelpgrep)
8976 {
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02008977 // If the help window is not opened or if it already points to the
8978 // correct location list, then free the new location list.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02008979 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008980 {
8981 if (new_qi)
8982 ll_free_all(&qi);
8983 }
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00008984 else if (curwin->w_llist == NULL && new_qi)
8985 // current window didn't have a location list associated with it
8986 // before. Associate the new location list now.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008987 curwin->w_llist = qi;
8988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989}
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008990
8991# if defined(EXITFREE) || defined(PROTO)
8992 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00008993free_quickfix(void)
Yegappan Lakshmanan975a6652022-10-14 13:11:13 +01008994{
8995 win_T *win;
8996 tabpage_T *tab;
8997
8998 qf_free_all(NULL);
8999 // Free all location lists
9000 FOR_ALL_TAB_WINDOWS(tab, win)
9001 qf_free_all(win);
9002
9003 ga_clear(&qfga);
9004}
9005# endif
9006
Bram Moolenaar63d9e732019-12-05 21:10:38 +01009007#endif // FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02009008
9009#if defined(FEAT_EVAL) || defined(PROTO)
9010# ifdef FEAT_QUICKFIX
9011 static void
9012get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
9013{
9014 if (what_arg->v_type == VAR_UNKNOWN)
9015 {
9016 if (rettv_list_alloc(rettv) == OK)
9017 if (is_qf || wp != NULL)
Bram Moolenaar858ba062020-05-31 23:11:59 +02009018 (void)get_errorlist(NULL, wp, -1, 0, rettv->vval.v_list);
Bram Moolenaare677df82019-09-02 22:31:11 +02009019 }
9020 else
9021 {
9022 if (rettv_dict_alloc(rettv) == OK)
9023 if (is_qf || (wp != NULL))
9024 {
9025 if (what_arg->v_type == VAR_DICT)
9026 {
9027 dict_T *d = what_arg->vval.v_dict;
9028
9029 if (d != NULL)
9030 qf_get_properties(wp, d, rettv->vval.v_dict);
9031 }
9032 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00009033 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02009034 }
9035 }
9036}
9037# endif
9038
9039/*
9040 * "getloclist()" function
9041 */
9042 void
9043f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9044{
9045# ifdef FEAT_QUICKFIX
9046 win_T *wp;
9047
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02009048 if (in_vim9script()
9049 && (check_for_number_arg(argvars, 0) == FAIL
9050 || check_for_opt_dict_arg(argvars, 1) == FAIL))
9051 return;
9052
Bram Moolenaare677df82019-09-02 22:31:11 +02009053 wp = find_win_by_nr_or_id(&argvars[0]);
9054 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
9055# endif
9056}
9057
9058/*
9059 * "getqflist()" function
9060 */
9061 void
9062f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9063{
9064# ifdef FEAT_QUICKFIX
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02009065 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
9066 return;
9067
Bram Moolenaare677df82019-09-02 22:31:11 +02009068 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
9069# endif
9070}
9071
9072/*
9073 * Used by "setqflist()" and "setloclist()" functions
9074 */
9075 static void
9076set_qf_ll_list(
9077 win_T *wp UNUSED,
9078 typval_T *list_arg UNUSED,
9079 typval_T *action_arg UNUSED,
9080 typval_T *what_arg UNUSED,
9081 typval_T *rettv)
9082{
9083# ifdef FEAT_QUICKFIX
Bram Moolenaare677df82019-09-02 22:31:11 +02009084 char_u *act;
9085 int action = 0;
9086 static int recursive = 0;
9087# endif
9088
9089 rettv->vval.v_number = -1;
9090
9091# ifdef FEAT_QUICKFIX
9092 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00009093 emsg(_(e_list_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02009094 else if (recursive != 0)
Bram Moolenaar74409f62022-01-01 15:58:22 +00009095 emsg(_(e_autocommand_caused_recursive_behavior));
Bram Moolenaare677df82019-09-02 22:31:11 +02009096 else
9097 {
9098 list_T *l = list_arg->vval.v_list;
Bram Moolenaar90049492020-07-01 13:04:05 +02009099 dict_T *what = NULL;
Bram Moolenaare677df82019-09-02 22:31:11 +02009100 int valid_dict = TRUE;
9101
9102 if (action_arg->v_type == VAR_STRING)
9103 {
9104 act = tv_get_string_chk(action_arg);
9105 if (act == NULL)
9106 return; // type error; errmsg already given
Jeremy Fleischman27fbf6e2024-10-14 20:46:27 +02009107 if ((*act == 'a' || *act == 'r' || *act == 'u' || *act == ' ' || *act == 'f') &&
Bram Moolenaare677df82019-09-02 22:31:11 +02009108 act[1] == NUL)
9109 action = *act;
9110 else
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009111 semsg(_(e_invalid_action_str_1), act);
Bram Moolenaare677df82019-09-02 22:31:11 +02009112 }
9113 else if (action_arg->v_type == VAR_UNKNOWN)
9114 action = ' ';
9115 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00009116 emsg(_(e_string_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02009117
9118 if (action_arg->v_type != VAR_UNKNOWN
9119 && what_arg->v_type != VAR_UNKNOWN)
9120 {
Bram Moolenaar90049492020-07-01 13:04:05 +02009121 if (what_arg->v_type == VAR_DICT && what_arg->vval.v_dict != NULL)
9122 what = what_arg->vval.v_dict;
Bram Moolenaare677df82019-09-02 22:31:11 +02009123 else
9124 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00009125 emsg(_(e_dictionary_required));
Bram Moolenaare677df82019-09-02 22:31:11 +02009126 valid_dict = FALSE;
9127 }
9128 }
9129
9130 ++recursive;
Bram Moolenaar90049492020-07-01 13:04:05 +02009131 if (l != NULL && action && valid_dict
9132 && set_errorlist(wp, l, action,
Bram Moolenaare677df82019-09-02 22:31:11 +02009133 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
Bram Moolenaar90049492020-07-01 13:04:05 +02009134 what) == OK)
Bram Moolenaare677df82019-09-02 22:31:11 +02009135 rettv->vval.v_number = 0;
9136 --recursive;
9137 }
9138# endif
9139}
9140
9141/*
9142 * "setloclist()" function
9143 */
9144 void
9145f_setloclist(typval_T *argvars, typval_T *rettv)
9146{
9147 win_T *win;
9148
9149 rettv->vval.v_number = -1;
9150
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02009151 if (in_vim9script()
9152 && (check_for_number_arg(argvars, 0) == FAIL
9153 || check_for_list_arg(argvars, 1) == FAIL
9154 || check_for_opt_string_arg(argvars, 2) == FAIL
9155 || (argvars[2].v_type != VAR_UNKNOWN
9156 && check_for_opt_dict_arg(argvars, 3) == FAIL)))
9157 return;
9158
Bram Moolenaare677df82019-09-02 22:31:11 +02009159 win = find_win_by_nr_or_id(&argvars[0]);
9160 if (win != NULL)
9161 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
9162}
9163
9164/*
9165 * "setqflist()" function
9166 */
9167 void
9168f_setqflist(typval_T *argvars, typval_T *rettv)
9169{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02009170 if (in_vim9script()
9171 && (check_for_list_arg(argvars, 0) == FAIL
9172 || check_for_opt_string_arg(argvars, 1) == FAIL
9173 || (argvars[1].v_type != VAR_UNKNOWN
9174 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
9175 return;
9176
Bram Moolenaare677df82019-09-02 22:31:11 +02009177 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
9178}
9179#endif